blob: 9874d58dcf2d538862752da880598eb2c1173541 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053018#if defined(PSA_CRYPTO_DRIVER_TEST)
19#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053020#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
21#else
22#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053023#endif
Ronald Cron28a45ed2021-02-09 20:35:42 +010024
Gilles Peskine4023c012021-05-27 13:21:20 +020025/* If this comes up, it's a bug in the test code or in the test data. */
26#define UNUSED 0xdeadbeef
27
Dave Rodgman647791d2021-06-23 12:49:59 +010028/* Assert that an operation is (not) active.
29 * This serves as a proxy for checking if the operation is aborted. */
30#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
31#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
Gilles Peskinef426e0f2019-02-25 17:42:03 +010032
33/** An invalid export length that will never be set by psa_export_key(). */
34static const size_t INVALID_EXPORT_LENGTH = ~0U;
35
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036/** Test if a buffer contains a constant byte value.
37 *
38 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020039 *
40 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042 * \param size Size of the buffer in bytes.
43 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 * \return 1 if the buffer is all-bits-zero.
45 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020046 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020047static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020048{
49 size_t i;
50 for( i = 0; i < size; i++ )
51 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020052 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020053 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020054 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020055 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020056}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010057#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
59static int asn1_write_10x( unsigned char **p,
60 unsigned char *start,
61 size_t bits,
62 unsigned char x )
63{
64 int ret;
65 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020066 if( bits == 0 )
67 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
68 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020069 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030070 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020071 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
72 *p -= len;
73 ( *p )[len-1] = x;
74 if( bits % 8 == 0 )
75 ( *p )[1] |= 1;
76 else
77 ( *p )[0] |= 1 << ( bits % 8 );
78 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
79 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
80 MBEDTLS_ASN1_INTEGER ) );
81 return( len );
82}
83
84static int construct_fake_rsa_key( unsigned char *buffer,
85 size_t buffer_size,
86 unsigned char **p,
87 size_t bits,
88 int keypair )
89{
90 size_t half_bits = ( bits + 1 ) / 2;
91 int ret;
92 int len = 0;
93 /* Construct something that looks like a DER encoding of
94 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
95 * RSAPrivateKey ::= SEQUENCE {
96 * version Version,
97 * modulus INTEGER, -- n
98 * publicExponent INTEGER, -- e
99 * privateExponent INTEGER, -- d
100 * prime1 INTEGER, -- p
101 * prime2 INTEGER, -- q
102 * exponent1 INTEGER, -- d mod (p-1)
103 * exponent2 INTEGER, -- d mod (q-1)
104 * coefficient INTEGER, -- (inverse of q) mod p
105 * otherPrimeInfos OtherPrimeInfos OPTIONAL
106 * }
107 * Or, for a public key, the same structure with only
108 * version, modulus and publicExponent.
109 */
110 *p = buffer + buffer_size;
111 if( keypair )
112 {
113 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
114 asn1_write_10x( p, buffer, half_bits, 1 ) );
115 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
116 asn1_write_10x( p, buffer, half_bits, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
118 asn1_write_10x( p, buffer, half_bits, 1 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, /* q */
120 asn1_write_10x( p, buffer, half_bits, 1 ) );
121 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
122 asn1_write_10x( p, buffer, half_bits, 3 ) );
123 MBEDTLS_ASN1_CHK_ADD( len, /* d */
124 asn1_write_10x( p, buffer, bits, 1 ) );
125 }
126 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
127 asn1_write_10x( p, buffer, 17, 1 ) );
128 MBEDTLS_ASN1_CHK_ADD( len, /* n */
129 asn1_write_10x( p, buffer, bits, 1 ) );
130 if( keypair )
131 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
132 mbedtls_asn1_write_int( p, buffer, 0 ) );
133 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
134 {
135 const unsigned char tag =
136 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
137 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
138 }
139 return( len );
140}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100141#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200142
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100143int exercise_mac_setup( psa_key_type_t key_type,
144 const unsigned char *key_bytes,
145 size_t key_length,
146 psa_algorithm_t alg,
147 psa_mac_operation_t *operation,
148 psa_status_t *status )
149{
Ronald Cron5425a212020-08-04 14:58:35 +0200150 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100152
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100153 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200154 psa_set_key_algorithm( &attributes, alg );
155 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200156 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157
Ronald Cron5425a212020-08-04 14:58:35 +0200158 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100159 /* Whether setup succeeded or failed, abort must succeed. */
160 PSA_ASSERT( psa_mac_abort( operation ) );
161 /* If setup failed, reproduce the failure, so that the caller can
162 * test the resulting state of the operation object. */
163 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100164 {
Ronald Cron5425a212020-08-04 14:58:35 +0200165 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100166 }
167
Ronald Cron5425a212020-08-04 14:58:35 +0200168 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100169 return( 1 );
170
171exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200172 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100173 return( 0 );
174}
175
176int exercise_cipher_setup( psa_key_type_t key_type,
177 const unsigned char *key_bytes,
178 size_t key_length,
179 psa_algorithm_t alg,
180 psa_cipher_operation_t *operation,
181 psa_status_t *status )
182{
Ronald Cron5425a212020-08-04 14:58:35 +0200183 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200184 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200186 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
187 psa_set_key_algorithm( &attributes, alg );
188 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200189 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100190
Ronald Cron5425a212020-08-04 14:58:35 +0200191 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 /* Whether setup succeeded or failed, abort must succeed. */
193 PSA_ASSERT( psa_cipher_abort( operation ) );
194 /* If setup failed, reproduce the failure, so that the caller can
195 * test the resulting state of the operation object. */
196 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197 {
Ronald Cron5425a212020-08-04 14:58:35 +0200198 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100199 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200 }
201
Ronald Cron5425a212020-08-04 14:58:35 +0200202 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100203 return( 1 );
204
205exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200206 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100207 return( 0 );
208}
209
Ronald Cron5425a212020-08-04 14:58:35 +0200210static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200211{
212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200213 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 uint8_t buffer[1];
215 size_t length;
216 int ok = 0;
217
Ronald Cronecfb2372020-07-23 17:13:42 +0200218 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
220 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
221 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200222 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200224 TEST_EQUAL(
225 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
226 TEST_EQUAL(
227 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200228 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200229 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
230 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
231 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
232 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
233
Ronald Cron5425a212020-08-04 14:58:35 +0200234 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000235 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200236 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000238 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200239
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 ok = 1;
241
242exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100243 /*
244 * Key attributes may have been returned by psa_get_key_attributes()
245 * thus reset them as required.
246 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200247 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100248
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200249 return( ok );
250}
251
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200252/* Assert that a key isn't reported as having a slot number. */
253#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
254#define ASSERT_NO_SLOT_NUMBER( attributes ) \
255 do \
256 { \
257 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
258 TEST_EQUAL( psa_get_key_slot_number( \
259 attributes, \
260 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
261 PSA_ERROR_INVALID_ARGUMENT ); \
262 } \
263 while( 0 )
264#else /* MBEDTLS_PSA_CRYPTO_SE_C */
265#define ASSERT_NO_SLOT_NUMBER( attributes ) \
266 ( (void) 0 )
267#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
268
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100269/* An overapproximation of the amount of storage needed for a key of the
270 * given type and with the given content. The API doesn't make it easy
271 * to find a good value for the size. The current implementation doesn't
272 * care about the value anyway. */
273#define KEY_BITS_FROM_DATA( type, data ) \
274 ( data )->len
275
Darryl Green0c6575a2018-11-07 16:05:30 +0000276typedef enum {
277 IMPORT_KEY = 0,
278 GENERATE_KEY = 1,
279 DERIVE_KEY = 2
280} generate_method;
281
Paul Elliott33746aa2021-09-15 16:40:40 +0100282typedef enum
283{
284 DO_NOT_SET_LENGTHS = 0,
285 SET_LENGTHS_BEFORE_NONCE = 1,
286 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100287} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100288
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100289typedef enum
290{
291 USE_NULL_TAG = 0,
292 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100293} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100294
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100295/*!
296 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100297 * \param key_type_arg Type of key passed in
298 * \param key_data The encryption / decryption key data
299 * \param alg_arg The type of algorithm used
300 * \param nonce Nonce data
301 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100302 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100303 * feed additional data in to be encrypted /
304 * decrypted. If -1, no chunking.
305 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100306 * \param data_part_len_arg If not -1, the length of chunks to feed
307 * the data in to be encrypted / decrypted. If
308 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100309 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100310 * expected here, this controls whether or not
311 * to set lengths, and in what order with
312 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100313 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100314 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100315 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100316 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100317 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100318 */
319static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
320 int alg_arg,
321 data_t *nonce,
322 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100323 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100324 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100325 int data_part_len_arg,
Paul Elliottbb979e72021-09-22 12:54:42 +0100326 set_lengths_method_t set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100327 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100328 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100329 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100330{
331 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
332 psa_key_type_t key_type = key_type_arg;
333 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100334 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100335 unsigned char *output_data = NULL;
336 unsigned char *part_data = NULL;
337 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100338 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100339 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 size_t output_size = 0;
341 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100342 size_t output_length = 0;
343 size_t key_bits = 0;
344 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100345 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100346 size_t part_length = 0;
347 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100348 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100349 size_t ad_part_len = 0;
350 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100351 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
353 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
354
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100355 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100356 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100357
Paul Elliottd3f82412021-06-16 16:52:21 +0100358 PSA_ASSERT( psa_crypto_init( ) );
359
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100360 if( is_encrypt )
361 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
362 else
363 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
364
Paul Elliottd3f82412021-06-16 16:52:21 +0100365 psa_set_key_algorithm( &attributes, alg );
366 psa_set_key_type( &attributes, key_type );
367
368 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
369 &key ) );
370
371 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
372 key_bits = psa_get_key_bits( &attributes );
373
374 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
375
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100376 if( is_encrypt )
377 {
378 /* Tag gets written at end of buffer. */
379 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
380 ( input_data->len +
381 tag_length ) );
382 data_true_size = input_data->len;
383 }
384 else
385 {
386 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
387 ( input_data->len -
388 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100389
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 /* Do not want to attempt to decrypt tag. */
391 data_true_size = input_data->len - tag_length;
392 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100393
394 ASSERT_ALLOC( output_data, output_size );
395
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100396 if( is_encrypt )
397 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100398 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
399 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100400 }
401 else
402 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100403 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
404 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100405 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100406
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100407 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100408
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100409 if( is_encrypt )
410 status = psa_aead_encrypt_setup( &operation, key, alg );
411 else
412 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100413
414 /* If the operation is not supported, just skip and not fail in case the
415 * encryption involves a common limitation of cryptography hardwares and
416 * an alternative implementation. */
417 if( status == PSA_ERROR_NOT_SUPPORTED )
418 {
419 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
420 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
421 }
422
423 PSA_ASSERT( status );
424
Paul Elliott33746aa2021-09-15 16:40:40 +0100425 if( set_lengths_method == DO_NOT_SET_LENGTHS )
426 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
427 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100428 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100429 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
430 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100431 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
432 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100433 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100434 {
435 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
436
Paul Elliott33746aa2021-09-15 16:40:40 +0100437 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
438 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100439 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100440
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100441 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100442 {
443 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100444 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100445
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100446 for( part_offset = 0, part_count = 0;
447 part_offset < additional_data->len;
448 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100449 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100450 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100451 {
Paul Elliott329d5382021-07-22 17:10:45 +0100452 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100453 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100454 else if( additional_data->len - part_offset < ad_part_len )
455 {
456 part_length = additional_data->len - part_offset;
457 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100458 else
459 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100460 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100461 }
462
463 PSA_ASSERT( psa_aead_update_ad( &operation,
464 additional_data->x + part_offset,
465 part_length ) );
466
Paul Elliottd3f82412021-06-16 16:52:21 +0100467 }
468 }
469 else
470 {
471 /* Pass additional data in one go. */
472 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
473 additional_data->len ) );
474 }
475
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100476 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100477 {
478 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100479 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100481 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100482
483 ASSERT_ALLOC( part_data, part_data_size );
484
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100485 for( part_offset = 0, part_count = 0;
486 part_offset < data_true_size;
487 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100489 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100490 {
Paul Elliott329d5382021-07-22 17:10:45 +0100491 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100492 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100493 else if( ( data_true_size - part_offset ) < data_part_len )
494 {
495 part_length = ( data_true_size - part_offset );
496 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100497 else
498 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100499 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100500 }
501
502 PSA_ASSERT( psa_aead_update( &operation,
503 ( input_data->x + part_offset ),
504 part_length, part_data,
505 part_data_size,
506 &output_part_length ) );
507
508 if( output_data && output_part_length )
509 {
510 memcpy( ( output_data + part_offset ), part_data,
511 output_part_length );
512 }
513
Paul Elliottd3f82412021-06-16 16:52:21 +0100514 output_length += output_part_length;
515 }
516 }
517 else
518 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100519 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100521 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100522 output_size, &output_length ) );
523 }
524
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100525 if( is_encrypt )
526 PSA_ASSERT( psa_aead_finish( &operation, final_data,
527 final_output_size,
528 &output_part_length,
529 tag_buffer, tag_length,
530 &tag_size ) );
531 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100532 {
Paul Elliott9961a662021-09-17 19:19:02 +0100533 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100534 final_output_size,
535 &output_part_length,
536 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100537 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100538 }
539
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100540 if( output_data && output_part_length )
541 memcpy( ( output_data + output_length ), final_data,
542 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100543
544 output_length += output_part_length;
545
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100546
547 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
548 * should be exact.*/
549 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100550 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100551 TEST_EQUAL( tag_length, tag_size );
552
553 if( output_data && tag_length )
554 memcpy( ( output_data + output_length ), tag_buffer,
555 tag_length );
556
557 output_length += tag_length;
558
559 TEST_EQUAL( output_length,
560 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
561 input_data->len ) );
562 TEST_ASSERT( output_length <=
563 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
564 }
565 else
566 {
567 TEST_EQUAL( output_length,
568 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
569 input_data->len ) );
570 TEST_ASSERT( output_length <=
571 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100572 }
573
Paul Elliottd3f82412021-06-16 16:52:21 +0100574
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100575 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100576 output_data, output_length );
577
Paul Elliottd3f82412021-06-16 16:52:21 +0100578
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100579 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
581exit:
582 psa_destroy_key( key );
583 psa_aead_abort( &operation );
584 mbedtls_free( output_data );
585 mbedtls_free( part_data );
586 mbedtls_free( final_data );
587 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100588
589 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100590}
591
Gilles Peskinee59236f2018-01-27 23:32:46 +0100592/* END_HEADER */
593
594/* BEGIN_DEPENDENCIES
595 * depends_on:MBEDTLS_PSA_CRYPTO_C
596 * END_DEPENDENCIES
597 */
598
599/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200600void static_checks( )
601{
602 size_t max_truncated_mac_size =
603 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
604
605 /* Check that the length for a truncated MAC always fits in the algorithm
606 * encoding. The shifted mask is the maximum truncated value. The
607 * untruncated algorithm may be one byte larger. */
608 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
609}
610/* END_CASE */
611
612/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200613void import_with_policy( int type_arg,
614 int usage_arg, int alg_arg,
615 int expected_status_arg )
616{
617 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
618 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200619 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200620 psa_key_type_t type = type_arg;
621 psa_key_usage_t usage = usage_arg;
622 psa_algorithm_t alg = alg_arg;
623 psa_status_t expected_status = expected_status_arg;
624 const uint8_t key_material[16] = {0};
625 psa_status_t status;
626
627 PSA_ASSERT( psa_crypto_init( ) );
628
629 psa_set_key_type( &attributes, type );
630 psa_set_key_usage_flags( &attributes, usage );
631 psa_set_key_algorithm( &attributes, alg );
632
633 status = psa_import_key( &attributes,
634 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200635 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200636 TEST_EQUAL( status, expected_status );
637 if( status != PSA_SUCCESS )
638 goto exit;
639
Ronald Cron5425a212020-08-04 14:58:35 +0200640 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200641 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200642 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200643 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200644 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200645 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200646
Ronald Cron5425a212020-08-04 14:58:35 +0200647 PSA_ASSERT( psa_destroy_key( key ) );
648 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200649
650exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100651 /*
652 * Key attributes may have been returned by psa_get_key_attributes()
653 * thus reset them as required.
654 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200655 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100656
657 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200658 PSA_DONE( );
659}
660/* END_CASE */
661
662/* BEGIN_CASE */
663void import_with_data( data_t *data, int type_arg,
664 int attr_bits_arg,
665 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200666{
667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
668 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200669 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200670 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200671 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200672 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100674
Gilles Peskine8817f612018-12-18 00:18:46 +0100675 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676
Gilles Peskine4747d192019-04-17 15:05:45 +0200677 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200678 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200679
Ronald Cron5425a212020-08-04 14:58:35 +0200680 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100681 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200682 if( status != PSA_SUCCESS )
683 goto exit;
684
Ronald Cron5425a212020-08-04 14:58:35 +0200685 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200686 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200687 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200688 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200689 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200690
Ronald Cron5425a212020-08-04 14:58:35 +0200691 PSA_ASSERT( psa_destroy_key( key ) );
692 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100693
694exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100695 /*
696 * Key attributes may have been returned by psa_get_key_attributes()
697 * thus reset them as required.
698 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200699 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100700
701 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200702 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100703}
704/* END_CASE */
705
706/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200707void import_large_key( int type_arg, int byte_size_arg,
708 int expected_status_arg )
709{
710 psa_key_type_t type = type_arg;
711 size_t byte_size = byte_size_arg;
712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
713 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200714 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200715 psa_status_t status;
716 uint8_t *buffer = NULL;
717 size_t buffer_size = byte_size + 1;
718 size_t n;
719
Steven Cooreman69967ce2021-01-18 18:01:08 +0100720 /* Skip the test case if the target running the test cannot
721 * accomodate large keys due to heap size constraints */
722 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200723 memset( buffer, 'K', byte_size );
724
725 PSA_ASSERT( psa_crypto_init( ) );
726
727 /* Try importing the key */
728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
729 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200730 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100731 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200732 TEST_EQUAL( status, expected_status );
733
734 if( status == PSA_SUCCESS )
735 {
Ronald Cron5425a212020-08-04 14:58:35 +0200736 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200737 TEST_EQUAL( psa_get_key_type( &attributes ), type );
738 TEST_EQUAL( psa_get_key_bits( &attributes ),
739 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200740 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200741 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200742 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200743 for( n = 0; n < byte_size; n++ )
744 TEST_EQUAL( buffer[n], 'K' );
745 for( n = byte_size; n < buffer_size; n++ )
746 TEST_EQUAL( buffer[n], 0 );
747 }
748
749exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100750 /*
751 * Key attributes may have been returned by psa_get_key_attributes()
752 * thus reset them as required.
753 */
754 psa_reset_key_attributes( &attributes );
755
Ronald Cron5425a212020-08-04 14:58:35 +0200756 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200757 PSA_DONE( );
758 mbedtls_free( buffer );
759}
760/* END_CASE */
761
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100762/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200763void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
764{
Ronald Cron5425a212020-08-04 14:58:35 +0200765 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200766 size_t bits = bits_arg;
767 psa_status_t expected_status = expected_status_arg;
768 psa_status_t status;
769 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200770 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200771 size_t buffer_size = /* Slight overapproximations */
772 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200773 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200774 unsigned char *p;
775 int ret;
776 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200777 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200778
Gilles Peskine8817f612018-12-18 00:18:46 +0100779 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200780 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200781
782 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
783 bits, keypair ) ) >= 0 );
784 length = ret;
785
786 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200787 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200788 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100789 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200790
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200791 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200792 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200793
794exit:
795 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200796 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200797}
798/* END_CASE */
799
800/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300801void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300802 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200803 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +0530804 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805 int expected_bits,
806 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200807 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100808 int canonical_input )
809{
Ronald Cron5425a212020-08-04 14:58:35 +0200810 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100811 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200812 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200813 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100814 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +0530815 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100816 unsigned char *exported = NULL;
817 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100818 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100819 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100820 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200821 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200822 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100823
Moran Pekercb088e72018-07-17 17:36:59 +0300824 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200825 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100826 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200827 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100828 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100829
Archana4d7ae1d2021-07-07 02:50:22 +0530830 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +0200831 psa_set_key_usage_flags( &attributes, usage_arg );
832 psa_set_key_algorithm( &attributes, alg );
833 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700834
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100835 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200836 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837
838 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200839 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200840 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
841 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200842 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100843
844 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200845 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100846 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100847
848 /* The exported length must be set by psa_export_key() to a value between 0
849 * and export_size. On errors, the exported length must be 0. */
850 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
851 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
852 TEST_ASSERT( exported_length <= export_size );
853
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200854 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200855 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100856 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200857 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100858 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100859 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200860 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100861
Gilles Peskineea38a922021-02-13 00:05:16 +0100862 /* Run sanity checks on the exported key. For non-canonical inputs,
863 * this validates the canonical representations. For canonical inputs,
864 * this doesn't directly validate the implementation, but it still helps
865 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +0530866 if( !psa_key_lifetime_is_external( lifetime ) )
867 {
868 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
869 goto exit;
870 }
Gilles Peskine8f609232018-08-11 01:24:55 +0200871
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200873 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100874 else
875 {
Ronald Cron5425a212020-08-04 14:58:35 +0200876 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200877 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200878 &key2 ) );
879 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100880 reexported,
881 export_size,
882 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200883 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +0530884 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200885 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100886 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100887 TEST_ASSERT( exported_length <=
Archana4d7ae1d2021-07-07 02:50:22 +0530888 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +0530889 psa_get_key_bits( &got_attributes ) ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100890 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100891
892destroy:
893 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200894 PSA_ASSERT( psa_destroy_key( key ) );
895 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100896
897exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100898 /*
899 * Key attributes may have been returned by psa_get_key_attributes()
900 * thus reset them as required.
901 */
902 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +0530903 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300904 mbedtls_free( exported );
905 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200906 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100907}
908/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100909
Moran Pekerf709f4a2018-06-06 17:26:04 +0300910/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300911void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200912 int type_arg,
913 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +0530914 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100915 int export_size_delta,
916 int expected_export_status_arg,
917 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300918{
Ronald Cron5425a212020-08-04 14:58:35 +0200919 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300920 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200921 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200922 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300923 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +0530924 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300925 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100926 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100927 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200928 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300929
Gilles Peskine8817f612018-12-18 00:18:46 +0100930 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300931
Archana4d7ae1d2021-07-07 02:50:22 +0530932 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +0200933 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
934 psa_set_key_algorithm( &attributes, alg );
935 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300936
937 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200938 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300939
Gilles Peskine49c25912018-10-29 15:15:31 +0100940 /* Export the public key */
941 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200942 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200943 exported, export_size,
944 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100945 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100946 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100947 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200948 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100949 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200950 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200951 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100952 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100953 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100954 TEST_ASSERT( expected_public_key->len <=
955 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
956 TEST_ASSERT( expected_public_key->len <=
957 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100958 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
959 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100960 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300961exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100962 /*
963 * Key attributes may have been returned by psa_get_key_attributes()
964 * thus reset them as required.
965 */
966 psa_reset_key_attributes( &attributes );
967
itayzafrir3e02b3b2018-06-12 17:06:52 +0300968 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200969 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200970 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300971}
972/* END_CASE */
973
Gilles Peskine20035e32018-02-03 22:44:14 +0100974/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200975void import_and_exercise_key( data_t *data,
976 int type_arg,
977 int bits_arg,
978 int alg_arg )
979{
Ronald Cron5425a212020-08-04 14:58:35 +0200980 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200981 psa_key_type_t type = type_arg;
982 size_t bits = bits_arg;
983 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100984 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200985 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200986 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200987
Gilles Peskine8817f612018-12-18 00:18:46 +0100988 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200989
Gilles Peskine4747d192019-04-17 15:05:45 +0200990 psa_set_key_usage_flags( &attributes, usage );
991 psa_set_key_algorithm( &attributes, alg );
992 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200993
994 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200995 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200996
997 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200998 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200999 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1000 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001001
1002 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001003 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001004 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001005
Ronald Cron5425a212020-08-04 14:58:35 +02001006 PSA_ASSERT( psa_destroy_key( key ) );
1007 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001008
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001009exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001010 /*
1011 * Key attributes may have been returned by psa_get_key_attributes()
1012 * thus reset them as required.
1013 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001014 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001015
1016 psa_reset_key_attributes( &attributes );
1017 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001018 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001019}
1020/* END_CASE */
1021
1022/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001023void effective_key_attributes( int type_arg, int expected_type_arg,
1024 int bits_arg, int expected_bits_arg,
1025 int usage_arg, int expected_usage_arg,
1026 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001027{
Ronald Cron5425a212020-08-04 14:58:35 +02001028 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001029 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001030 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001031 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001032 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001033 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001034 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001035 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001036 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001037 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001038
Gilles Peskine8817f612018-12-18 00:18:46 +01001039 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001040
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001041 psa_set_key_usage_flags( &attributes, usage );
1042 psa_set_key_algorithm( &attributes, alg );
1043 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001044 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001045
Ronald Cron5425a212020-08-04 14:58:35 +02001046 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001047 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001048
Ronald Cron5425a212020-08-04 14:58:35 +02001049 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001050 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1051 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1052 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1053 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001054
1055exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001056 /*
1057 * Key attributes may have been returned by psa_get_key_attributes()
1058 * thus reset them as required.
1059 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001060 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001061
1062 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001063 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001064}
1065/* END_CASE */
1066
1067/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001068void check_key_policy( int type_arg, int bits_arg,
1069 int usage_arg, int alg_arg )
1070{
1071 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001072 usage_arg,
1073 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001074 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001075 goto exit;
1076}
1077/* END_CASE */
1078
1079/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001080void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001081{
1082 /* Test each valid way of initializing the object, except for `= {0}`, as
1083 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1084 * though it's OK by the C standard. We could test for this, but we'd need
1085 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001086 psa_key_attributes_t func = psa_key_attributes_init( );
1087 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1088 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001089
1090 memset( &zero, 0, sizeof( zero ) );
1091
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001092 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1093 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1094 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001095
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001096 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1097 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1098 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1099
1100 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1101 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1102 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1103
1104 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1105 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1106 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1107
1108 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1109 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1110 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001111}
1112/* END_CASE */
1113
1114/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001115void mac_key_policy( int policy_usage_arg,
1116 int policy_alg_arg,
1117 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001118 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001119 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001120 int expected_status_sign_arg,
1121 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001122{
Ronald Cron5425a212020-08-04 14:58:35 +02001123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001125 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001126 psa_key_type_t key_type = key_type_arg;
1127 psa_algorithm_t policy_alg = policy_alg_arg;
1128 psa_algorithm_t exercise_alg = exercise_alg_arg;
1129 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001130 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001131 psa_status_t expected_status_sign = expected_status_sign_arg;
1132 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001133 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001134
Gilles Peskine8817f612018-12-18 00:18:46 +01001135 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001136
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001137 psa_set_key_usage_flags( &attributes, policy_usage );
1138 psa_set_key_algorithm( &attributes, policy_alg );
1139 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001140
Gilles Peskine049c7532019-05-15 20:22:09 +02001141 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001142 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001143
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001144 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1145 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001146
Ronald Cron5425a212020-08-04 14:58:35 +02001147 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001148 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001149
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001150 /* Calculate the MAC, one-shot case. */
1151 uint8_t input[128] = {0};
1152 size_t mac_len;
1153 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1154 input, 128,
1155 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1156 expected_status_sign );
1157
1158 /* Verify correct MAC, one-shot case. */
1159 status = psa_mac_verify( key, exercise_alg, input, 128,
1160 mac, mac_len );
1161
1162 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1163 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001164 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001165 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001166
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001167 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001168
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001169 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001170 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001171 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001172
1173exit:
1174 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001175 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001176 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001177}
1178/* END_CASE */
1179
1180/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001181void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001182 int policy_alg,
1183 int key_type,
1184 data_t *key_data,
1185 int exercise_alg )
1186{
Ronald Cron5425a212020-08-04 14:58:35 +02001187 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001188 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001189 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001190 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001191 psa_status_t status;
1192
Gilles Peskine8817f612018-12-18 00:18:46 +01001193 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001194
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001195 psa_set_key_usage_flags( &attributes, policy_usage );
1196 psa_set_key_algorithm( &attributes, policy_alg );
1197 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001198
Gilles Peskine049c7532019-05-15 20:22:09 +02001199 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001200 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001201
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001202 /* Check if no key usage flag implication is done */
1203 TEST_EQUAL( policy_usage,
1204 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001205
Ronald Cron5425a212020-08-04 14:58:35 +02001206 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001207 if( policy_alg == exercise_alg &&
1208 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001209 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001210 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001211 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001212 psa_cipher_abort( &operation );
1213
Ronald Cron5425a212020-08-04 14:58:35 +02001214 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001215 if( policy_alg == exercise_alg &&
1216 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001217 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001218 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001219 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001220
1221exit:
1222 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001223 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001224 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001225}
1226/* END_CASE */
1227
1228/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001229void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001230 int policy_alg,
1231 int key_type,
1232 data_t *key_data,
1233 int nonce_length_arg,
1234 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001235 int exercise_alg,
1236 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001237{
Ronald Cron5425a212020-08-04 14:58:35 +02001238 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001239 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001240 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001241 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001242 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001243 unsigned char nonce[16] = {0};
1244 size_t nonce_length = nonce_length_arg;
1245 unsigned char tag[16];
1246 size_t tag_length = tag_length_arg;
1247 size_t output_length;
1248
1249 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1250 TEST_ASSERT( tag_length <= sizeof( tag ) );
1251
Gilles Peskine8817f612018-12-18 00:18:46 +01001252 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001253
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001254 psa_set_key_usage_flags( &attributes, policy_usage );
1255 psa_set_key_algorithm( &attributes, policy_alg );
1256 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001257
Gilles Peskine049c7532019-05-15 20:22:09 +02001258 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001259 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001260
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001261 /* Check if no key usage implication is done */
1262 TEST_EQUAL( policy_usage,
1263 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001264
Ronald Cron5425a212020-08-04 14:58:35 +02001265 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001266 nonce, nonce_length,
1267 NULL, 0,
1268 NULL, 0,
1269 tag, tag_length,
1270 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001271 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1272 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001273 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001274 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001275
1276 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001277 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001278 nonce, nonce_length,
1279 NULL, 0,
1280 tag, tag_length,
1281 NULL, 0,
1282 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001283 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1284 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1285 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001286 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001287 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001288 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001289
1290exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001291 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001292 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001293}
1294/* END_CASE */
1295
1296/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001297void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001298 int policy_alg,
1299 int key_type,
1300 data_t *key_data,
1301 int exercise_alg )
1302{
Ronald Cron5425a212020-08-04 14:58:35 +02001303 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001304 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001305 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001306 psa_status_t status;
1307 size_t key_bits;
1308 size_t buffer_length;
1309 unsigned char *buffer = NULL;
1310 size_t output_length;
1311
Gilles Peskine8817f612018-12-18 00:18:46 +01001312 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001313
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001314 psa_set_key_usage_flags( &attributes, policy_usage );
1315 psa_set_key_algorithm( &attributes, policy_alg );
1316 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001317
Gilles Peskine049c7532019-05-15 20:22:09 +02001318 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001319 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001320
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001321 /* Check if no key usage implication is done */
1322 TEST_EQUAL( policy_usage,
1323 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001324
Ronald Cron5425a212020-08-04 14:58:35 +02001325 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001326 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001327 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1328 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001329 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001330
Ronald Cron5425a212020-08-04 14:58:35 +02001331 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001332 NULL, 0,
1333 NULL, 0,
1334 buffer, buffer_length,
1335 &output_length );
1336 if( policy_alg == exercise_alg &&
1337 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001338 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001339 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001340 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001341
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001342 if( buffer_length != 0 )
1343 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001344 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001345 buffer, buffer_length,
1346 NULL, 0,
1347 buffer, buffer_length,
1348 &output_length );
1349 if( policy_alg == exercise_alg &&
1350 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001351 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001352 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001353 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001354
1355exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001356 /*
1357 * Key attributes may have been returned by psa_get_key_attributes()
1358 * thus reset them as required.
1359 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001360 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001361
1362 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001363 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001364 mbedtls_free( buffer );
1365}
1366/* END_CASE */
1367
1368/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001369void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001370 int policy_alg,
1371 int key_type,
1372 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001373 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001374 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001375 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001376{
Ronald Cron5425a212020-08-04 14:58:35 +02001377 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001378 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001379 psa_key_usage_t policy_usage = policy_usage_arg;
1380 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001381 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001382 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1383 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1384 * compatible with the policy and `payload_length_arg` is supposed to be
1385 * a valid input length to sign. If `payload_length_arg <= 0`,
1386 * `exercise_alg` is supposed to be forbidden by the policy. */
1387 int compatible_alg = payload_length_arg > 0;
1388 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001389 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001390 size_t signature_length;
1391
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001392 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001393 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001394 TEST_EQUAL( expected_usage,
1395 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001396
Gilles Peskine8817f612018-12-18 00:18:46 +01001397 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001398
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001399 psa_set_key_usage_flags( &attributes, policy_usage );
1400 psa_set_key_algorithm( &attributes, policy_alg );
1401 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001402
Gilles Peskine049c7532019-05-15 20:22:09 +02001403 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001404 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001405
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001406 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1407
Ronald Cron5425a212020-08-04 14:58:35 +02001408 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001409 payload, payload_length,
1410 signature, sizeof( signature ),
1411 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001412 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001413 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001414 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001415 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001416
1417 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001418 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001419 payload, payload_length,
1420 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001421 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001422 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001423 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001424 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001425
Gilles Peskinef7b41372021-09-22 16:15:05 +02001426 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02001427 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001428 {
1429 status = psa_sign_message( key, exercise_alg,
1430 payload, payload_length,
1431 signature, sizeof( signature ),
1432 &signature_length );
1433 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1434 PSA_ASSERT( status );
1435 else
1436 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1437
1438 memset( signature, 0, sizeof( signature ) );
1439 status = psa_verify_message( key, exercise_alg,
1440 payload, payload_length,
1441 signature, sizeof( signature ) );
1442 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1443 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1444 else
1445 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1446 }
1447
Gilles Peskined5b33222018-06-18 22:20:03 +02001448exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001449 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001450 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001451}
1452/* END_CASE */
1453
Janos Follathba3fab92019-06-11 14:50:16 +01001454/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001455void derive_key_policy( int policy_usage,
1456 int policy_alg,
1457 int key_type,
1458 data_t *key_data,
1459 int exercise_alg )
1460{
Ronald Cron5425a212020-08-04 14:58:35 +02001461 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001462 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001463 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001464 psa_status_t status;
1465
Gilles Peskine8817f612018-12-18 00:18:46 +01001466 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001467
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001468 psa_set_key_usage_flags( &attributes, policy_usage );
1469 psa_set_key_algorithm( &attributes, policy_alg );
1470 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001471
Gilles Peskine049c7532019-05-15 20:22:09 +02001472 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001473 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001474
Janos Follathba3fab92019-06-11 14:50:16 +01001475 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1476
1477 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1478 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001479 {
Janos Follathba3fab92019-06-11 14:50:16 +01001480 PSA_ASSERT( psa_key_derivation_input_bytes(
1481 &operation,
1482 PSA_KEY_DERIVATION_INPUT_SEED,
1483 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001484 }
Janos Follathba3fab92019-06-11 14:50:16 +01001485
1486 status = psa_key_derivation_input_key( &operation,
1487 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001488 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001489
Gilles Peskineea0fb492018-07-12 17:17:20 +02001490 if( policy_alg == exercise_alg &&
1491 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001492 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001493 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001494 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001495
1496exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001497 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001498 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001499 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001500}
1501/* END_CASE */
1502
1503/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001504void agreement_key_policy( int policy_usage,
1505 int policy_alg,
1506 int key_type_arg,
1507 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001508 int exercise_alg,
1509 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001510{
Ronald Cron5425a212020-08-04 14:58:35 +02001511 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001512 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001513 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001514 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001515 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001516 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001517
Gilles Peskine8817f612018-12-18 00:18:46 +01001518 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001519
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001520 psa_set_key_usage_flags( &attributes, policy_usage );
1521 psa_set_key_algorithm( &attributes, policy_alg );
1522 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001523
Gilles Peskine049c7532019-05-15 20:22:09 +02001524 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001525 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001526
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001527 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001528 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001529
Steven Cooremance48e852020-10-05 16:02:45 +02001530 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001531
1532exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001533 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001534 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001535 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001536}
1537/* END_CASE */
1538
1539/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001540void key_policy_alg2( int key_type_arg, data_t *key_data,
1541 int usage_arg, int alg_arg, int alg2_arg )
1542{
Ronald Cron5425a212020-08-04 14:58:35 +02001543 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001544 psa_key_type_t key_type = key_type_arg;
1545 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1546 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1547 psa_key_usage_t usage = usage_arg;
1548 psa_algorithm_t alg = alg_arg;
1549 psa_algorithm_t alg2 = alg2_arg;
1550
1551 PSA_ASSERT( psa_crypto_init( ) );
1552
1553 psa_set_key_usage_flags( &attributes, usage );
1554 psa_set_key_algorithm( &attributes, alg );
1555 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1556 psa_set_key_type( &attributes, key_type );
1557 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001558 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001559
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001560 /* Update the usage flags to obtain implicit usage flags */
1561 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001562 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001563 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1564 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1565 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1566
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001567 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001568 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001569 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001570 goto exit;
1571
1572exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001573 /*
1574 * Key attributes may have been returned by psa_get_key_attributes()
1575 * thus reset them as required.
1576 */
1577 psa_reset_key_attributes( &got_attributes );
1578
Ronald Cron5425a212020-08-04 14:58:35 +02001579 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001580 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001581}
1582/* END_CASE */
1583
1584/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001585void raw_agreement_key_policy( int policy_usage,
1586 int policy_alg,
1587 int key_type_arg,
1588 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001589 int exercise_alg,
1590 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001591{
Ronald Cron5425a212020-08-04 14:58:35 +02001592 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001593 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001594 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001595 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001596 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001597 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001598
1599 PSA_ASSERT( psa_crypto_init( ) );
1600
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001601 psa_set_key_usage_flags( &attributes, policy_usage );
1602 psa_set_key_algorithm( &attributes, policy_alg );
1603 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001604
Gilles Peskine049c7532019-05-15 20:22:09 +02001605 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001606 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001607
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001608 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001609
Steven Cooremance48e852020-10-05 16:02:45 +02001610 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001611
1612exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001613 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001614 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001615 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001616}
1617/* END_CASE */
1618
1619/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001620void copy_success( int source_usage_arg,
1621 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301622 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001623 int type_arg, data_t *material,
1624 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001625 int target_usage_arg,
1626 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301627 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001628 int expected_usage_arg,
1629 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001630{
Gilles Peskineca25db92019-04-19 11:43:08 +02001631 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1632 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001633 psa_key_usage_t expected_usage = expected_usage_arg;
1634 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001635 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05301636 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
1637 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001638 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1639 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001640 uint8_t *export_buffer = NULL;
1641
Gilles Peskine57ab7212019-01-28 13:03:09 +01001642 PSA_ASSERT( psa_crypto_init( ) );
1643
Gilles Peskineca25db92019-04-19 11:43:08 +02001644 /* Prepare the source key. */
1645 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1646 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001647 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001648 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301649 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02001650 PSA_ASSERT( psa_import_key( &source_attributes,
1651 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001652 &source_key ) );
1653 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001654
Gilles Peskineca25db92019-04-19 11:43:08 +02001655 /* Prepare the target attributes. */
1656 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001657 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001658 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001659 }
Archana8a180362021-07-05 02:18:48 +05301660 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02001661
Gilles Peskineca25db92019-04-19 11:43:08 +02001662 if( target_usage_arg != -1 )
1663 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1664 if( target_alg_arg != -1 )
1665 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001666 if( target_alg2_arg != -1 )
1667 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001668
Archana8a180362021-07-05 02:18:48 +05301669
Gilles Peskine57ab7212019-01-28 13:03:09 +01001670 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001671 PSA_ASSERT( psa_copy_key( source_key,
1672 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001673
1674 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001675 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001676
1677 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001678 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001679 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1680 psa_get_key_type( &target_attributes ) );
1681 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1682 psa_get_key_bits( &target_attributes ) );
1683 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1684 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001685 TEST_EQUAL( expected_alg2,
1686 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001687 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1688 {
1689 size_t length;
1690 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001691 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001692 material->len, &length ) );
1693 ASSERT_COMPARE( material->x, material->len,
1694 export_buffer, length );
1695 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001696
Archana8a180362021-07-05 02:18:48 +05301697 if( !psa_key_lifetime_is_external( target_lifetime ) )
1698 {
1699 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
1700 goto exit;
1701 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
1702 goto exit;
1703 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001704
Ronald Cron5425a212020-08-04 14:58:35 +02001705 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001706
1707exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001708 /*
1709 * Source and target key attributes may have been returned by
1710 * psa_get_key_attributes() thus reset them as required.
1711 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001712 psa_reset_key_attributes( &source_attributes );
1713 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001714
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001715 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001716 mbedtls_free( export_buffer );
1717}
1718/* END_CASE */
1719
1720/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001721void copy_fail( int source_usage_arg,
1722 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301723 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001724 int type_arg, data_t *material,
1725 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001726 int target_usage_arg,
1727 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001728 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001729 int expected_status_arg )
1730{
1731 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1732 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001733 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1734 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001735 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001736
1737 PSA_ASSERT( psa_crypto_init( ) );
1738
1739 /* Prepare the source key. */
1740 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1741 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001742 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001743 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301744 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001745 PSA_ASSERT( psa_import_key( &source_attributes,
1746 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001747 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001748
1749 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001750 psa_set_key_id( &target_attributes, key_id );
1751 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001752 psa_set_key_type( &target_attributes, target_type_arg );
1753 psa_set_key_bits( &target_attributes, target_bits_arg );
1754 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1755 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001756 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001757
1758 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001759 TEST_EQUAL( psa_copy_key( source_key,
1760 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001761 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001762
Ronald Cron5425a212020-08-04 14:58:35 +02001763 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001764
Gilles Peskine4a644642019-05-03 17:14:08 +02001765exit:
1766 psa_reset_key_attributes( &source_attributes );
1767 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001768 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001769}
1770/* END_CASE */
1771
1772/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001773void hash_operation_init( )
1774{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001775 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001776 /* Test each valid way of initializing the object, except for `= {0}`, as
1777 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1778 * though it's OK by the C standard. We could test for this, but we'd need
1779 * to supress the Clang warning for the test. */
1780 psa_hash_operation_t func = psa_hash_operation_init( );
1781 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1782 psa_hash_operation_t zero;
1783
1784 memset( &zero, 0, sizeof( zero ) );
1785
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001786 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001787 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1788 PSA_ERROR_BAD_STATE );
1789 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1790 PSA_ERROR_BAD_STATE );
1791 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1792 PSA_ERROR_BAD_STATE );
1793
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001794 /* A default hash operation should be abortable without error. */
1795 PSA_ASSERT( psa_hash_abort( &func ) );
1796 PSA_ASSERT( psa_hash_abort( &init ) );
1797 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001798}
1799/* END_CASE */
1800
1801/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001802void hash_setup( int alg_arg,
1803 int expected_status_arg )
1804{
1805 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01001806 uint8_t *output = NULL;
1807 size_t output_size = 0;
1808 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001809 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001810 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001811 psa_status_t status;
1812
Gilles Peskine8817f612018-12-18 00:18:46 +01001813 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001814
Neil Armstrongedb20862022-02-07 15:47:44 +01001815 /* Hash Setup, one-shot */
1816 output_size = PSA_HASH_LENGTH(alg);
1817 ASSERT_ALLOC( output, output_size );
1818
1819 status = psa_hash_compute( alg, NULL, 0,
1820 output, output_size, &output_length );
1821 TEST_EQUAL( status, expected_status );
1822
1823 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001824 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001825 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001826
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001827 /* Whether setup succeeded or failed, abort must succeed. */
1828 PSA_ASSERT( psa_hash_abort( &operation ) );
1829
1830 /* If setup failed, reproduce the failure, so as to
1831 * test the resulting state of the operation object. */
1832 if( status != PSA_SUCCESS )
1833 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1834
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001835 /* Now the operation object should be reusable. */
1836#if defined(KNOWN_SUPPORTED_HASH_ALG)
1837 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1838 PSA_ASSERT( psa_hash_abort( &operation ) );
1839#endif
1840
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001841exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01001842 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001843 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001844}
1845/* END_CASE */
1846
1847/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001848void hash_compute_fail( int alg_arg, data_t *input,
1849 int output_size_arg, int expected_status_arg )
1850{
1851 psa_algorithm_t alg = alg_arg;
1852 uint8_t *output = NULL;
1853 size_t output_size = output_size_arg;
1854 size_t output_length = INVALID_EXPORT_LENGTH;
1855 psa_status_t expected_status = expected_status_arg;
1856 psa_status_t status;
1857
1858 ASSERT_ALLOC( output, output_size );
1859
1860 PSA_ASSERT( psa_crypto_init( ) );
1861
1862 status = psa_hash_compute( alg, input->x, input->len,
1863 output, output_size, &output_length );
1864 TEST_EQUAL( status, expected_status );
1865 TEST_ASSERT( output_length <= output_size );
1866
1867exit:
1868 mbedtls_free( output );
1869 PSA_DONE( );
1870}
1871/* END_CASE */
1872
1873/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001874void hash_compare_fail( int alg_arg, data_t *input,
1875 data_t *reference_hash,
1876 int expected_status_arg )
1877{
1878 psa_algorithm_t alg = alg_arg;
1879 psa_status_t expected_status = expected_status_arg;
1880 psa_status_t status;
1881
1882 PSA_ASSERT( psa_crypto_init( ) );
1883
1884 status = psa_hash_compare( alg, input->x, input->len,
1885 reference_hash->x, reference_hash->len );
1886 TEST_EQUAL( status, expected_status );
1887
1888exit:
1889 PSA_DONE( );
1890}
1891/* END_CASE */
1892
1893/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001894void hash_compute_compare( int alg_arg, data_t *input,
1895 data_t *expected_output )
1896{
1897 psa_algorithm_t alg = alg_arg;
1898 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1899 size_t output_length = INVALID_EXPORT_LENGTH;
1900 size_t i;
1901
1902 PSA_ASSERT( psa_crypto_init( ) );
1903
1904 /* Compute with tight buffer */
1905 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001906 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001907 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001908 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001909 ASSERT_COMPARE( output, output_length,
1910 expected_output->x, expected_output->len );
1911
1912 /* Compute with larger buffer */
1913 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1914 output, sizeof( output ),
1915 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001916 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001917 ASSERT_COMPARE( output, output_length,
1918 expected_output->x, expected_output->len );
1919
1920 /* Compare with correct hash */
1921 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1922 output, output_length ) );
1923
1924 /* Compare with trailing garbage */
1925 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1926 output, output_length + 1 ),
1927 PSA_ERROR_INVALID_SIGNATURE );
1928
1929 /* Compare with truncated hash */
1930 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1931 output, output_length - 1 ),
1932 PSA_ERROR_INVALID_SIGNATURE );
1933
1934 /* Compare with corrupted value */
1935 for( i = 0; i < output_length; i++ )
1936 {
Chris Jones9634bb12021-01-20 15:56:42 +00001937 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001938 output[i] ^= 1;
1939 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1940 output, output_length ),
1941 PSA_ERROR_INVALID_SIGNATURE );
1942 output[i] ^= 1;
1943 }
1944
1945exit:
1946 PSA_DONE( );
1947}
1948/* END_CASE */
1949
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001950/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001951void hash_bad_order( )
1952{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001953 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001954 unsigned char input[] = "";
1955 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001956 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001957 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1958 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1959 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001960 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001961 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001962 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001963
Gilles Peskine8817f612018-12-18 00:18:46 +01001964 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001965
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001966 /* Call setup twice in a row. */
1967 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001968 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001969 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1970 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001971 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001972 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001973 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001974
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001975 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001976 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001977 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001978 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001979
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001980 /* Check that update calls abort on error. */
1981 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01001982 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001983 ASSERT_OPERATION_IS_ACTIVE( operation );
1984 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1985 PSA_ERROR_BAD_STATE );
1986 ASSERT_OPERATION_IS_INACTIVE( operation );
1987 PSA_ASSERT( psa_hash_abort( &operation ) );
1988 ASSERT_OPERATION_IS_INACTIVE( operation );
1989
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001990 /* Call update after finish. */
1991 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1992 PSA_ASSERT( psa_hash_finish( &operation,
1993 hash, sizeof( hash ), &hash_len ) );
1994 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001995 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001996 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001997
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001998 /* Call verify without calling setup beforehand. */
1999 TEST_EQUAL( psa_hash_verify( &operation,
2000 valid_hash, sizeof( valid_hash ) ),
2001 PSA_ERROR_BAD_STATE );
2002 PSA_ASSERT( psa_hash_abort( &operation ) );
2003
2004 /* Call verify after finish. */
2005 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2006 PSA_ASSERT( psa_hash_finish( &operation,
2007 hash, sizeof( hash ), &hash_len ) );
2008 TEST_EQUAL( psa_hash_verify( &operation,
2009 valid_hash, sizeof( valid_hash ) ),
2010 PSA_ERROR_BAD_STATE );
2011 PSA_ASSERT( psa_hash_abort( &operation ) );
2012
2013 /* Call verify twice in a row. */
2014 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002015 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002016 PSA_ASSERT( psa_hash_verify( &operation,
2017 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002018 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002019 TEST_EQUAL( psa_hash_verify( &operation,
2020 valid_hash, sizeof( valid_hash ) ),
2021 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002022 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002023 PSA_ASSERT( psa_hash_abort( &operation ) );
2024
2025 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002026 TEST_EQUAL( psa_hash_finish( &operation,
2027 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002028 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002029 PSA_ASSERT( psa_hash_abort( &operation ) );
2030
2031 /* Call finish twice in a row. */
2032 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2033 PSA_ASSERT( psa_hash_finish( &operation,
2034 hash, sizeof( hash ), &hash_len ) );
2035 TEST_EQUAL( psa_hash_finish( &operation,
2036 hash, sizeof( hash ), &hash_len ),
2037 PSA_ERROR_BAD_STATE );
2038 PSA_ASSERT( psa_hash_abort( &operation ) );
2039
2040 /* Call finish after calling verify. */
2041 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2042 PSA_ASSERT( psa_hash_verify( &operation,
2043 valid_hash, sizeof( valid_hash ) ) );
2044 TEST_EQUAL( psa_hash_finish( &operation,
2045 hash, sizeof( hash ), &hash_len ),
2046 PSA_ERROR_BAD_STATE );
2047 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002048
2049exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002050 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002051}
2052/* END_CASE */
2053
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002054/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002055void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002056{
2057 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002058 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2059 * appended to it */
2060 unsigned char hash[] = {
2061 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2062 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2063 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002064 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002065 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002066
Gilles Peskine8817f612018-12-18 00:18:46 +01002067 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002068
itayzafrir27e69452018-11-01 14:26:34 +02002069 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002070 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002071 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002072 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002073 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002074 ASSERT_OPERATION_IS_INACTIVE( operation );
2075 PSA_ASSERT( psa_hash_abort( &operation ) );
2076 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002077
itayzafrir27e69452018-11-01 14:26:34 +02002078 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002079 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002080 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002081 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002082
itayzafrir27e69452018-11-01 14:26:34 +02002083 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002084 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002085 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002086 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002087
itayzafrirec93d302018-10-18 18:01:10 +03002088exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002089 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002090}
2091/* END_CASE */
2092
Ronald Cronee414c72021-03-18 18:50:08 +01002093/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002094void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002095{
2096 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002097 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002098 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002099 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002100 size_t hash_len;
2101
Gilles Peskine8817f612018-12-18 00:18:46 +01002102 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002103
itayzafrir58028322018-10-25 10:22:01 +03002104 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002105 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002106 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002107 hash, expected_size - 1, &hash_len ),
2108 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002109
2110exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002111 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002112}
2113/* END_CASE */
2114
Ronald Cronee414c72021-03-18 18:50:08 +01002115/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002116void hash_clone_source_state( )
2117{
2118 psa_algorithm_t alg = PSA_ALG_SHA_256;
2119 unsigned char hash[PSA_HASH_MAX_SIZE];
2120 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2121 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2122 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2123 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2124 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2125 size_t hash_len;
2126
2127 PSA_ASSERT( psa_crypto_init( ) );
2128 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2129
2130 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2131 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2132 PSA_ASSERT( psa_hash_finish( &op_finished,
2133 hash, sizeof( hash ), &hash_len ) );
2134 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2135 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2136
2137 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2138 PSA_ERROR_BAD_STATE );
2139
2140 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2141 PSA_ASSERT( psa_hash_finish( &op_init,
2142 hash, sizeof( hash ), &hash_len ) );
2143 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2144 PSA_ASSERT( psa_hash_finish( &op_finished,
2145 hash, sizeof( hash ), &hash_len ) );
2146 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2147 PSA_ASSERT( psa_hash_finish( &op_aborted,
2148 hash, sizeof( hash ), &hash_len ) );
2149
2150exit:
2151 psa_hash_abort( &op_source );
2152 psa_hash_abort( &op_init );
2153 psa_hash_abort( &op_setup );
2154 psa_hash_abort( &op_finished );
2155 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002156 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002157}
2158/* END_CASE */
2159
Ronald Cronee414c72021-03-18 18:50:08 +01002160/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002161void hash_clone_target_state( )
2162{
2163 psa_algorithm_t alg = PSA_ALG_SHA_256;
2164 unsigned char hash[PSA_HASH_MAX_SIZE];
2165 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2166 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2167 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2168 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2169 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2170 size_t hash_len;
2171
2172 PSA_ASSERT( psa_crypto_init( ) );
2173
2174 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2175 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2176 PSA_ASSERT( psa_hash_finish( &op_finished,
2177 hash, sizeof( hash ), &hash_len ) );
2178 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2179 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2180
2181 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2182 PSA_ASSERT( psa_hash_finish( &op_target,
2183 hash, sizeof( hash ), &hash_len ) );
2184
2185 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2186 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2187 PSA_ERROR_BAD_STATE );
2188 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2189 PSA_ERROR_BAD_STATE );
2190
2191exit:
2192 psa_hash_abort( &op_target );
2193 psa_hash_abort( &op_init );
2194 psa_hash_abort( &op_setup );
2195 psa_hash_abort( &op_finished );
2196 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002197 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002198}
2199/* END_CASE */
2200
itayzafrir58028322018-10-25 10:22:01 +03002201/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002202void mac_operation_init( )
2203{
Jaeden Amero252ef282019-02-15 14:05:35 +00002204 const uint8_t input[1] = { 0 };
2205
Jaeden Amero769ce272019-01-04 11:48:03 +00002206 /* Test each valid way of initializing the object, except for `= {0}`, as
2207 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2208 * though it's OK by the C standard. We could test for this, but we'd need
2209 * to supress the Clang warning for the test. */
2210 psa_mac_operation_t func = psa_mac_operation_init( );
2211 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2212 psa_mac_operation_t zero;
2213
2214 memset( &zero, 0, sizeof( zero ) );
2215
Jaeden Amero252ef282019-02-15 14:05:35 +00002216 /* A freshly-initialized MAC operation should not be usable. */
2217 TEST_EQUAL( psa_mac_update( &func,
2218 input, sizeof( input ) ),
2219 PSA_ERROR_BAD_STATE );
2220 TEST_EQUAL( psa_mac_update( &init,
2221 input, sizeof( input ) ),
2222 PSA_ERROR_BAD_STATE );
2223 TEST_EQUAL( psa_mac_update( &zero,
2224 input, sizeof( input ) ),
2225 PSA_ERROR_BAD_STATE );
2226
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002227 /* A default MAC operation should be abortable without error. */
2228 PSA_ASSERT( psa_mac_abort( &func ) );
2229 PSA_ASSERT( psa_mac_abort( &init ) );
2230 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002231}
2232/* END_CASE */
2233
2234/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002235void mac_setup( int key_type_arg,
2236 data_t *key,
2237 int alg_arg,
2238 int expected_status_arg )
2239{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002240 psa_key_type_t key_type = key_type_arg;
2241 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002242 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002243 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002244 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2245#if defined(KNOWN_SUPPORTED_MAC_ALG)
2246 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2247#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002248
Gilles Peskine8817f612018-12-18 00:18:46 +01002249 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002250
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002251 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2252 &operation, &status ) )
2253 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002254 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002255
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002256 /* The operation object should be reusable. */
2257#if defined(KNOWN_SUPPORTED_MAC_ALG)
2258 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2259 smoke_test_key_data,
2260 sizeof( smoke_test_key_data ),
2261 KNOWN_SUPPORTED_MAC_ALG,
2262 &operation, &status ) )
2263 goto exit;
2264 TEST_EQUAL( status, PSA_SUCCESS );
2265#endif
2266
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002267exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002268 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002269}
2270/* END_CASE */
2271
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002272/* 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 +00002273void mac_bad_order( )
2274{
Ronald Cron5425a212020-08-04 14:58:35 +02002275 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002276 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2277 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002278 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002279 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2280 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2281 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002282 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002283 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2284 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2285 size_t sign_mac_length = 0;
2286 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2287 const uint8_t verify_mac[] = {
2288 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2289 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2290 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2291
2292 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002293 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002294 psa_set_key_algorithm( &attributes, alg );
2295 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002296
Ronald Cron5425a212020-08-04 14:58:35 +02002297 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2298 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002299
Jaeden Amero252ef282019-02-15 14:05:35 +00002300 /* Call update without calling setup beforehand. */
2301 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2302 PSA_ERROR_BAD_STATE );
2303 PSA_ASSERT( psa_mac_abort( &operation ) );
2304
2305 /* Call sign finish without calling setup beforehand. */
2306 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2307 &sign_mac_length),
2308 PSA_ERROR_BAD_STATE );
2309 PSA_ASSERT( psa_mac_abort( &operation ) );
2310
2311 /* Call verify finish without calling setup beforehand. */
2312 TEST_EQUAL( psa_mac_verify_finish( &operation,
2313 verify_mac, sizeof( verify_mac ) ),
2314 PSA_ERROR_BAD_STATE );
2315 PSA_ASSERT( psa_mac_abort( &operation ) );
2316
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002317 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002318 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002319 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002320 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002321 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002322 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002323 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002324 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002325
Jaeden Amero252ef282019-02-15 14:05:35 +00002326 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002327 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002328 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2329 PSA_ASSERT( psa_mac_sign_finish( &operation,
2330 sign_mac, sizeof( sign_mac ),
2331 &sign_mac_length ) );
2332 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2333 PSA_ERROR_BAD_STATE );
2334 PSA_ASSERT( psa_mac_abort( &operation ) );
2335
2336 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002337 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002338 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2339 PSA_ASSERT( psa_mac_verify_finish( &operation,
2340 verify_mac, sizeof( verify_mac ) ) );
2341 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2342 PSA_ERROR_BAD_STATE );
2343 PSA_ASSERT( psa_mac_abort( &operation ) );
2344
2345 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002346 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002347 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2348 PSA_ASSERT( psa_mac_sign_finish( &operation,
2349 sign_mac, sizeof( sign_mac ),
2350 &sign_mac_length ) );
2351 TEST_EQUAL( psa_mac_sign_finish( &operation,
2352 sign_mac, sizeof( sign_mac ),
2353 &sign_mac_length ),
2354 PSA_ERROR_BAD_STATE );
2355 PSA_ASSERT( psa_mac_abort( &operation ) );
2356
2357 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002358 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002359 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2360 PSA_ASSERT( psa_mac_verify_finish( &operation,
2361 verify_mac, sizeof( verify_mac ) ) );
2362 TEST_EQUAL( psa_mac_verify_finish( &operation,
2363 verify_mac, sizeof( verify_mac ) ),
2364 PSA_ERROR_BAD_STATE );
2365 PSA_ASSERT( psa_mac_abort( &operation ) );
2366
2367 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002368 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002369 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002370 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002371 TEST_EQUAL( psa_mac_verify_finish( &operation,
2372 verify_mac, sizeof( verify_mac ) ),
2373 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002374 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002375 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002376 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002377
2378 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002379 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002380 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002381 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002382 TEST_EQUAL( psa_mac_sign_finish( &operation,
2383 sign_mac, sizeof( sign_mac ),
2384 &sign_mac_length ),
2385 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002386 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002387 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002388 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002389
Ronald Cron5425a212020-08-04 14:58:35 +02002390 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002391
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002392exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002393 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002394}
2395/* END_CASE */
2396
2397/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002398void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002399 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002400 int alg_arg,
2401 data_t *input,
2402 data_t *expected_mac )
2403{
Ronald Cron5425a212020-08-04 14:58:35 +02002404 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002405 psa_key_type_t key_type = key_type_arg;
2406 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002407 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002408 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002409 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002410 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002411 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002412 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002413 const size_t output_sizes_to_test[] = {
2414 0,
2415 1,
2416 expected_mac->len - 1,
2417 expected_mac->len,
2418 expected_mac->len + 1,
2419 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002420
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002421 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002422 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002423 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002424
Gilles Peskine8817f612018-12-18 00:18:46 +01002425 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002426
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002427 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002428 psa_set_key_algorithm( &attributes, alg );
2429 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002430
Ronald Cron5425a212020-08-04 14:58:35 +02002431 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2432 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002433
Gilles Peskine8b356b52020-08-25 23:44:59 +02002434 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2435 {
2436 const size_t output_size = output_sizes_to_test[i];
2437 psa_status_t expected_status =
2438 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2439 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002440
Chris Jones9634bb12021-01-20 15:56:42 +00002441 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002442 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002443
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002444 /* Calculate the MAC, one-shot case. */
2445 TEST_EQUAL( psa_mac_compute( key, alg,
2446 input->x, input->len,
2447 actual_mac, output_size, &mac_length ),
2448 expected_status );
2449 if( expected_status == PSA_SUCCESS )
2450 {
2451 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2452 actual_mac, mac_length );
2453 }
2454
2455 if( output_size > 0 )
2456 memset( actual_mac, 0, output_size );
2457
2458 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002459 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002460 PSA_ASSERT( psa_mac_update( &operation,
2461 input->x, input->len ) );
2462 TEST_EQUAL( psa_mac_sign_finish( &operation,
2463 actual_mac, output_size,
2464 &mac_length ),
2465 expected_status );
2466 PSA_ASSERT( psa_mac_abort( &operation ) );
2467
2468 if( expected_status == PSA_SUCCESS )
2469 {
2470 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2471 actual_mac, mac_length );
2472 }
2473 mbedtls_free( actual_mac );
2474 actual_mac = NULL;
2475 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002476
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002477exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002478 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002479 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002480 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002481 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002482}
2483/* END_CASE */
2484
2485/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002486void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002487 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002488 int alg_arg,
2489 data_t *input,
2490 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002491{
Ronald Cron5425a212020-08-04 14:58:35 +02002492 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002493 psa_key_type_t key_type = key_type_arg;
2494 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002495 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002496 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002497 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002498
Gilles Peskine69c12672018-06-28 00:07:19 +02002499 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2500
Gilles Peskine8817f612018-12-18 00:18:46 +01002501 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002502
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002503 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002504 psa_set_key_algorithm( &attributes, alg );
2505 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002506
Ronald Cron5425a212020-08-04 14:58:35 +02002507 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2508 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002509
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002510 /* Verify correct MAC, one-shot case. */
2511 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2512 expected_mac->x, expected_mac->len ) );
2513
2514 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002515 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002516 PSA_ASSERT( psa_mac_update( &operation,
2517 input->x, input->len ) );
2518 PSA_ASSERT( psa_mac_verify_finish( &operation,
2519 expected_mac->x,
2520 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002521
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002522 /* Test a MAC that's too short, one-shot case. */
2523 TEST_EQUAL( psa_mac_verify( key, alg,
2524 input->x, input->len,
2525 expected_mac->x,
2526 expected_mac->len - 1 ),
2527 PSA_ERROR_INVALID_SIGNATURE );
2528
2529 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002530 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002531 PSA_ASSERT( psa_mac_update( &operation,
2532 input->x, input->len ) );
2533 TEST_EQUAL( psa_mac_verify_finish( &operation,
2534 expected_mac->x,
2535 expected_mac->len - 1 ),
2536 PSA_ERROR_INVALID_SIGNATURE );
2537
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002538 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002539 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2540 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002541 TEST_EQUAL( psa_mac_verify( key, alg,
2542 input->x, input->len,
2543 perturbed_mac, expected_mac->len + 1 ),
2544 PSA_ERROR_INVALID_SIGNATURE );
2545
2546 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002547 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002548 PSA_ASSERT( psa_mac_update( &operation,
2549 input->x, input->len ) );
2550 TEST_EQUAL( psa_mac_verify_finish( &operation,
2551 perturbed_mac,
2552 expected_mac->len + 1 ),
2553 PSA_ERROR_INVALID_SIGNATURE );
2554
2555 /* Test changing one byte. */
2556 for( size_t i = 0; i < expected_mac->len; i++ )
2557 {
Chris Jones9634bb12021-01-20 15:56:42 +00002558 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002559 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002560
2561 TEST_EQUAL( psa_mac_verify( key, alg,
2562 input->x, input->len,
2563 perturbed_mac, expected_mac->len ),
2564 PSA_ERROR_INVALID_SIGNATURE );
2565
Ronald Cron5425a212020-08-04 14:58:35 +02002566 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002567 PSA_ASSERT( psa_mac_update( &operation,
2568 input->x, input->len ) );
2569 TEST_EQUAL( psa_mac_verify_finish( &operation,
2570 perturbed_mac,
2571 expected_mac->len ),
2572 PSA_ERROR_INVALID_SIGNATURE );
2573 perturbed_mac[i] ^= 1;
2574 }
2575
Gilles Peskine8c9def32018-02-08 10:02:12 +01002576exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002577 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002578 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002579 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002580 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002581}
2582/* END_CASE */
2583
2584/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002585void cipher_operation_init( )
2586{
Jaeden Ameroab439972019-02-15 14:12:05 +00002587 const uint8_t input[1] = { 0 };
2588 unsigned char output[1] = { 0 };
2589 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002590 /* Test each valid way of initializing the object, except for `= {0}`, as
2591 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2592 * though it's OK by the C standard. We could test for this, but we'd need
2593 * to supress the Clang warning for the test. */
2594 psa_cipher_operation_t func = psa_cipher_operation_init( );
2595 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2596 psa_cipher_operation_t zero;
2597
2598 memset( &zero, 0, sizeof( zero ) );
2599
Jaeden Ameroab439972019-02-15 14:12:05 +00002600 /* A freshly-initialized cipher operation should not be usable. */
2601 TEST_EQUAL( psa_cipher_update( &func,
2602 input, sizeof( input ),
2603 output, sizeof( output ),
2604 &output_length ),
2605 PSA_ERROR_BAD_STATE );
2606 TEST_EQUAL( psa_cipher_update( &init,
2607 input, sizeof( input ),
2608 output, sizeof( output ),
2609 &output_length ),
2610 PSA_ERROR_BAD_STATE );
2611 TEST_EQUAL( psa_cipher_update( &zero,
2612 input, sizeof( input ),
2613 output, sizeof( output ),
2614 &output_length ),
2615 PSA_ERROR_BAD_STATE );
2616
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002617 /* A default cipher operation should be abortable without error. */
2618 PSA_ASSERT( psa_cipher_abort( &func ) );
2619 PSA_ASSERT( psa_cipher_abort( &init ) );
2620 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002621}
2622/* END_CASE */
2623
2624/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002625void cipher_setup( int key_type_arg,
2626 data_t *key,
2627 int alg_arg,
2628 int expected_status_arg )
2629{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002630 psa_key_type_t key_type = key_type_arg;
2631 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002632 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002633 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002634 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002635#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002636 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2637#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002638
Gilles Peskine8817f612018-12-18 00:18:46 +01002639 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002640
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002641 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2642 &operation, &status ) )
2643 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002644 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002645
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002646 /* The operation object should be reusable. */
2647#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2648 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2649 smoke_test_key_data,
2650 sizeof( smoke_test_key_data ),
2651 KNOWN_SUPPORTED_CIPHER_ALG,
2652 &operation, &status ) )
2653 goto exit;
2654 TEST_EQUAL( status, PSA_SUCCESS );
2655#endif
2656
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002657exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002658 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002659 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002660}
2661/* END_CASE */
2662
Ronald Cronee414c72021-03-18 18:50:08 +01002663/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002664void cipher_bad_order( )
2665{
Ronald Cron5425a212020-08-04 14:58:35 +02002666 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002667 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2668 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002669 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002670 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002671 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002672 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002673 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2674 0xaa, 0xaa, 0xaa, 0xaa };
2675 const uint8_t text[] = {
2676 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2677 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002678 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002679 size_t length = 0;
2680
2681 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002682 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2683 psa_set_key_algorithm( &attributes, alg );
2684 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002685 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2686 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002687
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002688 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002689 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002690 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002691 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002692 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002693 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002694 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002695 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002696
2697 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002698 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002699 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002700 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002701 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002702 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002703 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002704 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002705
Jaeden Ameroab439972019-02-15 14:12:05 +00002706 /* Generate an IV without calling setup beforehand. */
2707 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2708 buffer, sizeof( buffer ),
2709 &length ),
2710 PSA_ERROR_BAD_STATE );
2711 PSA_ASSERT( psa_cipher_abort( &operation ) );
2712
2713 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002714 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002715 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2716 buffer, sizeof( buffer ),
2717 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002718 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002719 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2720 buffer, sizeof( buffer ),
2721 &length ),
2722 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002723 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002724 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002725 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002726
2727 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002728 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002729 PSA_ASSERT( psa_cipher_set_iv( &operation,
2730 iv, sizeof( iv ) ) );
2731 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2732 buffer, sizeof( buffer ),
2733 &length ),
2734 PSA_ERROR_BAD_STATE );
2735 PSA_ASSERT( psa_cipher_abort( &operation ) );
2736
2737 /* Set an IV without calling setup beforehand. */
2738 TEST_EQUAL( psa_cipher_set_iv( &operation,
2739 iv, sizeof( iv ) ),
2740 PSA_ERROR_BAD_STATE );
2741 PSA_ASSERT( psa_cipher_abort( &operation ) );
2742
2743 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002744 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002745 PSA_ASSERT( psa_cipher_set_iv( &operation,
2746 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002747 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002748 TEST_EQUAL( psa_cipher_set_iv( &operation,
2749 iv, sizeof( iv ) ),
2750 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002751 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002752 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002753 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002754
2755 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002756 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002757 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2758 buffer, sizeof( buffer ),
2759 &length ) );
2760 TEST_EQUAL( psa_cipher_set_iv( &operation,
2761 iv, sizeof( iv ) ),
2762 PSA_ERROR_BAD_STATE );
2763 PSA_ASSERT( psa_cipher_abort( &operation ) );
2764
2765 /* Call update without calling setup beforehand. */
2766 TEST_EQUAL( psa_cipher_update( &operation,
2767 text, sizeof( text ),
2768 buffer, sizeof( buffer ),
2769 &length ),
2770 PSA_ERROR_BAD_STATE );
2771 PSA_ASSERT( psa_cipher_abort( &operation ) );
2772
2773 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002774 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002775 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002776 TEST_EQUAL( psa_cipher_update( &operation,
2777 text, sizeof( text ),
2778 buffer, sizeof( buffer ),
2779 &length ),
2780 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002781 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002782 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002783 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002784
2785 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002786 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002787 PSA_ASSERT( psa_cipher_set_iv( &operation,
2788 iv, sizeof( iv ) ) );
2789 PSA_ASSERT( psa_cipher_finish( &operation,
2790 buffer, sizeof( buffer ), &length ) );
2791 TEST_EQUAL( psa_cipher_update( &operation,
2792 text, sizeof( text ),
2793 buffer, sizeof( buffer ),
2794 &length ),
2795 PSA_ERROR_BAD_STATE );
2796 PSA_ASSERT( psa_cipher_abort( &operation ) );
2797
2798 /* Call finish without calling setup beforehand. */
2799 TEST_EQUAL( psa_cipher_finish( &operation,
2800 buffer, sizeof( buffer ), &length ),
2801 PSA_ERROR_BAD_STATE );
2802 PSA_ASSERT( psa_cipher_abort( &operation ) );
2803
2804 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002805 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002806 /* Not calling update means we are encrypting an empty buffer, which is OK
2807 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01002808 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002809 TEST_EQUAL( psa_cipher_finish( &operation,
2810 buffer, sizeof( buffer ), &length ),
2811 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002812 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002813 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002814 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002815
2816 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002817 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002818 PSA_ASSERT( psa_cipher_set_iv( &operation,
2819 iv, sizeof( iv ) ) );
2820 PSA_ASSERT( psa_cipher_finish( &operation,
2821 buffer, sizeof( buffer ), &length ) );
2822 TEST_EQUAL( psa_cipher_finish( &operation,
2823 buffer, sizeof( buffer ), &length ),
2824 PSA_ERROR_BAD_STATE );
2825 PSA_ASSERT( psa_cipher_abort( &operation ) );
2826
Ronald Cron5425a212020-08-04 14:58:35 +02002827 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002828
Jaeden Ameroab439972019-02-15 14:12:05 +00002829exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002830 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002831 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002832}
2833/* END_CASE */
2834
2835/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002836void cipher_encrypt_fail( int alg_arg,
2837 int key_type_arg,
2838 data_t *key_data,
2839 data_t *input,
2840 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002841{
Ronald Cron5425a212020-08-04 14:58:35 +02002842 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002843 psa_status_t status;
2844 psa_key_type_t key_type = key_type_arg;
2845 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002846 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002847 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002848 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002849 size_t output_length = 0;
2850 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2851
2852 if ( PSA_ERROR_BAD_STATE != expected_status )
2853 {
2854 PSA_ASSERT( psa_crypto_init( ) );
2855
2856 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2857 psa_set_key_algorithm( &attributes, alg );
2858 psa_set_key_type( &attributes, key_type );
2859
2860 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2861 input->len );
2862 ASSERT_ALLOC( output, output_buffer_size );
2863
2864 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2865 &key ) );
2866 }
2867
2868 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2869 output_buffer_size, &output_length );
2870
2871 TEST_EQUAL( status, expected_status );
2872
2873exit:
2874 mbedtls_free( output );
2875 psa_destroy_key( key );
2876 PSA_DONE( );
2877}
2878/* END_CASE */
2879
2880/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02002881void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
2882 data_t *input, int iv_length,
2883 int expected_result )
2884{
2885 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2886 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2887 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2888 size_t output_buffer_size = 0;
2889 unsigned char *output = NULL;
2890
2891 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2892 ASSERT_ALLOC( output, output_buffer_size );
2893
2894 PSA_ASSERT( psa_crypto_init( ) );
2895
2896 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2897 psa_set_key_algorithm( &attributes, alg );
2898 psa_set_key_type( &attributes, key_type );
2899
2900 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2901 &key ) );
2902 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2903 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
2904 iv_length ) );
2905
2906exit:
2907 psa_cipher_abort( &operation );
2908 mbedtls_free( output );
2909 psa_destroy_key( key );
2910 PSA_DONE( );
2911}
2912/* END_CASE */
2913
2914/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002915void cipher_encrypt_alg_without_iv( int alg_arg,
2916 int key_type_arg,
2917 data_t *key_data,
2918 data_t *input,
2919 data_t *expected_output )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002920{
2921 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2922 psa_key_type_t key_type = key_type_arg;
2923 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02002924 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2925 uint8_t iv[1] = { 0x5a };
2926 size_t iv_length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002927 unsigned char *output = NULL;
2928 size_t output_buffer_size = 0;
2929 size_t output_length = 0;
2930 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2931
2932 PSA_ASSERT( psa_crypto_init( ) );
2933
2934 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2935 psa_set_key_algorithm( &attributes, alg );
2936 psa_set_key_type( &attributes, key_type );
2937
2938 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2939 ASSERT_ALLOC( output, output_buffer_size );
2940
2941 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2942 &key ) );
2943
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02002944 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2945 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
2946 PSA_ERROR_BAD_STATE );
2947 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2948 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
2949 &iv_length ),
2950 PSA_ERROR_BAD_STATE );
2951
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002952 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2953 output_buffer_size, &output_length ) );
2954 TEST_ASSERT( output_length <=
2955 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2956 TEST_ASSERT( output_length <=
2957 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2958
2959 ASSERT_COMPARE( expected_output->x, expected_output->len,
2960 output, output_length );
2961exit:
2962 mbedtls_free( output );
2963 psa_destroy_key( key );
2964 PSA_DONE( );
2965}
2966/* END_CASE */
2967
2968/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01002969void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
2970{
2971 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2972 psa_algorithm_t alg = alg_arg;
2973 psa_key_type_t key_type = key_type_arg;
2974 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2975 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2976 psa_status_t status;
2977
2978 PSA_ASSERT( psa_crypto_init( ) );
2979
2980 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2981 psa_set_key_algorithm( &attributes, alg );
2982 psa_set_key_type( &attributes, key_type );
2983
2984 /* Usage of either of these two size macros would cause divide by zero
2985 * with incorrect key types previously. Input length should be irrelevant
2986 * here. */
2987 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
2988 0 );
2989 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
2990
2991
2992 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2993 &key ) );
2994
2995 /* Should fail due to invalid alg type (to support invalid key type).
2996 * Encrypt or decrypt will end up in the same place. */
2997 status = psa_cipher_encrypt_setup( &operation, key, alg );
2998
2999 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
3000
3001exit:
3002 psa_cipher_abort( &operation );
3003 psa_destroy_key( key );
3004 PSA_DONE( );
3005}
3006/* END_CASE */
3007
3008/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003009void cipher_encrypt_validation( int alg_arg,
3010 int key_type_arg,
3011 data_t *key_data,
3012 data_t *input )
3013{
3014 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3015 psa_key_type_t key_type = key_type_arg;
3016 psa_algorithm_t alg = alg_arg;
3017 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
3018 unsigned char *output1 = NULL;
3019 size_t output1_buffer_size = 0;
3020 size_t output1_length = 0;
3021 unsigned char *output2 = NULL;
3022 size_t output2_buffer_size = 0;
3023 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003024 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003025 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003026 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003027
Gilles Peskine8817f612018-12-18 00:18:46 +01003028 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003029
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003030 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3031 psa_set_key_algorithm( &attributes, alg );
3032 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003033
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003034 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3035 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3036 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
3037 ASSERT_ALLOC( output1, output1_buffer_size );
3038 ASSERT_ALLOC( output2, output2_buffer_size );
3039
Ronald Cron5425a212020-08-04 14:58:35 +02003040 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3041 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003042
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02003043 /* The one-shot cipher encryption uses generated iv so validating
3044 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003045 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
3046 output1_buffer_size, &output1_length ) );
3047 TEST_ASSERT( output1_length <=
3048 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3049 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01003050 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003051
3052 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3053 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003054
Gilles Peskine8817f612018-12-18 00:18:46 +01003055 PSA_ASSERT( psa_cipher_update( &operation,
3056 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003057 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01003058 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003059 TEST_ASSERT( function_output_length <=
3060 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3061 TEST_ASSERT( function_output_length <=
3062 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003063 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003064
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003065 PSA_ASSERT( psa_cipher_finish( &operation,
3066 output2 + output2_length,
3067 output2_buffer_size - output2_length,
3068 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003069 TEST_ASSERT( function_output_length <=
3070 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3071 TEST_ASSERT( function_output_length <=
3072 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003073 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003074
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003075 PSA_ASSERT( psa_cipher_abort( &operation ) );
3076 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
3077 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003078
Gilles Peskine50e586b2018-06-08 14:28:46 +02003079exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003080 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003081 mbedtls_free( output1 );
3082 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003083 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003084 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003085}
3086/* END_CASE */
3087
3088/* BEGIN_CASE */
3089void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003090 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003091 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003092 int first_part_size_arg,
3093 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003094 data_t *expected_output,
3095 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003096{
Ronald Cron5425a212020-08-04 14:58:35 +02003097 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003098 psa_key_type_t key_type = key_type_arg;
3099 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003100 psa_status_t status;
3101 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003102 size_t first_part_size = first_part_size_arg;
3103 size_t output1_length = output1_length_arg;
3104 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003105 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003106 size_t output_buffer_size = 0;
3107 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003108 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003109 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003110 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003111
Gilles Peskine8817f612018-12-18 00:18:46 +01003112 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003113
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003114 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3115 psa_set_key_algorithm( &attributes, alg );
3116 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003117
Ronald Cron5425a212020-08-04 14:58:35 +02003118 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3119 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003120
Ronald Cron5425a212020-08-04 14:58:35 +02003121 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003122
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003123 if( iv->len > 0 )
3124 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003125 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003126 }
3127
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003128 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3129 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003130 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003131
Gilles Peskinee0866522019-02-19 19:44:00 +01003132 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003133 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3134 output, output_buffer_size,
3135 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003136 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003137 TEST_ASSERT( function_output_length <=
3138 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3139 TEST_ASSERT( function_output_length <=
3140 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003141 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003142
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003143 if( first_part_size < input->len )
3144 {
3145 PSA_ASSERT( psa_cipher_update( &operation,
3146 input->x + first_part_size,
3147 input->len - first_part_size,
3148 ( output_buffer_size == 0 ? NULL :
3149 output + total_output_length ),
3150 output_buffer_size - total_output_length,
3151 &function_output_length ) );
3152 TEST_ASSERT( function_output_length == output2_length );
3153 TEST_ASSERT( function_output_length <=
3154 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3155 alg,
3156 input->len - first_part_size ) );
3157 TEST_ASSERT( function_output_length <=
3158 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3159 total_output_length += function_output_length;
3160 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003161
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003162 status = psa_cipher_finish( &operation,
3163 ( output_buffer_size == 0 ? NULL :
3164 output + total_output_length ),
3165 output_buffer_size - total_output_length,
3166 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003167 TEST_ASSERT( function_output_length <=
3168 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3169 TEST_ASSERT( function_output_length <=
3170 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003171 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003172 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003173
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003174 if( expected_status == PSA_SUCCESS )
3175 {
3176 PSA_ASSERT( psa_cipher_abort( &operation ) );
3177
3178 ASSERT_COMPARE( expected_output->x, expected_output->len,
3179 output, total_output_length );
3180 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003181
3182exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003183 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003184 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003185 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003186 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003187}
3188/* END_CASE */
3189
3190/* BEGIN_CASE */
3191void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003192 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003193 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003194 int first_part_size_arg,
3195 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003196 data_t *expected_output,
3197 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003198{
Ronald Cron5425a212020-08-04 14:58:35 +02003199 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003200 psa_key_type_t key_type = key_type_arg;
3201 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003202 psa_status_t status;
3203 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003204 size_t first_part_size = first_part_size_arg;
3205 size_t output1_length = output1_length_arg;
3206 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003207 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003208 size_t output_buffer_size = 0;
3209 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003210 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003211 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003213
Gilles Peskine8817f612018-12-18 00:18:46 +01003214 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003215
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003216 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3217 psa_set_key_algorithm( &attributes, alg );
3218 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003219
Ronald Cron5425a212020-08-04 14:58:35 +02003220 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3221 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003222
Ronald Cron5425a212020-08-04 14:58:35 +02003223 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003224
Steven Cooreman177deba2020-09-07 17:14:14 +02003225 if( iv->len > 0 )
3226 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003227 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003228 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003229
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003230 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3231 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003232 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003233
Gilles Peskinee0866522019-02-19 19:44:00 +01003234 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003235 PSA_ASSERT( psa_cipher_update( &operation,
3236 input->x, first_part_size,
3237 output, output_buffer_size,
3238 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003239 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003240 TEST_ASSERT( function_output_length <=
3241 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3242 TEST_ASSERT( function_output_length <=
3243 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003244 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003245
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003246 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02003247 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003248 PSA_ASSERT( psa_cipher_update( &operation,
3249 input->x + first_part_size,
3250 input->len - first_part_size,
3251 ( output_buffer_size == 0 ? NULL :
3252 output + total_output_length ),
3253 output_buffer_size - total_output_length,
3254 &function_output_length ) );
3255 TEST_ASSERT( function_output_length == output2_length );
3256 TEST_ASSERT( function_output_length <=
3257 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3258 alg,
3259 input->len - first_part_size ) );
3260 TEST_ASSERT( function_output_length <=
3261 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3262 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003263 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003264
Gilles Peskine50e586b2018-06-08 14:28:46 +02003265 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003266 ( output_buffer_size == 0 ? NULL :
3267 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003268 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003269 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003270 TEST_ASSERT( function_output_length <=
3271 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3272 TEST_ASSERT( function_output_length <=
3273 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003274 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003275 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003276
3277 if( expected_status == PSA_SUCCESS )
3278 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003279 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003280
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003281 ASSERT_COMPARE( expected_output->x, expected_output->len,
3282 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003283 }
3284
Gilles Peskine50e586b2018-06-08 14:28:46 +02003285exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003286 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003287 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003288 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003289 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003290}
3291/* END_CASE */
3292
Gilles Peskine50e586b2018-06-08 14:28:46 +02003293/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003294void cipher_decrypt_fail( int alg_arg,
3295 int key_type_arg,
3296 data_t *key_data,
3297 data_t *iv,
3298 data_t *input_arg,
3299 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003300{
3301 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3302 psa_status_t status;
3303 psa_key_type_t key_type = key_type_arg;
3304 psa_algorithm_t alg = alg_arg;
3305 psa_status_t expected_status = expected_status_arg;
3306 unsigned char *input = NULL;
3307 size_t input_buffer_size = 0;
3308 unsigned char *output = NULL;
3309 size_t output_buffer_size = 0;
3310 size_t output_length = 0;
3311 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3312
3313 if ( PSA_ERROR_BAD_STATE != expected_status )
3314 {
3315 PSA_ASSERT( psa_crypto_init( ) );
3316
3317 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3318 psa_set_key_algorithm( &attributes, alg );
3319 psa_set_key_type( &attributes, key_type );
3320
3321 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3322 &key ) );
3323 }
3324
3325 /* Allocate input buffer and copy the iv and the plaintext */
3326 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3327 if ( input_buffer_size > 0 )
3328 {
3329 ASSERT_ALLOC( input, input_buffer_size );
3330 memcpy( input, iv->x, iv->len );
3331 memcpy( input + iv->len, input_arg->x, input_arg->len );
3332 }
3333
3334 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3335 ASSERT_ALLOC( output, output_buffer_size );
3336
3337 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3338 output_buffer_size, &output_length );
3339 TEST_EQUAL( status, expected_status );
3340
3341exit:
3342 mbedtls_free( input );
3343 mbedtls_free( output );
3344 psa_destroy_key( key );
3345 PSA_DONE( );
3346}
3347/* END_CASE */
3348
3349/* BEGIN_CASE */
3350void cipher_decrypt( int alg_arg,
3351 int key_type_arg,
3352 data_t *key_data,
3353 data_t *iv,
3354 data_t *input_arg,
3355 data_t *expected_output )
3356{
3357 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3358 psa_key_type_t key_type = key_type_arg;
3359 psa_algorithm_t alg = alg_arg;
3360 unsigned char *input = NULL;
3361 size_t input_buffer_size = 0;
3362 unsigned char *output = NULL;
3363 size_t output_buffer_size = 0;
3364 size_t output_length = 0;
3365 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3366
3367 PSA_ASSERT( psa_crypto_init( ) );
3368
3369 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3370 psa_set_key_algorithm( &attributes, alg );
3371 psa_set_key_type( &attributes, key_type );
3372
3373 /* Allocate input buffer and copy the iv and the plaintext */
3374 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3375 if ( input_buffer_size > 0 )
3376 {
3377 ASSERT_ALLOC( input, input_buffer_size );
3378 memcpy( input, iv->x, iv->len );
3379 memcpy( input + iv->len, input_arg->x, input_arg->len );
3380 }
3381
3382 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3383 ASSERT_ALLOC( output, output_buffer_size );
3384
3385 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3386 &key ) );
3387
3388 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3389 output_buffer_size, &output_length ) );
3390 TEST_ASSERT( output_length <=
3391 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3392 TEST_ASSERT( output_length <=
3393 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3394
3395 ASSERT_COMPARE( expected_output->x, expected_output->len,
3396 output, output_length );
3397exit:
3398 mbedtls_free( input );
3399 mbedtls_free( output );
3400 psa_destroy_key( key );
3401 PSA_DONE( );
3402}
3403/* END_CASE */
3404
3405/* BEGIN_CASE */
3406void cipher_verify_output( int alg_arg,
3407 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003408 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003409 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003410{
Ronald Cron5425a212020-08-04 14:58:35 +02003411 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003412 psa_key_type_t key_type = key_type_arg;
3413 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003414 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003415 size_t output1_size = 0;
3416 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003417 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003418 size_t output2_size = 0;
3419 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003420 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003421
Gilles Peskine8817f612018-12-18 00:18:46 +01003422 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003423
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003424 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3425 psa_set_key_algorithm( &attributes, alg );
3426 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003427
Ronald Cron5425a212020-08-04 14:58:35 +02003428 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3429 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003430 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003431 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003432
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003433 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3434 output1, output1_size,
3435 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003436 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003437 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003438 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003439 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003440
3441 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003442 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003443
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003444 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3445 output2, output2_size,
3446 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003447 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003448 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003449 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003450 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003451
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003452 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003453
3454exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003455 mbedtls_free( output1 );
3456 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003457 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003458 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003459}
3460/* END_CASE */
3461
3462/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003463void cipher_verify_output_multipart( int alg_arg,
3464 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003465 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003466 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003467 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003468{
Ronald Cron5425a212020-08-04 14:58:35 +02003469 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003470 psa_key_type_t key_type = key_type_arg;
3471 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003472 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003473 unsigned char iv[16] = {0};
3474 size_t iv_size = 16;
3475 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003476 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003477 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003478 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003479 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003480 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003481 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003482 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003483 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3484 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003485 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003486
Gilles Peskine8817f612018-12-18 00:18:46 +01003487 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003488
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003489 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3490 psa_set_key_algorithm( &attributes, alg );
3491 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003492
Ronald Cron5425a212020-08-04 14:58:35 +02003493 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3494 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003495
Ronald Cron5425a212020-08-04 14:58:35 +02003496 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3497 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003498
Steven Cooreman177deba2020-09-07 17:14:14 +02003499 if( alg != PSA_ALG_ECB_NO_PADDING )
3500 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003501 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3502 iv, iv_size,
3503 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003504 }
3505
gabor-mezei-armceface22021-01-21 12:26:17 +01003506 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3507 TEST_ASSERT( output1_buffer_size <=
3508 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003509 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003510
Gilles Peskinee0866522019-02-19 19:44:00 +01003511 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003512
Gilles Peskine8817f612018-12-18 00:18:46 +01003513 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3514 output1, output1_buffer_size,
3515 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003516 TEST_ASSERT( function_output_length <=
3517 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3518 TEST_ASSERT( function_output_length <=
3519 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( 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_update( &operation1,
3523 input->x + first_part_size,
3524 input->len - first_part_size,
3525 output1, output1_buffer_size,
3526 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003527 TEST_ASSERT( function_output_length <=
3528 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3529 alg,
3530 input->len - first_part_size ) );
3531 TEST_ASSERT( function_output_length <=
3532 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003533 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003534
Gilles Peskine8817f612018-12-18 00:18:46 +01003535 PSA_ASSERT( psa_cipher_finish( &operation1,
3536 output1 + output1_length,
3537 output1_buffer_size - output1_length,
3538 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003539 TEST_ASSERT( function_output_length <=
3540 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3541 TEST_ASSERT( function_output_length <=
3542 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003543 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003544
Gilles Peskine8817f612018-12-18 00:18:46 +01003545 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003546
Gilles Peskine048b7f02018-06-08 14:20:49 +02003547 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003548 TEST_ASSERT( output2_buffer_size <=
3549 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3550 TEST_ASSERT( output2_buffer_size <=
3551 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003552 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003553
Steven Cooreman177deba2020-09-07 17:14:14 +02003554 if( iv_length > 0 )
3555 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003556 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3557 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003558 }
Moran Pekerded84402018-06-06 16:36:50 +03003559
Gilles Peskine8817f612018-12-18 00:18:46 +01003560 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3561 output2, output2_buffer_size,
3562 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003563 TEST_ASSERT( function_output_length <=
3564 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3565 TEST_ASSERT( function_output_length <=
3566 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( 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_update( &operation2,
3570 output1 + first_part_size,
3571 output1_length - first_part_size,
3572 output2, output2_buffer_size,
3573 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003574 TEST_ASSERT( function_output_length <=
3575 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3576 alg,
3577 output1_length - first_part_size ) );
3578 TEST_ASSERT( function_output_length <=
3579 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003580 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003581
Gilles Peskine8817f612018-12-18 00:18:46 +01003582 PSA_ASSERT( psa_cipher_finish( &operation2,
3583 output2 + output2_length,
3584 output2_buffer_size - output2_length,
3585 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003586 TEST_ASSERT( function_output_length <=
3587 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3588 TEST_ASSERT( function_output_length <=
3589 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003590 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003591
Gilles Peskine8817f612018-12-18 00:18:46 +01003592 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003593
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003594 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003595
3596exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003597 psa_cipher_abort( &operation1 );
3598 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003599 mbedtls_free( output1 );
3600 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003601 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003602 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003603}
3604/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003605
Gilles Peskine20035e32018-02-03 22:44:14 +01003606/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003607void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003608 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003609 data_t *nonce,
3610 data_t *additional_data,
3611 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003612 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003613{
Ronald Cron5425a212020-08-04 14:58:35 +02003614 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003615 psa_key_type_t key_type = key_type_arg;
3616 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003617 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003618 unsigned char *output_data = NULL;
3619 size_t output_size = 0;
3620 size_t output_length = 0;
3621 unsigned char *output_data2 = NULL;
3622 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003623 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003624 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003625 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003626
Gilles Peskine8817f612018-12-18 00:18:46 +01003627 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003628
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003629 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3630 psa_set_key_algorithm( &attributes, alg );
3631 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003632
Gilles Peskine049c7532019-05-15 20:22:09 +02003633 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003634 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003635 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3636 key_bits = psa_get_key_bits( &attributes );
3637
3638 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3639 alg );
3640 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3641 * should be exact. */
3642 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3643 expected_result != PSA_ERROR_NOT_SUPPORTED )
3644 {
3645 TEST_EQUAL( output_size,
3646 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3647 TEST_ASSERT( output_size <=
3648 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3649 }
3650 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003651
Steven Cooremanf49478b2021-02-15 15:19:25 +01003652 status = psa_aead_encrypt( key, alg,
3653 nonce->x, nonce->len,
3654 additional_data->x,
3655 additional_data->len,
3656 input_data->x, input_data->len,
3657 output_data, output_size,
3658 &output_length );
3659
3660 /* If the operation is not supported, just skip and not fail in case the
3661 * encryption involves a common limitation of cryptography hardwares and
3662 * an alternative implementation. */
3663 if( status == PSA_ERROR_NOT_SUPPORTED )
3664 {
3665 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3666 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3667 }
3668
3669 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003670
3671 if( PSA_SUCCESS == expected_result )
3672 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003673 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003674
Gilles Peskine003a4a92019-05-14 16:09:40 +02003675 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3676 * should be exact. */
3677 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003678 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003679
gabor-mezei-armceface22021-01-21 12:26:17 +01003680 TEST_ASSERT( input_data->len <=
3681 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3682
Ronald Cron5425a212020-08-04 14:58:35 +02003683 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003684 nonce->x, nonce->len,
3685 additional_data->x,
3686 additional_data->len,
3687 output_data, output_length,
3688 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003689 &output_length2 ),
3690 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003691
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003692 ASSERT_COMPARE( input_data->x, input_data->len,
3693 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003694 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003695
Gilles Peskinea1cac842018-06-11 19:33:02 +02003696exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003697 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003698 mbedtls_free( output_data );
3699 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003700 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003701}
3702/* END_CASE */
3703
3704/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003705void aead_encrypt( int key_type_arg, data_t *key_data,
3706 int alg_arg,
3707 data_t *nonce,
3708 data_t *additional_data,
3709 data_t *input_data,
3710 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003711{
Ronald Cron5425a212020-08-04 14:58:35 +02003712 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003713 psa_key_type_t key_type = key_type_arg;
3714 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003715 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003716 unsigned char *output_data = NULL;
3717 size_t output_size = 0;
3718 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003719 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003720 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003721
Gilles Peskine8817f612018-12-18 00:18:46 +01003722 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003723
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003724 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3725 psa_set_key_algorithm( &attributes, alg );
3726 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003727
Gilles Peskine049c7532019-05-15 20:22:09 +02003728 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003729 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003730 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3731 key_bits = psa_get_key_bits( &attributes );
3732
3733 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3734 alg );
3735 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3736 * should be exact. */
3737 TEST_EQUAL( output_size,
3738 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3739 TEST_ASSERT( output_size <=
3740 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3741 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003742
Steven Cooremand588ea12021-01-11 19:36:04 +01003743 status = psa_aead_encrypt( key, alg,
3744 nonce->x, nonce->len,
3745 additional_data->x, additional_data->len,
3746 input_data->x, input_data->len,
3747 output_data, output_size,
3748 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003749
Ronald Cron28a45ed2021-02-09 20:35:42 +01003750 /* If the operation is not supported, just skip and not fail in case the
3751 * encryption involves a common limitation of cryptography hardwares and
3752 * an alternative implementation. */
3753 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003754 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003755 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3756 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003757 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003758
3759 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003760 ASSERT_COMPARE( expected_result->x, expected_result->len,
3761 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003762
Gilles Peskinea1cac842018-06-11 19:33:02 +02003763exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003764 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003765 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003766 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003767}
3768/* END_CASE */
3769
3770/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003771void aead_decrypt( int key_type_arg, data_t *key_data,
3772 int alg_arg,
3773 data_t *nonce,
3774 data_t *additional_data,
3775 data_t *input_data,
3776 data_t *expected_data,
3777 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003778{
Ronald Cron5425a212020-08-04 14:58:35 +02003779 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003780 psa_key_type_t key_type = key_type_arg;
3781 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003782 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003783 unsigned char *output_data = NULL;
3784 size_t output_size = 0;
3785 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003786 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003787 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003788 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003789
Gilles Peskine8817f612018-12-18 00:18:46 +01003790 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003791
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003792 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3793 psa_set_key_algorithm( &attributes, alg );
3794 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003795
Gilles Peskine049c7532019-05-15 20:22:09 +02003796 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003797 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003798 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3799 key_bits = psa_get_key_bits( &attributes );
3800
3801 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3802 alg );
3803 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3804 expected_result != PSA_ERROR_NOT_SUPPORTED )
3805 {
3806 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3807 * should be exact. */
3808 TEST_EQUAL( output_size,
3809 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3810 TEST_ASSERT( output_size <=
3811 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3812 }
3813 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003814
Steven Cooremand588ea12021-01-11 19:36:04 +01003815 status = psa_aead_decrypt( key, alg,
3816 nonce->x, nonce->len,
3817 additional_data->x,
3818 additional_data->len,
3819 input_data->x, input_data->len,
3820 output_data, output_size,
3821 &output_length );
3822
Ronald Cron28a45ed2021-02-09 20:35:42 +01003823 /* If the operation is not supported, just skip and not fail in case the
3824 * decryption involves a common limitation of cryptography hardwares and
3825 * an alternative implementation. */
3826 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003827 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003828 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3829 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003830 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003831
3832 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003833
Gilles Peskine2d277862018-06-18 15:41:12 +02003834 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003835 ASSERT_COMPARE( expected_data->x, expected_data->len,
3836 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003837
Gilles Peskinea1cac842018-06-11 19:33:02 +02003838exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003839 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003840 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003841 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003842}
3843/* END_CASE */
3844
3845/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003846void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3847 int alg_arg,
3848 data_t *nonce,
3849 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003850 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003851 int do_set_lengths,
3852 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003853{
Paul Elliottd3f82412021-06-16 16:52:21 +01003854 size_t ad_part_len = 0;
3855 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01003856 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003857
Paul Elliott32f46ba2021-09-23 18:24:36 +01003858 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003859 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003860 mbedtls_test_set_step( ad_part_len );
3861
3862 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003863 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003864 if( ad_part_len & 0x01 )
3865 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3866 else
3867 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003868 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01003869
3870 /* Split ad into length(ad_part_len) parts. */
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, 0 ) )
3879 break;
3880
3881 /* length(0) part, length(ad_part_len) part, length(0) part... */
3882 mbedtls_test_set_step( 1000 + ad_part_len );
3883
3884 if( !aead_multipart_internal_func( key_type_arg, key_data,
3885 alg_arg, nonce,
3886 additional_data,
3887 ad_part_len,
3888 input_data, -1,
3889 set_lengths_method,
3890 expected_output,
3891 1, 1 ) )
3892 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003893 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003894
Paul Elliott32f46ba2021-09-23 18:24:36 +01003895 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003896 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003897 /* Split data into length(data_part_len) parts. */
3898 mbedtls_test_set_step( 2000 + data_part_len );
3899
3900 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003901 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003902 if( data_part_len & 0x01 )
3903 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3904 else
3905 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003906 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003907
Paul Elliott32f46ba2021-09-23 18:24:36 +01003908 if( !aead_multipart_internal_func( key_type_arg, key_data,
3909 alg_arg, nonce,
3910 additional_data, -1,
3911 input_data, data_part_len,
3912 set_lengths_method,
3913 expected_output,
3914 1, 0 ) )
3915 break;
3916
3917 /* length(0) part, length(data_part_len) part, length(0) part... */
3918 mbedtls_test_set_step( 3000 + data_part_len );
3919
3920 if( !aead_multipart_internal_func( key_type_arg, key_data,
3921 alg_arg, nonce,
3922 additional_data, -1,
3923 input_data, data_part_len,
3924 set_lengths_method,
3925 expected_output,
3926 1, 1 ) )
3927 break;
3928 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003929
Paul Elliott8fc45162021-06-23 16:06:01 +01003930 /* Goto is required to silence warnings about unused labels, as we
3931 * don't actually do any test assertions in this function. */
3932 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003933}
3934/* END_CASE */
3935
3936/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003937void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3938 int alg_arg,
3939 data_t *nonce,
3940 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003941 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003942 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01003943 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003944{
Paul Elliottd3f82412021-06-16 16:52:21 +01003945 size_t ad_part_len = 0;
3946 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01003947 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003948
Paul Elliott32f46ba2021-09-23 18:24:36 +01003949 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003950 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003951 /* Split ad into length(ad_part_len) parts. */
3952 mbedtls_test_set_step( ad_part_len );
3953
3954 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003955 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003956 if( ad_part_len & 0x01 )
3957 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3958 else
3959 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003960 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01003961
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, 0 ) )
3970 break;
3971
3972 /* length(0) part, length(ad_part_len) part, length(0) part... */
3973 mbedtls_test_set_step( 1000 + ad_part_len );
3974
3975 if( !aead_multipart_internal_func( key_type_arg, key_data,
3976 alg_arg, nonce,
3977 additional_data,
3978 ad_part_len,
3979 input_data, -1,
3980 set_lengths_method,
3981 expected_output,
3982 0, 1 ) )
3983 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003984 }
3985
Paul Elliott32f46ba2021-09-23 18:24:36 +01003986 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003987 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003988 /* Split data into length(data_part_len) parts. */
3989 mbedtls_test_set_step( 2000 + data_part_len );
3990
3991 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003992 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003993 if( data_part_len & 0x01 )
3994 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3995 else
3996 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003997 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01003998
3999 if( !aead_multipart_internal_func( key_type_arg, key_data,
4000 alg_arg, nonce,
4001 additional_data, -1,
4002 input_data, data_part_len,
4003 set_lengths_method,
4004 expected_output,
4005 0, 0 ) )
4006 break;
4007
4008 /* length(0) part, length(data_part_len) part, length(0) part... */
4009 mbedtls_test_set_step( 3000 + data_part_len );
4010
4011 if( !aead_multipart_internal_func( key_type_arg, key_data,
4012 alg_arg, nonce,
4013 additional_data, -1,
4014 input_data, data_part_len,
4015 set_lengths_method,
4016 expected_output,
4017 0, 1 ) )
4018 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004019 }
4020
Paul Elliott8fc45162021-06-23 16:06:01 +01004021 /* Goto is required to silence warnings about unused labels, as we
4022 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01004023 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004024}
4025/* END_CASE */
4026
4027/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004028void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
4029 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01004030 int nonce_length,
4031 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004032 data_t *additional_data,
4033 data_t *input_data,
4034 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004035{
4036
4037 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4038 psa_key_type_t key_type = key_type_arg;
4039 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004040 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004041 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4042 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4043 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01004044 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01004045 size_t actual_nonce_length = 0;
4046 size_t expected_nonce_length = expected_nonce_length_arg;
4047 unsigned char *output = NULL;
4048 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004049 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01004050 size_t ciphertext_size = 0;
4051 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004052 size_t tag_length = 0;
4053 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004054
4055 PSA_ASSERT( psa_crypto_init( ) );
4056
4057 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
4058 psa_set_key_algorithm( & attributes, alg );
4059 psa_set_key_type( & attributes, key_type );
4060
4061 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4062 &key ) );
4063
4064 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4065
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004066 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4067
Paul Elliottf1277632021-08-24 18:11:37 +01004068 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004069
Paul Elliottf1277632021-08-24 18:11:37 +01004070 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004071
Paul Elliottf1277632021-08-24 18:11:37 +01004072 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004073
Paul Elliottf1277632021-08-24 18:11:37 +01004074 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004075
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004076 status = psa_aead_encrypt_setup( &operation, key, alg );
4077
4078 /* If the operation is not supported, just skip and not fail in case the
4079 * encryption involves a common limitation of cryptography hardwares and
4080 * an alternative implementation. */
4081 if( status == PSA_ERROR_NOT_SUPPORTED )
4082 {
4083 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01004084 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004085 }
4086
4087 PSA_ASSERT( status );
4088
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004089 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01004090 nonce_length,
4091 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004092
Paul Elliott693bf312021-07-23 17:40:41 +01004093 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004094
Paul Elliottf1277632021-08-24 18:11:37 +01004095 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01004096
Paul Elliott88ecbe12021-09-22 17:23:03 +01004097 if( expected_status == PSA_SUCCESS )
4098 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
4099 alg ) );
4100
Paul Elliottd79c5c52021-10-06 21:49:41 +01004101 TEST_ASSERT( actual_nonce_length <= PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01004102
Paul Elliott693bf312021-07-23 17:40:41 +01004103 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004104 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004105 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01004106 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4107 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004108
4109 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4110 additional_data->len ) );
4111
4112 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01004113 output, output_size,
4114 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004115
Paul Elliottf1277632021-08-24 18:11:37 +01004116 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4117 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004118 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4119 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004120
4121exit:
4122 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01004123 mbedtls_free( output );
4124 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004125 psa_aead_abort( &operation );
4126 PSA_DONE( );
4127}
4128/* END_CASE */
4129
4130/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01004131void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
4132 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01004133 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004134 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01004135 data_t *additional_data,
4136 data_t *input_data,
4137 int expected_status_arg )
4138{
4139
4140 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4141 psa_key_type_t key_type = key_type_arg;
4142 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004143 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01004144 uint8_t *nonce_buffer = NULL;
4145 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4146 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4147 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004148 unsigned char *output = NULL;
4149 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01004150 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01004151 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004152 size_t ciphertext_size = 0;
4153 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01004154 size_t tag_length = 0;
4155 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01004156 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004157 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01004158
4159 PSA_ASSERT( psa_crypto_init( ) );
4160
4161 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4162 psa_set_key_algorithm( &attributes, alg );
4163 psa_set_key_type( &attributes, key_type );
4164
4165 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4166 &key ) );
4167
4168 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4169
4170 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4171
Paul Elliott6f0e7202021-08-25 12:57:18 +01004172 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004173
Paul Elliott6f0e7202021-08-25 12:57:18 +01004174 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01004175
Paul Elliott6f0e7202021-08-25 12:57:18 +01004176 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01004177
Paul Elliott6f0e7202021-08-25 12:57:18 +01004178 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004179
Paul Elliott863864a2021-07-23 17:28:31 +01004180 status = psa_aead_encrypt_setup( &operation, key, alg );
4181
4182 /* If the operation is not supported, just skip and not fail in case the
4183 * encryption involves a common limitation of cryptography hardwares and
4184 * an alternative implementation. */
4185 if( status == PSA_ERROR_NOT_SUPPORTED )
4186 {
4187 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004188 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01004189 }
4190
4191 PSA_ASSERT( status );
4192
Paul Elliott4023ffd2021-09-10 16:21:22 +01004193 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
4194 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01004195 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01004196 /* Arbitrary size buffer, to test zero length valid buffer. */
4197 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004198 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01004199 }
4200 else
4201 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004202 /* If length is zero, then this will return NULL. */
4203 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004204 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01004205
Paul Elliott4023ffd2021-09-10 16:21:22 +01004206 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01004207 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004208 for( index = 0; index < nonce_length - 1; ++index )
4209 {
4210 nonce_buffer[index] = 'a' + index;
4211 }
Paul Elliott66696b52021-08-16 18:42:41 +01004212 }
Paul Elliott863864a2021-07-23 17:28:31 +01004213 }
4214
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004215 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
4216 {
4217 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4218 input_data->len ) );
4219 }
4220
Paul Elliott6f0e7202021-08-25 12:57:18 +01004221 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01004222
Paul Elliott693bf312021-07-23 17:40:41 +01004223 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004224
4225 if( expected_status == PSA_SUCCESS )
4226 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004227 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
4228 {
4229 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4230 input_data->len ) );
4231 }
4232 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
4233 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01004234
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004235 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
4236 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4237 additional_data->len ),
4238 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004239
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004240 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004241 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004242 &ciphertext_length ),
4243 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004244
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004245 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004246 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004247 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
4248 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004249 }
4250
4251exit:
4252 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01004253 mbedtls_free( output );
4254 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01004255 mbedtls_free( nonce_buffer );
4256 psa_aead_abort( &operation );
4257 PSA_DONE( );
4258}
4259/* END_CASE */
4260
4261/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004262void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
4263 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004264 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01004265 data_t *nonce,
4266 data_t *additional_data,
4267 data_t *input_data,
4268 int expected_status_arg )
4269{
4270
4271 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4272 psa_key_type_t key_type = key_type_arg;
4273 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004274 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01004275 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4276 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4277 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01004278 unsigned char *output = NULL;
4279 unsigned char *ciphertext = NULL;
4280 size_t output_size = output_size_arg;
4281 size_t ciphertext_size = 0;
4282 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01004283 size_t tag_length = 0;
4284 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4285
4286 PSA_ASSERT( psa_crypto_init( ) );
4287
4288 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4289 psa_set_key_algorithm( &attributes, alg );
4290 psa_set_key_type( &attributes, key_type );
4291
4292 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4293 &key ) );
4294
4295 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4296
Paul Elliottc6d11d02021-09-01 12:04:23 +01004297 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004298
Paul Elliottc6d11d02021-09-01 12:04:23 +01004299 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01004300
Paul Elliottc6d11d02021-09-01 12:04:23 +01004301 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004302
Paul Elliott43fbda62021-07-23 18:30:59 +01004303 status = psa_aead_encrypt_setup( &operation, key, alg );
4304
4305 /* If the operation is not supported, just skip and not fail in case the
4306 * encryption involves a common limitation of cryptography hardwares and
4307 * an alternative implementation. */
4308 if( status == PSA_ERROR_NOT_SUPPORTED )
4309 {
4310 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4311 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4312 }
4313
4314 PSA_ASSERT( status );
4315
Paul Elliott47b9a142021-10-07 15:04:57 +01004316 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4317 input_data->len ) );
4318
Paul Elliott43fbda62021-07-23 18:30:59 +01004319 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4320
4321 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4322 additional_data->len ) );
4323
4324 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004325 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01004326
4327 TEST_EQUAL( status, expected_status );
4328
4329 if( expected_status == PSA_SUCCESS )
4330 {
4331 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01004332 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4333 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01004334 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4335 }
4336
4337exit:
4338 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01004339 mbedtls_free( output );
4340 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01004341 psa_aead_abort( &operation );
4342 PSA_DONE( );
4343}
4344/* END_CASE */
4345
Paul Elliott91b021e2021-07-23 18:52:31 +01004346/* BEGIN_CASE */
4347void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
4348 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004349 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01004350 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01004351 data_t *nonce,
4352 data_t *additional_data,
4353 data_t *input_data,
4354 int expected_status_arg )
4355{
4356
4357 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4358 psa_key_type_t key_type = key_type_arg;
4359 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004360 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01004361 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4362 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4363 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004364 unsigned char *ciphertext = NULL;
4365 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004366 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004367 size_t ciphertext_size = 0;
4368 size_t ciphertext_length = 0;
4369 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004370 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004371 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004372
4373 PSA_ASSERT( psa_crypto_init( ) );
4374
4375 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4376 psa_set_key_algorithm( &attributes, alg );
4377 psa_set_key_type( &attributes, key_type );
4378
4379 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4380 &key ) );
4381
4382 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4383
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004384 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004385
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004386 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004387
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004388 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004389
Paul Elliott719c1322021-09-13 18:27:22 +01004390 ASSERT_ALLOC( tag_buffer, tag_size );
4391
Paul Elliott91b021e2021-07-23 18:52:31 +01004392 status = psa_aead_encrypt_setup( &operation, key, alg );
4393
4394 /* If the operation is not supported, just skip and not fail in case the
4395 * encryption involves a common limitation of cryptography hardwares and
4396 * an alternative implementation. */
4397 if( status == PSA_ERROR_NOT_SUPPORTED )
4398 {
4399 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4400 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4401 }
4402
4403 PSA_ASSERT( status );
4404
4405 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4406
Paul Elliott76bda482021-10-07 17:07:23 +01004407 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4408 input_data->len ) );
4409
Paul Elliott91b021e2021-07-23 18:52:31 +01004410 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4411 additional_data->len ) );
4412
4413 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004414 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004415
4416 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004417 status = psa_aead_finish( &operation, finish_ciphertext,
4418 finish_ciphertext_size,
4419 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004420 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004421
4422 TEST_EQUAL( status, expected_status );
4423
4424exit:
4425 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004426 mbedtls_free( ciphertext );
4427 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004428 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004429 psa_aead_abort( &operation );
4430 PSA_DONE( );
4431}
4432/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004433
4434/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004435void aead_multipart_verify( int key_type_arg, data_t *key_data,
4436 int alg_arg,
4437 data_t *nonce,
4438 data_t *additional_data,
4439 data_t *input_data,
4440 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004441 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01004442 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004443 int expected_status_arg )
4444{
4445 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4446 psa_key_type_t key_type = key_type_arg;
4447 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004448 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01004449 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4450 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4451 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004452 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01004453 unsigned char *plaintext = NULL;
4454 unsigned char *finish_plaintext = NULL;
4455 size_t plaintext_size = 0;
4456 size_t plaintext_length = 0;
4457 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004458 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004459 unsigned char *tag_buffer = NULL;
4460 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004461
4462 PSA_ASSERT( psa_crypto_init( ) );
4463
4464 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4465 psa_set_key_algorithm( &attributes, alg );
4466 psa_set_key_type( &attributes, key_type );
4467
4468 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4469 &key ) );
4470
4471 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4472
4473 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4474 input_data->len );
4475
4476 ASSERT_ALLOC( plaintext, plaintext_size );
4477
4478 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4479
4480 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4481
Paul Elliott9961a662021-09-17 19:19:02 +01004482 status = psa_aead_decrypt_setup( &operation, key, alg );
4483
4484 /* If the operation is not supported, just skip and not fail in case the
4485 * encryption involves a common limitation of cryptography hardwares and
4486 * an alternative implementation. */
4487 if( status == PSA_ERROR_NOT_SUPPORTED )
4488 {
4489 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4490 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4491 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004492 TEST_EQUAL( status, expected_setup_status );
4493
4494 if( status != PSA_SUCCESS )
4495 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01004496
4497 PSA_ASSERT( status );
4498
4499 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4500
Paul Elliottfec6f372021-10-06 17:15:02 +01004501 status = psa_aead_set_lengths( &operation, additional_data->len,
4502 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01004503 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01004504
Paul Elliott9961a662021-09-17 19:19:02 +01004505 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4506 additional_data->len ) );
4507
4508 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4509 input_data->len,
4510 plaintext, plaintext_size,
4511 &plaintext_length ) );
4512
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004513 if( tag_usage == USE_GIVEN_TAG )
4514 {
4515 tag_buffer = tag->x;
4516 tag_size = tag->len;
4517 }
4518
Paul Elliott9961a662021-09-17 19:19:02 +01004519 status = psa_aead_verify( &operation, finish_plaintext,
4520 verify_plaintext_size,
4521 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004522 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004523
4524 TEST_EQUAL( status, expected_status );
4525
4526exit:
4527 psa_destroy_key( key );
4528 mbedtls_free( plaintext );
4529 mbedtls_free( finish_plaintext );
4530 psa_aead_abort( &operation );
4531 PSA_DONE( );
4532}
4533/* END_CASE */
4534
Paul Elliott9961a662021-09-17 19:19:02 +01004535/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004536void aead_multipart_setup( int key_type_arg, data_t *key_data,
4537 int alg_arg, int expected_status_arg )
4538{
4539 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4540 psa_key_type_t key_type = key_type_arg;
4541 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004542 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01004543 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4544 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4545 psa_status_t expected_status = expected_status_arg;
4546
4547 PSA_ASSERT( psa_crypto_init( ) );
4548
4549 psa_set_key_usage_flags( &attributes,
4550 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4551 psa_set_key_algorithm( &attributes, alg );
4552 psa_set_key_type( &attributes, key_type );
4553
4554 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4555 &key ) );
4556
Paul Elliott5221ef62021-09-19 17:33:03 +01004557 status = psa_aead_encrypt_setup( &operation, key, alg );
4558
4559 TEST_EQUAL( status, expected_status );
4560
4561 psa_aead_abort( &operation );
4562
Paul Elliott5221ef62021-09-19 17:33:03 +01004563 status = psa_aead_decrypt_setup( &operation, key, alg );
4564
4565 TEST_EQUAL(status, expected_status );
4566
4567exit:
4568 psa_destroy_key( key );
4569 psa_aead_abort( &operation );
4570 PSA_DONE( );
4571}
4572/* END_CASE */
4573
4574/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004575void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4576 int alg_arg,
4577 data_t *nonce,
4578 data_t *additional_data,
4579 data_t *input_data )
4580{
4581 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4582 psa_key_type_t key_type = key_type_arg;
4583 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004584 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01004585 unsigned char *output_data = NULL;
4586 unsigned char *final_data = NULL;
4587 size_t output_size = 0;
4588 size_t finish_output_size = 0;
4589 size_t output_length = 0;
4590 size_t key_bits = 0;
4591 size_t tag_length = 0;
4592 size_t tag_size = 0;
4593 size_t nonce_length = 0;
4594 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4595 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4596 size_t output_part_length = 0;
4597 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4598
4599 PSA_ASSERT( psa_crypto_init( ) );
4600
4601 psa_set_key_usage_flags( & attributes,
4602 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4603 psa_set_key_algorithm( & attributes, alg );
4604 psa_set_key_type( & attributes, key_type );
4605
4606 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4607 &key ) );
4608
4609 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4610 key_bits = psa_get_key_bits( &attributes );
4611
4612 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4613
4614 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4615
4616 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4617
4618 ASSERT_ALLOC( output_data, output_size );
4619
4620 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4621
4622 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4623
4624 ASSERT_ALLOC( final_data, finish_output_size );
4625
4626 /* Test all operations error without calling setup first. */
4627
Paul Elliottc23a9a02021-06-21 18:32:46 +01004628 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4629 PSA_ERROR_BAD_STATE );
4630
4631 psa_aead_abort( &operation );
4632
Paul Elliottc23a9a02021-06-21 18:32:46 +01004633 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4634 PSA_AEAD_NONCE_MAX_SIZE,
4635 &nonce_length ),
4636 PSA_ERROR_BAD_STATE );
4637
4638 psa_aead_abort( &operation );
4639
Paul Elliott481be342021-07-16 17:38:47 +01004640 /* ------------------------------------------------------- */
4641
Paul Elliottc23a9a02021-06-21 18:32:46 +01004642 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4643 input_data->len ),
4644 PSA_ERROR_BAD_STATE );
4645
4646 psa_aead_abort( &operation );
4647
Paul Elliott481be342021-07-16 17:38:47 +01004648 /* ------------------------------------------------------- */
4649
Paul Elliottc23a9a02021-06-21 18:32:46 +01004650 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4651 additional_data->len ),
4652 PSA_ERROR_BAD_STATE );
4653
4654 psa_aead_abort( &operation );
4655
Paul Elliott481be342021-07-16 17:38:47 +01004656 /* ------------------------------------------------------- */
4657
Paul Elliottc23a9a02021-06-21 18:32:46 +01004658 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4659 input_data->len, output_data,
4660 output_size, &output_length ),
4661 PSA_ERROR_BAD_STATE );
4662
4663 psa_aead_abort( &operation );
4664
Paul Elliott481be342021-07-16 17:38:47 +01004665 /* ------------------------------------------------------- */
4666
Paul Elliottc23a9a02021-06-21 18:32:46 +01004667 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4668 finish_output_size,
4669 &output_part_length,
4670 tag_buffer, tag_length,
4671 &tag_size ),
4672 PSA_ERROR_BAD_STATE );
4673
4674 psa_aead_abort( &operation );
4675
Paul Elliott481be342021-07-16 17:38:47 +01004676 /* ------------------------------------------------------- */
4677
Paul Elliottc23a9a02021-06-21 18:32:46 +01004678 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4679 finish_output_size,
4680 &output_part_length,
4681 tag_buffer,
4682 tag_length ),
4683 PSA_ERROR_BAD_STATE );
4684
4685 psa_aead_abort( &operation );
4686
4687 /* Test for double setups. */
4688
Paul Elliottc23a9a02021-06-21 18:32:46 +01004689 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4690
4691 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4692 PSA_ERROR_BAD_STATE );
4693
4694 psa_aead_abort( &operation );
4695
Paul Elliott481be342021-07-16 17:38:47 +01004696 /* ------------------------------------------------------- */
4697
Paul Elliottc23a9a02021-06-21 18:32:46 +01004698 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4699
4700 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4701 PSA_ERROR_BAD_STATE );
4702
4703 psa_aead_abort( &operation );
4704
Paul Elliott374a2be2021-07-16 17:53:40 +01004705 /* ------------------------------------------------------- */
4706
Paul Elliott374a2be2021-07-16 17:53:40 +01004707 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4708
4709 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4710 PSA_ERROR_BAD_STATE );
4711
4712 psa_aead_abort( &operation );
4713
4714 /* ------------------------------------------------------- */
4715
Paul Elliott374a2be2021-07-16 17:53:40 +01004716 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4717
4718 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4719 PSA_ERROR_BAD_STATE );
4720
4721 psa_aead_abort( &operation );
4722
Paul Elliottc23a9a02021-06-21 18:32:46 +01004723 /* Test for not setting a nonce. */
4724
Paul Elliottc23a9a02021-06-21 18:32:46 +01004725 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4726
4727 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4728 additional_data->len ),
4729 PSA_ERROR_BAD_STATE );
4730
4731 psa_aead_abort( &operation );
4732
Paul Elliott7f628422021-09-01 12:08:29 +01004733 /* ------------------------------------------------------- */
4734
Paul Elliott7f628422021-09-01 12:08:29 +01004735 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4736
4737 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4738 input_data->len, output_data,
4739 output_size, &output_length ),
4740 PSA_ERROR_BAD_STATE );
4741
4742 psa_aead_abort( &operation );
4743
Paul Elliottbdc2c682021-09-21 18:37:10 +01004744 /* ------------------------------------------------------- */
4745
Paul Elliottbdc2c682021-09-21 18:37:10 +01004746 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4747
4748 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4749 finish_output_size,
4750 &output_part_length,
4751 tag_buffer, tag_length,
4752 &tag_size ),
4753 PSA_ERROR_BAD_STATE );
4754
4755 psa_aead_abort( &operation );
4756
4757 /* ------------------------------------------------------- */
4758
Paul Elliottbdc2c682021-09-21 18:37:10 +01004759 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4760
4761 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4762 finish_output_size,
4763 &output_part_length,
4764 tag_buffer,
4765 tag_length ),
4766 PSA_ERROR_BAD_STATE );
4767
4768 psa_aead_abort( &operation );
4769
Paul Elliottc23a9a02021-06-21 18:32:46 +01004770 /* Test for double setting nonce. */
4771
Paul Elliottc23a9a02021-06-21 18:32:46 +01004772 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4773
4774 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4775
4776 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4777 PSA_ERROR_BAD_STATE );
4778
4779 psa_aead_abort( &operation );
4780
Paul Elliott374a2be2021-07-16 17:53:40 +01004781 /* Test for double generating nonce. */
4782
Paul Elliott374a2be2021-07-16 17:53:40 +01004783 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4784
4785 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4786 PSA_AEAD_NONCE_MAX_SIZE,
4787 &nonce_length ) );
4788
4789 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4790 PSA_AEAD_NONCE_MAX_SIZE,
4791 &nonce_length ),
4792 PSA_ERROR_BAD_STATE );
4793
4794
4795 psa_aead_abort( &operation );
4796
4797 /* Test for generate nonce then set and vice versa */
4798
Paul Elliott374a2be2021-07-16 17:53:40 +01004799 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4800
4801 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4802 PSA_AEAD_NONCE_MAX_SIZE,
4803 &nonce_length ) );
4804
4805 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4806 PSA_ERROR_BAD_STATE );
4807
4808 psa_aead_abort( &operation );
4809
Andrzej Kurekad837522021-12-15 15:28:49 +01004810 /* Test for generating nonce after calling set lengths */
4811
4812 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4813
4814 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4815 input_data->len ) );
4816
4817 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4818 PSA_AEAD_NONCE_MAX_SIZE,
4819 &nonce_length ) );
4820
4821 psa_aead_abort( &operation );
4822
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004823 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01004824
4825 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4826
4827 if( operation.alg == PSA_ALG_CCM )
4828 {
4829 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
4830 input_data->len ),
4831 PSA_ERROR_INVALID_ARGUMENT );
4832 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4833 PSA_AEAD_NONCE_MAX_SIZE,
4834 &nonce_length ),
4835 PSA_ERROR_BAD_STATE );
4836 }
4837 else
4838 {
4839 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
4840 input_data->len ) );
4841 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4842 PSA_AEAD_NONCE_MAX_SIZE,
4843 &nonce_length ) );
4844 }
4845
4846 psa_aead_abort( &operation );
4847
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004848 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01004849#if SIZE_MAX > UINT32_MAX
4850 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4851
4852 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
4853 {
4854 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
4855 input_data->len ),
4856 PSA_ERROR_INVALID_ARGUMENT );
4857 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4858 PSA_AEAD_NONCE_MAX_SIZE,
4859 &nonce_length ),
4860 PSA_ERROR_BAD_STATE );
4861 }
4862 else
4863 {
4864 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
4865 input_data->len ) );
4866 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4867 PSA_AEAD_NONCE_MAX_SIZE,
4868 &nonce_length ) );
4869 }
4870
4871 psa_aead_abort( &operation );
4872#endif
4873
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004874 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01004875
4876 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4877
4878 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4879 PSA_AEAD_NONCE_MAX_SIZE,
4880 &nonce_length ) );
4881
4882 if( operation.alg == PSA_ALG_CCM )
4883 {
4884 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
4885 input_data->len ),
4886 PSA_ERROR_INVALID_ARGUMENT );
4887 }
4888 else
4889 {
4890 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
4891 input_data->len ) );
4892 }
4893
4894 psa_aead_abort( &operation );
4895
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01004896 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01004897 /* Test for setting nonce after calling set lengths */
4898
4899 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4900
4901 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4902 input_data->len ) );
4903
4904 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4905
4906 psa_aead_abort( &operation );
4907
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01004908 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01004909
4910 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4911
4912 if( operation.alg == PSA_ALG_CCM )
4913 {
4914 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
4915 input_data->len ),
4916 PSA_ERROR_INVALID_ARGUMENT );
4917 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4918 PSA_ERROR_BAD_STATE );
4919 }
4920 else
4921 {
4922 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
4923 input_data->len ) );
4924 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4925 }
4926
4927 psa_aead_abort( &operation );
4928
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01004929 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01004930#if SIZE_MAX > UINT32_MAX
4931 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4932
4933 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
4934 {
4935 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
4936 input_data->len ),
4937 PSA_ERROR_INVALID_ARGUMENT );
4938 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4939 PSA_ERROR_BAD_STATE );
4940 }
4941 else
4942 {
4943 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
4944 input_data->len ) );
4945 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4946 }
4947
4948 psa_aead_abort( &operation );
4949#endif
4950
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004951 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01004952
4953 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4954
4955 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4956
4957 if( operation.alg == PSA_ALG_CCM )
4958 {
4959 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
4960 input_data->len ),
4961 PSA_ERROR_INVALID_ARGUMENT );
4962 }
4963 else
4964 {
4965 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
4966 input_data->len ) );
4967 }
4968
4969 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01004970
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004971 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01004972#if SIZE_MAX > UINT32_MAX
4973 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4974
4975 if( operation.alg == PSA_ALG_GCM )
4976 {
4977 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4978 SIZE_MAX ),
4979 PSA_ERROR_INVALID_ARGUMENT );
4980 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4981 PSA_ERROR_BAD_STATE );
4982 }
4983 else if ( operation.alg != PSA_ALG_CCM )
4984 {
4985 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4986 SIZE_MAX ) );
4987 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4988 }
4989
4990 psa_aead_abort( &operation );
4991
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004992 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01004993 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4994
4995 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4996
4997 if( operation.alg == PSA_ALG_GCM )
4998 {
4999 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5000 SIZE_MAX ),
5001 PSA_ERROR_INVALID_ARGUMENT );
5002 }
5003 else if ( operation.alg != PSA_ALG_CCM )
5004 {
5005 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5006 SIZE_MAX ) );
5007 }
5008
5009 psa_aead_abort( &operation );
5010#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01005011
5012 /* ------------------------------------------------------- */
5013
Paul Elliott374a2be2021-07-16 17:53:40 +01005014 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5015
5016 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5017
5018 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5019 PSA_AEAD_NONCE_MAX_SIZE,
5020 &nonce_length ),
5021 PSA_ERROR_BAD_STATE );
5022
5023 psa_aead_abort( &operation );
5024
Paul Elliott7220cae2021-06-22 17:25:57 +01005025 /* Test for generating nonce in decrypt setup. */
5026
Paul Elliott7220cae2021-06-22 17:25:57 +01005027 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5028
5029 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5030 PSA_AEAD_NONCE_MAX_SIZE,
5031 &nonce_length ),
5032 PSA_ERROR_BAD_STATE );
5033
5034 psa_aead_abort( &operation );
5035
Paul Elliottc23a9a02021-06-21 18:32:46 +01005036 /* Test for setting lengths twice. */
5037
Paul Elliottc23a9a02021-06-21 18:32:46 +01005038 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5039
5040 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5041
5042 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5043 input_data->len ) );
5044
5045 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5046 input_data->len ),
5047 PSA_ERROR_BAD_STATE );
5048
5049 psa_aead_abort( &operation );
5050
Andrzej Kurekad837522021-12-15 15:28:49 +01005051 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005052
Paul Elliottc23a9a02021-06-21 18:32:46 +01005053 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5054
5055 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5056
Andrzej Kurekad837522021-12-15 15:28:49 +01005057 if( operation.alg == PSA_ALG_CCM )
5058 {
Paul Elliottf94bd992021-09-19 18:15:59 +01005059
Andrzej Kurekad837522021-12-15 15:28:49 +01005060 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5061 additional_data->len ),
5062 PSA_ERROR_BAD_STATE );
5063 }
5064 else
5065 {
5066 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5067 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01005068
Andrzej Kurekad837522021-12-15 15:28:49 +01005069 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5070 input_data->len ),
5071 PSA_ERROR_BAD_STATE );
5072 }
Paul Elliottf94bd992021-09-19 18:15:59 +01005073 psa_aead_abort( &operation );
5074
5075 /* ------------------------------------------------------- */
5076
Paul Elliottf94bd992021-09-19 18:15:59 +01005077 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5078
5079 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5080
Andrzej Kurekad837522021-12-15 15:28:49 +01005081 if( operation.alg == PSA_ALG_CCM )
5082 {
5083 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5084 input_data->len, output_data,
5085 output_size, &output_length ),
5086 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005087
Andrzej Kurekad837522021-12-15 15:28:49 +01005088 }
5089 else
5090 {
5091 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5092 input_data->len, output_data,
5093 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005094
Andrzej Kurekad837522021-12-15 15:28:49 +01005095 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5096 input_data->len ),
5097 PSA_ERROR_BAD_STATE );
5098 }
5099 psa_aead_abort( &operation );
5100
5101 /* ------------------------------------------------------- */
5102
5103 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5104
5105 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5106
5107 if( operation.alg == PSA_ALG_CCM )
5108 {
5109 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5110 finish_output_size,
5111 &output_part_length,
5112 tag_buffer, tag_length,
5113 &tag_size ) );
5114 }
5115 else
5116 {
5117 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5118 finish_output_size,
5119 &output_part_length,
5120 tag_buffer, tag_length,
5121 &tag_size ) );
5122
5123 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5124 input_data->len ),
5125 PSA_ERROR_BAD_STATE );
5126 }
5127 psa_aead_abort( &operation );
5128
5129 /* Test for setting lengths after generating nonce + already starting data. */
5130
5131 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5132
5133 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5134 PSA_AEAD_NONCE_MAX_SIZE,
5135 &nonce_length ) );
5136 if( operation.alg == PSA_ALG_CCM )
5137 {
5138
5139 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5140 additional_data->len ),
5141 PSA_ERROR_BAD_STATE );
5142 }
5143 else
5144 {
5145 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5146 additional_data->len ) );
5147
5148 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5149 input_data->len ),
5150 PSA_ERROR_BAD_STATE );
5151 }
5152 psa_aead_abort( &operation );
5153
5154 /* ------------------------------------------------------- */
5155
5156 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5157
5158 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5159 PSA_AEAD_NONCE_MAX_SIZE,
5160 &nonce_length ) );
5161 if( operation.alg == PSA_ALG_CCM )
5162 {
5163 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5164 input_data->len, output_data,
5165 output_size, &output_length ),
5166 PSA_ERROR_BAD_STATE );
5167
5168 }
5169 else
5170 {
5171 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5172 input_data->len, output_data,
5173 output_size, &output_length ) );
5174
5175 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5176 input_data->len ),
5177 PSA_ERROR_BAD_STATE );
5178 }
5179 psa_aead_abort( &operation );
5180
5181 /* ------------------------------------------------------- */
5182
5183 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5184
5185 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5186 PSA_AEAD_NONCE_MAX_SIZE,
5187 &nonce_length ) );
5188 if( operation.alg == PSA_ALG_CCM )
5189 {
5190 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5191 finish_output_size,
5192 &output_part_length,
5193 tag_buffer, tag_length,
5194 &tag_size ) );
5195 }
5196 else
5197 {
5198 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5199 finish_output_size,
5200 &output_part_length,
5201 tag_buffer, tag_length,
5202 &tag_size ) );
5203
5204 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5205 input_data->len ),
5206 PSA_ERROR_BAD_STATE );
5207 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005208 psa_aead_abort( &operation );
5209
Paul Elliott243080c2021-07-21 19:01:17 +01005210 /* Test for not sending any additional data or data after setting non zero
5211 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005212
Paul Elliottc23a9a02021-06-21 18:32:46 +01005213 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5214
5215 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5216
5217 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5218 input_data->len ) );
5219
5220 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5221 finish_output_size,
5222 &output_part_length,
5223 tag_buffer, tag_length,
5224 &tag_size ),
5225 PSA_ERROR_INVALID_ARGUMENT );
5226
5227 psa_aead_abort( &operation );
5228
Paul Elliott243080c2021-07-21 19:01:17 +01005229 /* Test for not sending any additional data or data after setting non-zero
5230 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005231
Paul Elliottc23a9a02021-06-21 18:32:46 +01005232 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5233
5234 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5235
5236 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5237 input_data->len ) );
5238
5239 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5240 finish_output_size,
5241 &output_part_length,
5242 tag_buffer,
5243 tag_length ),
5244 PSA_ERROR_INVALID_ARGUMENT );
5245
5246 psa_aead_abort( &operation );
5247
Paul Elliott243080c2021-07-21 19:01:17 +01005248 /* Test for not sending any additional data after setting a non-zero length
5249 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005250
Paul Elliottc23a9a02021-06-21 18:32:46 +01005251 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5252
5253 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5254
5255 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5256 input_data->len ) );
5257
5258 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5259 input_data->len, output_data,
5260 output_size, &output_length ),
5261 PSA_ERROR_INVALID_ARGUMENT );
5262
5263 psa_aead_abort( &operation );
5264
Paul Elliottf94bd992021-09-19 18:15:59 +01005265 /* Test for not sending any data after setting a non-zero length for it.*/
5266
Paul Elliottf94bd992021-09-19 18:15:59 +01005267 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5268
5269 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5270
5271 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5272 input_data->len ) );
5273
5274 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5275 additional_data->len ) );
5276
5277 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5278 finish_output_size,
5279 &output_part_length,
5280 tag_buffer, tag_length,
5281 &tag_size ),
5282 PSA_ERROR_INVALID_ARGUMENT );
5283
5284 psa_aead_abort( &operation );
5285
Paul Elliottb0450fe2021-09-01 15:06:26 +01005286 /* Test for sending too much additional data after setting lengths. */
5287
Paul Elliottb0450fe2021-09-01 15:06:26 +01005288 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5289
5290 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5291
5292 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5293
5294
5295 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5296 additional_data->len ),
5297 PSA_ERROR_INVALID_ARGUMENT );
5298
5299 psa_aead_abort( &operation );
5300
Paul Elliotta2a09b02021-09-22 14:56:40 +01005301 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005302
5303 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5304
5305 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5306
5307 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5308 input_data->len ) );
5309
5310 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5311 additional_data->len ) );
5312
5313 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5314 1 ),
5315 PSA_ERROR_INVALID_ARGUMENT );
5316
5317 psa_aead_abort( &operation );
5318
Paul Elliottb0450fe2021-09-01 15:06:26 +01005319 /* Test for sending too much data after setting lengths. */
5320
Paul Elliottb0450fe2021-09-01 15:06:26 +01005321 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5322
5323 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5324
5325 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5326
5327 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5328 input_data->len, output_data,
5329 output_size, &output_length ),
5330 PSA_ERROR_INVALID_ARGUMENT );
5331
5332 psa_aead_abort( &operation );
5333
Paul Elliotta2a09b02021-09-22 14:56:40 +01005334 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005335
5336 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5337
5338 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5339
5340 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5341 input_data->len ) );
5342
5343 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5344 additional_data->len ) );
5345
5346 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5347 input_data->len, output_data,
5348 output_size, &output_length ) );
5349
5350 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5351 1, output_data,
5352 output_size, &output_length ),
5353 PSA_ERROR_INVALID_ARGUMENT );
5354
5355 psa_aead_abort( &operation );
5356
Paul Elliottc23a9a02021-06-21 18:32:46 +01005357 /* Test sending additional data after data. */
5358
Paul Elliottc23a9a02021-06-21 18:32:46 +01005359 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5360
5361 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5362
Andrzej Kurekad837522021-12-15 15:28:49 +01005363 if( operation.alg != PSA_ALG_CCM )
5364 {
5365 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5366 input_data->len, output_data,
5367 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005368
Andrzej Kurekad837522021-12-15 15:28:49 +01005369 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5370 additional_data->len ),
5371 PSA_ERROR_BAD_STATE );
5372 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005373 psa_aead_abort( &operation );
5374
Paul Elliott534d0b42021-06-22 19:15:20 +01005375 /* Test calling finish on decryption. */
5376
Paul Elliott534d0b42021-06-22 19:15:20 +01005377 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5378
5379 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5380
5381 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5382 finish_output_size,
5383 &output_part_length,
5384 tag_buffer, tag_length,
5385 &tag_size ),
5386 PSA_ERROR_BAD_STATE );
5387
5388 psa_aead_abort( &operation );
5389
5390 /* Test calling verify on encryption. */
5391
Paul Elliott534d0b42021-06-22 19:15:20 +01005392 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5393
5394 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5395
5396 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5397 finish_output_size,
5398 &output_part_length,
5399 tag_buffer,
5400 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01005401 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01005402
5403 psa_aead_abort( &operation );
5404
5405
Paul Elliottc23a9a02021-06-21 18:32:46 +01005406exit:
5407 psa_destroy_key( key );
5408 psa_aead_abort( &operation );
5409 mbedtls_free( output_data );
5410 mbedtls_free( final_data );
5411 PSA_DONE( );
5412}
5413/* END_CASE */
5414
5415/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005416void signature_size( int type_arg,
5417 int bits,
5418 int alg_arg,
5419 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005420{
5421 psa_key_type_t type = type_arg;
5422 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005423 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005424
Gilles Peskinefe11b722018-12-18 00:24:04 +01005425 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005426
Gilles Peskinee59236f2018-01-27 23:32:46 +01005427exit:
5428 ;
5429}
5430/* END_CASE */
5431
5432/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005433void sign_hash_deterministic( int key_type_arg, data_t *key_data,
5434 int alg_arg, data_t *input_data,
5435 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005436{
Ronald Cron5425a212020-08-04 14:58:35 +02005437 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005438 psa_key_type_t key_type = key_type_arg;
5439 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005440 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01005441 unsigned char *signature = NULL;
5442 size_t signature_size;
5443 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005444 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005445
Gilles Peskine8817f612018-12-18 00:18:46 +01005446 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005447
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005448 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005449 psa_set_key_algorithm( &attributes, alg );
5450 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005451
Gilles Peskine049c7532019-05-15 20:22:09 +02005452 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005453 &key ) );
5454 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005455 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01005456
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005457 /* Allocate a buffer which has the size advertized by the
5458 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005459 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005460 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01005461 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005462 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005463 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005464
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005465 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005466 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005467 input_data->x, input_data->len,
5468 signature, signature_size,
5469 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005470 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005471 ASSERT_COMPARE( output_data->x, output_data->len,
5472 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01005473
5474exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005475 /*
5476 * Key attributes may have been returned by psa_get_key_attributes()
5477 * thus reset them as required.
5478 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005479 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005480
Ronald Cron5425a212020-08-04 14:58:35 +02005481 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01005482 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005483 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005484}
5485/* END_CASE */
5486
5487/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005488void sign_hash_fail( int key_type_arg, data_t *key_data,
5489 int alg_arg, data_t *input_data,
5490 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01005491{
Ronald Cron5425a212020-08-04 14:58:35 +02005492 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005493 psa_key_type_t key_type = key_type_arg;
5494 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005495 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005496 psa_status_t actual_status;
5497 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01005498 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01005499 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005500 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005501
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005502 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005503
Gilles Peskine8817f612018-12-18 00:18:46 +01005504 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005505
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005506 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005507 psa_set_key_algorithm( &attributes, alg );
5508 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005509
Gilles Peskine049c7532019-05-15 20:22:09 +02005510 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005511 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005512
Ronald Cron5425a212020-08-04 14:58:35 +02005513 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005514 input_data->x, input_data->len,
5515 signature, signature_size,
5516 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005517 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005518 /* The value of *signature_length is unspecified on error, but
5519 * whatever it is, it should be less than signature_size, so that
5520 * if the caller tries to read *signature_length bytes without
5521 * checking the error code then they don't overflow a buffer. */
5522 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005523
5524exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005525 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005526 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01005527 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005528 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005529}
5530/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03005531
5532/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005533void sign_verify_hash( int key_type_arg, data_t *key_data,
5534 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02005535{
Ronald Cron5425a212020-08-04 14:58:35 +02005536 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005537 psa_key_type_t key_type = key_type_arg;
5538 psa_algorithm_t alg = alg_arg;
5539 size_t key_bits;
5540 unsigned char *signature = NULL;
5541 size_t signature_size;
5542 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005543 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005544
Gilles Peskine8817f612018-12-18 00:18:46 +01005545 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005546
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005547 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005548 psa_set_key_algorithm( &attributes, alg );
5549 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02005550
Gilles Peskine049c7532019-05-15 20:22:09 +02005551 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005552 &key ) );
5553 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005554 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02005555
5556 /* Allocate a buffer which has the size advertized by the
5557 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005558 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02005559 key_bits, alg );
5560 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005561 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005562 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02005563
5564 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005565 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005566 input_data->x, input_data->len,
5567 signature, signature_size,
5568 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005569 /* Check that the signature length looks sensible. */
5570 TEST_ASSERT( signature_length <= signature_size );
5571 TEST_ASSERT( signature_length > 0 );
5572
5573 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02005574 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005575 input_data->x, input_data->len,
5576 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005577
5578 if( input_data->len != 0 )
5579 {
5580 /* Flip a bit in the input and verify that the signature is now
5581 * detected as invalid. Flip a bit at the beginning, not at the end,
5582 * because ECDSA may ignore the last few bits of the input. */
5583 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02005584 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005585 input_data->x, input_data->len,
5586 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005587 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02005588 }
5589
5590exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005591 /*
5592 * Key attributes may have been returned by psa_get_key_attributes()
5593 * thus reset them as required.
5594 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005595 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005596
Ronald Cron5425a212020-08-04 14:58:35 +02005597 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02005598 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005599 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02005600}
5601/* END_CASE */
5602
5603/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005604void verify_hash( int key_type_arg, data_t *key_data,
5605 int alg_arg, data_t *hash_data,
5606 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03005607{
Ronald Cron5425a212020-08-04 14:58:35 +02005608 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005609 psa_key_type_t key_type = key_type_arg;
5610 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005611 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005612
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005613 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02005614
Gilles Peskine8817f612018-12-18 00:18:46 +01005615 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03005616
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005617 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005618 psa_set_key_algorithm( &attributes, alg );
5619 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03005620
Gilles Peskine049c7532019-05-15 20:22:09 +02005621 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005622 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03005623
Ronald Cron5425a212020-08-04 14:58:35 +02005624 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005625 hash_data->x, hash_data->len,
5626 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01005627
itayzafrir5c753392018-05-08 11:18:38 +03005628exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005629 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005630 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005631 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03005632}
5633/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005634
5635/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005636void verify_hash_fail( int key_type_arg, data_t *key_data,
5637 int alg_arg, data_t *hash_data,
5638 data_t *signature_data,
5639 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005640{
Ronald Cron5425a212020-08-04 14:58:35 +02005641 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005642 psa_key_type_t key_type = key_type_arg;
5643 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005644 psa_status_t actual_status;
5645 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005646 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005647
Gilles Peskine8817f612018-12-18 00:18:46 +01005648 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005649
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005650 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005651 psa_set_key_algorithm( &attributes, alg );
5652 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005653
Gilles Peskine049c7532019-05-15 20:22:09 +02005654 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005655 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005656
Ronald Cron5425a212020-08-04 14:58:35 +02005657 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005658 hash_data->x, hash_data->len,
5659 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005660 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005661
5662exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005663 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005664 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005665 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005666}
5667/* END_CASE */
5668
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005669/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02005670void sign_message_deterministic( int key_type_arg,
5671 data_t *key_data,
5672 int alg_arg,
5673 data_t *input_data,
5674 data_t *output_data )
5675{
5676 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5677 psa_key_type_t key_type = key_type_arg;
5678 psa_algorithm_t alg = alg_arg;
5679 size_t key_bits;
5680 unsigned char *signature = NULL;
5681 size_t signature_size;
5682 size_t signature_length = 0xdeadbeef;
5683 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5684
5685 PSA_ASSERT( psa_crypto_init( ) );
5686
5687 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5688 psa_set_key_algorithm( &attributes, alg );
5689 psa_set_key_type( &attributes, key_type );
5690
5691 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5692 &key ) );
5693 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5694 key_bits = psa_get_key_bits( &attributes );
5695
5696 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5697 TEST_ASSERT( signature_size != 0 );
5698 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5699 ASSERT_ALLOC( signature, signature_size );
5700
5701 PSA_ASSERT( psa_sign_message( key, alg,
5702 input_data->x, input_data->len,
5703 signature, signature_size,
5704 &signature_length ) );
5705
5706 ASSERT_COMPARE( output_data->x, output_data->len,
5707 signature, signature_length );
5708
5709exit:
5710 psa_reset_key_attributes( &attributes );
5711
5712 psa_destroy_key( key );
5713 mbedtls_free( signature );
5714 PSA_DONE( );
5715
5716}
5717/* END_CASE */
5718
5719/* BEGIN_CASE */
5720void sign_message_fail( int key_type_arg,
5721 data_t *key_data,
5722 int alg_arg,
5723 data_t *input_data,
5724 int signature_size_arg,
5725 int expected_status_arg )
5726{
5727 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5728 psa_key_type_t key_type = key_type_arg;
5729 psa_algorithm_t alg = alg_arg;
5730 size_t signature_size = signature_size_arg;
5731 psa_status_t actual_status;
5732 psa_status_t expected_status = expected_status_arg;
5733 unsigned char *signature = NULL;
5734 size_t signature_length = 0xdeadbeef;
5735 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5736
5737 ASSERT_ALLOC( signature, signature_size );
5738
5739 PSA_ASSERT( psa_crypto_init( ) );
5740
5741 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5742 psa_set_key_algorithm( &attributes, alg );
5743 psa_set_key_type( &attributes, key_type );
5744
5745 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5746 &key ) );
5747
5748 actual_status = psa_sign_message( key, alg,
5749 input_data->x, input_data->len,
5750 signature, signature_size,
5751 &signature_length );
5752 TEST_EQUAL( actual_status, expected_status );
5753 /* The value of *signature_length is unspecified on error, but
5754 * whatever it is, it should be less than signature_size, so that
5755 * if the caller tries to read *signature_length bytes without
5756 * checking the error code then they don't overflow a buffer. */
5757 TEST_ASSERT( signature_length <= signature_size );
5758
5759exit:
5760 psa_reset_key_attributes( &attributes );
5761 psa_destroy_key( key );
5762 mbedtls_free( signature );
5763 PSA_DONE( );
5764}
5765/* END_CASE */
5766
5767/* BEGIN_CASE */
5768void sign_verify_message( int key_type_arg,
5769 data_t *key_data,
5770 int alg_arg,
5771 data_t *input_data )
5772{
5773 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5774 psa_key_type_t key_type = key_type_arg;
5775 psa_algorithm_t alg = alg_arg;
5776 size_t key_bits;
5777 unsigned char *signature = NULL;
5778 size_t signature_size;
5779 size_t signature_length = 0xdeadbeef;
5780 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5781
5782 PSA_ASSERT( psa_crypto_init( ) );
5783
5784 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5785 PSA_KEY_USAGE_VERIFY_MESSAGE );
5786 psa_set_key_algorithm( &attributes, alg );
5787 psa_set_key_type( &attributes, key_type );
5788
5789 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5790 &key ) );
5791 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5792 key_bits = psa_get_key_bits( &attributes );
5793
5794 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5795 TEST_ASSERT( signature_size != 0 );
5796 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5797 ASSERT_ALLOC( signature, signature_size );
5798
5799 PSA_ASSERT( psa_sign_message( key, alg,
5800 input_data->x, input_data->len,
5801 signature, signature_size,
5802 &signature_length ) );
5803 TEST_ASSERT( signature_length <= signature_size );
5804 TEST_ASSERT( signature_length > 0 );
5805
5806 PSA_ASSERT( psa_verify_message( key, alg,
5807 input_data->x, input_data->len,
5808 signature, signature_length ) );
5809
5810 if( input_data->len != 0 )
5811 {
5812 /* Flip a bit in the input and verify that the signature is now
5813 * detected as invalid. Flip a bit at the beginning, not at the end,
5814 * because ECDSA may ignore the last few bits of the input. */
5815 input_data->x[0] ^= 1;
5816 TEST_EQUAL( psa_verify_message( key, alg,
5817 input_data->x, input_data->len,
5818 signature, signature_length ),
5819 PSA_ERROR_INVALID_SIGNATURE );
5820 }
5821
5822exit:
5823 psa_reset_key_attributes( &attributes );
5824
5825 psa_destroy_key( key );
5826 mbedtls_free( signature );
5827 PSA_DONE( );
5828}
5829/* END_CASE */
5830
5831/* BEGIN_CASE */
5832void verify_message( int key_type_arg,
5833 data_t *key_data,
5834 int alg_arg,
5835 data_t *input_data,
5836 data_t *signature_data )
5837{
5838 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5839 psa_key_type_t key_type = key_type_arg;
5840 psa_algorithm_t alg = alg_arg;
5841 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5842
5843 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
5844
5845 PSA_ASSERT( psa_crypto_init( ) );
5846
5847 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5848 psa_set_key_algorithm( &attributes, alg );
5849 psa_set_key_type( &attributes, key_type );
5850
5851 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5852 &key ) );
5853
5854 PSA_ASSERT( psa_verify_message( key, alg,
5855 input_data->x, input_data->len,
5856 signature_data->x, signature_data->len ) );
5857
5858exit:
5859 psa_reset_key_attributes( &attributes );
5860 psa_destroy_key( key );
5861 PSA_DONE( );
5862}
5863/* END_CASE */
5864
5865/* BEGIN_CASE */
5866void verify_message_fail( int key_type_arg,
5867 data_t *key_data,
5868 int alg_arg,
5869 data_t *hash_data,
5870 data_t *signature_data,
5871 int expected_status_arg )
5872{
5873 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5874 psa_key_type_t key_type = key_type_arg;
5875 psa_algorithm_t alg = alg_arg;
5876 psa_status_t actual_status;
5877 psa_status_t expected_status = expected_status_arg;
5878 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5879
5880 PSA_ASSERT( psa_crypto_init( ) );
5881
5882 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5883 psa_set_key_algorithm( &attributes, alg );
5884 psa_set_key_type( &attributes, key_type );
5885
5886 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5887 &key ) );
5888
5889 actual_status = psa_verify_message( key, alg,
5890 hash_data->x, hash_data->len,
5891 signature_data->x,
5892 signature_data->len );
5893 TEST_EQUAL( actual_status, expected_status );
5894
5895exit:
5896 psa_reset_key_attributes( &attributes );
5897 psa_destroy_key( key );
5898 PSA_DONE( );
5899}
5900/* END_CASE */
5901
5902/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005903void asymmetric_encrypt( int key_type_arg,
5904 data_t *key_data,
5905 int alg_arg,
5906 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005907 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005908 int expected_output_length_arg,
5909 int expected_status_arg )
5910{
Ronald Cron5425a212020-08-04 14:58:35 +02005911 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005912 psa_key_type_t key_type = key_type_arg;
5913 psa_algorithm_t alg = alg_arg;
5914 size_t expected_output_length = expected_output_length_arg;
5915 size_t key_bits;
5916 unsigned char *output = NULL;
5917 size_t output_size;
5918 size_t output_length = ~0;
5919 psa_status_t actual_status;
5920 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005921 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005922
Gilles Peskine8817f612018-12-18 00:18:46 +01005923 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005924
Gilles Peskine656896e2018-06-29 19:12:28 +02005925 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005926 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5927 psa_set_key_algorithm( &attributes, alg );
5928 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005929 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005930 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005931
5932 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005933 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005934 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005935
Gilles Peskine656896e2018-06-29 19:12:28 +02005936 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005937 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005938 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005939
5940 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005941 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005942 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005943 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005944 output, output_size,
5945 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005946 TEST_EQUAL( actual_status, expected_status );
5947 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005948
Gilles Peskine68428122018-06-30 18:42:41 +02005949 /* If the label is empty, the test framework puts a non-null pointer
5950 * in label->x. Test that a null pointer works as well. */
5951 if( label->len == 0 )
5952 {
5953 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005954 if( output_size != 0 )
5955 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005956 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005957 input_data->x, input_data->len,
5958 NULL, label->len,
5959 output, output_size,
5960 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005961 TEST_EQUAL( actual_status, expected_status );
5962 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005963 }
5964
Gilles Peskine656896e2018-06-29 19:12:28 +02005965exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005966 /*
5967 * Key attributes may have been returned by psa_get_key_attributes()
5968 * thus reset them as required.
5969 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005970 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005971
Ronald Cron5425a212020-08-04 14:58:35 +02005972 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005973 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005974 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005975}
5976/* END_CASE */
5977
5978/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005979void asymmetric_encrypt_decrypt( int key_type_arg,
5980 data_t *key_data,
5981 int alg_arg,
5982 data_t *input_data,
5983 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005984{
Ronald Cron5425a212020-08-04 14:58:35 +02005985 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005986 psa_key_type_t key_type = key_type_arg;
5987 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005988 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005989 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005990 size_t output_size;
5991 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005992 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005993 size_t output2_size;
5994 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005995 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005996
Gilles Peskine8817f612018-12-18 00:18:46 +01005997 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005998
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005999 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
6000 psa_set_key_algorithm( &attributes, alg );
6001 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006002
Gilles Peskine049c7532019-05-15 20:22:09 +02006003 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006004 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006005
6006 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02006007 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006008 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006009
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006010 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01006011 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006012 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01006013
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006014 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01006015 TEST_ASSERT( output2_size <=
6016 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
6017 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006018 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006019
Gilles Peskineeebd7382018-06-08 18:11:54 +02006020 /* We test encryption by checking that encrypt-then-decrypt gives back
6021 * the original plaintext because of the non-optional random
6022 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02006023 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006024 input_data->x, input_data->len,
6025 label->x, label->len,
6026 output, output_size,
6027 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006028 /* We don't know what ciphertext length to expect, but check that
6029 * it looks sensible. */
6030 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006031
Ronald Cron5425a212020-08-04 14:58:35 +02006032 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006033 output, output_length,
6034 label->x, label->len,
6035 output2, output2_size,
6036 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006037 ASSERT_COMPARE( input_data->x, input_data->len,
6038 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006039
6040exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006041 /*
6042 * Key attributes may have been returned by psa_get_key_attributes()
6043 * thus reset them as required.
6044 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006045 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006046
Ronald Cron5425a212020-08-04 14:58:35 +02006047 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006048 mbedtls_free( output );
6049 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006050 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006051}
6052/* END_CASE */
6053
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006054/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006055void asymmetric_decrypt( int key_type_arg,
6056 data_t *key_data,
6057 int alg_arg,
6058 data_t *input_data,
6059 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02006060 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006061{
Ronald Cron5425a212020-08-04 14:58:35 +02006062 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006063 psa_key_type_t key_type = key_type_arg;
6064 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01006065 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006066 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03006067 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006068 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006069 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006070
Gilles Peskine8817f612018-12-18 00:18:46 +01006071 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006072
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006073 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6074 psa_set_key_algorithm( &attributes, alg );
6075 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006076
Gilles Peskine049c7532019-05-15 20:22:09 +02006077 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006078 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006079
gabor-mezei-armceface22021-01-21 12:26:17 +01006080 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6081 key_bits = psa_get_key_bits( &attributes );
6082
6083 /* Determine the maximum ciphertext length */
6084 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
6085 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
6086 ASSERT_ALLOC( output, output_size );
6087
Ronald Cron5425a212020-08-04 14:58:35 +02006088 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006089 input_data->x, input_data->len,
6090 label->x, label->len,
6091 output,
6092 output_size,
6093 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006094 ASSERT_COMPARE( expected_data->x, expected_data->len,
6095 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006096
Gilles Peskine68428122018-06-30 18:42:41 +02006097 /* If the label is empty, the test framework puts a non-null pointer
6098 * in label->x. Test that a null pointer works as well. */
6099 if( label->len == 0 )
6100 {
6101 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006102 if( output_size != 0 )
6103 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006104 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006105 input_data->x, input_data->len,
6106 NULL, label->len,
6107 output,
6108 output_size,
6109 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006110 ASSERT_COMPARE( expected_data->x, expected_data->len,
6111 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006112 }
6113
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006114exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006115 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006116 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006117 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006118 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006119}
6120/* END_CASE */
6121
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006122/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006123void asymmetric_decrypt_fail( int key_type_arg,
6124 data_t *key_data,
6125 int alg_arg,
6126 data_t *input_data,
6127 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00006128 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02006129 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006130{
Ronald Cron5425a212020-08-04 14:58:35 +02006131 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006132 psa_key_type_t key_type = key_type_arg;
6133 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006134 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00006135 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006136 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006137 psa_status_t actual_status;
6138 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006139 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006140
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006141 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006142
Gilles Peskine8817f612018-12-18 00:18:46 +01006143 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006144
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006145 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6146 psa_set_key_algorithm( &attributes, alg );
6147 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006148
Gilles Peskine049c7532019-05-15 20:22:09 +02006149 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006150 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006151
Ronald Cron5425a212020-08-04 14:58:35 +02006152 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006153 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006154 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006155 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02006156 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006157 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006158 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006159
Gilles Peskine68428122018-06-30 18:42:41 +02006160 /* If the label is empty, the test framework puts a non-null pointer
6161 * in label->x. Test that a null pointer works as well. */
6162 if( label->len == 0 )
6163 {
6164 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006165 if( output_size != 0 )
6166 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006167 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006168 input_data->x, input_data->len,
6169 NULL, label->len,
6170 output, output_size,
6171 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006172 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006173 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02006174 }
6175
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006176exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006177 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006178 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02006179 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006180 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006181}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006182/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02006183
6184/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02006185void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00006186{
6187 /* Test each valid way of initializing the object, except for `= {0}`, as
6188 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
6189 * though it's OK by the C standard. We could test for this, but we'd need
6190 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006191 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006192 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
6193 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
6194 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00006195
6196 memset( &zero, 0, sizeof( zero ) );
6197
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006198 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006199 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006200 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006201 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006202 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006203 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006204 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006205
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006206 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006207 PSA_ASSERT( psa_key_derivation_abort(&func) );
6208 PSA_ASSERT( psa_key_derivation_abort(&init) );
6209 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00006210}
6211/* END_CASE */
6212
Janos Follath16de4a42019-06-13 16:32:24 +01006213/* BEGIN_CASE */
6214void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02006215{
Gilles Peskineea0fb492018-07-12 17:17:20 +02006216 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006217 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006218 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006219
Gilles Peskine8817f612018-12-18 00:18:46 +01006220 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006221
Janos Follath16de4a42019-06-13 16:32:24 +01006222 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006223 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006224
6225exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006226 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006227 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006228}
6229/* END_CASE */
6230
Janos Follathaf3c2a02019-06-12 12:34:34 +01006231/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01006232void derive_set_capacity( int alg_arg, int capacity_arg,
6233 int expected_status_arg )
6234{
6235 psa_algorithm_t alg = alg_arg;
6236 size_t capacity = capacity_arg;
6237 psa_status_t expected_status = expected_status_arg;
6238 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6239
6240 PSA_ASSERT( psa_crypto_init( ) );
6241
6242 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6243
6244 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
6245 expected_status );
6246
6247exit:
6248 psa_key_derivation_abort( &operation );
6249 PSA_DONE( );
6250}
6251/* END_CASE */
6252
6253/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01006254void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02006255 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006256 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02006257 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006258 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02006259 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006260 int expected_status_arg3,
6261 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006262{
6263 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02006264 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
6265 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01006266 psa_status_t expected_statuses[] = {expected_status_arg1,
6267 expected_status_arg2,
6268 expected_status_arg3};
6269 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006270 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6271 MBEDTLS_SVC_KEY_ID_INIT,
6272 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01006273 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6274 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6275 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006276 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006277 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006278 psa_status_t expected_output_status = expected_output_status_arg;
6279 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01006280
6281 PSA_ASSERT( psa_crypto_init( ) );
6282
6283 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6284 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006285
6286 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6287
6288 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
6289 {
Gilles Peskine4023c012021-05-27 13:21:20 +02006290 mbedtls_test_set_step( i );
6291 if( steps[i] == 0 )
6292 {
6293 /* Skip this step */
6294 }
6295 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006296 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02006297 psa_set_key_type( &attributes, key_types[i] );
6298 PSA_ASSERT( psa_import_key( &attributes,
6299 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006300 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006301 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
6302 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
6303 {
6304 // When taking a private key as secret input, use key agreement
6305 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006306 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
6307 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006308 expected_statuses[i] );
6309 }
6310 else
6311 {
6312 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02006313 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006314 expected_statuses[i] );
6315 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02006316 }
6317 else
6318 {
6319 TEST_EQUAL( psa_key_derivation_input_bytes(
6320 &operation, steps[i],
6321 inputs[i]->x, inputs[i]->len ),
6322 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006323 }
6324 }
6325
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006326 if( output_key_type != PSA_KEY_TYPE_NONE )
6327 {
6328 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00006329 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006330 psa_set_key_bits( &attributes, 8 );
6331 actual_output_status =
6332 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006333 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006334 }
6335 else
6336 {
6337 uint8_t buffer[1];
6338 actual_output_status =
6339 psa_key_derivation_output_bytes( &operation,
6340 buffer, sizeof( buffer ) );
6341 }
6342 TEST_EQUAL( actual_output_status, expected_output_status );
6343
Janos Follathaf3c2a02019-06-12 12:34:34 +01006344exit:
6345 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006346 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6347 psa_destroy_key( keys[i] );
6348 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006349 PSA_DONE( );
6350}
6351/* END_CASE */
6352
Janos Follathd958bb72019-07-03 15:02:16 +01006353/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006354void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006355{
Janos Follathd958bb72019-07-03 15:02:16 +01006356 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006357 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02006358 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006359 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01006360 unsigned char input1[] = "Input 1";
6361 size_t input1_length = sizeof( input1 );
6362 unsigned char input2[] = "Input 2";
6363 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006364 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02006365 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02006366 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6367 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6368 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006369 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006370
Gilles Peskine8817f612018-12-18 00:18:46 +01006371 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006372
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006373 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6374 psa_set_key_algorithm( &attributes, alg );
6375 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006376
Gilles Peskine73676cb2019-05-15 20:15:10 +02006377 PSA_ASSERT( psa_import_key( &attributes,
6378 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02006379 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006380
6381 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006382 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6383 input1, input1_length,
6384 input2, input2_length,
6385 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01006386 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006387
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006388 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01006389 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006390 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006391
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006392 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006393
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006394 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02006395 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006396
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006397exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006398 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006399 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006400 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006401}
6402/* END_CASE */
6403
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006404/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006405void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006406{
6407 uint8_t output_buffer[16];
6408 size_t buffer_size = 16;
6409 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006410 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006411
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006412 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6413 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006414 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006415
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006416 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006417 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006418
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006419 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006420
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006421 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6422 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006423 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006424
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006425 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006426 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006427
6428exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006429 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006430}
6431/* END_CASE */
6432
6433/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006434void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02006435 int step1_arg, data_t *input1,
6436 int step2_arg, data_t *input2,
6437 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006438 int requested_capacity_arg,
6439 data_t *expected_output1,
6440 data_t *expected_output2 )
6441{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006442 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02006443 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
6444 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006445 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6446 MBEDTLS_SVC_KEY_ID_INIT,
6447 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006448 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006449 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006450 uint8_t *expected_outputs[2] =
6451 {expected_output1->x, expected_output2->x};
6452 size_t output_sizes[2] =
6453 {expected_output1->len, expected_output2->len};
6454 size_t output_buffer_size = 0;
6455 uint8_t *output_buffer = NULL;
6456 size_t expected_capacity;
6457 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006458 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006459 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02006460 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006461
6462 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6463 {
6464 if( output_sizes[i] > output_buffer_size )
6465 output_buffer_size = output_sizes[i];
6466 if( output_sizes[i] == 0 )
6467 expected_outputs[i] = NULL;
6468 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006469 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01006470 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006471
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006472 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6473 psa_set_key_algorithm( &attributes, alg );
6474 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006475
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006476 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02006477 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6478 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
6479 requested_capacity ) );
6480 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006481 {
Gilles Peskine1468da72019-05-29 17:35:49 +02006482 switch( steps[i] )
6483 {
6484 case 0:
6485 break;
6486 case PSA_KEY_DERIVATION_INPUT_SECRET:
6487 PSA_ASSERT( psa_import_key( &attributes,
6488 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006489 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01006490
6491 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
6492 {
6493 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
6494 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
6495 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
6496 }
6497
Gilles Peskine1468da72019-05-29 17:35:49 +02006498 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02006499 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02006500 break;
6501 default:
6502 PSA_ASSERT( psa_key_derivation_input_bytes(
6503 &operation, steps[i],
6504 inputs[i]->x, inputs[i]->len ) );
6505 break;
6506 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006507 }
Gilles Peskine1468da72019-05-29 17:35:49 +02006508
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006509 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006510 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006511 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006512 expected_capacity = requested_capacity;
6513
6514 /* Expansion phase. */
6515 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6516 {
6517 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006518 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006519 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006520 if( expected_capacity == 0 && output_sizes[i] == 0 )
6521 {
6522 /* Reading 0 bytes when 0 bytes are available can go either way. */
6523 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02006524 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006525 continue;
6526 }
6527 else if( expected_capacity == 0 ||
6528 output_sizes[i] > expected_capacity )
6529 {
6530 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02006531 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006532 expected_capacity = 0;
6533 continue;
6534 }
6535 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01006536 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006537 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006538 ASSERT_COMPARE( output_buffer, output_sizes[i],
6539 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006540 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006541 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006542 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006543 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006544 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006545 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006546 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006547
6548exit:
6549 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006550 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006551 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6552 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006553 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006554}
6555/* END_CASE */
6556
6557/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02006558void derive_full( int alg_arg,
6559 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01006560 data_t *input1,
6561 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02006562 int requested_capacity_arg )
6563{
Ronald Cron5425a212020-08-04 14:58:35 +02006564 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006565 psa_algorithm_t alg = alg_arg;
6566 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006567 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006568 unsigned char output_buffer[16];
6569 size_t expected_capacity = requested_capacity;
6570 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006571 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006572
Gilles Peskine8817f612018-12-18 00:18:46 +01006573 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006574
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006575 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6576 psa_set_key_algorithm( &attributes, alg );
6577 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02006578
Gilles Peskine049c7532019-05-15 20:22:09 +02006579 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006580 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006581
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006582 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6583 input1->x, input1->len,
6584 input2->x, input2->len,
6585 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01006586 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01006587
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006588 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006589 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006590 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006591
6592 /* Expansion phase. */
6593 while( current_capacity > 0 )
6594 {
6595 size_t read_size = sizeof( output_buffer );
6596 if( read_size > current_capacity )
6597 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006598 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006599 output_buffer,
6600 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006601 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006602 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006603 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006604 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006605 }
6606
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006607 /* Check that the operation refuses to go over capacity. */
6608 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006609 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02006610
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006611 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006612
6613exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006614 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006615 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006616 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02006617}
6618/* END_CASE */
6619
Janos Follathe60c9052019-07-03 13:51:30 +01006620/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006621void derive_key_exercise( int alg_arg,
6622 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01006623 data_t *input1,
6624 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006625 int derived_type_arg,
6626 int derived_bits_arg,
6627 int derived_usage_arg,
6628 int derived_alg_arg )
6629{
Ronald Cron5425a212020-08-04 14:58:35 +02006630 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6631 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006632 psa_algorithm_t alg = alg_arg;
6633 psa_key_type_t derived_type = derived_type_arg;
6634 size_t derived_bits = derived_bits_arg;
6635 psa_key_usage_t derived_usage = derived_usage_arg;
6636 psa_algorithm_t derived_alg = derived_alg_arg;
6637 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006638 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006639 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006640 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006641
Gilles Peskine8817f612018-12-18 00:18:46 +01006642 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006643
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006644 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6645 psa_set_key_algorithm( &attributes, alg );
6646 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006647 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006648 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006649
6650 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006651 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6652 input1->x, input1->len,
6653 input2->x, input2->len,
6654 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01006655 goto exit;
6656
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006657 psa_set_key_usage_flags( &attributes, derived_usage );
6658 psa_set_key_algorithm( &attributes, derived_alg );
6659 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006660 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006661 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006662 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006663
6664 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006665 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006666 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
6667 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006668
6669 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006670 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02006671 goto exit;
6672
6673exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006674 /*
6675 * Key attributes may have been returned by psa_get_key_attributes()
6676 * thus reset them as required.
6677 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006678 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006679
6680 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006681 psa_destroy_key( base_key );
6682 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006683 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006684}
6685/* END_CASE */
6686
Janos Follath42fd8882019-07-03 14:17:09 +01006687/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006688void derive_key_export( int alg_arg,
6689 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01006690 data_t *input1,
6691 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006692 int bytes1_arg,
6693 int bytes2_arg )
6694{
Ronald Cron5425a212020-08-04 14:58:35 +02006695 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6696 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006697 psa_algorithm_t alg = alg_arg;
6698 size_t bytes1 = bytes1_arg;
6699 size_t bytes2 = bytes2_arg;
6700 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006701 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006702 uint8_t *output_buffer = NULL;
6703 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006704 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6705 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006706 size_t length;
6707
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006708 ASSERT_ALLOC( output_buffer, capacity );
6709 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01006710 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006711
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006712 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6713 psa_set_key_algorithm( &base_attributes, alg );
6714 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006715 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006716 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006717
6718 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006719 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6720 input1->x, input1->len,
6721 input2->x, input2->len,
6722 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006723 goto exit;
6724
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006725 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006726 output_buffer,
6727 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006728 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006729
6730 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006731 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6732 input1->x, input1->len,
6733 input2->x, input2->len,
6734 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006735 goto exit;
6736
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006737 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6738 psa_set_key_algorithm( &derived_attributes, 0 );
6739 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006740 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006741 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006742 &derived_key ) );
6743 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006744 export_buffer, bytes1,
6745 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006746 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02006747 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006748 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006749 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006750 &derived_key ) );
6751 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006752 export_buffer + bytes1, bytes2,
6753 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006754 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006755
6756 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006757 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
6758 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006759
6760exit:
6761 mbedtls_free( output_buffer );
6762 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006763 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006764 psa_destroy_key( base_key );
6765 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006766 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006767}
6768/* END_CASE */
6769
6770/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006771void derive_key( int alg_arg,
6772 data_t *key_data, data_t *input1, data_t *input2,
6773 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006774 int expected_status_arg,
6775 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006776{
Ronald Cron5425a212020-08-04 14:58:35 +02006777 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6778 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006779 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006780 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006781 size_t bits = bits_arg;
6782 psa_status_t expected_status = expected_status_arg;
6783 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6784 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6785 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6786
6787 PSA_ASSERT( psa_crypto_init( ) );
6788
6789 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6790 psa_set_key_algorithm( &base_attributes, alg );
6791 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6792 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006793 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02006794
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006795 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6796 input1->x, input1->len,
6797 input2->x, input2->len,
6798 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02006799 goto exit;
6800
6801 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6802 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006803 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02006804 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01006805
6806 psa_status_t status =
6807 psa_key_derivation_output_key( &derived_attributes,
6808 &operation,
6809 &derived_key );
6810 if( is_large_output > 0 )
6811 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6812 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02006813
6814exit:
6815 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006816 psa_destroy_key( base_key );
6817 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02006818 PSA_DONE( );
6819}
6820/* END_CASE */
6821
6822/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006823void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02006824 int our_key_type_arg, int our_key_alg_arg,
6825 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006826 int expected_status_arg )
6827{
Ronald Cron5425a212020-08-04 14:58:35 +02006828 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006829 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006830 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006831 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006832 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006833 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02006834 psa_status_t expected_status = expected_status_arg;
6835 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006836
Gilles Peskine8817f612018-12-18 00:18:46 +01006837 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006838
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006839 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006840 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006841 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006842 PSA_ASSERT( psa_import_key( &attributes,
6843 our_key_data->x, our_key_data->len,
6844 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006845
Gilles Peskine77f40d82019-04-11 21:27:06 +02006846 /* The tests currently include inputs that should fail at either step.
6847 * Test cases that fail at the setup step should be changed to call
6848 * key_derivation_setup instead, and this function should be renamed
6849 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006850 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02006851 if( status == PSA_SUCCESS )
6852 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006853 TEST_EQUAL( psa_key_derivation_key_agreement(
6854 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
6855 our_key,
6856 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02006857 expected_status );
6858 }
6859 else
6860 {
6861 TEST_ASSERT( status == expected_status );
6862 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02006863
6864exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006865 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006866 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006867 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006868}
6869/* END_CASE */
6870
6871/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006872void raw_key_agreement( int alg_arg,
6873 int our_key_type_arg, data_t *our_key_data,
6874 data_t *peer_key_data,
6875 data_t *expected_output )
6876{
Ronald Cron5425a212020-08-04 14:58:35 +02006877 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006878 psa_algorithm_t alg = alg_arg;
6879 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006880 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006881 unsigned char *output = NULL;
6882 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006883 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006884
6885 ASSERT_ALLOC( output, expected_output->len );
6886 PSA_ASSERT( psa_crypto_init( ) );
6887
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006888 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6889 psa_set_key_algorithm( &attributes, alg );
6890 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006891 PSA_ASSERT( psa_import_key( &attributes,
6892 our_key_data->x, our_key_data->len,
6893 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006894
gabor-mezei-armceface22021-01-21 12:26:17 +01006895 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6896 key_bits = psa_get_key_bits( &attributes );
6897
Gilles Peskinebe697d82019-05-16 18:00:41 +02006898 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6899 peer_key_data->x, peer_key_data->len,
6900 output, expected_output->len,
6901 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006902 ASSERT_COMPARE( output, output_length,
6903 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006904 TEST_ASSERT( output_length <=
6905 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6906 TEST_ASSERT( output_length <=
6907 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006908
6909exit:
6910 mbedtls_free( output );
6911 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006912 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006913}
6914/* END_CASE */
6915
6916/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006917void key_agreement_capacity( int alg_arg,
6918 int our_key_type_arg, data_t *our_key_data,
6919 data_t *peer_key_data,
6920 int expected_capacity_arg )
6921{
Ronald Cron5425a212020-08-04 14:58:35 +02006922 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006923 psa_algorithm_t alg = alg_arg;
6924 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006925 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006926 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006927 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006928 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006929
Gilles Peskine8817f612018-12-18 00:18:46 +01006930 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006931
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006932 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6933 psa_set_key_algorithm( &attributes, alg );
6934 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006935 PSA_ASSERT( psa_import_key( &attributes,
6936 our_key_data->x, our_key_data->len,
6937 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006938
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006939 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006940 PSA_ASSERT( psa_key_derivation_key_agreement(
6941 &operation,
6942 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6943 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006944 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6945 {
6946 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006947 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006948 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006949 NULL, 0 ) );
6950 }
Gilles Peskine59685592018-09-18 12:11:34 +02006951
Gilles Peskinebf491972018-10-25 22:36:12 +02006952 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006953 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006954 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006955 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006956
Gilles Peskinebf491972018-10-25 22:36:12 +02006957 /* Test the actual capacity by reading the output. */
6958 while( actual_capacity > sizeof( output ) )
6959 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006960 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006961 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006962 actual_capacity -= sizeof( output );
6963 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006964 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006965 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006966 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006967 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006968
Gilles Peskine59685592018-09-18 12:11:34 +02006969exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006970 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006971 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006972 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006973}
6974/* END_CASE */
6975
6976/* BEGIN_CASE */
6977void key_agreement_output( int alg_arg,
6978 int our_key_type_arg, data_t *our_key_data,
6979 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006980 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006981{
Ronald Cron5425a212020-08-04 14:58:35 +02006982 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006983 psa_algorithm_t alg = alg_arg;
6984 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006985 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006986 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006987 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006988
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006989 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6990 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006991
Gilles Peskine8817f612018-12-18 00:18:46 +01006992 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006993
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006994 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6995 psa_set_key_algorithm( &attributes, alg );
6996 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006997 PSA_ASSERT( psa_import_key( &attributes,
6998 our_key_data->x, our_key_data->len,
6999 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007000
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007001 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007002 PSA_ASSERT( psa_key_derivation_key_agreement(
7003 &operation,
7004 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7005 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007006 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7007 {
7008 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007009 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007010 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007011 NULL, 0 ) );
7012 }
Gilles Peskine59685592018-09-18 12:11:34 +02007013
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007014 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007015 actual_output,
7016 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007017 ASSERT_COMPARE( actual_output, expected_output1->len,
7018 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007019 if( expected_output2->len != 0 )
7020 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007021 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007022 actual_output,
7023 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007024 ASSERT_COMPARE( actual_output, expected_output2->len,
7025 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007026 }
Gilles Peskine59685592018-09-18 12:11:34 +02007027
7028exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007029 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007030 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007031 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007032 mbedtls_free( actual_output );
7033}
7034/* END_CASE */
7035
7036/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02007037void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02007038{
Gilles Peskinea50d7392018-06-21 10:22:13 +02007039 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007040 unsigned char *output = NULL;
7041 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02007042 size_t i;
7043 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02007044
Simon Butcher49f8e312020-03-03 15:51:50 +00007045 TEST_ASSERT( bytes_arg >= 0 );
7046
Gilles Peskine91892022021-02-08 19:50:26 +01007047 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007048 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02007049
Gilles Peskine8817f612018-12-18 00:18:46 +01007050 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02007051
Gilles Peskinea50d7392018-06-21 10:22:13 +02007052 /* Run several times, to ensure that every output byte will be
7053 * nonzero at least once with overwhelming probability
7054 * (2^(-8*number_of_runs)). */
7055 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02007056 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007057 if( bytes != 0 )
7058 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01007059 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007060
Gilles Peskinea50d7392018-06-21 10:22:13 +02007061 for( i = 0; i < bytes; i++ )
7062 {
7063 if( output[i] != 0 )
7064 ++changed[i];
7065 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007066 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02007067
7068 /* Check that every byte was changed to nonzero at least once. This
7069 * validates that psa_generate_random is overwriting every byte of
7070 * the output buffer. */
7071 for( i = 0; i < bytes; i++ )
7072 {
7073 TEST_ASSERT( changed[i] != 0 );
7074 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007075
7076exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007077 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007078 mbedtls_free( output );
7079 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02007080}
7081/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02007082
7083/* BEGIN_CASE */
7084void generate_key( int type_arg,
7085 int bits_arg,
7086 int usage_arg,
7087 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007088 int expected_status_arg,
7089 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02007090{
Ronald Cron5425a212020-08-04 14:58:35 +02007091 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007092 psa_key_type_t type = type_arg;
7093 psa_key_usage_t usage = usage_arg;
7094 size_t bits = bits_arg;
7095 psa_algorithm_t alg = alg_arg;
7096 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007097 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007098 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007099
Gilles Peskine8817f612018-12-18 00:18:46 +01007100 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007101
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007102 psa_set_key_usage_flags( &attributes, usage );
7103 psa_set_key_algorithm( &attributes, alg );
7104 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007105 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007106
7107 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01007108 psa_status_t status = psa_generate_key( &attributes, &key );
7109
7110 if( is_large_key > 0 )
7111 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7112 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007113 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007114 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007115
7116 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007117 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007118 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
7119 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007120
Gilles Peskine818ca122018-06-20 18:16:48 +02007121 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007122 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02007123 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007124
7125exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007126 /*
7127 * Key attributes may have been returned by psa_get_key_attributes()
7128 * thus reset them as required.
7129 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007130 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007131
Ronald Cron5425a212020-08-04 14:58:35 +02007132 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007133 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007134}
7135/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03007136
Ronald Cronee414c72021-03-18 18:50:08 +01007137/* 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 +02007138void generate_key_rsa( int bits_arg,
7139 data_t *e_arg,
7140 int expected_status_arg )
7141{
Ronald Cron5425a212020-08-04 14:58:35 +02007142 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02007143 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02007144 size_t bits = bits_arg;
7145 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
7146 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
7147 psa_status_t expected_status = expected_status_arg;
7148 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7149 uint8_t *exported = NULL;
7150 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007151 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007152 size_t exported_length = SIZE_MAX;
7153 uint8_t *e_read_buffer = NULL;
7154 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02007155 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007156 size_t e_read_length = SIZE_MAX;
7157
7158 if( e_arg->len == 0 ||
7159 ( e_arg->len == 3 &&
7160 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
7161 {
7162 is_default_public_exponent = 1;
7163 e_read_size = 0;
7164 }
7165 ASSERT_ALLOC( e_read_buffer, e_read_size );
7166 ASSERT_ALLOC( exported, exported_size );
7167
7168 PSA_ASSERT( psa_crypto_init( ) );
7169
7170 psa_set_key_usage_flags( &attributes, usage );
7171 psa_set_key_algorithm( &attributes, alg );
7172 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
7173 e_arg->x, e_arg->len ) );
7174 psa_set_key_bits( &attributes, bits );
7175
7176 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007177 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007178 if( expected_status != PSA_SUCCESS )
7179 goto exit;
7180
7181 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007182 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007183 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7184 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
7185 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
7186 e_read_buffer, e_read_size,
7187 &e_read_length ) );
7188 if( is_default_public_exponent )
7189 TEST_EQUAL( e_read_length, 0 );
7190 else
7191 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
7192
7193 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007194 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02007195 goto exit;
7196
7197 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02007198 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02007199 exported, exported_size,
7200 &exported_length ) );
7201 {
7202 uint8_t *p = exported;
7203 uint8_t *end = exported + exported_length;
7204 size_t len;
7205 /* RSAPublicKey ::= SEQUENCE {
7206 * modulus INTEGER, -- n
7207 * publicExponent INTEGER } -- e
7208 */
7209 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007210 MBEDTLS_ASN1_SEQUENCE |
7211 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01007212 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007213 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
7214 MBEDTLS_ASN1_INTEGER ) );
7215 if( len >= 1 && p[0] == 0 )
7216 {
7217 ++p;
7218 --len;
7219 }
7220 if( e_arg->len == 0 )
7221 {
7222 TEST_EQUAL( len, 3 );
7223 TEST_EQUAL( p[0], 1 );
7224 TEST_EQUAL( p[1], 0 );
7225 TEST_EQUAL( p[2], 1 );
7226 }
7227 else
7228 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
7229 }
7230
7231exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007232 /*
7233 * Key attributes may have been returned by psa_get_key_attributes() or
7234 * set by psa_set_key_domain_parameters() thus reset them as required.
7235 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02007236 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007237
Ronald Cron5425a212020-08-04 14:58:35 +02007238 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007239 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007240 mbedtls_free( e_read_buffer );
7241 mbedtls_free( exported );
7242}
7243/* END_CASE */
7244
Darryl Greend49a4992018-06-18 17:27:26 +01007245/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007246void persistent_key_load_key_from_storage( data_t *data,
7247 int type_arg, int bits_arg,
7248 int usage_flags_arg, int alg_arg,
7249 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01007250{
Ronald Cron71016a92020-08-28 19:01:50 +02007251 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007252 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02007253 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7254 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007255 psa_key_type_t type = type_arg;
7256 size_t bits = bits_arg;
7257 psa_key_usage_t usage_flags = usage_flags_arg;
7258 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007259 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01007260 unsigned char *first_export = NULL;
7261 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007262 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007263 size_t first_exported_length;
7264 size_t second_exported_length;
7265
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007266 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7267 {
7268 ASSERT_ALLOC( first_export, export_size );
7269 ASSERT_ALLOC( second_export, export_size );
7270 }
Darryl Greend49a4992018-06-18 17:27:26 +01007271
Gilles Peskine8817f612018-12-18 00:18:46 +01007272 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007273
Gilles Peskinec87af662019-05-15 16:12:22 +02007274 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007275 psa_set_key_usage_flags( &attributes, usage_flags );
7276 psa_set_key_algorithm( &attributes, alg );
7277 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007278 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007279
Darryl Green0c6575a2018-11-07 16:05:30 +00007280 switch( generation_method )
7281 {
7282 case IMPORT_KEY:
7283 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02007284 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007285 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007286 break;
Darryl Greend49a4992018-06-18 17:27:26 +01007287
Darryl Green0c6575a2018-11-07 16:05:30 +00007288 case GENERATE_KEY:
7289 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007290 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007291 break;
7292
7293 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01007294#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007295 {
7296 /* Create base key */
7297 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
7298 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7299 psa_set_key_usage_flags( &base_attributes,
7300 PSA_KEY_USAGE_DERIVE );
7301 psa_set_key_algorithm( &base_attributes, derive_alg );
7302 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007303 PSA_ASSERT( psa_import_key( &base_attributes,
7304 data->x, data->len,
7305 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007306 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007307 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007308 PSA_ASSERT( psa_key_derivation_input_key(
7309 &operation,
7310 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007311 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007312 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007313 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007314 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
7315 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007316 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007317 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007318 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02007319 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007320 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01007321#else
7322 TEST_ASSUME( ! "KDF not supported in this configuration" );
7323#endif
7324 break;
7325
7326 default:
7327 TEST_ASSERT( ! "generation_method not implemented in test" );
7328 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00007329 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007330 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01007331
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007332 /* Export the key if permitted by the key policy. */
7333 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7334 {
Ronald Cron5425a212020-08-04 14:58:35 +02007335 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007336 first_export, export_size,
7337 &first_exported_length ) );
7338 if( generation_method == IMPORT_KEY )
7339 ASSERT_COMPARE( data->x, data->len,
7340 first_export, first_exported_length );
7341 }
Darryl Greend49a4992018-06-18 17:27:26 +01007342
7343 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02007344 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007345 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01007346 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007347
Darryl Greend49a4992018-06-18 17:27:26 +01007348 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02007349 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02007350 TEST_ASSERT( mbedtls_svc_key_id_equal(
7351 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007352 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
7353 PSA_KEY_LIFETIME_PERSISTENT );
7354 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7355 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02007356 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02007357 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007358 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01007359
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007360 /* Export the key again if permitted by the key policy. */
7361 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00007362 {
Ronald Cron5425a212020-08-04 14:58:35 +02007363 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007364 second_export, export_size,
7365 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007366 ASSERT_COMPARE( first_export, first_exported_length,
7367 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00007368 }
7369
7370 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007371 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00007372 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01007373
7374exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007375 /*
7376 * Key attributes may have been returned by psa_get_key_attributes()
7377 * thus reset them as required.
7378 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007379 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007380
Darryl Greend49a4992018-06-18 17:27:26 +01007381 mbedtls_free( first_export );
7382 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007383 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007384 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02007385 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007386 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01007387}
7388/* END_CASE */