blob: 81bd246718b0c7238d1651df638794db9053e625 [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}
Gilles Peskine818ca122018-06-20 18:16:48 +020057
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}
141
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100142int exercise_mac_setup( psa_key_type_t key_type,
143 const unsigned char *key_bytes,
144 size_t key_length,
145 psa_algorithm_t alg,
146 psa_mac_operation_t *operation,
147 psa_status_t *status )
148{
Ronald Cron5425a212020-08-04 14:58:35 +0200149 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200150 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100151
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100152 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200153 psa_set_key_algorithm( &attributes, alg );
154 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200155 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100156
Ronald Cron5425a212020-08-04 14:58:35 +0200157 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100158 /* Whether setup succeeded or failed, abort must succeed. */
159 PSA_ASSERT( psa_mac_abort( operation ) );
160 /* If setup failed, reproduce the failure, so that the caller can
161 * test the resulting state of the operation object. */
162 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100163 {
Ronald Cron5425a212020-08-04 14:58:35 +0200164 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100165 }
166
Ronald Cron5425a212020-08-04 14:58:35 +0200167 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100168 return( 1 );
169
170exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200171 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100172 return( 0 );
173}
174
175int exercise_cipher_setup( psa_key_type_t key_type,
176 const unsigned char *key_bytes,
177 size_t key_length,
178 psa_algorithm_t alg,
179 psa_cipher_operation_t *operation,
180 psa_status_t *status )
181{
Ronald Cron5425a212020-08-04 14:58:35 +0200182 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100184
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200185 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
186 psa_set_key_algorithm( &attributes, alg );
187 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200188 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100189
Ronald Cron5425a212020-08-04 14:58:35 +0200190 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100191 /* Whether setup succeeded or failed, abort must succeed. */
192 PSA_ASSERT( psa_cipher_abort( operation ) );
193 /* If setup failed, reproduce the failure, so that the caller can
194 * test the resulting state of the operation object. */
195 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100196 {
Ronald Cron5425a212020-08-04 14:58:35 +0200197 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100198 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100199 }
200
Ronald Cron5425a212020-08-04 14:58:35 +0200201 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100202 return( 1 );
203
204exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200205 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100206 return( 0 );
207}
208
Ronald Cron5425a212020-08-04 14:58:35 +0200209static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200210{
211 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200212 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200213 uint8_t buffer[1];
214 size_t length;
215 int ok = 0;
216
Ronald Cronecfb2372020-07-23 17:13:42 +0200217 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200218 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
219 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
220 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200221 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000222 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200223 TEST_EQUAL(
224 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
225 TEST_EQUAL(
226 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200227 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200228 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
229 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
230 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
231 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
232
Ronald Cron5425a212020-08-04 14:58:35 +0200233 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000234 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200235 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200236 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000237 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200238
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200239 ok = 1;
240
241exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100242 /*
243 * Key attributes may have been returned by psa_get_key_attributes()
244 * thus reset them as required.
245 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200246 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100247
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200248 return( ok );
249}
250
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200251/* Assert that a key isn't reported as having a slot number. */
252#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
253#define ASSERT_NO_SLOT_NUMBER( attributes ) \
254 do \
255 { \
256 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
257 TEST_EQUAL( psa_get_key_slot_number( \
258 attributes, \
259 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
260 PSA_ERROR_INVALID_ARGUMENT ); \
261 } \
262 while( 0 )
263#else /* MBEDTLS_PSA_CRYPTO_SE_C */
264#define ASSERT_NO_SLOT_NUMBER( attributes ) \
265 ( (void) 0 )
266#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
267
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100268/* An overapproximation of the amount of storage needed for a key of the
269 * given type and with the given content. The API doesn't make it easy
270 * to find a good value for the size. The current implementation doesn't
271 * care about the value anyway. */
272#define KEY_BITS_FROM_DATA( type, data ) \
273 ( data )->len
274
Darryl Green0c6575a2018-11-07 16:05:30 +0000275typedef enum {
276 IMPORT_KEY = 0,
277 GENERATE_KEY = 1,
278 DERIVE_KEY = 2
279} generate_method;
280
Paul Elliott33746aa2021-09-15 16:40:40 +0100281typedef enum
282{
283 DO_NOT_SET_LENGTHS = 0,
284 SET_LENGTHS_BEFORE_NONCE = 1,
285 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100286} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100287
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100288typedef enum
289{
290 USE_NULL_TAG = 0,
291 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100292} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100293
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100294/*!
295 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100296 * \param key_type_arg Type of key passed in
297 * \param key_data The encryption / decryption key data
298 * \param alg_arg The type of algorithm used
299 * \param nonce Nonce data
300 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100301 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100302 * feed additional data in to be encrypted /
303 * decrypted. If -1, no chunking.
304 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100305 * \param data_part_len_arg If not -1, the length of chunks to feed
306 * the data in to be encrypted / decrypted. If
307 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100308 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100309 * expected here, this controls whether or not
310 * to set lengths, and in what order with
311 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100312 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100313 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100314 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100315 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100316 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100317 */
318static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
319 int alg_arg,
320 data_t *nonce,
321 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100322 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100323 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100324 int data_part_len_arg,
Paul Elliottbb979e72021-09-22 12:54:42 +0100325 set_lengths_method_t set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100326 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100327 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100328 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100329{
330 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
331 psa_key_type_t key_type = key_type_arg;
332 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100333 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100334 unsigned char *output_data = NULL;
335 unsigned char *part_data = NULL;
336 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100337 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100338 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100339 size_t output_size = 0;
340 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100341 size_t output_length = 0;
342 size_t key_bits = 0;
343 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100344 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100345 size_t part_length = 0;
346 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100347 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100348 size_t ad_part_len = 0;
349 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100350 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100351 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
352 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
353
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100354 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100355 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100356
Paul Elliottd3f82412021-06-16 16:52:21 +0100357 PSA_ASSERT( psa_crypto_init( ) );
358
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100359 if( is_encrypt )
360 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
361 else
362 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
363
Paul Elliottd3f82412021-06-16 16:52:21 +0100364 psa_set_key_algorithm( &attributes, alg );
365 psa_set_key_type( &attributes, key_type );
366
367 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
368 &key ) );
369
370 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
371 key_bits = psa_get_key_bits( &attributes );
372
373 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
374
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100375 if( is_encrypt )
376 {
377 /* Tag gets written at end of buffer. */
378 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
379 ( input_data->len +
380 tag_length ) );
381 data_true_size = input_data->len;
382 }
383 else
384 {
385 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
386 ( input_data->len -
387 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100388
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100389 /* Do not want to attempt to decrypt tag. */
390 data_true_size = input_data->len - tag_length;
391 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100392
393 ASSERT_ALLOC( output_data, output_size );
394
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100395 if( is_encrypt )
396 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100397 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
398 TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100399 }
400 else
401 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100402 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
403 TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100404 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100405
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100406 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100407
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100408 if( is_encrypt )
409 status = psa_aead_encrypt_setup( &operation, key, alg );
410 else
411 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100412
413 /* If the operation is not supported, just skip and not fail in case the
414 * encryption involves a common limitation of cryptography hardwares and
415 * an alternative implementation. */
416 if( status == PSA_ERROR_NOT_SUPPORTED )
417 {
418 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
419 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
420 }
421
422 PSA_ASSERT( status );
423
Paul Elliott33746aa2021-09-15 16:40:40 +0100424 if( set_lengths_method == DO_NOT_SET_LENGTHS )
425 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
426 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100427 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100428 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
429 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100430 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
431 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100432 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100433 {
434 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
435
Paul Elliott33746aa2021-09-15 16:40:40 +0100436 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
437 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100438 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100439
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100440 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100441 {
442 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100443 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100444
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100445 for( part_offset = 0, part_count = 0;
446 part_offset < additional_data->len;
447 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100448 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100449 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100450 {
Paul Elliott329d5382021-07-22 17:10:45 +0100451 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100452 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100453 else if( additional_data->len - part_offset < ad_part_len )
454 {
455 part_length = additional_data->len - part_offset;
456 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100457 else
458 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100459 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100460 }
461
462 PSA_ASSERT( psa_aead_update_ad( &operation,
463 additional_data->x + part_offset,
464 part_length ) );
465
Paul Elliottd3f82412021-06-16 16:52:21 +0100466 }
467 }
468 else
469 {
470 /* Pass additional data in one go. */
471 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
472 additional_data->len ) );
473 }
474
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100475 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100476 {
477 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100478 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100479 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100480 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100481
482 ASSERT_ALLOC( part_data, part_data_size );
483
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100484 for( part_offset = 0, part_count = 0;
485 part_offset < data_true_size;
486 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100487 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100488 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100489 {
Paul Elliott329d5382021-07-22 17:10:45 +0100490 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100491 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100492 else if( ( data_true_size - part_offset ) < data_part_len )
493 {
494 part_length = ( data_true_size - part_offset );
495 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100496 else
497 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100498 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100499 }
500
501 PSA_ASSERT( psa_aead_update( &operation,
502 ( input_data->x + part_offset ),
503 part_length, part_data,
504 part_data_size,
505 &output_part_length ) );
506
507 if( output_data && output_part_length )
508 {
509 memcpy( ( output_data + part_offset ), part_data,
510 output_part_length );
511 }
512
Paul Elliottd3f82412021-06-16 16:52:21 +0100513 output_length += output_part_length;
514 }
515 }
516 else
517 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100518 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100519 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100520 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100521 output_size, &output_length ) );
522 }
523
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100524 if( is_encrypt )
525 PSA_ASSERT( psa_aead_finish( &operation, final_data,
526 final_output_size,
527 &output_part_length,
528 tag_buffer, tag_length,
529 &tag_size ) );
530 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100531 {
Paul Elliott9961a662021-09-17 19:19:02 +0100532 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100533 final_output_size,
534 &output_part_length,
535 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100536 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100537 }
538
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100539 if( output_data && output_part_length )
540 memcpy( ( output_data + output_length ), final_data,
541 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100542
543 output_length += output_part_length;
544
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100545
546 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
547 * should be exact.*/
548 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100549 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100550 TEST_EQUAL( tag_length, tag_size );
551
552 if( output_data && tag_length )
553 memcpy( ( output_data + output_length ), tag_buffer,
554 tag_length );
555
556 output_length += tag_length;
557
558 TEST_EQUAL( output_length,
559 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
560 input_data->len ) );
561 TEST_ASSERT( output_length <=
562 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
563 }
564 else
565 {
566 TEST_EQUAL( output_length,
567 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
568 input_data->len ) );
569 TEST_ASSERT( output_length <=
570 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100571 }
572
Paul Elliottd3f82412021-06-16 16:52:21 +0100573
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100574 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100575 output_data, output_length );
576
Paul Elliottd3f82412021-06-16 16:52:21 +0100577
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100578 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100579
580exit:
581 psa_destroy_key( key );
582 psa_aead_abort( &operation );
583 mbedtls_free( output_data );
584 mbedtls_free( part_data );
585 mbedtls_free( final_data );
586 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100587
588 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100589}
590
Gilles Peskinee59236f2018-01-27 23:32:46 +0100591/* END_HEADER */
592
593/* BEGIN_DEPENDENCIES
594 * depends_on:MBEDTLS_PSA_CRYPTO_C
595 * END_DEPENDENCIES
596 */
597
598/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200599void static_checks( )
600{
601 size_t max_truncated_mac_size =
602 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
603
604 /* Check that the length for a truncated MAC always fits in the algorithm
605 * encoding. The shifted mask is the maximum truncated value. The
606 * untruncated algorithm may be one byte larger. */
607 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
608}
609/* END_CASE */
610
611/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200612void import_with_policy( int type_arg,
613 int usage_arg, int alg_arg,
614 int expected_status_arg )
615{
616 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
617 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200618 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200619 psa_key_type_t type = type_arg;
620 psa_key_usage_t usage = usage_arg;
621 psa_algorithm_t alg = alg_arg;
622 psa_status_t expected_status = expected_status_arg;
623 const uint8_t key_material[16] = {0};
624 psa_status_t status;
625
626 PSA_ASSERT( psa_crypto_init( ) );
627
628 psa_set_key_type( &attributes, type );
629 psa_set_key_usage_flags( &attributes, usage );
630 psa_set_key_algorithm( &attributes, alg );
631
632 status = psa_import_key( &attributes,
633 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200634 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200635 TEST_EQUAL( status, expected_status );
636 if( status != PSA_SUCCESS )
637 goto exit;
638
Ronald Cron5425a212020-08-04 14:58:35 +0200639 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200640 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200641 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200642 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200643 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200644 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200645
Ronald Cron5425a212020-08-04 14:58:35 +0200646 PSA_ASSERT( psa_destroy_key( key ) );
647 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200648
649exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100650 /*
651 * Key attributes may have been returned by psa_get_key_attributes()
652 * thus reset them as required.
653 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200654 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100655
656 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200657 PSA_DONE( );
658}
659/* END_CASE */
660
661/* BEGIN_CASE */
662void import_with_data( data_t *data, int type_arg,
663 int attr_bits_arg,
664 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200665{
666 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
667 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200668 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200669 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200670 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200671 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100672 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673
Gilles Peskine8817f612018-12-18 00:18:46 +0100674 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675
Gilles Peskine4747d192019-04-17 15:05:45 +0200676 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200677 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200678
Ronald Cron5425a212020-08-04 14:58:35 +0200679 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100680 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200681 if( status != PSA_SUCCESS )
682 goto exit;
683
Ronald Cron5425a212020-08-04 14:58:35 +0200684 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200685 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200686 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200687 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200688 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200689
Ronald Cron5425a212020-08-04 14:58:35 +0200690 PSA_ASSERT( psa_destroy_key( key ) );
691 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100692
693exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100694 /*
695 * Key attributes may have been returned by psa_get_key_attributes()
696 * thus reset them as required.
697 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200698 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100699
700 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200701 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100702}
703/* END_CASE */
704
705/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200706void import_large_key( int type_arg, int byte_size_arg,
707 int expected_status_arg )
708{
709 psa_key_type_t type = type_arg;
710 size_t byte_size = byte_size_arg;
711 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
712 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200713 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200714 psa_status_t status;
715 uint8_t *buffer = NULL;
716 size_t buffer_size = byte_size + 1;
717 size_t n;
718
Steven Cooreman69967ce2021-01-18 18:01:08 +0100719 /* Skip the test case if the target running the test cannot
720 * accomodate large keys due to heap size constraints */
721 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200722 memset( buffer, 'K', byte_size );
723
724 PSA_ASSERT( psa_crypto_init( ) );
725
726 /* Try importing the key */
727 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
728 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200729 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100730 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200731 TEST_EQUAL( status, expected_status );
732
733 if( status == PSA_SUCCESS )
734 {
Ronald Cron5425a212020-08-04 14:58:35 +0200735 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200736 TEST_EQUAL( psa_get_key_type( &attributes ), type );
737 TEST_EQUAL( psa_get_key_bits( &attributes ),
738 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200739 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200740 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200741 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200742 for( n = 0; n < byte_size; n++ )
743 TEST_EQUAL( buffer[n], 'K' );
744 for( n = byte_size; n < buffer_size; n++ )
745 TEST_EQUAL( buffer[n], 0 );
746 }
747
748exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100749 /*
750 * Key attributes may have been returned by psa_get_key_attributes()
751 * thus reset them as required.
752 */
753 psa_reset_key_attributes( &attributes );
754
Ronald Cron5425a212020-08-04 14:58:35 +0200755 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200756 PSA_DONE( );
757 mbedtls_free( buffer );
758}
759/* END_CASE */
760
761/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200762void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
763{
Ronald Cron5425a212020-08-04 14:58:35 +0200764 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200765 size_t bits = bits_arg;
766 psa_status_t expected_status = expected_status_arg;
767 psa_status_t status;
768 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200769 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200770 size_t buffer_size = /* Slight overapproximations */
771 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200772 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200773 unsigned char *p;
774 int ret;
775 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200776 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200777
Gilles Peskine8817f612018-12-18 00:18:46 +0100778 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200779 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200780
781 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
782 bits, keypair ) ) >= 0 );
783 length = ret;
784
785 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200786 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200787 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100788 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200789
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200790 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200791 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200792
793exit:
794 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200795 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200796}
797/* END_CASE */
798
799/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300800void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300801 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200802 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +0530803 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100804 int expected_bits,
805 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200806 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100807 int canonical_input )
808{
Ronald Cron5425a212020-08-04 14:58:35 +0200809 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100810 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200811 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200812 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100813 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +0530814 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100815 unsigned char *exported = NULL;
816 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100817 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100818 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200820 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200821 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100822
Moran Pekercb088e72018-07-17 17:36:59 +0300823 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200824 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100825 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200826 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100827 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100828
Archana4d7ae1d2021-07-07 02:50:22 +0530829 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +0200830 psa_set_key_usage_flags( &attributes, usage_arg );
831 psa_set_key_algorithm( &attributes, alg );
832 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700833
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100834 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200835 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100836
837 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200838 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200839 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
840 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200841 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100842
843 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200844 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100845 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100846
847 /* The exported length must be set by psa_export_key() to a value between 0
848 * and export_size. On errors, the exported length must be 0. */
849 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
850 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
851 TEST_ASSERT( exported_length <= export_size );
852
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200853 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200854 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100855 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200856 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100857 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100858 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200859 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100860
Gilles Peskineea38a922021-02-13 00:05:16 +0100861 /* Run sanity checks on the exported key. For non-canonical inputs,
862 * this validates the canonical representations. For canonical inputs,
863 * this doesn't directly validate the implementation, but it still helps
864 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +0530865 if( !psa_key_lifetime_is_external( lifetime ) )
866 {
867 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
868 goto exit;
869 }
Gilles Peskine8f609232018-08-11 01:24:55 +0200870
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100871 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200872 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100873 else
874 {
Ronald Cron5425a212020-08-04 14:58:35 +0200875 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200876 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200877 &key2 ) );
878 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100879 reexported,
880 export_size,
881 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200882 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +0530883 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200884 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100885 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100886 TEST_ASSERT( exported_length <=
Archana4d7ae1d2021-07-07 02:50:22 +0530887 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +0530888 psa_get_key_bits( &got_attributes ) ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100889 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100890
891destroy:
892 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200893 PSA_ASSERT( psa_destroy_key( key ) );
894 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100895
896exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100897 /*
898 * Key attributes may have been returned by psa_get_key_attributes()
899 * thus reset them as required.
900 */
901 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +0530902 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300903 mbedtls_free( exported );
904 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200905 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100906}
907/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100908
Moran Pekerf709f4a2018-06-06 17:26:04 +0300909/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300910void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200911 int type_arg,
912 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +0530913 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100914 int export_size_delta,
915 int expected_export_status_arg,
916 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300917{
Ronald Cron5425a212020-08-04 14:58:35 +0200918 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300919 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200920 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200921 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300922 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +0530923 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300924 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100925 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100926 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200927 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300928
Gilles Peskine8817f612018-12-18 00:18:46 +0100929 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300930
Archana4d7ae1d2021-07-07 02:50:22 +0530931 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +0200932 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
933 psa_set_key_algorithm( &attributes, alg );
934 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300935
936 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200937 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300938
Gilles Peskine49c25912018-10-29 15:15:31 +0100939 /* Export the public key */
940 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200941 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200942 exported, export_size,
943 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100944 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100945 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100946 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200947 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100948 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200949 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200950 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100951 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100952 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100953 TEST_ASSERT( expected_public_key->len <=
954 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
955 TEST_ASSERT( expected_public_key->len <=
956 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100957 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
958 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100959 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300960exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100961 /*
962 * Key attributes may have been returned by psa_get_key_attributes()
963 * thus reset them as required.
964 */
965 psa_reset_key_attributes( &attributes );
966
itayzafrir3e02b3b2018-06-12 17:06:52 +0300967 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200968 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200969 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300970}
971/* END_CASE */
972
Gilles Peskine20035e32018-02-03 22:44:14 +0100973/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200974void import_and_exercise_key( data_t *data,
975 int type_arg,
976 int bits_arg,
977 int alg_arg )
978{
Ronald Cron5425a212020-08-04 14:58:35 +0200979 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200980 psa_key_type_t type = type_arg;
981 size_t bits = bits_arg;
982 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100983 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200984 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200985 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200986
Gilles Peskine8817f612018-12-18 00:18:46 +0100987 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200988
Gilles Peskine4747d192019-04-17 15:05:45 +0200989 psa_set_key_usage_flags( &attributes, usage );
990 psa_set_key_algorithm( &attributes, alg );
991 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200992
993 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200994 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200995
996 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200997 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200998 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
999 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001000
1001 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001002 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001003 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001004
Ronald Cron5425a212020-08-04 14:58:35 +02001005 PSA_ASSERT( psa_destroy_key( key ) );
1006 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001007
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001008exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001009 /*
1010 * Key attributes may have been returned by psa_get_key_attributes()
1011 * thus reset them as required.
1012 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001013 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001014
1015 psa_reset_key_attributes( &attributes );
1016 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001017 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001018}
1019/* END_CASE */
1020
1021/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001022void effective_key_attributes( int type_arg, int expected_type_arg,
1023 int bits_arg, int expected_bits_arg,
1024 int usage_arg, int expected_usage_arg,
1025 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001026{
Ronald Cron5425a212020-08-04 14:58:35 +02001027 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001028 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001029 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001030 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001031 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001032 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001033 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001034 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001035 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001036 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001037
Gilles Peskine8817f612018-12-18 00:18:46 +01001038 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001039
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001040 psa_set_key_usage_flags( &attributes, usage );
1041 psa_set_key_algorithm( &attributes, alg );
1042 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001043 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001044
Ronald Cron5425a212020-08-04 14:58:35 +02001045 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001046 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001047
Ronald Cron5425a212020-08-04 14:58:35 +02001048 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001049 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1050 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1051 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1052 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001053
1054exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001055 /*
1056 * Key attributes may have been returned by psa_get_key_attributes()
1057 * thus reset them as required.
1058 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001059 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001060
1061 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001062 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001063}
1064/* END_CASE */
1065
1066/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001067void check_key_policy( int type_arg, int bits_arg,
1068 int usage_arg, int alg_arg )
1069{
1070 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001071 usage_arg,
1072 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001073 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001074 goto exit;
1075}
1076/* END_CASE */
1077
1078/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001079void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001080{
1081 /* Test each valid way of initializing the object, except for `= {0}`, as
1082 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1083 * though it's OK by the C standard. We could test for this, but we'd need
1084 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001085 psa_key_attributes_t func = psa_key_attributes_init( );
1086 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1087 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001088
1089 memset( &zero, 0, sizeof( zero ) );
1090
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001091 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1092 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1093 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001094
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001095 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1096 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1097 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1098
1099 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1100 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1101 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1102
1103 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1104 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1105 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1106
1107 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1108 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1109 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001110}
1111/* END_CASE */
1112
1113/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001114void mac_key_policy( int policy_usage_arg,
1115 int policy_alg_arg,
1116 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001117 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001118 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001119 int expected_status_sign_arg,
1120 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001121{
Ronald Cron5425a212020-08-04 14:58:35 +02001122 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001123 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001124 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001125 psa_key_type_t key_type = key_type_arg;
1126 psa_algorithm_t policy_alg = policy_alg_arg;
1127 psa_algorithm_t exercise_alg = exercise_alg_arg;
1128 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001129 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001130 psa_status_t expected_status_sign = expected_status_sign_arg;
1131 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001132 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001133
Gilles Peskine8817f612018-12-18 00:18:46 +01001134 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001135
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001136 psa_set_key_usage_flags( &attributes, policy_usage );
1137 psa_set_key_algorithm( &attributes, policy_alg );
1138 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001139
Gilles Peskine049c7532019-05-15 20:22:09 +02001140 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001141 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001142
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001143 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1144 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001145
Ronald Cron5425a212020-08-04 14:58:35 +02001146 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001147 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001148
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001149 /* Calculate the MAC, one-shot case. */
1150 uint8_t input[128] = {0};
1151 size_t mac_len;
1152 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1153 input, 128,
1154 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1155 expected_status_sign );
1156
1157 /* Verify correct MAC, one-shot case. */
1158 status = psa_mac_verify( key, exercise_alg, input, 128,
1159 mac, mac_len );
1160
1161 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1162 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001163 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001164 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001165
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001166 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001167
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001168 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001169 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001170 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001171
1172exit:
1173 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001174 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001175 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001176}
1177/* END_CASE */
1178
1179/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001180void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001181 int policy_alg,
1182 int key_type,
1183 data_t *key_data,
1184 int exercise_alg )
1185{
Ronald Cron5425a212020-08-04 14:58:35 +02001186 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001187 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001188 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001189 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001190 psa_status_t status;
1191
Gilles Peskine8817f612018-12-18 00:18:46 +01001192 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001193
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001194 psa_set_key_usage_flags( &attributes, policy_usage );
1195 psa_set_key_algorithm( &attributes, policy_alg );
1196 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001197
Gilles Peskine049c7532019-05-15 20:22:09 +02001198 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001199 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001200
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001201 /* Check if no key usage flag implication is done */
1202 TEST_EQUAL( policy_usage,
1203 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001204
Ronald Cron5425a212020-08-04 14:58:35 +02001205 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001206 if( policy_alg == exercise_alg &&
1207 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001208 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001209 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001210 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001211 psa_cipher_abort( &operation );
1212
Ronald Cron5425a212020-08-04 14:58:35 +02001213 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001214 if( policy_alg == exercise_alg &&
1215 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001216 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001217 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001218 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001219
1220exit:
1221 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001222 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001223 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001224}
1225/* END_CASE */
1226
1227/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001228void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001229 int policy_alg,
1230 int key_type,
1231 data_t *key_data,
1232 int nonce_length_arg,
1233 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001234 int exercise_alg,
1235 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001236{
Ronald Cron5425a212020-08-04 14:58:35 +02001237 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001238 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001239 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001240 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001241 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001242 unsigned char nonce[16] = {0};
1243 size_t nonce_length = nonce_length_arg;
1244 unsigned char tag[16];
1245 size_t tag_length = tag_length_arg;
1246 size_t output_length;
1247
1248 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1249 TEST_ASSERT( tag_length <= sizeof( tag ) );
1250
Gilles Peskine8817f612018-12-18 00:18:46 +01001251 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001252
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001253 psa_set_key_usage_flags( &attributes, policy_usage );
1254 psa_set_key_algorithm( &attributes, policy_alg );
1255 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001256
Gilles Peskine049c7532019-05-15 20:22:09 +02001257 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001258 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001259
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001260 /* Check if no key usage implication is done */
1261 TEST_EQUAL( policy_usage,
1262 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001263
Ronald Cron5425a212020-08-04 14:58:35 +02001264 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001265 nonce, nonce_length,
1266 NULL, 0,
1267 NULL, 0,
1268 tag, tag_length,
1269 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001270 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1271 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001272 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001273 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001274
1275 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001276 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001277 nonce, nonce_length,
1278 NULL, 0,
1279 tag, tag_length,
1280 NULL, 0,
1281 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001282 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1283 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1284 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001285 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001286 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001287 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001288
1289exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001290 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001291 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001292}
1293/* END_CASE */
1294
1295/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001296void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001297 int policy_alg,
1298 int key_type,
1299 data_t *key_data,
1300 int exercise_alg )
1301{
Ronald Cron5425a212020-08-04 14:58:35 +02001302 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001303 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001304 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001305 psa_status_t status;
1306 size_t key_bits;
1307 size_t buffer_length;
1308 unsigned char *buffer = NULL;
1309 size_t output_length;
1310
Gilles Peskine8817f612018-12-18 00:18:46 +01001311 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001312
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001313 psa_set_key_usage_flags( &attributes, policy_usage );
1314 psa_set_key_algorithm( &attributes, policy_alg );
1315 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001316
Gilles Peskine049c7532019-05-15 20:22:09 +02001317 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001318 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001319
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001320 /* Check if no key usage implication is done */
1321 TEST_EQUAL( policy_usage,
1322 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001323
Ronald Cron5425a212020-08-04 14:58:35 +02001324 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001325 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001326 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1327 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001328 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001329
Ronald Cron5425a212020-08-04 14:58:35 +02001330 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001331 NULL, 0,
1332 NULL, 0,
1333 buffer, buffer_length,
1334 &output_length );
1335 if( policy_alg == exercise_alg &&
1336 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001337 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001338 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001339 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001340
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001341 if( buffer_length != 0 )
1342 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001343 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001344 buffer, buffer_length,
1345 NULL, 0,
1346 buffer, buffer_length,
1347 &output_length );
1348 if( policy_alg == exercise_alg &&
1349 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001350 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001351 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001352 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001353
1354exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001355 /*
1356 * Key attributes may have been returned by psa_get_key_attributes()
1357 * thus reset them as required.
1358 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001359 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001360
1361 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001362 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001363 mbedtls_free( buffer );
1364}
1365/* END_CASE */
1366
1367/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001368void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001369 int policy_alg,
1370 int key_type,
1371 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001372 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001373 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001374 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001375{
Ronald Cron5425a212020-08-04 14:58:35 +02001376 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001377 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001378 psa_key_usage_t policy_usage = policy_usage_arg;
1379 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001380 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001381 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1382 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1383 * compatible with the policy and `payload_length_arg` is supposed to be
1384 * a valid input length to sign. If `payload_length_arg <= 0`,
1385 * `exercise_alg` is supposed to be forbidden by the policy. */
1386 int compatible_alg = payload_length_arg > 0;
1387 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001388 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001389 size_t signature_length;
1390
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001391 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001392 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001393 TEST_EQUAL( expected_usage,
1394 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001395
Gilles Peskine8817f612018-12-18 00:18:46 +01001396 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001397
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001398 psa_set_key_usage_flags( &attributes, policy_usage );
1399 psa_set_key_algorithm( &attributes, policy_alg );
1400 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001401
Gilles Peskine049c7532019-05-15 20:22:09 +02001402 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001403 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001404
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001405 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1406
Ronald Cron5425a212020-08-04 14:58:35 +02001407 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001408 payload, payload_length,
1409 signature, sizeof( signature ),
1410 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001411 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001412 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001413 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001414 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001415
1416 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001417 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001418 payload, payload_length,
1419 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001420 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001421 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001422 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001423 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001424
gabor-mezei-armd851d682021-06-28 14:53:49 +02001425 if( PSA_ALG_IS_HASH_AND_SIGN( exercise_alg ) &&
1426 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001427 {
1428 status = psa_sign_message( key, exercise_alg,
1429 payload, payload_length,
1430 signature, sizeof( signature ),
1431 &signature_length );
1432 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1433 PSA_ASSERT( status );
1434 else
1435 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1436
1437 memset( signature, 0, sizeof( signature ) );
1438 status = psa_verify_message( key, exercise_alg,
1439 payload, payload_length,
1440 signature, sizeof( signature ) );
1441 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1442 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1443 else
1444 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1445 }
1446
Gilles Peskined5b33222018-06-18 22:20:03 +02001447exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001448 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001449 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001450}
1451/* END_CASE */
1452
Janos Follathba3fab92019-06-11 14:50:16 +01001453/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001454void derive_key_policy( int policy_usage,
1455 int policy_alg,
1456 int key_type,
1457 data_t *key_data,
1458 int exercise_alg )
1459{
Ronald Cron5425a212020-08-04 14:58:35 +02001460 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001461 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001462 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001463 psa_status_t status;
1464
Gilles Peskine8817f612018-12-18 00:18:46 +01001465 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001466
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001467 psa_set_key_usage_flags( &attributes, policy_usage );
1468 psa_set_key_algorithm( &attributes, policy_alg );
1469 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001470
Gilles Peskine049c7532019-05-15 20:22:09 +02001471 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001472 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001473
Janos Follathba3fab92019-06-11 14:50:16 +01001474 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1475
1476 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1477 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001478 {
Janos Follathba3fab92019-06-11 14:50:16 +01001479 PSA_ASSERT( psa_key_derivation_input_bytes(
1480 &operation,
1481 PSA_KEY_DERIVATION_INPUT_SEED,
1482 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001483 }
Janos Follathba3fab92019-06-11 14:50:16 +01001484
1485 status = psa_key_derivation_input_key( &operation,
1486 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001487 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001488
Gilles Peskineea0fb492018-07-12 17:17:20 +02001489 if( policy_alg == exercise_alg &&
1490 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001491 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001492 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001493 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001494
1495exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001496 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001497 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001498 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001499}
1500/* END_CASE */
1501
1502/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001503void agreement_key_policy( int policy_usage,
1504 int policy_alg,
1505 int key_type_arg,
1506 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001507 int exercise_alg,
1508 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001509{
Ronald Cron5425a212020-08-04 14:58:35 +02001510 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001511 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001512 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001513 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001514 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001515 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001516
Gilles Peskine8817f612018-12-18 00:18:46 +01001517 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001518
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001519 psa_set_key_usage_flags( &attributes, policy_usage );
1520 psa_set_key_algorithm( &attributes, policy_alg );
1521 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001522
Gilles Peskine049c7532019-05-15 20:22:09 +02001523 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001524 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001525
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001526 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001527 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001528
Steven Cooremance48e852020-10-05 16:02:45 +02001529 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001530
1531exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001532 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001533 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001534 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001535}
1536/* END_CASE */
1537
1538/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001539void key_policy_alg2( int key_type_arg, data_t *key_data,
1540 int usage_arg, int alg_arg, int alg2_arg )
1541{
Ronald Cron5425a212020-08-04 14:58:35 +02001542 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001543 psa_key_type_t key_type = key_type_arg;
1544 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1545 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1546 psa_key_usage_t usage = usage_arg;
1547 psa_algorithm_t alg = alg_arg;
1548 psa_algorithm_t alg2 = alg2_arg;
1549
1550 PSA_ASSERT( psa_crypto_init( ) );
1551
1552 psa_set_key_usage_flags( &attributes, usage );
1553 psa_set_key_algorithm( &attributes, alg );
1554 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1555 psa_set_key_type( &attributes, key_type );
1556 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001557 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001558
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001559 /* Update the usage flags to obtain implicit usage flags */
1560 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001561 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001562 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1563 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1564 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1565
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001566 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001567 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001568 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001569 goto exit;
1570
1571exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001572 /*
1573 * Key attributes may have been returned by psa_get_key_attributes()
1574 * thus reset them as required.
1575 */
1576 psa_reset_key_attributes( &got_attributes );
1577
Ronald Cron5425a212020-08-04 14:58:35 +02001578 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001579 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001580}
1581/* END_CASE */
1582
1583/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001584void raw_agreement_key_policy( int policy_usage,
1585 int policy_alg,
1586 int key_type_arg,
1587 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001588 int exercise_alg,
1589 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001590{
Ronald Cron5425a212020-08-04 14:58:35 +02001591 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001592 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001593 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001594 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001595 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001596 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001597
1598 PSA_ASSERT( psa_crypto_init( ) );
1599
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001600 psa_set_key_usage_flags( &attributes, policy_usage );
1601 psa_set_key_algorithm( &attributes, policy_alg );
1602 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001603
Gilles Peskine049c7532019-05-15 20:22:09 +02001604 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001605 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001606
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001607 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001608
Steven Cooremance48e852020-10-05 16:02:45 +02001609 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001610
1611exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001612 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001613 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001614 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001615}
1616/* END_CASE */
1617
1618/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001619void copy_success( int source_usage_arg,
1620 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301621 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001622 int type_arg, data_t *material,
1623 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001624 int target_usage_arg,
1625 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301626 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001627 int expected_usage_arg,
1628 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001629{
Gilles Peskineca25db92019-04-19 11:43:08 +02001630 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1631 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001632 psa_key_usage_t expected_usage = expected_usage_arg;
1633 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001634 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05301635 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
1636 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001637 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1638 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001639 uint8_t *export_buffer = NULL;
1640
Gilles Peskine57ab7212019-01-28 13:03:09 +01001641 PSA_ASSERT( psa_crypto_init( ) );
1642
Gilles Peskineca25db92019-04-19 11:43:08 +02001643 /* Prepare the source key. */
1644 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1645 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001646 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001647 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301648 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02001649 PSA_ASSERT( psa_import_key( &source_attributes,
1650 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001651 &source_key ) );
1652 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001653
Gilles Peskineca25db92019-04-19 11:43:08 +02001654 /* Prepare the target attributes. */
1655 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001656 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001657 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001658 }
Archana8a180362021-07-05 02:18:48 +05301659 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02001660
Gilles Peskineca25db92019-04-19 11:43:08 +02001661 if( target_usage_arg != -1 )
1662 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1663 if( target_alg_arg != -1 )
1664 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001665 if( target_alg2_arg != -1 )
1666 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001667
Archana8a180362021-07-05 02:18:48 +05301668
Gilles Peskine57ab7212019-01-28 13:03:09 +01001669 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001670 PSA_ASSERT( psa_copy_key( source_key,
1671 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001672
1673 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001674 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001675
1676 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001677 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001678 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1679 psa_get_key_type( &target_attributes ) );
1680 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1681 psa_get_key_bits( &target_attributes ) );
1682 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1683 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001684 TEST_EQUAL( expected_alg2,
1685 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001686 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1687 {
1688 size_t length;
1689 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001690 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001691 material->len, &length ) );
1692 ASSERT_COMPARE( material->x, material->len,
1693 export_buffer, length );
1694 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001695
Archana8a180362021-07-05 02:18:48 +05301696 if( !psa_key_lifetime_is_external( target_lifetime ) )
1697 {
1698 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
1699 goto exit;
1700 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
1701 goto exit;
1702 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001703
Ronald Cron5425a212020-08-04 14:58:35 +02001704 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001705
1706exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001707 /*
1708 * Source and target key attributes may have been returned by
1709 * psa_get_key_attributes() thus reset them as required.
1710 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001711 psa_reset_key_attributes( &source_attributes );
1712 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001713
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001714 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001715 mbedtls_free( export_buffer );
1716}
1717/* END_CASE */
1718
1719/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001720void copy_fail( int source_usage_arg,
1721 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301722 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001723 int type_arg, data_t *material,
1724 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001725 int target_usage_arg,
1726 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001727 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001728 int expected_status_arg )
1729{
1730 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1731 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001732 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1733 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001734 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001735
1736 PSA_ASSERT( psa_crypto_init( ) );
1737
1738 /* Prepare the source key. */
1739 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1740 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001741 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001742 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301743 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001744 PSA_ASSERT( psa_import_key( &source_attributes,
1745 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001746 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001747
1748 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001749 psa_set_key_id( &target_attributes, key_id );
1750 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001751 psa_set_key_type( &target_attributes, target_type_arg );
1752 psa_set_key_bits( &target_attributes, target_bits_arg );
1753 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1754 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001755 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001756
1757 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001758 TEST_EQUAL( psa_copy_key( source_key,
1759 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001760 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001761
Ronald Cron5425a212020-08-04 14:58:35 +02001762 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001763
Gilles Peskine4a644642019-05-03 17:14:08 +02001764exit:
1765 psa_reset_key_attributes( &source_attributes );
1766 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001767 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001768}
1769/* END_CASE */
1770
1771/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001772void hash_operation_init( )
1773{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001774 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001775 /* Test each valid way of initializing the object, except for `= {0}`, as
1776 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1777 * though it's OK by the C standard. We could test for this, but we'd need
1778 * to supress the Clang warning for the test. */
1779 psa_hash_operation_t func = psa_hash_operation_init( );
1780 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1781 psa_hash_operation_t zero;
1782
1783 memset( &zero, 0, sizeof( zero ) );
1784
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001785 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001786 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1787 PSA_ERROR_BAD_STATE );
1788 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1789 PSA_ERROR_BAD_STATE );
1790 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1791 PSA_ERROR_BAD_STATE );
1792
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001793 /* A default hash operation should be abortable without error. */
1794 PSA_ASSERT( psa_hash_abort( &func ) );
1795 PSA_ASSERT( psa_hash_abort( &init ) );
1796 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001797}
1798/* END_CASE */
1799
1800/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001801void hash_setup( int alg_arg,
1802 int expected_status_arg )
1803{
1804 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001805 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001806 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001807 psa_status_t status;
1808
Gilles Peskine8817f612018-12-18 00:18:46 +01001809 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001810
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001811 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001812 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001813
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001814 /* Whether setup succeeded or failed, abort must succeed. */
1815 PSA_ASSERT( psa_hash_abort( &operation ) );
1816
1817 /* If setup failed, reproduce the failure, so as to
1818 * test the resulting state of the operation object. */
1819 if( status != PSA_SUCCESS )
1820 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1821
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001822 /* Now the operation object should be reusable. */
1823#if defined(KNOWN_SUPPORTED_HASH_ALG)
1824 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1825 PSA_ASSERT( psa_hash_abort( &operation ) );
1826#endif
1827
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001828exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001829 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001830}
1831/* END_CASE */
1832
1833/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001834void hash_compute_fail( int alg_arg, data_t *input,
1835 int output_size_arg, int expected_status_arg )
1836{
1837 psa_algorithm_t alg = alg_arg;
1838 uint8_t *output = NULL;
1839 size_t output_size = output_size_arg;
1840 size_t output_length = INVALID_EXPORT_LENGTH;
1841 psa_status_t expected_status = expected_status_arg;
1842 psa_status_t status;
1843
1844 ASSERT_ALLOC( output, output_size );
1845
1846 PSA_ASSERT( psa_crypto_init( ) );
1847
1848 status = psa_hash_compute( alg, input->x, input->len,
1849 output, output_size, &output_length );
1850 TEST_EQUAL( status, expected_status );
1851 TEST_ASSERT( output_length <= output_size );
1852
1853exit:
1854 mbedtls_free( output );
1855 PSA_DONE( );
1856}
1857/* END_CASE */
1858
1859/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001860void hash_compare_fail( int alg_arg, data_t *input,
1861 data_t *reference_hash,
1862 int expected_status_arg )
1863{
1864 psa_algorithm_t alg = alg_arg;
1865 psa_status_t expected_status = expected_status_arg;
1866 psa_status_t status;
1867
1868 PSA_ASSERT( psa_crypto_init( ) );
1869
1870 status = psa_hash_compare( alg, input->x, input->len,
1871 reference_hash->x, reference_hash->len );
1872 TEST_EQUAL( status, expected_status );
1873
1874exit:
1875 PSA_DONE( );
1876}
1877/* END_CASE */
1878
1879/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001880void hash_compute_compare( int alg_arg, data_t *input,
1881 data_t *expected_output )
1882{
1883 psa_algorithm_t alg = alg_arg;
1884 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1885 size_t output_length = INVALID_EXPORT_LENGTH;
1886 size_t i;
1887
1888 PSA_ASSERT( psa_crypto_init( ) );
1889
1890 /* Compute with tight buffer */
1891 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001892 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001893 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001894 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001895 ASSERT_COMPARE( output, output_length,
1896 expected_output->x, expected_output->len );
1897
1898 /* Compute with larger buffer */
1899 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1900 output, sizeof( output ),
1901 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001902 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001903 ASSERT_COMPARE( output, output_length,
1904 expected_output->x, expected_output->len );
1905
1906 /* Compare with correct hash */
1907 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1908 output, output_length ) );
1909
1910 /* Compare with trailing garbage */
1911 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1912 output, output_length + 1 ),
1913 PSA_ERROR_INVALID_SIGNATURE );
1914
1915 /* Compare with truncated hash */
1916 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1917 output, output_length - 1 ),
1918 PSA_ERROR_INVALID_SIGNATURE );
1919
1920 /* Compare with corrupted value */
1921 for( i = 0; i < output_length; i++ )
1922 {
Chris Jones9634bb12021-01-20 15:56:42 +00001923 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001924 output[i] ^= 1;
1925 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1926 output, output_length ),
1927 PSA_ERROR_INVALID_SIGNATURE );
1928 output[i] ^= 1;
1929 }
1930
1931exit:
1932 PSA_DONE( );
1933}
1934/* END_CASE */
1935
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001936/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001937void hash_bad_order( )
1938{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001939 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001940 unsigned char input[] = "";
1941 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001942 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001943 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1944 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1945 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001946 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001947 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001948 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001949
Gilles Peskine8817f612018-12-18 00:18:46 +01001950 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001951
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001952 /* Call setup twice in a row. */
1953 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001954 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001955 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1956 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001957 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001958 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001959 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001960
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001961 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001962 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001963 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001964 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001965
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001966 /* Check that update calls abort on error. */
1967 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01001968 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001969 ASSERT_OPERATION_IS_ACTIVE( operation );
1970 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1971 PSA_ERROR_BAD_STATE );
1972 ASSERT_OPERATION_IS_INACTIVE( operation );
1973 PSA_ASSERT( psa_hash_abort( &operation ) );
1974 ASSERT_OPERATION_IS_INACTIVE( operation );
1975
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001976 /* Call update after finish. */
1977 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1978 PSA_ASSERT( psa_hash_finish( &operation,
1979 hash, sizeof( hash ), &hash_len ) );
1980 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001981 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001982 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001983
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001984 /* Call verify without calling setup beforehand. */
1985 TEST_EQUAL( psa_hash_verify( &operation,
1986 valid_hash, sizeof( valid_hash ) ),
1987 PSA_ERROR_BAD_STATE );
1988 PSA_ASSERT( psa_hash_abort( &operation ) );
1989
1990 /* Call verify 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_verify( &operation,
1995 valid_hash, sizeof( valid_hash ) ),
1996 PSA_ERROR_BAD_STATE );
1997 PSA_ASSERT( psa_hash_abort( &operation ) );
1998
1999 /* Call verify twice in a row. */
2000 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002001 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002002 PSA_ASSERT( psa_hash_verify( &operation,
2003 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002004 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002005 TEST_EQUAL( psa_hash_verify( &operation,
2006 valid_hash, sizeof( valid_hash ) ),
2007 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002008 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002009 PSA_ASSERT( psa_hash_abort( &operation ) );
2010
2011 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002012 TEST_EQUAL( psa_hash_finish( &operation,
2013 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002014 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002015 PSA_ASSERT( psa_hash_abort( &operation ) );
2016
2017 /* Call finish twice in a row. */
2018 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2019 PSA_ASSERT( psa_hash_finish( &operation,
2020 hash, sizeof( hash ), &hash_len ) );
2021 TEST_EQUAL( psa_hash_finish( &operation,
2022 hash, sizeof( hash ), &hash_len ),
2023 PSA_ERROR_BAD_STATE );
2024 PSA_ASSERT( psa_hash_abort( &operation ) );
2025
2026 /* Call finish after calling verify. */
2027 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2028 PSA_ASSERT( psa_hash_verify( &operation,
2029 valid_hash, sizeof( valid_hash ) ) );
2030 TEST_EQUAL( psa_hash_finish( &operation,
2031 hash, sizeof( hash ), &hash_len ),
2032 PSA_ERROR_BAD_STATE );
2033 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002034
2035exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002036 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002037}
2038/* END_CASE */
2039
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002040/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002041void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002042{
2043 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002044 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2045 * appended to it */
2046 unsigned char hash[] = {
2047 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2048 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2049 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002050 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002051 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002052
Gilles Peskine8817f612018-12-18 00:18:46 +01002053 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002054
itayzafrir27e69452018-11-01 14:26:34 +02002055 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002056 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002057 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002058 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002059 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002060 ASSERT_OPERATION_IS_INACTIVE( operation );
2061 PSA_ASSERT( psa_hash_abort( &operation ) );
2062 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002063
itayzafrir27e69452018-11-01 14:26:34 +02002064 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002065 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002066 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002067 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002068
itayzafrir27e69452018-11-01 14:26:34 +02002069 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002070 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002071 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002072 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002073
itayzafrirec93d302018-10-18 18:01:10 +03002074exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002075 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002076}
2077/* END_CASE */
2078
Ronald Cronee414c72021-03-18 18:50:08 +01002079/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002080void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002081{
2082 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002083 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002084 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002085 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002086 size_t hash_len;
2087
Gilles Peskine8817f612018-12-18 00:18:46 +01002088 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002089
itayzafrir58028322018-10-25 10:22:01 +03002090 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002091 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002092 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002093 hash, expected_size - 1, &hash_len ),
2094 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002095
2096exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002097 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002098}
2099/* END_CASE */
2100
Ronald Cronee414c72021-03-18 18:50:08 +01002101/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002102void hash_clone_source_state( )
2103{
2104 psa_algorithm_t alg = PSA_ALG_SHA_256;
2105 unsigned char hash[PSA_HASH_MAX_SIZE];
2106 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2107 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2108 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2109 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2110 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2111 size_t hash_len;
2112
2113 PSA_ASSERT( psa_crypto_init( ) );
2114 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2115
2116 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2117 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2118 PSA_ASSERT( psa_hash_finish( &op_finished,
2119 hash, sizeof( hash ), &hash_len ) );
2120 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2121 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2122
2123 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2124 PSA_ERROR_BAD_STATE );
2125
2126 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2127 PSA_ASSERT( psa_hash_finish( &op_init,
2128 hash, sizeof( hash ), &hash_len ) );
2129 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2130 PSA_ASSERT( psa_hash_finish( &op_finished,
2131 hash, sizeof( hash ), &hash_len ) );
2132 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2133 PSA_ASSERT( psa_hash_finish( &op_aborted,
2134 hash, sizeof( hash ), &hash_len ) );
2135
2136exit:
2137 psa_hash_abort( &op_source );
2138 psa_hash_abort( &op_init );
2139 psa_hash_abort( &op_setup );
2140 psa_hash_abort( &op_finished );
2141 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002142 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002143}
2144/* END_CASE */
2145
Ronald Cronee414c72021-03-18 18:50:08 +01002146/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002147void hash_clone_target_state( )
2148{
2149 psa_algorithm_t alg = PSA_ALG_SHA_256;
2150 unsigned char hash[PSA_HASH_MAX_SIZE];
2151 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2152 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2153 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2154 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2155 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2156 size_t hash_len;
2157
2158 PSA_ASSERT( psa_crypto_init( ) );
2159
2160 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2161 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2162 PSA_ASSERT( psa_hash_finish( &op_finished,
2163 hash, sizeof( hash ), &hash_len ) );
2164 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2165 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2166
2167 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2168 PSA_ASSERT( psa_hash_finish( &op_target,
2169 hash, sizeof( hash ), &hash_len ) );
2170
2171 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2172 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2173 PSA_ERROR_BAD_STATE );
2174 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2175 PSA_ERROR_BAD_STATE );
2176
2177exit:
2178 psa_hash_abort( &op_target );
2179 psa_hash_abort( &op_init );
2180 psa_hash_abort( &op_setup );
2181 psa_hash_abort( &op_finished );
2182 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002183 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002184}
2185/* END_CASE */
2186
itayzafrir58028322018-10-25 10:22:01 +03002187/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002188void mac_operation_init( )
2189{
Jaeden Amero252ef282019-02-15 14:05:35 +00002190 const uint8_t input[1] = { 0 };
2191
Jaeden Amero769ce272019-01-04 11:48:03 +00002192 /* Test each valid way of initializing the object, except for `= {0}`, as
2193 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2194 * though it's OK by the C standard. We could test for this, but we'd need
2195 * to supress the Clang warning for the test. */
2196 psa_mac_operation_t func = psa_mac_operation_init( );
2197 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2198 psa_mac_operation_t zero;
2199
2200 memset( &zero, 0, sizeof( zero ) );
2201
Jaeden Amero252ef282019-02-15 14:05:35 +00002202 /* A freshly-initialized MAC operation should not be usable. */
2203 TEST_EQUAL( psa_mac_update( &func,
2204 input, sizeof( input ) ),
2205 PSA_ERROR_BAD_STATE );
2206 TEST_EQUAL( psa_mac_update( &init,
2207 input, sizeof( input ) ),
2208 PSA_ERROR_BAD_STATE );
2209 TEST_EQUAL( psa_mac_update( &zero,
2210 input, sizeof( input ) ),
2211 PSA_ERROR_BAD_STATE );
2212
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002213 /* A default MAC operation should be abortable without error. */
2214 PSA_ASSERT( psa_mac_abort( &func ) );
2215 PSA_ASSERT( psa_mac_abort( &init ) );
2216 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002217}
2218/* END_CASE */
2219
2220/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002221void mac_setup( int key_type_arg,
2222 data_t *key,
2223 int alg_arg,
2224 int expected_status_arg )
2225{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002226 psa_key_type_t key_type = key_type_arg;
2227 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002228 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002229 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002230 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2231#if defined(KNOWN_SUPPORTED_MAC_ALG)
2232 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2233#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002234
Gilles Peskine8817f612018-12-18 00:18:46 +01002235 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002236
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002237 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2238 &operation, &status ) )
2239 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002240 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002241
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002242 /* The operation object should be reusable. */
2243#if defined(KNOWN_SUPPORTED_MAC_ALG)
2244 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2245 smoke_test_key_data,
2246 sizeof( smoke_test_key_data ),
2247 KNOWN_SUPPORTED_MAC_ALG,
2248 &operation, &status ) )
2249 goto exit;
2250 TEST_EQUAL( status, PSA_SUCCESS );
2251#endif
2252
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002253exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002254 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002255}
2256/* END_CASE */
2257
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002258/* 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 +00002259void mac_bad_order( )
2260{
Ronald Cron5425a212020-08-04 14:58:35 +02002261 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002262 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2263 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002264 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002265 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2266 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2267 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002268 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002269 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2270 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2271 size_t sign_mac_length = 0;
2272 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2273 const uint8_t verify_mac[] = {
2274 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2275 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2276 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2277
2278 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002279 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002280 psa_set_key_algorithm( &attributes, alg );
2281 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002282
Ronald Cron5425a212020-08-04 14:58:35 +02002283 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2284 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002285
Jaeden Amero252ef282019-02-15 14:05:35 +00002286 /* Call update without calling setup beforehand. */
2287 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2288 PSA_ERROR_BAD_STATE );
2289 PSA_ASSERT( psa_mac_abort( &operation ) );
2290
2291 /* Call sign finish without calling setup beforehand. */
2292 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2293 &sign_mac_length),
2294 PSA_ERROR_BAD_STATE );
2295 PSA_ASSERT( psa_mac_abort( &operation ) );
2296
2297 /* Call verify finish without calling setup beforehand. */
2298 TEST_EQUAL( psa_mac_verify_finish( &operation,
2299 verify_mac, sizeof( verify_mac ) ),
2300 PSA_ERROR_BAD_STATE );
2301 PSA_ASSERT( psa_mac_abort( &operation ) );
2302
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002303 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002304 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002305 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002306 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002307 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002308 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002309 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002310 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002311
Jaeden Amero252ef282019-02-15 14:05:35 +00002312 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002313 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002314 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2315 PSA_ASSERT( psa_mac_sign_finish( &operation,
2316 sign_mac, sizeof( sign_mac ),
2317 &sign_mac_length ) );
2318 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2319 PSA_ERROR_BAD_STATE );
2320 PSA_ASSERT( psa_mac_abort( &operation ) );
2321
2322 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002323 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002324 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2325 PSA_ASSERT( psa_mac_verify_finish( &operation,
2326 verify_mac, sizeof( verify_mac ) ) );
2327 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2328 PSA_ERROR_BAD_STATE );
2329 PSA_ASSERT( psa_mac_abort( &operation ) );
2330
2331 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002332 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002333 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2334 PSA_ASSERT( psa_mac_sign_finish( &operation,
2335 sign_mac, sizeof( sign_mac ),
2336 &sign_mac_length ) );
2337 TEST_EQUAL( psa_mac_sign_finish( &operation,
2338 sign_mac, sizeof( sign_mac ),
2339 &sign_mac_length ),
2340 PSA_ERROR_BAD_STATE );
2341 PSA_ASSERT( psa_mac_abort( &operation ) );
2342
2343 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002344 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002345 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2346 PSA_ASSERT( psa_mac_verify_finish( &operation,
2347 verify_mac, sizeof( verify_mac ) ) );
2348 TEST_EQUAL( psa_mac_verify_finish( &operation,
2349 verify_mac, sizeof( verify_mac ) ),
2350 PSA_ERROR_BAD_STATE );
2351 PSA_ASSERT( psa_mac_abort( &operation ) );
2352
2353 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002354 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002355 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002356 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002357 TEST_EQUAL( psa_mac_verify_finish( &operation,
2358 verify_mac, sizeof( verify_mac ) ),
2359 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002360 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002361 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002362 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002363
2364 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002365 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002366 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002367 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002368 TEST_EQUAL( psa_mac_sign_finish( &operation,
2369 sign_mac, sizeof( sign_mac ),
2370 &sign_mac_length ),
2371 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002372 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002373 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002374 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002375
Ronald Cron5425a212020-08-04 14:58:35 +02002376 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002377
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002378exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002379 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002380}
2381/* END_CASE */
2382
2383/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002384void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002385 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002386 int alg_arg,
2387 data_t *input,
2388 data_t *expected_mac )
2389{
Ronald Cron5425a212020-08-04 14:58:35 +02002390 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002391 psa_key_type_t key_type = key_type_arg;
2392 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002393 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002394 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002395 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002396 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002397 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002398 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002399 const size_t output_sizes_to_test[] = {
2400 0,
2401 1,
2402 expected_mac->len - 1,
2403 expected_mac->len,
2404 expected_mac->len + 1,
2405 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002406
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002407 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002408 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002409 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002410
Gilles Peskine8817f612018-12-18 00:18:46 +01002411 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002412
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002413 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002414 psa_set_key_algorithm( &attributes, alg );
2415 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002416
Ronald Cron5425a212020-08-04 14:58:35 +02002417 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2418 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002419
Gilles Peskine8b356b52020-08-25 23:44:59 +02002420 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2421 {
2422 const size_t output_size = output_sizes_to_test[i];
2423 psa_status_t expected_status =
2424 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2425 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002426
Chris Jones9634bb12021-01-20 15:56:42 +00002427 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002428 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002429
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002430 /* Calculate the MAC, one-shot case. */
2431 TEST_EQUAL( psa_mac_compute( key, alg,
2432 input->x, input->len,
2433 actual_mac, output_size, &mac_length ),
2434 expected_status );
2435 if( expected_status == PSA_SUCCESS )
2436 {
2437 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2438 actual_mac, mac_length );
2439 }
2440
2441 if( output_size > 0 )
2442 memset( actual_mac, 0, output_size );
2443
2444 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002445 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002446 PSA_ASSERT( psa_mac_update( &operation,
2447 input->x, input->len ) );
2448 TEST_EQUAL( psa_mac_sign_finish( &operation,
2449 actual_mac, output_size,
2450 &mac_length ),
2451 expected_status );
2452 PSA_ASSERT( psa_mac_abort( &operation ) );
2453
2454 if( expected_status == PSA_SUCCESS )
2455 {
2456 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2457 actual_mac, mac_length );
2458 }
2459 mbedtls_free( actual_mac );
2460 actual_mac = NULL;
2461 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002462
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002463exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002464 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002465 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002466 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002467 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002468}
2469/* END_CASE */
2470
2471/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002472void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002473 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002474 int alg_arg,
2475 data_t *input,
2476 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002477{
Ronald Cron5425a212020-08-04 14:58:35 +02002478 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002479 psa_key_type_t key_type = key_type_arg;
2480 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002481 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002482 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002483 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002484
Gilles Peskine69c12672018-06-28 00:07:19 +02002485 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2486
Gilles Peskine8817f612018-12-18 00:18:46 +01002487 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002488
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002489 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002490 psa_set_key_algorithm( &attributes, alg );
2491 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002492
Ronald Cron5425a212020-08-04 14:58:35 +02002493 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2494 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002495
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002496 /* Verify correct MAC, one-shot case. */
2497 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2498 expected_mac->x, expected_mac->len ) );
2499
2500 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002501 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002502 PSA_ASSERT( psa_mac_update( &operation,
2503 input->x, input->len ) );
2504 PSA_ASSERT( psa_mac_verify_finish( &operation,
2505 expected_mac->x,
2506 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002507
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002508 /* Test a MAC that's too short, one-shot case. */
2509 TEST_EQUAL( psa_mac_verify( key, alg,
2510 input->x, input->len,
2511 expected_mac->x,
2512 expected_mac->len - 1 ),
2513 PSA_ERROR_INVALID_SIGNATURE );
2514
2515 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002516 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002517 PSA_ASSERT( psa_mac_update( &operation,
2518 input->x, input->len ) );
2519 TEST_EQUAL( psa_mac_verify_finish( &operation,
2520 expected_mac->x,
2521 expected_mac->len - 1 ),
2522 PSA_ERROR_INVALID_SIGNATURE );
2523
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002524 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002525 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2526 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002527 TEST_EQUAL( psa_mac_verify( key, alg,
2528 input->x, input->len,
2529 perturbed_mac, expected_mac->len + 1 ),
2530 PSA_ERROR_INVALID_SIGNATURE );
2531
2532 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002533 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002534 PSA_ASSERT( psa_mac_update( &operation,
2535 input->x, input->len ) );
2536 TEST_EQUAL( psa_mac_verify_finish( &operation,
2537 perturbed_mac,
2538 expected_mac->len + 1 ),
2539 PSA_ERROR_INVALID_SIGNATURE );
2540
2541 /* Test changing one byte. */
2542 for( size_t i = 0; i < expected_mac->len; i++ )
2543 {
Chris Jones9634bb12021-01-20 15:56:42 +00002544 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002545 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002546
2547 TEST_EQUAL( psa_mac_verify( key, alg,
2548 input->x, input->len,
2549 perturbed_mac, expected_mac->len ),
2550 PSA_ERROR_INVALID_SIGNATURE );
2551
Ronald Cron5425a212020-08-04 14:58:35 +02002552 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002553 PSA_ASSERT( psa_mac_update( &operation,
2554 input->x, input->len ) );
2555 TEST_EQUAL( psa_mac_verify_finish( &operation,
2556 perturbed_mac,
2557 expected_mac->len ),
2558 PSA_ERROR_INVALID_SIGNATURE );
2559 perturbed_mac[i] ^= 1;
2560 }
2561
Gilles Peskine8c9def32018-02-08 10:02:12 +01002562exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002563 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002564 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002565 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002566 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002567}
2568/* END_CASE */
2569
2570/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002571void cipher_operation_init( )
2572{
Jaeden Ameroab439972019-02-15 14:12:05 +00002573 const uint8_t input[1] = { 0 };
2574 unsigned char output[1] = { 0 };
2575 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002576 /* Test each valid way of initializing the object, except for `= {0}`, as
2577 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2578 * though it's OK by the C standard. We could test for this, but we'd need
2579 * to supress the Clang warning for the test. */
2580 psa_cipher_operation_t func = psa_cipher_operation_init( );
2581 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2582 psa_cipher_operation_t zero;
2583
2584 memset( &zero, 0, sizeof( zero ) );
2585
Jaeden Ameroab439972019-02-15 14:12:05 +00002586 /* A freshly-initialized cipher operation should not be usable. */
2587 TEST_EQUAL( psa_cipher_update( &func,
2588 input, sizeof( input ),
2589 output, sizeof( output ),
2590 &output_length ),
2591 PSA_ERROR_BAD_STATE );
2592 TEST_EQUAL( psa_cipher_update( &init,
2593 input, sizeof( input ),
2594 output, sizeof( output ),
2595 &output_length ),
2596 PSA_ERROR_BAD_STATE );
2597 TEST_EQUAL( psa_cipher_update( &zero,
2598 input, sizeof( input ),
2599 output, sizeof( output ),
2600 &output_length ),
2601 PSA_ERROR_BAD_STATE );
2602
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002603 /* A default cipher operation should be abortable without error. */
2604 PSA_ASSERT( psa_cipher_abort( &func ) );
2605 PSA_ASSERT( psa_cipher_abort( &init ) );
2606 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002607}
2608/* END_CASE */
2609
2610/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002611void cipher_setup( int key_type_arg,
2612 data_t *key,
2613 int alg_arg,
2614 int expected_status_arg )
2615{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002616 psa_key_type_t key_type = key_type_arg;
2617 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002618 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002619 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002620 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002621#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002622 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2623#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002624
Gilles Peskine8817f612018-12-18 00:18:46 +01002625 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002626
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002627 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2628 &operation, &status ) )
2629 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002630 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002631
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002632 /* The operation object should be reusable. */
2633#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2634 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2635 smoke_test_key_data,
2636 sizeof( smoke_test_key_data ),
2637 KNOWN_SUPPORTED_CIPHER_ALG,
2638 &operation, &status ) )
2639 goto exit;
2640 TEST_EQUAL( status, PSA_SUCCESS );
2641#endif
2642
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002643exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002644 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002645 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002646}
2647/* END_CASE */
2648
Ronald Cronee414c72021-03-18 18:50:08 +01002649/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002650void cipher_bad_order( )
2651{
Ronald Cron5425a212020-08-04 14:58:35 +02002652 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002653 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2654 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002655 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002656 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002657 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002658 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002659 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2660 0xaa, 0xaa, 0xaa, 0xaa };
2661 const uint8_t text[] = {
2662 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2663 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002664 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002665 size_t length = 0;
2666
2667 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002668 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2669 psa_set_key_algorithm( &attributes, alg );
2670 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002671 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2672 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002673
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002674 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002675 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002676 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002677 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002678 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002679 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002680 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002681 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002682
2683 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002684 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002685 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002686 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002687 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002688 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002689 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002690 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002691
Jaeden Ameroab439972019-02-15 14:12:05 +00002692 /* Generate an IV without calling setup beforehand. */
2693 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2694 buffer, sizeof( buffer ),
2695 &length ),
2696 PSA_ERROR_BAD_STATE );
2697 PSA_ASSERT( psa_cipher_abort( &operation ) );
2698
2699 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002700 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002701 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2702 buffer, sizeof( buffer ),
2703 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002704 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002705 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2706 buffer, sizeof( buffer ),
2707 &length ),
2708 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002709 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002710 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002711 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002712
2713 /* Generate an IV after it's already set. */
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_set_iv( &operation,
2716 iv, sizeof( iv ) ) );
2717 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2718 buffer, sizeof( buffer ),
2719 &length ),
2720 PSA_ERROR_BAD_STATE );
2721 PSA_ASSERT( psa_cipher_abort( &operation ) );
2722
2723 /* Set an IV without calling setup beforehand. */
2724 TEST_EQUAL( psa_cipher_set_iv( &operation,
2725 iv, sizeof( iv ) ),
2726 PSA_ERROR_BAD_STATE );
2727 PSA_ASSERT( psa_cipher_abort( &operation ) );
2728
2729 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002730 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002731 PSA_ASSERT( psa_cipher_set_iv( &operation,
2732 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002733 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002734 TEST_EQUAL( psa_cipher_set_iv( &operation,
2735 iv, sizeof( iv ) ),
2736 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002737 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002738 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002739 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002740
2741 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002742 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002743 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2744 buffer, sizeof( buffer ),
2745 &length ) );
2746 TEST_EQUAL( psa_cipher_set_iv( &operation,
2747 iv, sizeof( iv ) ),
2748 PSA_ERROR_BAD_STATE );
2749 PSA_ASSERT( psa_cipher_abort( &operation ) );
2750
2751 /* Call update without calling setup beforehand. */
2752 TEST_EQUAL( psa_cipher_update( &operation,
2753 text, sizeof( text ),
2754 buffer, sizeof( buffer ),
2755 &length ),
2756 PSA_ERROR_BAD_STATE );
2757 PSA_ASSERT( psa_cipher_abort( &operation ) );
2758
2759 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002760 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002761 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002762 TEST_EQUAL( psa_cipher_update( &operation,
2763 text, sizeof( text ),
2764 buffer, sizeof( buffer ),
2765 &length ),
2766 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002767 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002768 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002769 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002770
2771 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002772 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002773 PSA_ASSERT( psa_cipher_set_iv( &operation,
2774 iv, sizeof( iv ) ) );
2775 PSA_ASSERT( psa_cipher_finish( &operation,
2776 buffer, sizeof( buffer ), &length ) );
2777 TEST_EQUAL( psa_cipher_update( &operation,
2778 text, sizeof( text ),
2779 buffer, sizeof( buffer ),
2780 &length ),
2781 PSA_ERROR_BAD_STATE );
2782 PSA_ASSERT( psa_cipher_abort( &operation ) );
2783
2784 /* Call finish without calling setup beforehand. */
2785 TEST_EQUAL( psa_cipher_finish( &operation,
2786 buffer, sizeof( buffer ), &length ),
2787 PSA_ERROR_BAD_STATE );
2788 PSA_ASSERT( psa_cipher_abort( &operation ) );
2789
2790 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002791 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002792 /* Not calling update means we are encrypting an empty buffer, which is OK
2793 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01002794 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002795 TEST_EQUAL( psa_cipher_finish( &operation,
2796 buffer, sizeof( buffer ), &length ),
2797 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002798 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002799 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002800 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002801
2802 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002803 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002804 PSA_ASSERT( psa_cipher_set_iv( &operation,
2805 iv, sizeof( iv ) ) );
2806 PSA_ASSERT( psa_cipher_finish( &operation,
2807 buffer, sizeof( buffer ), &length ) );
2808 TEST_EQUAL( psa_cipher_finish( &operation,
2809 buffer, sizeof( buffer ), &length ),
2810 PSA_ERROR_BAD_STATE );
2811 PSA_ASSERT( psa_cipher_abort( &operation ) );
2812
Ronald Cron5425a212020-08-04 14:58:35 +02002813 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002814
Jaeden Ameroab439972019-02-15 14:12:05 +00002815exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002816 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002817 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002818}
2819/* END_CASE */
2820
2821/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002822void cipher_encrypt_fail( int alg_arg,
2823 int key_type_arg,
2824 data_t *key_data,
2825 data_t *input,
2826 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002827{
Ronald Cron5425a212020-08-04 14:58:35 +02002828 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002829 psa_status_t status;
2830 psa_key_type_t key_type = key_type_arg;
2831 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002832 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002833 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002834 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002835 size_t output_length = 0;
2836 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2837
2838 if ( PSA_ERROR_BAD_STATE != expected_status )
2839 {
2840 PSA_ASSERT( psa_crypto_init( ) );
2841
2842 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2843 psa_set_key_algorithm( &attributes, alg );
2844 psa_set_key_type( &attributes, key_type );
2845
2846 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
2847 input->len );
2848 ASSERT_ALLOC( output, output_buffer_size );
2849
2850 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2851 &key ) );
2852 }
2853
2854 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
2855 output_buffer_size, &output_length );
2856
2857 TEST_EQUAL( status, expected_status );
2858
2859exit:
2860 mbedtls_free( output );
2861 psa_destroy_key( key );
2862 PSA_DONE( );
2863}
2864/* END_CASE */
2865
2866/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002867void cipher_encrypt_alg_without_iv( int alg_arg,
2868 int key_type_arg,
2869 data_t *key_data,
2870 data_t *input,
2871 data_t *expected_output )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002872{
2873 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2874 psa_key_type_t key_type = key_type_arg;
2875 psa_algorithm_t alg = alg_arg;
2876 unsigned char *output = NULL;
2877 size_t output_buffer_size = 0;
2878 size_t output_length = 0;
2879 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2880
2881 PSA_ASSERT( psa_crypto_init( ) );
2882
2883 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2884 psa_set_key_algorithm( &attributes, alg );
2885 psa_set_key_type( &attributes, key_type );
2886
2887 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2888 ASSERT_ALLOC( output, output_buffer_size );
2889
2890 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2891 &key ) );
2892
2893 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2894 output_buffer_size, &output_length ) );
2895 TEST_ASSERT( output_length <=
2896 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2897 TEST_ASSERT( output_length <=
2898 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2899
2900 ASSERT_COMPARE( expected_output->x, expected_output->len,
2901 output, output_length );
2902exit:
2903 mbedtls_free( output );
2904 psa_destroy_key( key );
2905 PSA_DONE( );
2906}
2907/* END_CASE */
2908
2909/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01002910void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
2911{
2912 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2913 psa_algorithm_t alg = alg_arg;
2914 psa_key_type_t key_type = key_type_arg;
2915 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2916 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2917 psa_status_t status;
2918
2919 PSA_ASSERT( psa_crypto_init( ) );
2920
2921 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2922 psa_set_key_algorithm( &attributes, alg );
2923 psa_set_key_type( &attributes, key_type );
2924
2925 /* Usage of either of these two size macros would cause divide by zero
2926 * with incorrect key types previously. Input length should be irrelevant
2927 * here. */
2928 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
2929 0 );
2930 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
2931
2932
2933 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2934 &key ) );
2935
2936 /* Should fail due to invalid alg type (to support invalid key type).
2937 * Encrypt or decrypt will end up in the same place. */
2938 status = psa_cipher_encrypt_setup( &operation, key, alg );
2939
2940 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
2941
2942exit:
2943 psa_cipher_abort( &operation );
2944 psa_destroy_key( key );
2945 PSA_DONE( );
2946}
2947/* END_CASE */
2948
2949/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002950void cipher_encrypt_validation( int alg_arg,
2951 int key_type_arg,
2952 data_t *key_data,
2953 data_t *input )
2954{
2955 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2956 psa_key_type_t key_type = key_type_arg;
2957 psa_algorithm_t alg = alg_arg;
2958 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
2959 unsigned char *output1 = NULL;
2960 size_t output1_buffer_size = 0;
2961 size_t output1_length = 0;
2962 unsigned char *output2 = NULL;
2963 size_t output2_buffer_size = 0;
2964 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002965 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002966 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002967 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002968
Gilles Peskine8817f612018-12-18 00:18:46 +01002969 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002970
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002971 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2972 psa_set_key_algorithm( &attributes, alg );
2973 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002974
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002975 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2976 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
2977 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
2978 ASSERT_ALLOC( output1, output1_buffer_size );
2979 ASSERT_ALLOC( output2, output2_buffer_size );
2980
Ronald Cron5425a212020-08-04 14:58:35 +02002981 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2982 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002983
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02002984 /* The one-shot cipher encryption uses generated iv so validating
2985 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002986 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
2987 output1_buffer_size, &output1_length ) );
2988 TEST_ASSERT( output1_length <=
2989 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2990 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01002991 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002992
2993 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2994 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002995
Gilles Peskine8817f612018-12-18 00:18:46 +01002996 PSA_ASSERT( psa_cipher_update( &operation,
2997 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002998 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01002999 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003000 TEST_ASSERT( function_output_length <=
3001 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3002 TEST_ASSERT( function_output_length <=
3003 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003004 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003005
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003006 PSA_ASSERT( psa_cipher_finish( &operation,
3007 output2 + output2_length,
3008 output2_buffer_size - output2_length,
3009 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003010 TEST_ASSERT( function_output_length <=
3011 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3012 TEST_ASSERT( function_output_length <=
3013 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003014 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003015
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003016 PSA_ASSERT( psa_cipher_abort( &operation ) );
3017 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
3018 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003019
Gilles Peskine50e586b2018-06-08 14:28:46 +02003020exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003021 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003022 mbedtls_free( output1 );
3023 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003024 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003025 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003026}
3027/* END_CASE */
3028
3029/* BEGIN_CASE */
3030void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003031 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003032 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003033 int first_part_size_arg,
3034 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003035 data_t *expected_output,
3036 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003037{
Ronald Cron5425a212020-08-04 14:58:35 +02003038 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003039 psa_key_type_t key_type = key_type_arg;
3040 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003041 psa_status_t status;
3042 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003043 size_t first_part_size = first_part_size_arg;
3044 size_t output1_length = output1_length_arg;
3045 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003046 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003047 size_t output_buffer_size = 0;
3048 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003049 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003050 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003051 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003052
Gilles Peskine8817f612018-12-18 00:18:46 +01003053 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003054
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003055 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3056 psa_set_key_algorithm( &attributes, alg );
3057 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003058
Ronald Cron5425a212020-08-04 14:58:35 +02003059 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3060 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003061
Ronald Cron5425a212020-08-04 14:58:35 +02003062 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003063
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003064 if( iv->len > 0 )
3065 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003066 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003067 }
3068
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003069 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3070 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003071 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003072
Gilles Peskinee0866522019-02-19 19:44:00 +01003073 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003074 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3075 output, output_buffer_size,
3076 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003077 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003078 TEST_ASSERT( function_output_length <=
3079 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3080 TEST_ASSERT( function_output_length <=
3081 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003082 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003083
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003084 if( first_part_size < input->len )
3085 {
3086 PSA_ASSERT( psa_cipher_update( &operation,
3087 input->x + first_part_size,
3088 input->len - first_part_size,
3089 ( output_buffer_size == 0 ? NULL :
3090 output + total_output_length ),
3091 output_buffer_size - total_output_length,
3092 &function_output_length ) );
3093 TEST_ASSERT( function_output_length == output2_length );
3094 TEST_ASSERT( function_output_length <=
3095 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3096 alg,
3097 input->len - first_part_size ) );
3098 TEST_ASSERT( function_output_length <=
3099 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3100 total_output_length += function_output_length;
3101 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003102
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003103 status = psa_cipher_finish( &operation,
3104 ( output_buffer_size == 0 ? NULL :
3105 output + total_output_length ),
3106 output_buffer_size - total_output_length,
3107 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003108 TEST_ASSERT( function_output_length <=
3109 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3110 TEST_ASSERT( function_output_length <=
3111 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003112 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003113 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003114
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003115 if( expected_status == PSA_SUCCESS )
3116 {
3117 PSA_ASSERT( psa_cipher_abort( &operation ) );
3118
3119 ASSERT_COMPARE( expected_output->x, expected_output->len,
3120 output, total_output_length );
3121 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003122
3123exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003124 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003125 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003126 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003127 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003128}
3129/* END_CASE */
3130
3131/* BEGIN_CASE */
3132void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003133 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003134 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003135 int first_part_size_arg,
3136 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003137 data_t *expected_output,
3138 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003139{
Ronald Cron5425a212020-08-04 14:58:35 +02003140 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003141 psa_key_type_t key_type = key_type_arg;
3142 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003143 psa_status_t status;
3144 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003145 size_t first_part_size = first_part_size_arg;
3146 size_t output1_length = output1_length_arg;
3147 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003148 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003149 size_t output_buffer_size = 0;
3150 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003151 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003152 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003153 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003154
Gilles Peskine8817f612018-12-18 00:18:46 +01003155 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003156
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003157 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3158 psa_set_key_algorithm( &attributes, alg );
3159 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003160
Ronald Cron5425a212020-08-04 14:58:35 +02003161 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3162 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003163
Ronald Cron5425a212020-08-04 14:58:35 +02003164 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003165
Steven Cooreman177deba2020-09-07 17:14:14 +02003166 if( iv->len > 0 )
3167 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003168 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003169 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003170
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003171 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3172 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003173 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003174
Gilles Peskinee0866522019-02-19 19:44:00 +01003175 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003176 PSA_ASSERT( psa_cipher_update( &operation,
3177 input->x, first_part_size,
3178 output, output_buffer_size,
3179 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003180 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003181 TEST_ASSERT( function_output_length <=
3182 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3183 TEST_ASSERT( function_output_length <=
3184 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003185 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003186
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003187 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02003188 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003189 PSA_ASSERT( psa_cipher_update( &operation,
3190 input->x + first_part_size,
3191 input->len - first_part_size,
3192 ( output_buffer_size == 0 ? NULL :
3193 output + total_output_length ),
3194 output_buffer_size - total_output_length,
3195 &function_output_length ) );
3196 TEST_ASSERT( function_output_length == output2_length );
3197 TEST_ASSERT( function_output_length <=
3198 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3199 alg,
3200 input->len - first_part_size ) );
3201 TEST_ASSERT( function_output_length <=
3202 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3203 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003204 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003205
Gilles Peskine50e586b2018-06-08 14:28:46 +02003206 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003207 ( output_buffer_size == 0 ? NULL :
3208 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003209 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003210 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003211 TEST_ASSERT( function_output_length <=
3212 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3213 TEST_ASSERT( function_output_length <=
3214 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003215 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003216 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003217
3218 if( expected_status == PSA_SUCCESS )
3219 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003220 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003221
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003222 ASSERT_COMPARE( expected_output->x, expected_output->len,
3223 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003224 }
3225
Gilles Peskine50e586b2018-06-08 14:28:46 +02003226exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003227 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003228 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003229 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003230 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003231}
3232/* END_CASE */
3233
Gilles Peskine50e586b2018-06-08 14:28:46 +02003234/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003235void cipher_decrypt_fail( int alg_arg,
3236 int key_type_arg,
3237 data_t *key_data,
3238 data_t *iv,
3239 data_t *input_arg,
3240 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003241{
3242 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3243 psa_status_t status;
3244 psa_key_type_t key_type = key_type_arg;
3245 psa_algorithm_t alg = alg_arg;
3246 psa_status_t expected_status = expected_status_arg;
3247 unsigned char *input = NULL;
3248 size_t input_buffer_size = 0;
3249 unsigned char *output = NULL;
3250 size_t output_buffer_size = 0;
3251 size_t output_length = 0;
3252 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3253
3254 if ( PSA_ERROR_BAD_STATE != expected_status )
3255 {
3256 PSA_ASSERT( psa_crypto_init( ) );
3257
3258 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3259 psa_set_key_algorithm( &attributes, alg );
3260 psa_set_key_type( &attributes, key_type );
3261
3262 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3263 &key ) );
3264 }
3265
3266 /* Allocate input buffer and copy the iv and the plaintext */
3267 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3268 if ( input_buffer_size > 0 )
3269 {
3270 ASSERT_ALLOC( input, input_buffer_size );
3271 memcpy( input, iv->x, iv->len );
3272 memcpy( input + iv->len, input_arg->x, input_arg->len );
3273 }
3274
3275 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3276 ASSERT_ALLOC( output, output_buffer_size );
3277
3278 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3279 output_buffer_size, &output_length );
3280 TEST_EQUAL( status, expected_status );
3281
3282exit:
3283 mbedtls_free( input );
3284 mbedtls_free( output );
3285 psa_destroy_key( key );
3286 PSA_DONE( );
3287}
3288/* END_CASE */
3289
3290/* BEGIN_CASE */
3291void cipher_decrypt( int alg_arg,
3292 int key_type_arg,
3293 data_t *key_data,
3294 data_t *iv,
3295 data_t *input_arg,
3296 data_t *expected_output )
3297{
3298 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3299 psa_key_type_t key_type = key_type_arg;
3300 psa_algorithm_t alg = alg_arg;
3301 unsigned char *input = NULL;
3302 size_t input_buffer_size = 0;
3303 unsigned char *output = NULL;
3304 size_t output_buffer_size = 0;
3305 size_t output_length = 0;
3306 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3307
3308 PSA_ASSERT( psa_crypto_init( ) );
3309
3310 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3311 psa_set_key_algorithm( &attributes, alg );
3312 psa_set_key_type( &attributes, key_type );
3313
3314 /* Allocate input buffer and copy the iv and the plaintext */
3315 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3316 if ( input_buffer_size > 0 )
3317 {
3318 ASSERT_ALLOC( input, input_buffer_size );
3319 memcpy( input, iv->x, iv->len );
3320 memcpy( input + iv->len, input_arg->x, input_arg->len );
3321 }
3322
3323 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3324 ASSERT_ALLOC( output, output_buffer_size );
3325
3326 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3327 &key ) );
3328
3329 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3330 output_buffer_size, &output_length ) );
3331 TEST_ASSERT( output_length <=
3332 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3333 TEST_ASSERT( output_length <=
3334 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3335
3336 ASSERT_COMPARE( expected_output->x, expected_output->len,
3337 output, output_length );
3338exit:
3339 mbedtls_free( input );
3340 mbedtls_free( output );
3341 psa_destroy_key( key );
3342 PSA_DONE( );
3343}
3344/* END_CASE */
3345
3346/* BEGIN_CASE */
3347void cipher_verify_output( int alg_arg,
3348 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003349 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003350 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003351{
Ronald Cron5425a212020-08-04 14:58:35 +02003352 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003353 psa_key_type_t key_type = key_type_arg;
3354 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003355 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003356 size_t output1_size = 0;
3357 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003358 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003359 size_t output2_size = 0;
3360 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003361 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003362
Gilles Peskine8817f612018-12-18 00:18:46 +01003363 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003364
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003365 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3366 psa_set_key_algorithm( &attributes, alg );
3367 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003368
Ronald Cron5425a212020-08-04 14:58:35 +02003369 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3370 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003371 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003372 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003373
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003374 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3375 output1, output1_size,
3376 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003377 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003378 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003379 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003380 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003381
3382 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003383 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003384
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003385 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3386 output2, output2_size,
3387 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003388 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003389 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003390 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003391 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003392
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003393 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003394
3395exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003396 mbedtls_free( output1 );
3397 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003398 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003399 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003400}
3401/* END_CASE */
3402
3403/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003404void cipher_verify_output_multipart( int alg_arg,
3405 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003406 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003407 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003408 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003409{
Ronald Cron5425a212020-08-04 14:58:35 +02003410 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003411 psa_key_type_t key_type = key_type_arg;
3412 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003413 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003414 unsigned char iv[16] = {0};
3415 size_t iv_size = 16;
3416 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003417 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003418 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003419 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003420 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003421 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003422 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003423 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003424 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3425 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003426 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003427
Gilles Peskine8817f612018-12-18 00:18:46 +01003428 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003429
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003430 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3431 psa_set_key_algorithm( &attributes, alg );
3432 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003433
Ronald Cron5425a212020-08-04 14:58:35 +02003434 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3435 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003436
Ronald Cron5425a212020-08-04 14:58:35 +02003437 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3438 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003439
Steven Cooreman177deba2020-09-07 17:14:14 +02003440 if( alg != PSA_ALG_ECB_NO_PADDING )
3441 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003442 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3443 iv, iv_size,
3444 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003445 }
3446
gabor-mezei-armceface22021-01-21 12:26:17 +01003447 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3448 TEST_ASSERT( output1_buffer_size <=
3449 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003450 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003451
Gilles Peskinee0866522019-02-19 19:44:00 +01003452 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003453
Gilles Peskine8817f612018-12-18 00:18:46 +01003454 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3455 output1, output1_buffer_size,
3456 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003457 TEST_ASSERT( function_output_length <=
3458 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3459 TEST_ASSERT( function_output_length <=
3460 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003461 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003462
Gilles Peskine8817f612018-12-18 00:18:46 +01003463 PSA_ASSERT( psa_cipher_update( &operation1,
3464 input->x + first_part_size,
3465 input->len - first_part_size,
3466 output1, output1_buffer_size,
3467 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003468 TEST_ASSERT( function_output_length <=
3469 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3470 alg,
3471 input->len - first_part_size ) );
3472 TEST_ASSERT( function_output_length <=
3473 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003474 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003475
Gilles Peskine8817f612018-12-18 00:18:46 +01003476 PSA_ASSERT( psa_cipher_finish( &operation1,
3477 output1 + output1_length,
3478 output1_buffer_size - output1_length,
3479 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003480 TEST_ASSERT( function_output_length <=
3481 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3482 TEST_ASSERT( function_output_length <=
3483 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003484 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003485
Gilles Peskine8817f612018-12-18 00:18:46 +01003486 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003487
Gilles Peskine048b7f02018-06-08 14:20:49 +02003488 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003489 TEST_ASSERT( output2_buffer_size <=
3490 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3491 TEST_ASSERT( output2_buffer_size <=
3492 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003493 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003494
Steven Cooreman177deba2020-09-07 17:14:14 +02003495 if( iv_length > 0 )
3496 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003497 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3498 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003499 }
Moran Pekerded84402018-06-06 16:36:50 +03003500
Gilles Peskine8817f612018-12-18 00:18:46 +01003501 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3502 output2, output2_buffer_size,
3503 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003504 TEST_ASSERT( function_output_length <=
3505 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3506 TEST_ASSERT( function_output_length <=
3507 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003508 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003509
Gilles Peskine8817f612018-12-18 00:18:46 +01003510 PSA_ASSERT( psa_cipher_update( &operation2,
3511 output1 + first_part_size,
3512 output1_length - first_part_size,
3513 output2, output2_buffer_size,
3514 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003515 TEST_ASSERT( function_output_length <=
3516 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3517 alg,
3518 output1_length - first_part_size ) );
3519 TEST_ASSERT( function_output_length <=
3520 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003521 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003522
Gilles Peskine8817f612018-12-18 00:18:46 +01003523 PSA_ASSERT( psa_cipher_finish( &operation2,
3524 output2 + output2_length,
3525 output2_buffer_size - output2_length,
3526 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003527 TEST_ASSERT( function_output_length <=
3528 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3529 TEST_ASSERT( function_output_length <=
3530 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003531 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003532
Gilles Peskine8817f612018-12-18 00:18:46 +01003533 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003534
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003535 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003536
3537exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003538 psa_cipher_abort( &operation1 );
3539 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003540 mbedtls_free( output1 );
3541 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003542 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003543 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003544}
3545/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003546
Gilles Peskine20035e32018-02-03 22:44:14 +01003547/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003548void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003549 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003550 data_t *nonce,
3551 data_t *additional_data,
3552 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003553 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003554{
Ronald Cron5425a212020-08-04 14:58:35 +02003555 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003556 psa_key_type_t key_type = key_type_arg;
3557 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003558 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003559 unsigned char *output_data = NULL;
3560 size_t output_size = 0;
3561 size_t output_length = 0;
3562 unsigned char *output_data2 = NULL;
3563 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003564 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003565 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003566 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003567
Gilles Peskine8817f612018-12-18 00:18:46 +01003568 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003569
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003570 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3571 psa_set_key_algorithm( &attributes, alg );
3572 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003573
Gilles Peskine049c7532019-05-15 20:22:09 +02003574 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003575 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003576 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3577 key_bits = psa_get_key_bits( &attributes );
3578
3579 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3580 alg );
3581 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3582 * should be exact. */
3583 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3584 expected_result != PSA_ERROR_NOT_SUPPORTED )
3585 {
3586 TEST_EQUAL( output_size,
3587 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3588 TEST_ASSERT( output_size <=
3589 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3590 }
3591 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003592
Steven Cooremanf49478b2021-02-15 15:19:25 +01003593 status = psa_aead_encrypt( key, alg,
3594 nonce->x, nonce->len,
3595 additional_data->x,
3596 additional_data->len,
3597 input_data->x, input_data->len,
3598 output_data, output_size,
3599 &output_length );
3600
3601 /* If the operation is not supported, just skip and not fail in case the
3602 * encryption involves a common limitation of cryptography hardwares and
3603 * an alternative implementation. */
3604 if( status == PSA_ERROR_NOT_SUPPORTED )
3605 {
3606 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3607 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3608 }
3609
3610 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003611
3612 if( PSA_SUCCESS == expected_result )
3613 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003614 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003615
Gilles Peskine003a4a92019-05-14 16:09:40 +02003616 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3617 * should be exact. */
3618 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003619 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003620
gabor-mezei-armceface22021-01-21 12:26:17 +01003621 TEST_ASSERT( input_data->len <=
3622 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3623
Ronald Cron5425a212020-08-04 14:58:35 +02003624 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003625 nonce->x, nonce->len,
3626 additional_data->x,
3627 additional_data->len,
3628 output_data, output_length,
3629 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003630 &output_length2 ),
3631 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003632
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003633 ASSERT_COMPARE( input_data->x, input_data->len,
3634 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003635 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003636
Gilles Peskinea1cac842018-06-11 19:33:02 +02003637exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003638 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003639 mbedtls_free( output_data );
3640 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003641 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003642}
3643/* END_CASE */
3644
3645/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003646void aead_encrypt( int key_type_arg, data_t *key_data,
3647 int alg_arg,
3648 data_t *nonce,
3649 data_t *additional_data,
3650 data_t *input_data,
3651 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003652{
Ronald Cron5425a212020-08-04 14:58:35 +02003653 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003654 psa_key_type_t key_type = key_type_arg;
3655 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003656 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003657 unsigned char *output_data = NULL;
3658 size_t output_size = 0;
3659 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003660 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003661 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003662
Gilles Peskine8817f612018-12-18 00:18:46 +01003663 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003664
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003665 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3666 psa_set_key_algorithm( &attributes, alg );
3667 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003668
Gilles Peskine049c7532019-05-15 20:22:09 +02003669 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003670 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003671 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3672 key_bits = psa_get_key_bits( &attributes );
3673
3674 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3675 alg );
3676 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3677 * should be exact. */
3678 TEST_EQUAL( output_size,
3679 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3680 TEST_ASSERT( output_size <=
3681 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3682 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003683
Steven Cooremand588ea12021-01-11 19:36:04 +01003684 status = psa_aead_encrypt( key, alg,
3685 nonce->x, nonce->len,
3686 additional_data->x, additional_data->len,
3687 input_data->x, input_data->len,
3688 output_data, output_size,
3689 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003690
Ronald Cron28a45ed2021-02-09 20:35:42 +01003691 /* If the operation is not supported, just skip and not fail in case the
3692 * encryption involves a common limitation of cryptography hardwares and
3693 * an alternative implementation. */
3694 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003695 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003696 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3697 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003698 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003699
3700 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003701 ASSERT_COMPARE( expected_result->x, expected_result->len,
3702 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003703
Gilles Peskinea1cac842018-06-11 19:33:02 +02003704exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003705 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003706 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003707 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003708}
3709/* END_CASE */
3710
3711/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003712void aead_decrypt( int key_type_arg, data_t *key_data,
3713 int alg_arg,
3714 data_t *nonce,
3715 data_t *additional_data,
3716 data_t *input_data,
3717 data_t *expected_data,
3718 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003719{
Ronald Cron5425a212020-08-04 14:58:35 +02003720 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003721 psa_key_type_t key_type = key_type_arg;
3722 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003723 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003724 unsigned char *output_data = NULL;
3725 size_t output_size = 0;
3726 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003727 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003728 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003729 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003730
Gilles Peskine8817f612018-12-18 00:18:46 +01003731 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003732
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003733 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3734 psa_set_key_algorithm( &attributes, alg );
3735 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003736
Gilles Peskine049c7532019-05-15 20:22:09 +02003737 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003738 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003739 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3740 key_bits = psa_get_key_bits( &attributes );
3741
3742 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3743 alg );
3744 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3745 expected_result != PSA_ERROR_NOT_SUPPORTED )
3746 {
3747 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3748 * should be exact. */
3749 TEST_EQUAL( output_size,
3750 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3751 TEST_ASSERT( output_size <=
3752 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3753 }
3754 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003755
Steven Cooremand588ea12021-01-11 19:36:04 +01003756 status = psa_aead_decrypt( key, alg,
3757 nonce->x, nonce->len,
3758 additional_data->x,
3759 additional_data->len,
3760 input_data->x, input_data->len,
3761 output_data, output_size,
3762 &output_length );
3763
Ronald Cron28a45ed2021-02-09 20:35:42 +01003764 /* If the operation is not supported, just skip and not fail in case the
3765 * decryption involves a common limitation of cryptography hardwares and
3766 * an alternative implementation. */
3767 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003768 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003769 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3770 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003771 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003772
3773 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003774
Gilles Peskine2d277862018-06-18 15:41:12 +02003775 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003776 ASSERT_COMPARE( expected_data->x, expected_data->len,
3777 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003778
Gilles Peskinea1cac842018-06-11 19:33:02 +02003779exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003780 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003781 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003782 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003783}
3784/* END_CASE */
3785
3786/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003787void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3788 int alg_arg,
3789 data_t *nonce,
3790 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003791 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003792 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003793 int do_test_data_chunked,
3794 int do_set_lengths,
3795 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003796{
Paul Elliottd3f82412021-06-16 16:52:21 +01003797 size_t ad_part_len = 0;
3798 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01003799 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003800
Paul Elliotte64deda2021-09-09 14:07:23 +01003801 /* Ensure that either one part of the test or the other is done, i.e this
3802 * test does something. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003803 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3804
3805 /* Temporary whilst we have algorithms that cannot support chunking */
3806 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003807 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003808 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3809 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003810 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003811 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003812
Paul Elliott33746aa2021-09-15 16:40:40 +01003813 if( do_set_lengths )
3814 {
3815 if( ad_part_len & 0x01 )
3816 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3817 else
3818 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3819 }
3820
Paul Elliott329d5382021-07-22 17:10:45 +01003821 /* Split ad into length(ad_part_len) parts. */
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003822 if( !aead_multipart_internal_func( key_type_arg, key_data,
3823 alg_arg, nonce,
3824 additional_data,
3825 ad_part_len,
3826 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003827 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003828 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003829 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003830 break;
3831
3832 /* length(0) part, length(ad_part_len) part, length(0) part... */
3833 mbedtls_test_set_step( 1000 + ad_part_len );
3834
3835 if( !aead_multipart_internal_func( key_type_arg, key_data,
3836 alg_arg, nonce,
3837 additional_data,
3838 ad_part_len,
3839 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003840 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003841 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003842 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003843 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003844 }
3845 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003846
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003847 /* Temporary whilst we have algorithms that cannot support chunking */
3848 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003849 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003850 for( data_part_len = 1; data_part_len <= input_data->len;
3851 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003852 {
Paul Elliott329d5382021-07-22 17:10:45 +01003853 /* Split data into length(data_part_len) parts. */
3854 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003855
Paul Elliott33746aa2021-09-15 16:40:40 +01003856 if( do_set_lengths )
3857 {
3858 if( data_part_len & 0x01 )
3859 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3860 else
3861 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3862 }
3863
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003864 if( !aead_multipart_internal_func( key_type_arg, key_data,
3865 alg_arg, nonce,
3866 additional_data, -1,
3867 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003868 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003869 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003870 1, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003871 break;
3872
3873 /* length(0) part, length(data_part_len) part, length(0) part... */
3874 mbedtls_test_set_step( 3000 + data_part_len );
3875
3876 if( !aead_multipart_internal_func( key_type_arg, key_data,
3877 alg_arg, nonce,
3878 additional_data, -1,
3879 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003880 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003881 expected_output,
Paul Elliott9961a662021-09-17 19:19:02 +01003882 1, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003883 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003884 }
3885 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003886
Paul Elliott0023e0a2021-04-27 10:06:22 +01003887
Paul Elliott8fc45162021-06-23 16:06:01 +01003888 /* Goto is required to silence warnings about unused labels, as we
3889 * don't actually do any test assertions in this function. */
3890 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003891}
3892/* END_CASE */
3893
3894/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003895void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3896 int alg_arg,
3897 data_t *nonce,
3898 data_t *additional_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003899 int do_test_ad_chunked,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003900 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003901 int do_test_data_chunked,
3902 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01003903 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003904{
Paul Elliottd3f82412021-06-16 16:52:21 +01003905 size_t ad_part_len = 0;
3906 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01003907 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003908
Paul Elliotte64deda2021-09-09 14:07:23 +01003909 /* Ensure that either one part of the test or the other is done, i.e this
3910 * test does something. */
3911 TEST_ASSERT( do_test_ad_chunked || do_test_data_chunked );
3912
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003913 /* Temporary whilst we have algorithms that cannot support chunking */
3914 if( do_test_ad_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003915 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003916 for( ad_part_len = 1; ad_part_len <= additional_data->len;
3917 ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003918 {
Paul Elliott329d5382021-07-22 17:10:45 +01003919 /* Split ad into length(ad_part_len) parts. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003920 mbedtls_test_set_step( ad_part_len );
Paul Elliott0023e0a2021-04-27 10:06:22 +01003921
Paul Elliott33746aa2021-09-15 16:40:40 +01003922 if( do_set_lengths )
3923 {
3924 if( ad_part_len & 0x01 )
3925 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3926 else
3927 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3928 }
3929
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003930 if( !aead_multipart_internal_func( key_type_arg, key_data,
3931 alg_arg, nonce,
3932 additional_data,
3933 ad_part_len,
3934 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003935 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003936 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003937 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003938 break;
3939
3940 /* length(0) part, length(ad_part_len) part, length(0) part... */
3941 mbedtls_test_set_step( 1000 + ad_part_len );
3942
3943 if( !aead_multipart_internal_func( key_type_arg, key_data,
3944 alg_arg, nonce,
3945 additional_data,
3946 ad_part_len,
3947 input_data, -1,
Paul Elliott33746aa2021-09-15 16:40:40 +01003948 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003949 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003950 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003951 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003952 }
3953 }
3954
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003955 /* Temporary whilst we have algorithms that cannot support chunking */
3956 if( do_test_data_chunked == 1 )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003957 {
Paul Elliottd3f82412021-06-16 16:52:21 +01003958 for( data_part_len = 1; data_part_len <= input_data->len;
3959 data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003960 {
Paul Elliott329d5382021-07-22 17:10:45 +01003961 /* Split data into length(data_part_len) parts. */
3962 mbedtls_test_set_step( 2000 + data_part_len );
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003963
Paul Elliott33746aa2021-09-15 16:40:40 +01003964 if( do_set_lengths )
3965 {
3966 if( data_part_len & 0x01 )
3967 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3968 else
3969 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
3970 }
3971
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003972 if( !aead_multipart_internal_func( key_type_arg, key_data,
3973 alg_arg, nonce,
3974 additional_data, -1,
3975 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003976 set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003977 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003978 0, 0 ) )
Paul Elliott329d5382021-07-22 17:10:45 +01003979 break;
3980
3981 /* length(0) part, length(data_part_len) part, length(0) part... */
3982 mbedtls_test_set_step( 3000 + data_part_len );
3983
3984 if( !aead_multipart_internal_func( key_type_arg, key_data,
3985 alg_arg, nonce,
3986 additional_data, -1,
3987 input_data, data_part_len,
Paul Elliott33746aa2021-09-15 16:40:40 +01003988 set_lengths_method,
Paul Elliott329d5382021-07-22 17:10:45 +01003989 expected_output,
Paul Elliott33746aa2021-09-15 16:40:40 +01003990 0, 1 ) )
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003991 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003992 }
3993 }
3994
Paul Elliott8fc45162021-06-23 16:06:01 +01003995 /* Goto is required to silence warnings about unused labels, as we
3996 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01003997 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003998}
3999/* END_CASE */
4000
4001/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004002void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
4003 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01004004 int nonce_length,
4005 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004006 data_t *additional_data,
4007 data_t *input_data,
4008 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004009{
4010
4011 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4012 psa_key_type_t key_type = key_type_arg;
4013 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004014 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004015 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4016 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4017 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01004018 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01004019 size_t actual_nonce_length = 0;
4020 size_t expected_nonce_length = expected_nonce_length_arg;
4021 unsigned char *output = NULL;
4022 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004023 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01004024 size_t ciphertext_size = 0;
4025 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004026 size_t tag_length = 0;
4027 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004028
4029 PSA_ASSERT( psa_crypto_init( ) );
4030
4031 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
4032 psa_set_key_algorithm( & attributes, alg );
4033 psa_set_key_type( & attributes, key_type );
4034
4035 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4036 &key ) );
4037
4038 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4039
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004040 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4041
Paul Elliottf1277632021-08-24 18:11:37 +01004042 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004043
Paul Elliottf1277632021-08-24 18:11:37 +01004044 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004045
Paul Elliottf1277632021-08-24 18:11:37 +01004046 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004047
Paul Elliottf1277632021-08-24 18:11:37 +01004048 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004049
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004050 status = psa_aead_encrypt_setup( &operation, key, alg );
4051
4052 /* If the operation is not supported, just skip and not fail in case the
4053 * encryption involves a common limitation of cryptography hardwares and
4054 * an alternative implementation. */
4055 if( status == PSA_ERROR_NOT_SUPPORTED )
4056 {
4057 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01004058 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004059 }
4060
4061 PSA_ASSERT( status );
4062
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004063 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01004064 nonce_length,
4065 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004066
Paul Elliott693bf312021-07-23 17:40:41 +01004067 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004068
Paul Elliottf1277632021-08-24 18:11:37 +01004069 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01004070
Paul Elliott88ecbe12021-09-22 17:23:03 +01004071 if( expected_status == PSA_SUCCESS )
4072 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
4073 alg ) );
4074
Paul Elliottf1277632021-08-24 18:11:37 +01004075 TEST_ASSERT( actual_nonce_length < PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01004076
Paul Elliott693bf312021-07-23 17:40:41 +01004077 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004078 {
4079
4080 /* Ensure we can still complete operation. */
4081
4082 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4083 additional_data->len ) );
4084
4085 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01004086 output, output_size,
4087 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004088
Paul Elliottf1277632021-08-24 18:11:37 +01004089 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4090 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004091 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4092 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004093
4094exit:
4095 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01004096 mbedtls_free( output );
4097 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004098 psa_aead_abort( &operation );
4099 PSA_DONE( );
4100}
4101/* END_CASE */
4102
4103/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01004104void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
4105 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01004106 int nonce_length_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01004107 data_t *additional_data,
4108 data_t *input_data,
4109 int expected_status_arg )
4110{
4111
4112 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4113 psa_key_type_t key_type = key_type_arg;
4114 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004115 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01004116 uint8_t *nonce_buffer = NULL;
4117 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4118 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4119 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004120 unsigned char *output = NULL;
4121 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01004122 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01004123 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004124 size_t ciphertext_size = 0;
4125 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01004126 size_t tag_length = 0;
4127 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01004128 size_t index = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01004129
4130 PSA_ASSERT( psa_crypto_init( ) );
4131
4132 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4133 psa_set_key_algorithm( &attributes, alg );
4134 psa_set_key_type( &attributes, key_type );
4135
4136 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4137 &key ) );
4138
4139 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4140
4141 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4142
Paul Elliott6f0e7202021-08-25 12:57:18 +01004143 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004144
Paul Elliott6f0e7202021-08-25 12:57:18 +01004145 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01004146
Paul Elliott6f0e7202021-08-25 12:57:18 +01004147 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01004148
Paul Elliott6f0e7202021-08-25 12:57:18 +01004149 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004150
Paul Elliott863864a2021-07-23 17:28:31 +01004151 status = psa_aead_encrypt_setup( &operation, key, alg );
4152
4153 /* If the operation is not supported, just skip and not fail in case the
4154 * encryption involves a common limitation of cryptography hardwares and
4155 * an alternative implementation. */
4156 if( status == PSA_ERROR_NOT_SUPPORTED )
4157 {
4158 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004159 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01004160 }
4161
4162 PSA_ASSERT( status );
4163
Paul Elliott4023ffd2021-09-10 16:21:22 +01004164 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
4165 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01004166 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01004167 /* Arbitrary size buffer, to test zero length valid buffer. */
4168 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004169 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01004170 }
4171 else
4172 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004173 /* If length is zero, then this will return NULL. */
4174 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004175 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01004176
Paul Elliott4023ffd2021-09-10 16:21:22 +01004177 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01004178 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004179 for( index = 0; index < nonce_length - 1; ++index )
4180 {
4181 nonce_buffer[index] = 'a' + index;
4182 }
Paul Elliott66696b52021-08-16 18:42:41 +01004183 }
Paul Elliott863864a2021-07-23 17:28:31 +01004184 }
4185
Paul Elliott6f0e7202021-08-25 12:57:18 +01004186 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01004187
Paul Elliott693bf312021-07-23 17:40:41 +01004188 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004189
4190 if( expected_status == PSA_SUCCESS )
4191 {
4192 /* Ensure we can still complete operation. */
4193
4194 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4195 additional_data->len ) );
4196
4197 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004198 output, output_size,
4199 &ciphertext_length ) );
Paul Elliott863864a2021-07-23 17:28:31 +01004200
Paul Elliott6f0e7202021-08-25 12:57:18 +01004201 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4202 &ciphertext_length, tag_buffer,
Paul Elliott863864a2021-07-23 17:28:31 +01004203 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4204 }
4205
4206exit:
4207 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01004208 mbedtls_free( output );
4209 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01004210 mbedtls_free( nonce_buffer );
4211 psa_aead_abort( &operation );
4212 PSA_DONE( );
4213}
4214/* END_CASE */
4215
4216/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004217void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
4218 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004219 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01004220 data_t *nonce,
4221 data_t *additional_data,
4222 data_t *input_data,
4223 int expected_status_arg )
4224{
4225
4226 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4227 psa_key_type_t key_type = key_type_arg;
4228 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004229 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01004230 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4231 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4232 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01004233 unsigned char *output = NULL;
4234 unsigned char *ciphertext = NULL;
4235 size_t output_size = output_size_arg;
4236 size_t ciphertext_size = 0;
4237 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01004238 size_t tag_length = 0;
4239 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4240
4241 PSA_ASSERT( psa_crypto_init( ) );
4242
4243 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4244 psa_set_key_algorithm( &attributes, alg );
4245 psa_set_key_type( &attributes, key_type );
4246
4247 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4248 &key ) );
4249
4250 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4251
Paul Elliottc6d11d02021-09-01 12:04:23 +01004252 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004253
Paul Elliottc6d11d02021-09-01 12:04:23 +01004254 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01004255
Paul Elliottc6d11d02021-09-01 12:04:23 +01004256 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004257
Paul Elliott43fbda62021-07-23 18:30:59 +01004258 status = psa_aead_encrypt_setup( &operation, key, alg );
4259
4260 /* If the operation is not supported, just skip and not fail in case the
4261 * encryption involves a common limitation of cryptography hardwares and
4262 * an alternative implementation. */
4263 if( status == PSA_ERROR_NOT_SUPPORTED )
4264 {
4265 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4266 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4267 }
4268
4269 PSA_ASSERT( status );
4270
4271 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4272
4273 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4274 additional_data->len ) );
4275
4276 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004277 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01004278
4279 TEST_EQUAL( status, expected_status );
4280
4281 if( expected_status == PSA_SUCCESS )
4282 {
4283 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01004284 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4285 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01004286 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4287 }
4288
4289exit:
4290 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01004291 mbedtls_free( output );
4292 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01004293 psa_aead_abort( &operation );
4294 PSA_DONE( );
4295}
4296/* END_CASE */
4297
Paul Elliott91b021e2021-07-23 18:52:31 +01004298/* BEGIN_CASE */
4299void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
4300 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004301 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01004302 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01004303 data_t *nonce,
4304 data_t *additional_data,
4305 data_t *input_data,
4306 int expected_status_arg )
4307{
4308
4309 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4310 psa_key_type_t key_type = key_type_arg;
4311 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004312 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01004313 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4314 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4315 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004316 unsigned char *ciphertext = NULL;
4317 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004318 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004319 size_t ciphertext_size = 0;
4320 size_t ciphertext_length = 0;
4321 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004322 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004323 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004324
4325 PSA_ASSERT( psa_crypto_init( ) );
4326
4327 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4328 psa_set_key_algorithm( &attributes, alg );
4329 psa_set_key_type( &attributes, key_type );
4330
4331 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4332 &key ) );
4333
4334 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4335
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004336 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004337
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004338 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004339
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004340 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004341
Paul Elliott719c1322021-09-13 18:27:22 +01004342 ASSERT_ALLOC( tag_buffer, tag_size );
4343
Paul Elliott91b021e2021-07-23 18:52:31 +01004344 status = psa_aead_encrypt_setup( &operation, key, alg );
4345
4346 /* If the operation is not supported, just skip and not fail in case the
4347 * encryption involves a common limitation of cryptography hardwares and
4348 * an alternative implementation. */
4349 if( status == PSA_ERROR_NOT_SUPPORTED )
4350 {
4351 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4352 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4353 }
4354
4355 PSA_ASSERT( status );
4356
4357 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4358
4359 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4360 additional_data->len ) );
4361
4362 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004363 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004364
4365 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004366 status = psa_aead_finish( &operation, finish_ciphertext,
4367 finish_ciphertext_size,
4368 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004369 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004370
4371 TEST_EQUAL( status, expected_status );
4372
4373exit:
4374 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004375 mbedtls_free( ciphertext );
4376 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004377 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004378 psa_aead_abort( &operation );
4379 PSA_DONE( );
4380}
4381/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004382
4383/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004384void aead_multipart_verify( int key_type_arg, data_t *key_data,
4385 int alg_arg,
4386 data_t *nonce,
4387 data_t *additional_data,
4388 data_t *input_data,
4389 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004390 int tag_usage_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004391 int expected_status_arg )
4392{
4393 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4394 psa_key_type_t key_type = key_type_arg;
4395 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004396 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01004397 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4398 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4399 psa_status_t expected_status = expected_status_arg;
4400 unsigned char *plaintext = NULL;
4401 unsigned char *finish_plaintext = NULL;
4402 size_t plaintext_size = 0;
4403 size_t plaintext_length = 0;
4404 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004405 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004406 unsigned char *tag_buffer = NULL;
4407 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004408
4409 PSA_ASSERT( psa_crypto_init( ) );
4410
4411 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4412 psa_set_key_algorithm( &attributes, alg );
4413 psa_set_key_type( &attributes, key_type );
4414
4415 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4416 &key ) );
4417
4418 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4419
4420 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4421 input_data->len );
4422
4423 ASSERT_ALLOC( plaintext, plaintext_size );
4424
4425 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4426
4427 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4428
Paul Elliott9961a662021-09-17 19:19:02 +01004429 status = psa_aead_decrypt_setup( &operation, key, alg );
4430
4431 /* If the operation is not supported, just skip and not fail in case the
4432 * encryption involves a common limitation of cryptography hardwares and
4433 * an alternative implementation. */
4434 if( status == PSA_ERROR_NOT_SUPPORTED )
4435 {
4436 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4437 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4438 }
4439
4440 PSA_ASSERT( status );
4441
4442 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4443
4444 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4445 additional_data->len ) );
4446
4447 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4448 input_data->len,
4449 plaintext, plaintext_size,
4450 &plaintext_length ) );
4451
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004452 if( tag_usage == USE_GIVEN_TAG )
4453 {
4454 tag_buffer = tag->x;
4455 tag_size = tag->len;
4456 }
4457
Paul Elliott9961a662021-09-17 19:19:02 +01004458 status = psa_aead_verify( &operation, finish_plaintext,
4459 verify_plaintext_size,
4460 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004461 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004462
4463 TEST_EQUAL( status, expected_status );
4464
4465exit:
4466 psa_destroy_key( key );
4467 mbedtls_free( plaintext );
4468 mbedtls_free( finish_plaintext );
4469 psa_aead_abort( &operation );
4470 PSA_DONE( );
4471}
4472/* END_CASE */
4473
Paul Elliott9961a662021-09-17 19:19:02 +01004474/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004475void aead_multipart_setup( int key_type_arg, data_t *key_data,
4476 int alg_arg, int expected_status_arg )
4477{
4478 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4479 psa_key_type_t key_type = key_type_arg;
4480 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004481 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01004482 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4483 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4484 psa_status_t expected_status = expected_status_arg;
4485
4486 PSA_ASSERT( psa_crypto_init( ) );
4487
4488 psa_set_key_usage_flags( &attributes,
4489 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4490 psa_set_key_algorithm( &attributes, alg );
4491 psa_set_key_type( &attributes, key_type );
4492
4493 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4494 &key ) );
4495
Paul Elliott5221ef62021-09-19 17:33:03 +01004496 status = psa_aead_encrypt_setup( &operation, key, alg );
4497
4498 TEST_EQUAL( status, expected_status );
4499
4500 psa_aead_abort( &operation );
4501
Paul Elliott5221ef62021-09-19 17:33:03 +01004502 status = psa_aead_decrypt_setup( &operation, key, alg );
4503
4504 TEST_EQUAL(status, expected_status );
4505
4506exit:
4507 psa_destroy_key( key );
4508 psa_aead_abort( &operation );
4509 PSA_DONE( );
4510}
4511/* END_CASE */
4512
4513/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004514void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4515 int alg_arg,
4516 data_t *nonce,
4517 data_t *additional_data,
4518 data_t *input_data )
4519{
4520 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4521 psa_key_type_t key_type = key_type_arg;
4522 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004523 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01004524 unsigned char *output_data = NULL;
4525 unsigned char *final_data = NULL;
4526 size_t output_size = 0;
4527 size_t finish_output_size = 0;
4528 size_t output_length = 0;
4529 size_t key_bits = 0;
4530 size_t tag_length = 0;
4531 size_t tag_size = 0;
4532 size_t nonce_length = 0;
4533 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4534 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4535 size_t output_part_length = 0;
4536 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4537
4538 PSA_ASSERT( psa_crypto_init( ) );
4539
4540 psa_set_key_usage_flags( & attributes,
4541 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4542 psa_set_key_algorithm( & attributes, alg );
4543 psa_set_key_type( & attributes, key_type );
4544
4545 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4546 &key ) );
4547
4548 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4549 key_bits = psa_get_key_bits( &attributes );
4550
4551 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4552
4553 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4554
4555 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4556
4557 ASSERT_ALLOC( output_data, output_size );
4558
4559 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4560
4561 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4562
4563 ASSERT_ALLOC( final_data, finish_output_size );
4564
4565 /* Test all operations error without calling setup first. */
4566
Paul Elliottc23a9a02021-06-21 18:32:46 +01004567 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4568 PSA_ERROR_BAD_STATE );
4569
4570 psa_aead_abort( &operation );
4571
Paul Elliottc23a9a02021-06-21 18:32:46 +01004572 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4573 PSA_AEAD_NONCE_MAX_SIZE,
4574 &nonce_length ),
4575 PSA_ERROR_BAD_STATE );
4576
4577 psa_aead_abort( &operation );
4578
Paul Elliott481be342021-07-16 17:38:47 +01004579 /* ------------------------------------------------------- */
4580
Paul Elliottc23a9a02021-06-21 18:32:46 +01004581 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4582 input_data->len ),
4583 PSA_ERROR_BAD_STATE );
4584
4585 psa_aead_abort( &operation );
4586
Paul Elliott481be342021-07-16 17:38:47 +01004587 /* ------------------------------------------------------- */
4588
Paul Elliottc23a9a02021-06-21 18:32:46 +01004589 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4590 additional_data->len ),
4591 PSA_ERROR_BAD_STATE );
4592
4593 psa_aead_abort( &operation );
4594
Paul Elliott481be342021-07-16 17:38:47 +01004595 /* ------------------------------------------------------- */
4596
Paul Elliottc23a9a02021-06-21 18:32:46 +01004597 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4598 input_data->len, output_data,
4599 output_size, &output_length ),
4600 PSA_ERROR_BAD_STATE );
4601
4602 psa_aead_abort( &operation );
4603
Paul Elliott481be342021-07-16 17:38:47 +01004604 /* ------------------------------------------------------- */
4605
Paul Elliottc23a9a02021-06-21 18:32:46 +01004606 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4607 finish_output_size,
4608 &output_part_length,
4609 tag_buffer, tag_length,
4610 &tag_size ),
4611 PSA_ERROR_BAD_STATE );
4612
4613 psa_aead_abort( &operation );
4614
Paul Elliott481be342021-07-16 17:38:47 +01004615 /* ------------------------------------------------------- */
4616
Paul Elliottc23a9a02021-06-21 18:32:46 +01004617 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4618 finish_output_size,
4619 &output_part_length,
4620 tag_buffer,
4621 tag_length ),
4622 PSA_ERROR_BAD_STATE );
4623
4624 psa_aead_abort( &operation );
4625
4626 /* Test for double setups. */
4627
Paul Elliottc23a9a02021-06-21 18:32:46 +01004628 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4629
4630 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4631 PSA_ERROR_BAD_STATE );
4632
4633 psa_aead_abort( &operation );
4634
Paul Elliott481be342021-07-16 17:38:47 +01004635 /* ------------------------------------------------------- */
4636
Paul Elliottc23a9a02021-06-21 18:32:46 +01004637 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4638
4639 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4640 PSA_ERROR_BAD_STATE );
4641
4642 psa_aead_abort( &operation );
4643
Paul Elliott374a2be2021-07-16 17:53:40 +01004644 /* ------------------------------------------------------- */
4645
Paul Elliott374a2be2021-07-16 17:53:40 +01004646 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4647
4648 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4649 PSA_ERROR_BAD_STATE );
4650
4651 psa_aead_abort( &operation );
4652
4653 /* ------------------------------------------------------- */
4654
Paul Elliott374a2be2021-07-16 17:53:40 +01004655 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4656
4657 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4658 PSA_ERROR_BAD_STATE );
4659
4660 psa_aead_abort( &operation );
4661
Paul Elliottc23a9a02021-06-21 18:32:46 +01004662 /* Test for not setting a nonce. */
4663
Paul Elliottc23a9a02021-06-21 18:32:46 +01004664 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4665
4666 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4667 additional_data->len ),
4668 PSA_ERROR_BAD_STATE );
4669
4670 psa_aead_abort( &operation );
4671
Paul Elliott7f628422021-09-01 12:08:29 +01004672 /* ------------------------------------------------------- */
4673
Paul Elliott7f628422021-09-01 12:08:29 +01004674 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4675
4676 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4677 input_data->len, output_data,
4678 output_size, &output_length ),
4679 PSA_ERROR_BAD_STATE );
4680
4681 psa_aead_abort( &operation );
4682
Paul Elliottbdc2c682021-09-21 18:37:10 +01004683 /* ------------------------------------------------------- */
4684
Paul Elliottbdc2c682021-09-21 18:37:10 +01004685 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4686
4687 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4688 finish_output_size,
4689 &output_part_length,
4690 tag_buffer, tag_length,
4691 &tag_size ),
4692 PSA_ERROR_BAD_STATE );
4693
4694 psa_aead_abort( &operation );
4695
4696 /* ------------------------------------------------------- */
4697
Paul Elliottbdc2c682021-09-21 18:37:10 +01004698 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4699
4700 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4701 finish_output_size,
4702 &output_part_length,
4703 tag_buffer,
4704 tag_length ),
4705 PSA_ERROR_BAD_STATE );
4706
4707 psa_aead_abort( &operation );
4708
Paul Elliottc23a9a02021-06-21 18:32:46 +01004709 /* Test for double setting nonce. */
4710
Paul Elliottc23a9a02021-06-21 18:32:46 +01004711 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4712
4713 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4714
4715 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4716 PSA_ERROR_BAD_STATE );
4717
4718 psa_aead_abort( &operation );
4719
Paul Elliott374a2be2021-07-16 17:53:40 +01004720 /* Test for double generating nonce. */
4721
Paul Elliott374a2be2021-07-16 17:53:40 +01004722 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4723
4724 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4725 PSA_AEAD_NONCE_MAX_SIZE,
4726 &nonce_length ) );
4727
4728 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4729 PSA_AEAD_NONCE_MAX_SIZE,
4730 &nonce_length ),
4731 PSA_ERROR_BAD_STATE );
4732
4733
4734 psa_aead_abort( &operation );
4735
4736 /* Test for generate nonce then set and vice versa */
4737
Paul Elliott374a2be2021-07-16 17:53:40 +01004738 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4739
4740 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4741 PSA_AEAD_NONCE_MAX_SIZE,
4742 &nonce_length ) );
4743
4744 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4745 PSA_ERROR_BAD_STATE );
4746
4747 psa_aead_abort( &operation );
4748
4749 /* ------------------------------------------------------- */
4750
Paul Elliott374a2be2021-07-16 17:53:40 +01004751 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4752
4753 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4754
4755 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4756 PSA_AEAD_NONCE_MAX_SIZE,
4757 &nonce_length ),
4758 PSA_ERROR_BAD_STATE );
4759
4760 psa_aead_abort( &operation );
4761
Paul Elliott7220cae2021-06-22 17:25:57 +01004762 /* Test for generating nonce in decrypt setup. */
4763
Paul Elliott7220cae2021-06-22 17:25:57 +01004764 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4765
4766 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4767 PSA_AEAD_NONCE_MAX_SIZE,
4768 &nonce_length ),
4769 PSA_ERROR_BAD_STATE );
4770
4771 psa_aead_abort( &operation );
4772
Paul Elliottc23a9a02021-06-21 18:32:46 +01004773 /* Test for setting lengths twice. */
4774
Paul Elliottc23a9a02021-06-21 18:32:46 +01004775 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4776
4777 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4778
4779 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4780 input_data->len ) );
4781
4782 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4783 input_data->len ),
4784 PSA_ERROR_BAD_STATE );
4785
4786 psa_aead_abort( &operation );
4787
4788 /* Test for setting lengths after already starting data. */
4789
Paul Elliottc23a9a02021-06-21 18:32:46 +01004790 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4791
4792 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4793
Paul Elliottf94bd992021-09-19 18:15:59 +01004794 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4795 additional_data->len ) );
4796
4797 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4798 input_data->len ),
4799 PSA_ERROR_BAD_STATE );
4800
4801 psa_aead_abort( &operation );
4802
4803 /* ------------------------------------------------------- */
4804
Paul Elliottf94bd992021-09-19 18:15:59 +01004805 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4806
4807 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4808
Paul Elliottc23a9a02021-06-21 18:32:46 +01004809 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4810 input_data->len, output_data,
4811 output_size, &output_length ) );
4812
4813 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4814 input_data->len ),
4815 PSA_ERROR_BAD_STATE );
4816
4817 psa_aead_abort( &operation );
4818
Paul Elliott243080c2021-07-21 19:01:17 +01004819 /* Test for not sending any additional data or data after setting non zero
4820 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004821
Paul Elliottc23a9a02021-06-21 18:32:46 +01004822 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4823
4824 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4825
4826 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4827 input_data->len ) );
4828
4829 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4830 finish_output_size,
4831 &output_part_length,
4832 tag_buffer, tag_length,
4833 &tag_size ),
4834 PSA_ERROR_INVALID_ARGUMENT );
4835
4836 psa_aead_abort( &operation );
4837
Paul Elliott243080c2021-07-21 19:01:17 +01004838 /* Test for not sending any additional data or data after setting non-zero
4839 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004840
Paul Elliottc23a9a02021-06-21 18:32:46 +01004841 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4842
4843 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4844
4845 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4846 input_data->len ) );
4847
4848 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4849 finish_output_size,
4850 &output_part_length,
4851 tag_buffer,
4852 tag_length ),
4853 PSA_ERROR_INVALID_ARGUMENT );
4854
4855 psa_aead_abort( &operation );
4856
Paul Elliott243080c2021-07-21 19:01:17 +01004857 /* Test for not sending any additional data after setting a non-zero length
4858 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004859
Paul Elliottc23a9a02021-06-21 18:32:46 +01004860 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4861
4862 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4863
4864 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4865 input_data->len ) );
4866
4867 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4868 input_data->len, output_data,
4869 output_size, &output_length ),
4870 PSA_ERROR_INVALID_ARGUMENT );
4871
4872 psa_aead_abort( &operation );
4873
Paul Elliottf94bd992021-09-19 18:15:59 +01004874 /* Test for not sending any data after setting a non-zero length for it.*/
4875
Paul Elliottf94bd992021-09-19 18:15:59 +01004876 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4877
4878 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4879
4880 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4881 input_data->len ) );
4882
4883 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4884 additional_data->len ) );
4885
4886 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4887 finish_output_size,
4888 &output_part_length,
4889 tag_buffer, tag_length,
4890 &tag_size ),
4891 PSA_ERROR_INVALID_ARGUMENT );
4892
4893 psa_aead_abort( &operation );
4894
Paul Elliottb0450fe2021-09-01 15:06:26 +01004895 /* Test for sending too much additional data after setting lengths. */
4896
Paul Elliottb0450fe2021-09-01 15:06:26 +01004897 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4898
4899 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4900
4901 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4902
4903
4904 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4905 additional_data->len ),
4906 PSA_ERROR_INVALID_ARGUMENT );
4907
4908 psa_aead_abort( &operation );
4909
Paul Elliotta2a09b02021-09-22 14:56:40 +01004910 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004911
4912 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4913
4914 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4915
4916 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4917 input_data->len ) );
4918
4919 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4920 additional_data->len ) );
4921
4922 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4923 1 ),
4924 PSA_ERROR_INVALID_ARGUMENT );
4925
4926 psa_aead_abort( &operation );
4927
Paul Elliottb0450fe2021-09-01 15:06:26 +01004928 /* Test for sending too much data after setting lengths. */
4929
Paul Elliottb0450fe2021-09-01 15:06:26 +01004930 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4931
4932 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4933
4934 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
4935
4936 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4937 input_data->len, output_data,
4938 output_size, &output_length ),
4939 PSA_ERROR_INVALID_ARGUMENT );
4940
4941 psa_aead_abort( &operation );
4942
Paul Elliotta2a09b02021-09-22 14:56:40 +01004943 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01004944
4945 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4946
4947 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4948
4949 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4950 input_data->len ) );
4951
4952 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4953 additional_data->len ) );
4954
4955 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4956 input_data->len, output_data,
4957 output_size, &output_length ) );
4958
4959 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4960 1, output_data,
4961 output_size, &output_length ),
4962 PSA_ERROR_INVALID_ARGUMENT );
4963
4964 psa_aead_abort( &operation );
4965
Paul Elliottc23a9a02021-06-21 18:32:46 +01004966 /* Test sending additional data after data. */
4967
Paul Elliottc23a9a02021-06-21 18:32:46 +01004968 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4969
4970 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4971
4972 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4973 input_data->len, output_data,
4974 output_size, &output_length ) );
4975
4976 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4977 additional_data->len ),
4978 PSA_ERROR_BAD_STATE );
4979
4980 psa_aead_abort( &operation );
4981
Paul Elliott534d0b42021-06-22 19:15:20 +01004982 /* Test calling finish on decryption. */
4983
Paul Elliott534d0b42021-06-22 19:15:20 +01004984 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4985
4986 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4987
4988 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4989 finish_output_size,
4990 &output_part_length,
4991 tag_buffer, tag_length,
4992 &tag_size ),
4993 PSA_ERROR_BAD_STATE );
4994
4995 psa_aead_abort( &operation );
4996
4997 /* Test calling verify on encryption. */
4998
Paul Elliott534d0b42021-06-22 19:15:20 +01004999 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5000
5001 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5002
5003 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5004 finish_output_size,
5005 &output_part_length,
5006 tag_buffer,
5007 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01005008 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01005009
5010 psa_aead_abort( &operation );
5011
5012
Paul Elliottc23a9a02021-06-21 18:32:46 +01005013exit:
5014 psa_destroy_key( key );
5015 psa_aead_abort( &operation );
5016 mbedtls_free( output_data );
5017 mbedtls_free( final_data );
5018 PSA_DONE( );
5019}
5020/* END_CASE */
5021
5022/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005023void signature_size( int type_arg,
5024 int bits,
5025 int alg_arg,
5026 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005027{
5028 psa_key_type_t type = type_arg;
5029 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005030 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005031
Gilles Peskinefe11b722018-12-18 00:24:04 +01005032 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005033
Gilles Peskinee59236f2018-01-27 23:32:46 +01005034exit:
5035 ;
5036}
5037/* END_CASE */
5038
5039/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005040void sign_hash_deterministic( int key_type_arg, data_t *key_data,
5041 int alg_arg, data_t *input_data,
5042 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005043{
Ronald Cron5425a212020-08-04 14:58:35 +02005044 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005045 psa_key_type_t key_type = key_type_arg;
5046 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005047 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01005048 unsigned char *signature = NULL;
5049 size_t signature_size;
5050 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005051 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005052
Gilles Peskine8817f612018-12-18 00:18:46 +01005053 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005054
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005055 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005056 psa_set_key_algorithm( &attributes, alg );
5057 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005058
Gilles Peskine049c7532019-05-15 20:22:09 +02005059 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005060 &key ) );
5061 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005062 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01005063
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005064 /* Allocate a buffer which has the size advertized by the
5065 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005066 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005067 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01005068 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005069 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005070 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005071
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005072 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005073 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005074 input_data->x, input_data->len,
5075 signature, signature_size,
5076 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005077 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005078 ASSERT_COMPARE( output_data->x, output_data->len,
5079 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01005080
5081exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005082 /*
5083 * Key attributes may have been returned by psa_get_key_attributes()
5084 * thus reset them as required.
5085 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005086 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005087
Ronald Cron5425a212020-08-04 14:58:35 +02005088 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01005089 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005090 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005091}
5092/* END_CASE */
5093
5094/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005095void sign_hash_fail( int key_type_arg, data_t *key_data,
5096 int alg_arg, data_t *input_data,
5097 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01005098{
Ronald Cron5425a212020-08-04 14:58:35 +02005099 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005100 psa_key_type_t key_type = key_type_arg;
5101 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005102 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005103 psa_status_t actual_status;
5104 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01005105 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01005106 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005107 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005108
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005109 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005110
Gilles Peskine8817f612018-12-18 00:18:46 +01005111 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005112
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005113 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005114 psa_set_key_algorithm( &attributes, alg );
5115 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005116
Gilles Peskine049c7532019-05-15 20:22:09 +02005117 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005118 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005119
Ronald Cron5425a212020-08-04 14:58:35 +02005120 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005121 input_data->x, input_data->len,
5122 signature, signature_size,
5123 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005124 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005125 /* The value of *signature_length is unspecified on error, but
5126 * whatever it is, it should be less than signature_size, so that
5127 * if the caller tries to read *signature_length bytes without
5128 * checking the error code then they don't overflow a buffer. */
5129 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005130
5131exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005132 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005133 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01005134 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005135 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005136}
5137/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03005138
5139/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005140void sign_verify_hash( int key_type_arg, data_t *key_data,
5141 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02005142{
Ronald Cron5425a212020-08-04 14:58:35 +02005143 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005144 psa_key_type_t key_type = key_type_arg;
5145 psa_algorithm_t alg = alg_arg;
5146 size_t key_bits;
5147 unsigned char *signature = NULL;
5148 size_t signature_size;
5149 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005150 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005151
Gilles Peskine8817f612018-12-18 00:18:46 +01005152 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005153
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005154 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005155 psa_set_key_algorithm( &attributes, alg );
5156 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02005157
Gilles Peskine049c7532019-05-15 20:22:09 +02005158 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005159 &key ) );
5160 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005161 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02005162
5163 /* Allocate a buffer which has the size advertized by the
5164 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005165 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02005166 key_bits, alg );
5167 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005168 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005169 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02005170
5171 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005172 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005173 input_data->x, input_data->len,
5174 signature, signature_size,
5175 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005176 /* Check that the signature length looks sensible. */
5177 TEST_ASSERT( signature_length <= signature_size );
5178 TEST_ASSERT( signature_length > 0 );
5179
5180 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02005181 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005182 input_data->x, input_data->len,
5183 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005184
5185 if( input_data->len != 0 )
5186 {
5187 /* Flip a bit in the input and verify that the signature is now
5188 * detected as invalid. Flip a bit at the beginning, not at the end,
5189 * because ECDSA may ignore the last few bits of the input. */
5190 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02005191 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005192 input_data->x, input_data->len,
5193 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005194 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02005195 }
5196
5197exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005198 /*
5199 * Key attributes may have been returned by psa_get_key_attributes()
5200 * thus reset them as required.
5201 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005202 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005203
Ronald Cron5425a212020-08-04 14:58:35 +02005204 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02005205 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005206 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02005207}
5208/* END_CASE */
5209
5210/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005211void verify_hash( int key_type_arg, data_t *key_data,
5212 int alg_arg, data_t *hash_data,
5213 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03005214{
Ronald Cron5425a212020-08-04 14:58:35 +02005215 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005216 psa_key_type_t key_type = key_type_arg;
5217 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005218 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005219
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005220 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02005221
Gilles Peskine8817f612018-12-18 00:18:46 +01005222 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03005223
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005224 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005225 psa_set_key_algorithm( &attributes, alg );
5226 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03005227
Gilles Peskine049c7532019-05-15 20:22:09 +02005228 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005229 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03005230
Ronald Cron5425a212020-08-04 14:58:35 +02005231 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005232 hash_data->x, hash_data->len,
5233 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01005234
itayzafrir5c753392018-05-08 11:18:38 +03005235exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005236 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005237 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005238 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03005239}
5240/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005241
5242/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005243void verify_hash_fail( int key_type_arg, data_t *key_data,
5244 int alg_arg, data_t *hash_data,
5245 data_t *signature_data,
5246 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005247{
Ronald Cron5425a212020-08-04 14:58:35 +02005248 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005249 psa_key_type_t key_type = key_type_arg;
5250 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005251 psa_status_t actual_status;
5252 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005253 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005254
Gilles Peskine8817f612018-12-18 00:18:46 +01005255 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005256
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005257 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005258 psa_set_key_algorithm( &attributes, alg );
5259 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005260
Gilles Peskine049c7532019-05-15 20:22:09 +02005261 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005262 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005263
Ronald Cron5425a212020-08-04 14:58:35 +02005264 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005265 hash_data->x, hash_data->len,
5266 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005267 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005268
5269exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005270 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005271 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005272 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005273}
5274/* END_CASE */
5275
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005276/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02005277void sign_message_deterministic( int key_type_arg,
5278 data_t *key_data,
5279 int alg_arg,
5280 data_t *input_data,
5281 data_t *output_data )
5282{
5283 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5284 psa_key_type_t key_type = key_type_arg;
5285 psa_algorithm_t alg = alg_arg;
5286 size_t key_bits;
5287 unsigned char *signature = NULL;
5288 size_t signature_size;
5289 size_t signature_length = 0xdeadbeef;
5290 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5291
5292 PSA_ASSERT( psa_crypto_init( ) );
5293
5294 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5295 psa_set_key_algorithm( &attributes, alg );
5296 psa_set_key_type( &attributes, key_type );
5297
5298 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5299 &key ) );
5300 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5301 key_bits = psa_get_key_bits( &attributes );
5302
5303 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5304 TEST_ASSERT( signature_size != 0 );
5305 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5306 ASSERT_ALLOC( signature, signature_size );
5307
5308 PSA_ASSERT( psa_sign_message( key, alg,
5309 input_data->x, input_data->len,
5310 signature, signature_size,
5311 &signature_length ) );
5312
5313 ASSERT_COMPARE( output_data->x, output_data->len,
5314 signature, signature_length );
5315
5316exit:
5317 psa_reset_key_attributes( &attributes );
5318
5319 psa_destroy_key( key );
5320 mbedtls_free( signature );
5321 PSA_DONE( );
5322
5323}
5324/* END_CASE */
5325
5326/* BEGIN_CASE */
5327void sign_message_fail( int key_type_arg,
5328 data_t *key_data,
5329 int alg_arg,
5330 data_t *input_data,
5331 int signature_size_arg,
5332 int expected_status_arg )
5333{
5334 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5335 psa_key_type_t key_type = key_type_arg;
5336 psa_algorithm_t alg = alg_arg;
5337 size_t signature_size = signature_size_arg;
5338 psa_status_t actual_status;
5339 psa_status_t expected_status = expected_status_arg;
5340 unsigned char *signature = NULL;
5341 size_t signature_length = 0xdeadbeef;
5342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5343
5344 ASSERT_ALLOC( signature, signature_size );
5345
5346 PSA_ASSERT( psa_crypto_init( ) );
5347
5348 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5349 psa_set_key_algorithm( &attributes, alg );
5350 psa_set_key_type( &attributes, key_type );
5351
5352 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5353 &key ) );
5354
5355 actual_status = psa_sign_message( key, alg,
5356 input_data->x, input_data->len,
5357 signature, signature_size,
5358 &signature_length );
5359 TEST_EQUAL( actual_status, expected_status );
5360 /* The value of *signature_length is unspecified on error, but
5361 * whatever it is, it should be less than signature_size, so that
5362 * if the caller tries to read *signature_length bytes without
5363 * checking the error code then they don't overflow a buffer. */
5364 TEST_ASSERT( signature_length <= signature_size );
5365
5366exit:
5367 psa_reset_key_attributes( &attributes );
5368 psa_destroy_key( key );
5369 mbedtls_free( signature );
5370 PSA_DONE( );
5371}
5372/* END_CASE */
5373
5374/* BEGIN_CASE */
5375void sign_verify_message( int key_type_arg,
5376 data_t *key_data,
5377 int alg_arg,
5378 data_t *input_data )
5379{
5380 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5381 psa_key_type_t key_type = key_type_arg;
5382 psa_algorithm_t alg = alg_arg;
5383 size_t key_bits;
5384 unsigned char *signature = NULL;
5385 size_t signature_size;
5386 size_t signature_length = 0xdeadbeef;
5387 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5388
5389 PSA_ASSERT( psa_crypto_init( ) );
5390
5391 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5392 PSA_KEY_USAGE_VERIFY_MESSAGE );
5393 psa_set_key_algorithm( &attributes, alg );
5394 psa_set_key_type( &attributes, key_type );
5395
5396 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5397 &key ) );
5398 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5399 key_bits = psa_get_key_bits( &attributes );
5400
5401 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5402 TEST_ASSERT( signature_size != 0 );
5403 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5404 ASSERT_ALLOC( signature, signature_size );
5405
5406 PSA_ASSERT( psa_sign_message( key, alg,
5407 input_data->x, input_data->len,
5408 signature, signature_size,
5409 &signature_length ) );
5410 TEST_ASSERT( signature_length <= signature_size );
5411 TEST_ASSERT( signature_length > 0 );
5412
5413 PSA_ASSERT( psa_verify_message( key, alg,
5414 input_data->x, input_data->len,
5415 signature, signature_length ) );
5416
5417 if( input_data->len != 0 )
5418 {
5419 /* Flip a bit in the input and verify that the signature is now
5420 * detected as invalid. Flip a bit at the beginning, not at the end,
5421 * because ECDSA may ignore the last few bits of the input. */
5422 input_data->x[0] ^= 1;
5423 TEST_EQUAL( psa_verify_message( key, alg,
5424 input_data->x, input_data->len,
5425 signature, signature_length ),
5426 PSA_ERROR_INVALID_SIGNATURE );
5427 }
5428
5429exit:
5430 psa_reset_key_attributes( &attributes );
5431
5432 psa_destroy_key( key );
5433 mbedtls_free( signature );
5434 PSA_DONE( );
5435}
5436/* END_CASE */
5437
5438/* BEGIN_CASE */
5439void verify_message( int key_type_arg,
5440 data_t *key_data,
5441 int alg_arg,
5442 data_t *input_data,
5443 data_t *signature_data )
5444{
5445 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5446 psa_key_type_t key_type = key_type_arg;
5447 psa_algorithm_t alg = alg_arg;
5448 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5449
5450 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
5451
5452 PSA_ASSERT( psa_crypto_init( ) );
5453
5454 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5455 psa_set_key_algorithm( &attributes, alg );
5456 psa_set_key_type( &attributes, key_type );
5457
5458 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5459 &key ) );
5460
5461 PSA_ASSERT( psa_verify_message( key, alg,
5462 input_data->x, input_data->len,
5463 signature_data->x, signature_data->len ) );
5464
5465exit:
5466 psa_reset_key_attributes( &attributes );
5467 psa_destroy_key( key );
5468 PSA_DONE( );
5469}
5470/* END_CASE */
5471
5472/* BEGIN_CASE */
5473void verify_message_fail( int key_type_arg,
5474 data_t *key_data,
5475 int alg_arg,
5476 data_t *hash_data,
5477 data_t *signature_data,
5478 int expected_status_arg )
5479{
5480 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5481 psa_key_type_t key_type = key_type_arg;
5482 psa_algorithm_t alg = alg_arg;
5483 psa_status_t actual_status;
5484 psa_status_t expected_status = expected_status_arg;
5485 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5486
5487 PSA_ASSERT( psa_crypto_init( ) );
5488
5489 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5490 psa_set_key_algorithm( &attributes, alg );
5491 psa_set_key_type( &attributes, key_type );
5492
5493 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5494 &key ) );
5495
5496 actual_status = psa_verify_message( key, alg,
5497 hash_data->x, hash_data->len,
5498 signature_data->x,
5499 signature_data->len );
5500 TEST_EQUAL( actual_status, expected_status );
5501
5502exit:
5503 psa_reset_key_attributes( &attributes );
5504 psa_destroy_key( key );
5505 PSA_DONE( );
5506}
5507/* END_CASE */
5508
5509/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005510void asymmetric_encrypt( int key_type_arg,
5511 data_t *key_data,
5512 int alg_arg,
5513 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005514 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005515 int expected_output_length_arg,
5516 int expected_status_arg )
5517{
Ronald Cron5425a212020-08-04 14:58:35 +02005518 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005519 psa_key_type_t key_type = key_type_arg;
5520 psa_algorithm_t alg = alg_arg;
5521 size_t expected_output_length = expected_output_length_arg;
5522 size_t key_bits;
5523 unsigned char *output = NULL;
5524 size_t output_size;
5525 size_t output_length = ~0;
5526 psa_status_t actual_status;
5527 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005528 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005529
Gilles Peskine8817f612018-12-18 00:18:46 +01005530 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005531
Gilles Peskine656896e2018-06-29 19:12:28 +02005532 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005533 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5534 psa_set_key_algorithm( &attributes, alg );
5535 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005536 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005537 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005538
5539 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005540 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005541 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005542
Gilles Peskine656896e2018-06-29 19:12:28 +02005543 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005544 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005545 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005546
5547 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005548 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005549 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005550 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005551 output, output_size,
5552 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005553 TEST_EQUAL( actual_status, expected_status );
5554 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005555
Gilles Peskine68428122018-06-30 18:42:41 +02005556 /* If the label is empty, the test framework puts a non-null pointer
5557 * in label->x. Test that a null pointer works as well. */
5558 if( label->len == 0 )
5559 {
5560 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005561 if( output_size != 0 )
5562 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005563 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005564 input_data->x, input_data->len,
5565 NULL, label->len,
5566 output, output_size,
5567 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005568 TEST_EQUAL( actual_status, expected_status );
5569 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005570 }
5571
Gilles Peskine656896e2018-06-29 19:12:28 +02005572exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005573 /*
5574 * Key attributes may have been returned by psa_get_key_attributes()
5575 * thus reset them as required.
5576 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005577 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005578
Ronald Cron5425a212020-08-04 14:58:35 +02005579 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005580 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005581 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005582}
5583/* END_CASE */
5584
5585/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005586void asymmetric_encrypt_decrypt( int key_type_arg,
5587 data_t *key_data,
5588 int alg_arg,
5589 data_t *input_data,
5590 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005591{
Ronald Cron5425a212020-08-04 14:58:35 +02005592 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005593 psa_key_type_t key_type = key_type_arg;
5594 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005595 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005596 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005597 size_t output_size;
5598 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005599 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005600 size_t output2_size;
5601 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005602 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005603
Gilles Peskine8817f612018-12-18 00:18:46 +01005604 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005605
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005606 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5607 psa_set_key_algorithm( &attributes, alg );
5608 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005609
Gilles Peskine049c7532019-05-15 20:22:09 +02005610 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005611 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005612
5613 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005614 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005615 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005616
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005617 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005618 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005619 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005620
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005621 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01005622 TEST_ASSERT( output2_size <=
5623 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
5624 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005625 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005626
Gilles Peskineeebd7382018-06-08 18:11:54 +02005627 /* We test encryption by checking that encrypt-then-decrypt gives back
5628 * the original plaintext because of the non-optional random
5629 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02005630 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005631 input_data->x, input_data->len,
5632 label->x, label->len,
5633 output, output_size,
5634 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005635 /* We don't know what ciphertext length to expect, but check that
5636 * it looks sensible. */
5637 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005638
Ronald Cron5425a212020-08-04 14:58:35 +02005639 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005640 output, output_length,
5641 label->x, label->len,
5642 output2, output2_size,
5643 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005644 ASSERT_COMPARE( input_data->x, input_data->len,
5645 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005646
5647exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005648 /*
5649 * Key attributes may have been returned by psa_get_key_attributes()
5650 * thus reset them as required.
5651 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005652 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005653
Ronald Cron5425a212020-08-04 14:58:35 +02005654 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005655 mbedtls_free( output );
5656 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005657 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005658}
5659/* END_CASE */
5660
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005661/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005662void asymmetric_decrypt( int key_type_arg,
5663 data_t *key_data,
5664 int alg_arg,
5665 data_t *input_data,
5666 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02005667 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005668{
Ronald Cron5425a212020-08-04 14:58:35 +02005669 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005670 psa_key_type_t key_type = key_type_arg;
5671 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01005672 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005673 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03005674 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005675 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005676 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005677
Gilles Peskine8817f612018-12-18 00:18:46 +01005678 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005679
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005680 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5681 psa_set_key_algorithm( &attributes, alg );
5682 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005683
Gilles Peskine049c7532019-05-15 20:22:09 +02005684 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005685 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005686
gabor-mezei-armceface22021-01-21 12:26:17 +01005687 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5688 key_bits = psa_get_key_bits( &attributes );
5689
5690 /* Determine the maximum ciphertext length */
5691 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
5692 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
5693 ASSERT_ALLOC( output, output_size );
5694
Ronald Cron5425a212020-08-04 14:58:35 +02005695 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005696 input_data->x, input_data->len,
5697 label->x, label->len,
5698 output,
5699 output_size,
5700 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005701 ASSERT_COMPARE( expected_data->x, expected_data->len,
5702 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005703
Gilles Peskine68428122018-06-30 18:42:41 +02005704 /* If the label is empty, the test framework puts a non-null pointer
5705 * in label->x. Test that a null pointer works as well. */
5706 if( label->len == 0 )
5707 {
5708 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005709 if( output_size != 0 )
5710 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005711 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01005712 input_data->x, input_data->len,
5713 NULL, label->len,
5714 output,
5715 output_size,
5716 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005717 ASSERT_COMPARE( expected_data->x, expected_data->len,
5718 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005719 }
5720
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005721exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005722 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005723 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03005724 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005725 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005726}
5727/* END_CASE */
5728
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005729/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005730void asymmetric_decrypt_fail( int key_type_arg,
5731 data_t *key_data,
5732 int alg_arg,
5733 data_t *input_data,
5734 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00005735 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02005736 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005737{
Ronald Cron5425a212020-08-04 14:58:35 +02005738 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005739 psa_key_type_t key_type = key_type_arg;
5740 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005741 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00005742 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005743 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005744 psa_status_t actual_status;
5745 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005746 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005747
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005748 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005749
Gilles Peskine8817f612018-12-18 00:18:46 +01005750 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005751
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005752 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5753 psa_set_key_algorithm( &attributes, alg );
5754 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005755
Gilles Peskine049c7532019-05-15 20:22:09 +02005756 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005757 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005758
Ronald Cron5425a212020-08-04 14:58:35 +02005759 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005760 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005761 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02005762 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02005763 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005764 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005765 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005766
Gilles Peskine68428122018-06-30 18:42:41 +02005767 /* If the label is empty, the test framework puts a non-null pointer
5768 * in label->x. Test that a null pointer works as well. */
5769 if( label->len == 0 )
5770 {
5771 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005772 if( output_size != 0 )
5773 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005774 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005775 input_data->x, input_data->len,
5776 NULL, label->len,
5777 output, output_size,
5778 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005779 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005780 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02005781 }
5782
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005783exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005784 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005785 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02005786 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005787 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005788}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02005789/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02005790
5791/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005792void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00005793{
5794 /* Test each valid way of initializing the object, except for `= {0}`, as
5795 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
5796 * though it's OK by the C standard. We could test for this, but we'd need
5797 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005798 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005799 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
5800 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
5801 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00005802
5803 memset( &zero, 0, sizeof( zero ) );
5804
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005805 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005806 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005807 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005808 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005809 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005810 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005811 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00005812
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005813 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005814 PSA_ASSERT( psa_key_derivation_abort(&func) );
5815 PSA_ASSERT( psa_key_derivation_abort(&init) );
5816 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00005817}
5818/* END_CASE */
5819
Janos Follath16de4a42019-06-13 16:32:24 +01005820/* BEGIN_CASE */
5821void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02005822{
Gilles Peskineea0fb492018-07-12 17:17:20 +02005823 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005824 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005825 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02005826
Gilles Peskine8817f612018-12-18 00:18:46 +01005827 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005828
Janos Follath16de4a42019-06-13 16:32:24 +01005829 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005830 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005831
5832exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005833 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005834 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02005835}
5836/* END_CASE */
5837
Janos Follathaf3c2a02019-06-12 12:34:34 +01005838/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01005839void derive_set_capacity( int alg_arg, int capacity_arg,
5840 int expected_status_arg )
5841{
5842 psa_algorithm_t alg = alg_arg;
5843 size_t capacity = capacity_arg;
5844 psa_status_t expected_status = expected_status_arg;
5845 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5846
5847 PSA_ASSERT( psa_crypto_init( ) );
5848
5849 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5850
5851 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
5852 expected_status );
5853
5854exit:
5855 psa_key_derivation_abort( &operation );
5856 PSA_DONE( );
5857}
5858/* END_CASE */
5859
5860/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01005861void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02005862 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005863 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02005864 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01005865 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02005866 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005867 int expected_status_arg3,
5868 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005869{
5870 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02005871 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
5872 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01005873 psa_status_t expected_statuses[] = {expected_status_arg1,
5874 expected_status_arg2,
5875 expected_status_arg3};
5876 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02005877 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
5878 MBEDTLS_SVC_KEY_ID_INIT,
5879 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01005880 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5881 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5882 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005883 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005884 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005885 psa_status_t expected_output_status = expected_output_status_arg;
5886 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01005887
5888 PSA_ASSERT( psa_crypto_init( ) );
5889
5890 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5891 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005892
5893 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
5894
5895 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
5896 {
Gilles Peskine4023c012021-05-27 13:21:20 +02005897 mbedtls_test_set_step( i );
5898 if( steps[i] == 0 )
5899 {
5900 /* Skip this step */
5901 }
5902 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01005903 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02005904 psa_set_key_type( &attributes, key_types[i] );
5905 PSA_ASSERT( psa_import_key( &attributes,
5906 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005907 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005908 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
5909 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
5910 {
5911 // When taking a private key as secret input, use key agreement
5912 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005913 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
5914 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005915 expected_statuses[i] );
5916 }
5917 else
5918 {
5919 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02005920 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02005921 expected_statuses[i] );
5922 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02005923 }
5924 else
5925 {
5926 TEST_EQUAL( psa_key_derivation_input_bytes(
5927 &operation, steps[i],
5928 inputs[i]->x, inputs[i]->len ),
5929 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005930 }
5931 }
5932
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005933 if( output_key_type != PSA_KEY_TYPE_NONE )
5934 {
5935 psa_reset_key_attributes( &attributes );
5936 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
5937 psa_set_key_bits( &attributes, 8 );
5938 actual_output_status =
5939 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005940 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02005941 }
5942 else
5943 {
5944 uint8_t buffer[1];
5945 actual_output_status =
5946 psa_key_derivation_output_bytes( &operation,
5947 buffer, sizeof( buffer ) );
5948 }
5949 TEST_EQUAL( actual_output_status, expected_output_status );
5950
Janos Follathaf3c2a02019-06-12 12:34:34 +01005951exit:
5952 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02005953 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
5954 psa_destroy_key( keys[i] );
5955 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01005956 PSA_DONE( );
5957}
5958/* END_CASE */
5959
Janos Follathd958bb72019-07-03 15:02:16 +01005960/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02005961void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005962{
Janos Follathd958bb72019-07-03 15:02:16 +01005963 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02005964 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02005965 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005966 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01005967 unsigned char input1[] = "Input 1";
5968 size_t input1_length = sizeof( input1 );
5969 unsigned char input2[] = "Input 2";
5970 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005971 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02005972 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02005973 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5974 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
5975 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005976 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03005977
Gilles Peskine8817f612018-12-18 00:18:46 +01005978 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005979
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005980 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5981 psa_set_key_algorithm( &attributes, alg );
5982 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005983
Gilles Peskine73676cb2019-05-15 20:15:10 +02005984 PSA_ASSERT( psa_import_key( &attributes,
5985 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02005986 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005987
5988 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005989 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
5990 input1, input1_length,
5991 input2, input2_length,
5992 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01005993 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005994
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005995 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01005996 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005997 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03005998
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005999 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006000
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006001 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02006002 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006003
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006004exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006005 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006006 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006007 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006008}
6009/* END_CASE */
6010
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006011/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006012void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006013{
6014 uint8_t output_buffer[16];
6015 size_t buffer_size = 16;
6016 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006017 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006018
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006019 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6020 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006021 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006022
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006023 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006024 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006025
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006026 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006027
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006028 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6029 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006030 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006031
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006032 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006033 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006034
6035exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006036 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006037}
6038/* END_CASE */
6039
6040/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006041void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02006042 int step1_arg, data_t *input1,
6043 int step2_arg, data_t *input2,
6044 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006045 int requested_capacity_arg,
6046 data_t *expected_output1,
6047 data_t *expected_output2 )
6048{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006049 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02006050 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
6051 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006052 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6053 MBEDTLS_SVC_KEY_ID_INIT,
6054 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006055 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006056 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006057 uint8_t *expected_outputs[2] =
6058 {expected_output1->x, expected_output2->x};
6059 size_t output_sizes[2] =
6060 {expected_output1->len, expected_output2->len};
6061 size_t output_buffer_size = 0;
6062 uint8_t *output_buffer = NULL;
6063 size_t expected_capacity;
6064 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006065 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006066 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02006067 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006068
6069 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6070 {
6071 if( output_sizes[i] > output_buffer_size )
6072 output_buffer_size = output_sizes[i];
6073 if( output_sizes[i] == 0 )
6074 expected_outputs[i] = NULL;
6075 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006076 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01006077 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006078
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006079 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6080 psa_set_key_algorithm( &attributes, alg );
6081 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006082
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006083 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02006084 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6085 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
6086 requested_capacity ) );
6087 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006088 {
Gilles Peskine1468da72019-05-29 17:35:49 +02006089 switch( steps[i] )
6090 {
6091 case 0:
6092 break;
6093 case PSA_KEY_DERIVATION_INPUT_SECRET:
6094 PSA_ASSERT( psa_import_key( &attributes,
6095 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006096 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01006097
6098 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
6099 {
6100 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
6101 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
6102 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
6103 }
6104
Gilles Peskine1468da72019-05-29 17:35:49 +02006105 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02006106 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02006107 break;
6108 default:
6109 PSA_ASSERT( psa_key_derivation_input_bytes(
6110 &operation, steps[i],
6111 inputs[i]->x, inputs[i]->len ) );
6112 break;
6113 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006114 }
Gilles Peskine1468da72019-05-29 17:35:49 +02006115
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006116 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006117 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006118 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006119 expected_capacity = requested_capacity;
6120
6121 /* Expansion phase. */
6122 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6123 {
6124 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006125 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006126 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006127 if( expected_capacity == 0 && output_sizes[i] == 0 )
6128 {
6129 /* Reading 0 bytes when 0 bytes are available can go either way. */
6130 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02006131 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006132 continue;
6133 }
6134 else if( expected_capacity == 0 ||
6135 output_sizes[i] > expected_capacity )
6136 {
6137 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02006138 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006139 expected_capacity = 0;
6140 continue;
6141 }
6142 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01006143 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006144 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006145 ASSERT_COMPARE( output_buffer, output_sizes[i],
6146 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006147 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006148 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006149 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006150 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006151 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006152 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006153 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006154
6155exit:
6156 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006157 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006158 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6159 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006160 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006161}
6162/* END_CASE */
6163
6164/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02006165void derive_full( int alg_arg,
6166 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01006167 data_t *input1,
6168 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02006169 int requested_capacity_arg )
6170{
Ronald Cron5425a212020-08-04 14:58:35 +02006171 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006172 psa_algorithm_t alg = alg_arg;
6173 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006174 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006175 unsigned char output_buffer[16];
6176 size_t expected_capacity = requested_capacity;
6177 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006178 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006179
Gilles Peskine8817f612018-12-18 00:18:46 +01006180 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006181
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006182 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6183 psa_set_key_algorithm( &attributes, alg );
6184 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02006185
Gilles Peskine049c7532019-05-15 20:22:09 +02006186 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006187 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006188
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006189 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6190 input1->x, input1->len,
6191 input2->x, input2->len,
6192 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01006193 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01006194
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006195 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006196 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006197 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006198
6199 /* Expansion phase. */
6200 while( current_capacity > 0 )
6201 {
6202 size_t read_size = sizeof( output_buffer );
6203 if( read_size > current_capacity )
6204 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006205 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006206 output_buffer,
6207 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006208 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006209 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006210 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006211 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006212 }
6213
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006214 /* Check that the operation refuses to go over capacity. */
6215 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006216 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02006217
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006218 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006219
6220exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006221 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006222 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006223 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02006224}
6225/* END_CASE */
6226
Janos Follathe60c9052019-07-03 13:51:30 +01006227/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006228void derive_key_exercise( int alg_arg,
6229 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01006230 data_t *input1,
6231 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006232 int derived_type_arg,
6233 int derived_bits_arg,
6234 int derived_usage_arg,
6235 int derived_alg_arg )
6236{
Ronald Cron5425a212020-08-04 14:58:35 +02006237 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6238 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006239 psa_algorithm_t alg = alg_arg;
6240 psa_key_type_t derived_type = derived_type_arg;
6241 size_t derived_bits = derived_bits_arg;
6242 psa_key_usage_t derived_usage = derived_usage_arg;
6243 psa_algorithm_t derived_alg = derived_alg_arg;
6244 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006245 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006246 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006247 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006248
Gilles Peskine8817f612018-12-18 00:18:46 +01006249 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006250
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006251 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6252 psa_set_key_algorithm( &attributes, alg );
6253 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006254 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006255 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006256
6257 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006258 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6259 input1->x, input1->len,
6260 input2->x, input2->len,
6261 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01006262 goto exit;
6263
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006264 psa_set_key_usage_flags( &attributes, derived_usage );
6265 psa_set_key_algorithm( &attributes, derived_alg );
6266 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006267 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006268 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006269 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006270
6271 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006272 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006273 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
6274 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006275
6276 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006277 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02006278 goto exit;
6279
6280exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006281 /*
6282 * Key attributes may have been returned by psa_get_key_attributes()
6283 * thus reset them as required.
6284 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006285 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006286
6287 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006288 psa_destroy_key( base_key );
6289 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006290 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006291}
6292/* END_CASE */
6293
Janos Follath42fd8882019-07-03 14:17:09 +01006294/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006295void derive_key_export( int alg_arg,
6296 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01006297 data_t *input1,
6298 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006299 int bytes1_arg,
6300 int bytes2_arg )
6301{
Ronald Cron5425a212020-08-04 14:58:35 +02006302 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6303 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006304 psa_algorithm_t alg = alg_arg;
6305 size_t bytes1 = bytes1_arg;
6306 size_t bytes2 = bytes2_arg;
6307 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006308 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006309 uint8_t *output_buffer = NULL;
6310 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006311 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6312 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006313 size_t length;
6314
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006315 ASSERT_ALLOC( output_buffer, capacity );
6316 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01006317 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006318
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006319 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6320 psa_set_key_algorithm( &base_attributes, alg );
6321 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006322 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006323 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006324
6325 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006326 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6327 input1->x, input1->len,
6328 input2->x, input2->len,
6329 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006330 goto exit;
6331
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006332 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006333 output_buffer,
6334 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006335 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006336
6337 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006338 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6339 input1->x, input1->len,
6340 input2->x, input2->len,
6341 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006342 goto exit;
6343
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006344 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6345 psa_set_key_algorithm( &derived_attributes, 0 );
6346 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006347 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006348 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006349 &derived_key ) );
6350 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006351 export_buffer, bytes1,
6352 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006353 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02006354 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006355 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006356 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006357 &derived_key ) );
6358 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006359 export_buffer + bytes1, bytes2,
6360 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006361 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006362
6363 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006364 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
6365 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006366
6367exit:
6368 mbedtls_free( output_buffer );
6369 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006370 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006371 psa_destroy_key( base_key );
6372 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006373 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006374}
6375/* END_CASE */
6376
6377/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006378void derive_key( int alg_arg,
6379 data_t *key_data, data_t *input1, data_t *input2,
6380 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006381 int expected_status_arg,
6382 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006383{
Ronald Cron5425a212020-08-04 14:58:35 +02006384 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6385 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006386 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006387 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006388 size_t bits = bits_arg;
6389 psa_status_t expected_status = expected_status_arg;
6390 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6391 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6392 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6393
6394 PSA_ASSERT( psa_crypto_init( ) );
6395
6396 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6397 psa_set_key_algorithm( &base_attributes, alg );
6398 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6399 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006400 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02006401
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006402 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6403 input1->x, input1->len,
6404 input2->x, input2->len,
6405 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02006406 goto exit;
6407
6408 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6409 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006410 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02006411 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01006412
6413 psa_status_t status =
6414 psa_key_derivation_output_key( &derived_attributes,
6415 &operation,
6416 &derived_key );
6417 if( is_large_output > 0 )
6418 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6419 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02006420
6421exit:
6422 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006423 psa_destroy_key( base_key );
6424 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02006425 PSA_DONE( );
6426}
6427/* END_CASE */
6428
6429/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006430void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02006431 int our_key_type_arg, int our_key_alg_arg,
6432 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006433 int expected_status_arg )
6434{
Ronald Cron5425a212020-08-04 14:58:35 +02006435 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006436 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006437 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006438 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006439 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006440 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02006441 psa_status_t expected_status = expected_status_arg;
6442 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006443
Gilles Peskine8817f612018-12-18 00:18:46 +01006444 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006445
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006446 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006447 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006448 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006449 PSA_ASSERT( psa_import_key( &attributes,
6450 our_key_data->x, our_key_data->len,
6451 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006452
Gilles Peskine77f40d82019-04-11 21:27:06 +02006453 /* The tests currently include inputs that should fail at either step.
6454 * Test cases that fail at the setup step should be changed to call
6455 * key_derivation_setup instead, and this function should be renamed
6456 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006457 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02006458 if( status == PSA_SUCCESS )
6459 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006460 TEST_EQUAL( psa_key_derivation_key_agreement(
6461 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
6462 our_key,
6463 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02006464 expected_status );
6465 }
6466 else
6467 {
6468 TEST_ASSERT( status == expected_status );
6469 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02006470
6471exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006472 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006473 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006474 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006475}
6476/* END_CASE */
6477
6478/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006479void raw_key_agreement( int alg_arg,
6480 int our_key_type_arg, data_t *our_key_data,
6481 data_t *peer_key_data,
6482 data_t *expected_output )
6483{
Ronald Cron5425a212020-08-04 14:58:35 +02006484 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006485 psa_algorithm_t alg = alg_arg;
6486 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006487 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006488 unsigned char *output = NULL;
6489 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006490 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006491
6492 ASSERT_ALLOC( output, expected_output->len );
6493 PSA_ASSERT( psa_crypto_init( ) );
6494
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006495 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6496 psa_set_key_algorithm( &attributes, alg );
6497 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006498 PSA_ASSERT( psa_import_key( &attributes,
6499 our_key_data->x, our_key_data->len,
6500 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006501
gabor-mezei-armceface22021-01-21 12:26:17 +01006502 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6503 key_bits = psa_get_key_bits( &attributes );
6504
Gilles Peskinebe697d82019-05-16 18:00:41 +02006505 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6506 peer_key_data->x, peer_key_data->len,
6507 output, expected_output->len,
6508 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006509 ASSERT_COMPARE( output, output_length,
6510 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006511 TEST_ASSERT( output_length <=
6512 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6513 TEST_ASSERT( output_length <=
6514 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006515
6516exit:
6517 mbedtls_free( output );
6518 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006519 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006520}
6521/* END_CASE */
6522
6523/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006524void key_agreement_capacity( int alg_arg,
6525 int our_key_type_arg, data_t *our_key_data,
6526 data_t *peer_key_data,
6527 int expected_capacity_arg )
6528{
Ronald Cron5425a212020-08-04 14:58:35 +02006529 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006530 psa_algorithm_t alg = alg_arg;
6531 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006532 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006533 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006534 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006535 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006536
Gilles Peskine8817f612018-12-18 00:18:46 +01006537 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006538
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006539 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6540 psa_set_key_algorithm( &attributes, alg );
6541 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006542 PSA_ASSERT( psa_import_key( &attributes,
6543 our_key_data->x, our_key_data->len,
6544 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006545
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006546 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006547 PSA_ASSERT( psa_key_derivation_key_agreement(
6548 &operation,
6549 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6550 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006551 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6552 {
6553 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006554 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006555 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006556 NULL, 0 ) );
6557 }
Gilles Peskine59685592018-09-18 12:11:34 +02006558
Gilles Peskinebf491972018-10-25 22:36:12 +02006559 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006560 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006561 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006562 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006563
Gilles Peskinebf491972018-10-25 22:36:12 +02006564 /* Test the actual capacity by reading the output. */
6565 while( actual_capacity > sizeof( output ) )
6566 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006567 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006568 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006569 actual_capacity -= sizeof( output );
6570 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006571 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006572 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006573 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006574 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006575
Gilles Peskine59685592018-09-18 12:11:34 +02006576exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006577 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006578 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006579 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006580}
6581/* END_CASE */
6582
6583/* BEGIN_CASE */
6584void key_agreement_output( int alg_arg,
6585 int our_key_type_arg, data_t *our_key_data,
6586 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006587 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006588{
Ronald Cron5425a212020-08-04 14:58:35 +02006589 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006590 psa_algorithm_t alg = alg_arg;
6591 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006592 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006593 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006594 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006595
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006596 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6597 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006598
Gilles Peskine8817f612018-12-18 00:18:46 +01006599 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006600
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006601 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6602 psa_set_key_algorithm( &attributes, alg );
6603 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006604 PSA_ASSERT( psa_import_key( &attributes,
6605 our_key_data->x, our_key_data->len,
6606 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006607
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006608 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006609 PSA_ASSERT( psa_key_derivation_key_agreement(
6610 &operation,
6611 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6612 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006613 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6614 {
6615 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006616 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006617 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006618 NULL, 0 ) );
6619 }
Gilles Peskine59685592018-09-18 12:11:34 +02006620
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006621 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006622 actual_output,
6623 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006624 ASSERT_COMPARE( actual_output, expected_output1->len,
6625 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006626 if( expected_output2->len != 0 )
6627 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006628 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006629 actual_output,
6630 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006631 ASSERT_COMPARE( actual_output, expected_output2->len,
6632 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006633 }
Gilles Peskine59685592018-09-18 12:11:34 +02006634
6635exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006636 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006637 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006638 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006639 mbedtls_free( actual_output );
6640}
6641/* END_CASE */
6642
6643/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02006644void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02006645{
Gilles Peskinea50d7392018-06-21 10:22:13 +02006646 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006647 unsigned char *output = NULL;
6648 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02006649 size_t i;
6650 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02006651
Simon Butcher49f8e312020-03-03 15:51:50 +00006652 TEST_ASSERT( bytes_arg >= 0 );
6653
Gilles Peskine91892022021-02-08 19:50:26 +01006654 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006655 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02006656
Gilles Peskine8817f612018-12-18 00:18:46 +01006657 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02006658
Gilles Peskinea50d7392018-06-21 10:22:13 +02006659 /* Run several times, to ensure that every output byte will be
6660 * nonzero at least once with overwhelming probability
6661 * (2^(-8*number_of_runs)). */
6662 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02006663 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006664 if( bytes != 0 )
6665 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01006666 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006667
Gilles Peskinea50d7392018-06-21 10:22:13 +02006668 for( i = 0; i < bytes; i++ )
6669 {
6670 if( output[i] != 0 )
6671 ++changed[i];
6672 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006673 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02006674
6675 /* Check that every byte was changed to nonzero at least once. This
6676 * validates that psa_generate_random is overwriting every byte of
6677 * the output buffer. */
6678 for( i = 0; i < bytes; i++ )
6679 {
6680 TEST_ASSERT( changed[i] != 0 );
6681 }
Gilles Peskine05d69892018-06-19 22:00:52 +02006682
6683exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006684 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02006685 mbedtls_free( output );
6686 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02006687}
6688/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02006689
6690/* BEGIN_CASE */
6691void generate_key( int type_arg,
6692 int bits_arg,
6693 int usage_arg,
6694 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006695 int expected_status_arg,
6696 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006697{
Ronald Cron5425a212020-08-04 14:58:35 +02006698 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006699 psa_key_type_t type = type_arg;
6700 psa_key_usage_t usage = usage_arg;
6701 size_t bits = bits_arg;
6702 psa_algorithm_t alg = alg_arg;
6703 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006704 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006705 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006706
Gilles Peskine8817f612018-12-18 00:18:46 +01006707 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006708
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006709 psa_set_key_usage_flags( &attributes, usage );
6710 psa_set_key_algorithm( &attributes, alg );
6711 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006712 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006713
6714 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01006715 psa_status_t status = psa_generate_key( &attributes, &key );
6716
6717 if( is_large_key > 0 )
6718 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6719 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006720 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006721 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006722
6723 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006724 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006725 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
6726 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006727
Gilles Peskine818ca122018-06-20 18:16:48 +02006728 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006729 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02006730 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02006731
6732exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006733 /*
6734 * Key attributes may have been returned by psa_get_key_attributes()
6735 * thus reset them as required.
6736 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006737 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006738
Ronald Cron5425a212020-08-04 14:58:35 +02006739 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006740 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006741}
6742/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03006743
Ronald Cronee414c72021-03-18 18:50:08 +01006744/* 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 +02006745void generate_key_rsa( int bits_arg,
6746 data_t *e_arg,
6747 int expected_status_arg )
6748{
Ronald Cron5425a212020-08-04 14:58:35 +02006749 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006750 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006751 size_t bits = bits_arg;
6752 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
6753 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
6754 psa_status_t expected_status = expected_status_arg;
6755 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6756 uint8_t *exported = NULL;
6757 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006758 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006759 size_t exported_length = SIZE_MAX;
6760 uint8_t *e_read_buffer = NULL;
6761 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02006762 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006763 size_t e_read_length = SIZE_MAX;
6764
6765 if( e_arg->len == 0 ||
6766 ( e_arg->len == 3 &&
6767 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
6768 {
6769 is_default_public_exponent = 1;
6770 e_read_size = 0;
6771 }
6772 ASSERT_ALLOC( e_read_buffer, e_read_size );
6773 ASSERT_ALLOC( exported, exported_size );
6774
6775 PSA_ASSERT( psa_crypto_init( ) );
6776
6777 psa_set_key_usage_flags( &attributes, usage );
6778 psa_set_key_algorithm( &attributes, alg );
6779 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
6780 e_arg->x, e_arg->len ) );
6781 psa_set_key_bits( &attributes, bits );
6782
6783 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006784 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006785 if( expected_status != PSA_SUCCESS )
6786 goto exit;
6787
6788 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006789 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006790 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6791 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
6792 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
6793 e_read_buffer, e_read_size,
6794 &e_read_length ) );
6795 if( is_default_public_exponent )
6796 TEST_EQUAL( e_read_length, 0 );
6797 else
6798 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
6799
6800 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006801 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02006802 goto exit;
6803
6804 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02006805 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006806 exported, exported_size,
6807 &exported_length ) );
6808 {
6809 uint8_t *p = exported;
6810 uint8_t *end = exported + exported_length;
6811 size_t len;
6812 /* RSAPublicKey ::= SEQUENCE {
6813 * modulus INTEGER, -- n
6814 * publicExponent INTEGER } -- e
6815 */
6816 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006817 MBEDTLS_ASN1_SEQUENCE |
6818 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01006819 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006820 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
6821 MBEDTLS_ASN1_INTEGER ) );
6822 if( len >= 1 && p[0] == 0 )
6823 {
6824 ++p;
6825 --len;
6826 }
6827 if( e_arg->len == 0 )
6828 {
6829 TEST_EQUAL( len, 3 );
6830 TEST_EQUAL( p[0], 1 );
6831 TEST_EQUAL( p[1], 0 );
6832 TEST_EQUAL( p[2], 1 );
6833 }
6834 else
6835 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
6836 }
6837
6838exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006839 /*
6840 * Key attributes may have been returned by psa_get_key_attributes() or
6841 * set by psa_set_key_domain_parameters() thus reset them as required.
6842 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006843 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006844
Ronald Cron5425a212020-08-04 14:58:35 +02006845 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006846 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006847 mbedtls_free( e_read_buffer );
6848 mbedtls_free( exported );
6849}
6850/* END_CASE */
6851
Darryl Greend49a4992018-06-18 17:27:26 +01006852/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006853void persistent_key_load_key_from_storage( data_t *data,
6854 int type_arg, int bits_arg,
6855 int usage_flags_arg, int alg_arg,
6856 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01006857{
Ronald Cron71016a92020-08-28 19:01:50 +02006858 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006859 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02006860 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6861 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006862 psa_key_type_t type = type_arg;
6863 size_t bits = bits_arg;
6864 psa_key_usage_t usage_flags = usage_flags_arg;
6865 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006866 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01006867 unsigned char *first_export = NULL;
6868 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01006869 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006870 size_t first_exported_length;
6871 size_t second_exported_length;
6872
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006873 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6874 {
6875 ASSERT_ALLOC( first_export, export_size );
6876 ASSERT_ALLOC( second_export, export_size );
6877 }
Darryl Greend49a4992018-06-18 17:27:26 +01006878
Gilles Peskine8817f612018-12-18 00:18:46 +01006879 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006880
Gilles Peskinec87af662019-05-15 16:12:22 +02006881 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006882 psa_set_key_usage_flags( &attributes, usage_flags );
6883 psa_set_key_algorithm( &attributes, alg );
6884 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006885 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01006886
Darryl Green0c6575a2018-11-07 16:05:30 +00006887 switch( generation_method )
6888 {
6889 case IMPORT_KEY:
6890 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02006891 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006892 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006893 break;
Darryl Greend49a4992018-06-18 17:27:26 +01006894
Darryl Green0c6575a2018-11-07 16:05:30 +00006895 case GENERATE_KEY:
6896 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02006897 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006898 break;
6899
6900 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01006901#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006902 {
6903 /* Create base key */
6904 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
6905 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6906 psa_set_key_usage_flags( &base_attributes,
6907 PSA_KEY_USAGE_DERIVE );
6908 psa_set_key_algorithm( &base_attributes, derive_alg );
6909 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006910 PSA_ASSERT( psa_import_key( &base_attributes,
6911 data->x, data->len,
6912 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006913 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006914 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006915 PSA_ASSERT( psa_key_derivation_input_key(
6916 &operation,
6917 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006918 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006919 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006920 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006921 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
6922 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006923 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006924 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006925 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02006926 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006927 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01006928#else
6929 TEST_ASSUME( ! "KDF not supported in this configuration" );
6930#endif
6931 break;
6932
6933 default:
6934 TEST_ASSERT( ! "generation_method not implemented in test" );
6935 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00006936 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006937 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01006938
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006939 /* Export the key if permitted by the key policy. */
6940 if( usage_flags & PSA_KEY_USAGE_EXPORT )
6941 {
Ronald Cron5425a212020-08-04 14:58:35 +02006942 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006943 first_export, export_size,
6944 &first_exported_length ) );
6945 if( generation_method == IMPORT_KEY )
6946 ASSERT_COMPARE( data->x, data->len,
6947 first_export, first_exported_length );
6948 }
Darryl Greend49a4992018-06-18 17:27:26 +01006949
6950 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02006951 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006952 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01006953 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01006954
Darryl Greend49a4992018-06-18 17:27:26 +01006955 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02006956 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02006957 TEST_ASSERT( mbedtls_svc_key_id_equal(
6958 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006959 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
6960 PSA_KEY_LIFETIME_PERSISTENT );
6961 TEST_EQUAL( psa_get_key_type( &attributes ), type );
6962 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02006963 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02006964 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006965 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01006966
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006967 /* Export the key again if permitted by the key policy. */
6968 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00006969 {
Ronald Cron5425a212020-08-04 14:58:35 +02006970 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006971 second_export, export_size,
6972 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00006973 ASSERT_COMPARE( first_export, first_exported_length,
6974 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006975 }
6976
6977 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006978 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00006979 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01006980
6981exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006982 /*
6983 * Key attributes may have been returned by psa_get_key_attributes()
6984 * thus reset them as required.
6985 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006986 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006987
Darryl Greend49a4992018-06-18 17:27:26 +01006988 mbedtls_free( first_export );
6989 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006990 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02006991 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02006992 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006993 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01006994}
6995/* END_CASE */