blob: a66ad7587fd174d80427327a02fc41e721cee1d5 [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 {
Mircea Udrea657ff4f2022-01-31 13:51:56 +0100509 memcpy( ( output_data + output_length ), part_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100510 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
Gilles Peskinef7b41372021-09-22 16:15:05 +02001425 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02001426 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 */
Mateusz Starzyked71e922021-10-21 10:04:57 +02002867void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
2868 data_t *input, int iv_length,
2869 int expected_result )
2870{
2871 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2872 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2873 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2874 size_t output_buffer_size = 0;
2875 unsigned char *output = NULL;
2876
2877 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2878 ASSERT_ALLOC( output, output_buffer_size );
2879
2880 PSA_ASSERT( psa_crypto_init( ) );
2881
2882 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2883 psa_set_key_algorithm( &attributes, alg );
2884 psa_set_key_type( &attributes, key_type );
2885
2886 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2887 &key ) );
2888 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2889 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
2890 iv_length ) );
2891
2892exit:
2893 psa_cipher_abort( &operation );
2894 mbedtls_free( output );
2895 psa_destroy_key( key );
2896 PSA_DONE( );
2897}
2898/* END_CASE */
2899
2900/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02002901void cipher_encrypt_alg_without_iv( int alg_arg,
2902 int key_type_arg,
2903 data_t *key_data,
2904 data_t *input,
2905 data_t *expected_output )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002906{
2907 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2908 psa_key_type_t key_type = key_type_arg;
2909 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02002910 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2911 uint8_t iv[1] = { 0x5a };
2912 size_t iv_length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002913 unsigned char *output = NULL;
2914 size_t output_buffer_size = 0;
2915 size_t output_length = 0;
2916 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2917
2918 PSA_ASSERT( psa_crypto_init( ) );
2919
2920 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2921 psa_set_key_algorithm( &attributes, alg );
2922 psa_set_key_type( &attributes, key_type );
2923
2924 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2925 ASSERT_ALLOC( output, output_buffer_size );
2926
2927 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2928 &key ) );
2929
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02002930 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2931 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
2932 PSA_ERROR_BAD_STATE );
2933 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
2934 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
2935 &iv_length ),
2936 PSA_ERROR_BAD_STATE );
2937
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002938 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output,
2939 output_buffer_size, &output_length ) );
2940 TEST_ASSERT( output_length <=
2941 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
2942 TEST_ASSERT( output_length <=
2943 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
2944
2945 ASSERT_COMPARE( expected_output->x, expected_output->len,
2946 output, output_length );
2947exit:
2948 mbedtls_free( output );
2949 psa_destroy_key( key );
2950 PSA_DONE( );
2951}
2952/* END_CASE */
2953
2954/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01002955void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
2956{
2957 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
2958 psa_algorithm_t alg = alg_arg;
2959 psa_key_type_t key_type = key_type_arg;
2960 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2961 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2962 psa_status_t status;
2963
2964 PSA_ASSERT( psa_crypto_init( ) );
2965
2966 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2967 psa_set_key_algorithm( &attributes, alg );
2968 psa_set_key_type( &attributes, key_type );
2969
2970 /* Usage of either of these two size macros would cause divide by zero
2971 * with incorrect key types previously. Input length should be irrelevant
2972 * here. */
2973 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
2974 0 );
2975 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
2976
2977
2978 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2979 &key ) );
2980
2981 /* Should fail due to invalid alg type (to support invalid key type).
2982 * Encrypt or decrypt will end up in the same place. */
2983 status = psa_cipher_encrypt_setup( &operation, key, alg );
2984
2985 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
2986
2987exit:
2988 psa_cipher_abort( &operation );
2989 psa_destroy_key( key );
2990 PSA_DONE( );
2991}
2992/* END_CASE */
2993
2994/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01002995void cipher_encrypt_validation( int alg_arg,
2996 int key_type_arg,
2997 data_t *key_data,
2998 data_t *input )
2999{
3000 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3001 psa_key_type_t key_type = key_type_arg;
3002 psa_algorithm_t alg = alg_arg;
3003 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
3004 unsigned char *output1 = NULL;
3005 size_t output1_buffer_size = 0;
3006 size_t output1_length = 0;
3007 unsigned char *output2 = NULL;
3008 size_t output2_buffer_size = 0;
3009 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003010 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003011 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003012 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003013
Gilles Peskine8817f612018-12-18 00:18:46 +01003014 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003015
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003016 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3017 psa_set_key_algorithm( &attributes, alg );
3018 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003019
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003020 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3021 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3022 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
3023 ASSERT_ALLOC( output1, output1_buffer_size );
3024 ASSERT_ALLOC( output2, output2_buffer_size );
3025
Ronald Cron5425a212020-08-04 14:58:35 +02003026 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3027 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003028
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02003029 /* The one-shot cipher encryption uses generated iv so validating
3030 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003031 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
3032 output1_buffer_size, &output1_length ) );
3033 TEST_ASSERT( output1_length <=
3034 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3035 TEST_ASSERT( output1_length <=
gabor-mezei-armceface22021-01-21 12:26:17 +01003036 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003037
3038 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3039 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003040
Gilles Peskine8817f612018-12-18 00:18:46 +01003041 PSA_ASSERT( psa_cipher_update( &operation,
3042 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003043 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01003044 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003045 TEST_ASSERT( function_output_length <=
3046 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3047 TEST_ASSERT( function_output_length <=
3048 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003049 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003050
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003051 PSA_ASSERT( psa_cipher_finish( &operation,
3052 output2 + output2_length,
3053 output2_buffer_size - output2_length,
3054 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003055 TEST_ASSERT( function_output_length <=
3056 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3057 TEST_ASSERT( function_output_length <=
3058 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003059 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003060
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003061 PSA_ASSERT( psa_cipher_abort( &operation ) );
3062 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
3063 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003064
Gilles Peskine50e586b2018-06-08 14:28:46 +02003065exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003066 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003067 mbedtls_free( output1 );
3068 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003069 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003070 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003071}
3072/* END_CASE */
3073
3074/* BEGIN_CASE */
3075void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003076 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003077 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003078 int first_part_size_arg,
3079 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003080 data_t *expected_output,
3081 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003082{
Ronald Cron5425a212020-08-04 14:58:35 +02003083 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003084 psa_key_type_t key_type = key_type_arg;
3085 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003086 psa_status_t status;
3087 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003088 size_t first_part_size = first_part_size_arg;
3089 size_t output1_length = output1_length_arg;
3090 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003091 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003092 size_t output_buffer_size = 0;
3093 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003094 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003095 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003096 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003097
Gilles Peskine8817f612018-12-18 00:18:46 +01003098 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003099
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003100 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3101 psa_set_key_algorithm( &attributes, alg );
3102 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003103
Ronald Cron5425a212020-08-04 14:58:35 +02003104 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3105 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003106
Ronald Cron5425a212020-08-04 14:58:35 +02003107 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003108
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003109 if( iv->len > 0 )
3110 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003111 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003112 }
3113
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003114 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3115 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003116 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003117
Gilles Peskinee0866522019-02-19 19:44:00 +01003118 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003119 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3120 output, output_buffer_size,
3121 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003122 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003123 TEST_ASSERT( function_output_length <=
3124 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3125 TEST_ASSERT( function_output_length <=
3126 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003127 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003128
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003129 if( first_part_size < input->len )
3130 {
3131 PSA_ASSERT( psa_cipher_update( &operation,
3132 input->x + first_part_size,
3133 input->len - first_part_size,
3134 ( output_buffer_size == 0 ? NULL :
3135 output + total_output_length ),
3136 output_buffer_size - total_output_length,
3137 &function_output_length ) );
3138 TEST_ASSERT( function_output_length == output2_length );
3139 TEST_ASSERT( function_output_length <=
3140 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3141 alg,
3142 input->len - first_part_size ) );
3143 TEST_ASSERT( function_output_length <=
3144 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3145 total_output_length += function_output_length;
3146 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003147
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003148 status = psa_cipher_finish( &operation,
3149 ( output_buffer_size == 0 ? NULL :
3150 output + total_output_length ),
3151 output_buffer_size - total_output_length,
3152 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003153 TEST_ASSERT( function_output_length <=
3154 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3155 TEST_ASSERT( function_output_length <=
3156 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003157 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003158 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003159
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003160 if( expected_status == PSA_SUCCESS )
3161 {
3162 PSA_ASSERT( psa_cipher_abort( &operation ) );
3163
3164 ASSERT_COMPARE( expected_output->x, expected_output->len,
3165 output, total_output_length );
3166 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003167
3168exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003169 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003170 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003171 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003172 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003173}
3174/* END_CASE */
3175
3176/* BEGIN_CASE */
3177void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003178 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003179 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003180 int first_part_size_arg,
3181 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003182 data_t *expected_output,
3183 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003184{
Ronald Cron5425a212020-08-04 14:58:35 +02003185 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003186 psa_key_type_t key_type = key_type_arg;
3187 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003188 psa_status_t status;
3189 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003190 size_t first_part_size = first_part_size_arg;
3191 size_t output1_length = output1_length_arg;
3192 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003193 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003194 size_t output_buffer_size = 0;
3195 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003196 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003197 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003198 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003199
Gilles Peskine8817f612018-12-18 00:18:46 +01003200 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003201
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003202 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3203 psa_set_key_algorithm( &attributes, alg );
3204 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003205
Ronald Cron5425a212020-08-04 14:58:35 +02003206 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3207 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003208
Ronald Cron5425a212020-08-04 14:58:35 +02003209 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003210
Steven Cooreman177deba2020-09-07 17:14:14 +02003211 if( iv->len > 0 )
3212 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003213 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003214 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003215
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003216 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3217 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003218 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003219
Gilles Peskinee0866522019-02-19 19:44:00 +01003220 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003221 PSA_ASSERT( psa_cipher_update( &operation,
3222 input->x, first_part_size,
3223 output, output_buffer_size,
3224 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003225 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003226 TEST_ASSERT( function_output_length <=
3227 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3228 TEST_ASSERT( function_output_length <=
3229 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003230 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003231
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003232 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02003233 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003234 PSA_ASSERT( psa_cipher_update( &operation,
3235 input->x + first_part_size,
3236 input->len - first_part_size,
3237 ( output_buffer_size == 0 ? NULL :
3238 output + total_output_length ),
3239 output_buffer_size - total_output_length,
3240 &function_output_length ) );
3241 TEST_ASSERT( function_output_length == output2_length );
3242 TEST_ASSERT( function_output_length <=
3243 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3244 alg,
3245 input->len - first_part_size ) );
3246 TEST_ASSERT( function_output_length <=
3247 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
3248 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003249 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003250
Gilles Peskine50e586b2018-06-08 14:28:46 +02003251 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003252 ( output_buffer_size == 0 ? NULL :
3253 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003254 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003255 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01003256 TEST_ASSERT( function_output_length <=
3257 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3258 TEST_ASSERT( function_output_length <=
3259 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003260 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003261 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003262
3263 if( expected_status == PSA_SUCCESS )
3264 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003265 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003266
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003267 ASSERT_COMPARE( expected_output->x, expected_output->len,
3268 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003269 }
3270
Gilles Peskine50e586b2018-06-08 14:28:46 +02003271exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003272 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003273 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003274 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003275 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003276}
3277/* END_CASE */
3278
Gilles Peskine50e586b2018-06-08 14:28:46 +02003279/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003280void cipher_decrypt_fail( int alg_arg,
3281 int key_type_arg,
3282 data_t *key_data,
3283 data_t *iv,
3284 data_t *input_arg,
3285 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003286{
3287 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3288 psa_status_t status;
3289 psa_key_type_t key_type = key_type_arg;
3290 psa_algorithm_t alg = alg_arg;
3291 psa_status_t expected_status = expected_status_arg;
3292 unsigned char *input = NULL;
3293 size_t input_buffer_size = 0;
3294 unsigned char *output = NULL;
3295 size_t output_buffer_size = 0;
3296 size_t output_length = 0;
3297 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3298
3299 if ( PSA_ERROR_BAD_STATE != expected_status )
3300 {
3301 PSA_ASSERT( psa_crypto_init( ) );
3302
3303 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3304 psa_set_key_algorithm( &attributes, alg );
3305 psa_set_key_type( &attributes, key_type );
3306
3307 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3308 &key ) );
3309 }
3310
3311 /* Allocate input buffer and copy the iv and the plaintext */
3312 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3313 if ( input_buffer_size > 0 )
3314 {
3315 ASSERT_ALLOC( input, input_buffer_size );
3316 memcpy( input, iv->x, iv->len );
3317 memcpy( input + iv->len, input_arg->x, input_arg->len );
3318 }
3319
3320 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3321 ASSERT_ALLOC( output, output_buffer_size );
3322
3323 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3324 output_buffer_size, &output_length );
3325 TEST_EQUAL( status, expected_status );
3326
3327exit:
3328 mbedtls_free( input );
3329 mbedtls_free( output );
3330 psa_destroy_key( key );
3331 PSA_DONE( );
3332}
3333/* END_CASE */
3334
3335/* BEGIN_CASE */
3336void cipher_decrypt( int alg_arg,
3337 int key_type_arg,
3338 data_t *key_data,
3339 data_t *iv,
3340 data_t *input_arg,
3341 data_t *expected_output )
3342{
3343 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3344 psa_key_type_t key_type = key_type_arg;
3345 psa_algorithm_t alg = alg_arg;
3346 unsigned char *input = NULL;
3347 size_t input_buffer_size = 0;
3348 unsigned char *output = NULL;
3349 size_t output_buffer_size = 0;
3350 size_t output_length = 0;
3351 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3352
3353 PSA_ASSERT( psa_crypto_init( ) );
3354
3355 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3356 psa_set_key_algorithm( &attributes, alg );
3357 psa_set_key_type( &attributes, key_type );
3358
3359 /* Allocate input buffer and copy the iv and the plaintext */
3360 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3361 if ( input_buffer_size > 0 )
3362 {
3363 ASSERT_ALLOC( input, input_buffer_size );
3364 memcpy( input, iv->x, iv->len );
3365 memcpy( input + iv->len, input_arg->x, input_arg->len );
3366 }
3367
3368 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3369 ASSERT_ALLOC( output, output_buffer_size );
3370
3371 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3372 &key ) );
3373
3374 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3375 output_buffer_size, &output_length ) );
3376 TEST_ASSERT( output_length <=
3377 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3378 TEST_ASSERT( output_length <=
3379 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
3380
3381 ASSERT_COMPARE( expected_output->x, expected_output->len,
3382 output, output_length );
3383exit:
3384 mbedtls_free( input );
3385 mbedtls_free( output );
3386 psa_destroy_key( key );
3387 PSA_DONE( );
3388}
3389/* END_CASE */
3390
3391/* BEGIN_CASE */
3392void cipher_verify_output( int alg_arg,
3393 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003394 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003395 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003396{
Ronald Cron5425a212020-08-04 14:58:35 +02003397 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003398 psa_key_type_t key_type = key_type_arg;
3399 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003400 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003401 size_t output1_size = 0;
3402 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003403 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003404 size_t output2_size = 0;
3405 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003406 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003407
Gilles Peskine8817f612018-12-18 00:18:46 +01003408 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003409
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003410 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3411 psa_set_key_algorithm( &attributes, alg );
3412 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003413
Ronald Cron5425a212020-08-04 14:58:35 +02003414 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3415 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003416 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003417 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003418
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003419 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3420 output1, output1_size,
3421 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003422 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003423 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003424 TEST_ASSERT( output1_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003425 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003426
3427 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003428 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003429
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003430 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3431 output2, output2_size,
3432 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003433 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003434 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003435 TEST_ASSERT( output2_length <=
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003436 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003437
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003438 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003439
3440exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003441 mbedtls_free( output1 );
3442 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003443 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003444 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003445}
3446/* END_CASE */
3447
3448/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003449void cipher_verify_output_multipart( int alg_arg,
3450 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003451 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003452 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003453 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003454{
Ronald Cron5425a212020-08-04 14:58:35 +02003455 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003456 psa_key_type_t key_type = key_type_arg;
3457 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003458 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003459 unsigned char iv[16] = {0};
3460 size_t iv_size = 16;
3461 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003462 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003463 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003464 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003465 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003466 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003467 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003468 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003469 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3470 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003471 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003472
Gilles Peskine8817f612018-12-18 00:18:46 +01003473 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003474
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003475 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3476 psa_set_key_algorithm( &attributes, alg );
3477 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003478
Ronald Cron5425a212020-08-04 14:58:35 +02003479 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3480 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03003481
Ronald Cron5425a212020-08-04 14:58:35 +02003482 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
3483 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003484
Steven Cooreman177deba2020-09-07 17:14:14 +02003485 if( alg != PSA_ALG_ECB_NO_PADDING )
3486 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003487 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3488 iv, iv_size,
3489 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003490 }
3491
gabor-mezei-armceface22021-01-21 12:26:17 +01003492 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3493 TEST_ASSERT( output1_buffer_size <=
3494 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003495 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003496
Gilles Peskinee0866522019-02-19 19:44:00 +01003497 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003498
Gilles Peskine8817f612018-12-18 00:18:46 +01003499 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3500 output1, output1_buffer_size,
3501 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003502 TEST_ASSERT( function_output_length <=
3503 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3504 TEST_ASSERT( function_output_length <=
3505 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003506 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003507
Gilles Peskine8817f612018-12-18 00:18:46 +01003508 PSA_ASSERT( psa_cipher_update( &operation1,
3509 input->x + first_part_size,
3510 input->len - first_part_size,
3511 output1, output1_buffer_size,
3512 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003513 TEST_ASSERT( function_output_length <=
3514 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3515 alg,
3516 input->len - first_part_size ) );
3517 TEST_ASSERT( function_output_length <=
3518 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003519 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003520
Gilles Peskine8817f612018-12-18 00:18:46 +01003521 PSA_ASSERT( psa_cipher_finish( &operation1,
3522 output1 + output1_length,
3523 output1_buffer_size - output1_length,
3524 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003525 TEST_ASSERT( function_output_length <=
3526 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3527 TEST_ASSERT( function_output_length <=
3528 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003529 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003530
Gilles Peskine8817f612018-12-18 00:18:46 +01003531 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003532
Gilles Peskine048b7f02018-06-08 14:20:49 +02003533 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003534 TEST_ASSERT( output2_buffer_size <=
3535 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3536 TEST_ASSERT( output2_buffer_size <=
3537 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003538 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003539
Steven Cooreman177deba2020-09-07 17:14:14 +02003540 if( iv_length > 0 )
3541 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003542 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3543 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003544 }
Moran Pekerded84402018-06-06 16:36:50 +03003545
Gilles Peskine8817f612018-12-18 00:18:46 +01003546 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3547 output2, output2_buffer_size,
3548 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003549 TEST_ASSERT( function_output_length <=
3550 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3551 TEST_ASSERT( function_output_length <=
3552 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003553 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003554
Gilles Peskine8817f612018-12-18 00:18:46 +01003555 PSA_ASSERT( psa_cipher_update( &operation2,
3556 output1 + first_part_size,
3557 output1_length - first_part_size,
3558 output2, output2_buffer_size,
3559 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003560 TEST_ASSERT( function_output_length <=
3561 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3562 alg,
3563 output1_length - first_part_size ) );
3564 TEST_ASSERT( function_output_length <=
3565 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003566 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003567
Gilles Peskine8817f612018-12-18 00:18:46 +01003568 PSA_ASSERT( psa_cipher_finish( &operation2,
3569 output2 + output2_length,
3570 output2_buffer_size - output2_length,
3571 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003572 TEST_ASSERT( function_output_length <=
3573 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3574 TEST_ASSERT( function_output_length <=
3575 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003576 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003577
Gilles Peskine8817f612018-12-18 00:18:46 +01003578 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003579
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003580 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003581
3582exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003583 psa_cipher_abort( &operation1 );
3584 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003585 mbedtls_free( output1 );
3586 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003587 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003588 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003589}
3590/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003591
Gilles Peskine20035e32018-02-03 22:44:14 +01003592/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003593void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003594 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003595 data_t *nonce,
3596 data_t *additional_data,
3597 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003598 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003599{
Ronald Cron5425a212020-08-04 14:58:35 +02003600 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003601 psa_key_type_t key_type = key_type_arg;
3602 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003603 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003604 unsigned char *output_data = NULL;
3605 size_t output_size = 0;
3606 size_t output_length = 0;
3607 unsigned char *output_data2 = NULL;
3608 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003609 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003610 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003611 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003612
Gilles Peskine8817f612018-12-18 00:18:46 +01003613 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003614
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003615 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3616 psa_set_key_algorithm( &attributes, alg );
3617 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003618
Gilles Peskine049c7532019-05-15 20:22:09 +02003619 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003620 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003621 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3622 key_bits = psa_get_key_bits( &attributes );
3623
3624 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3625 alg );
3626 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3627 * should be exact. */
3628 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3629 expected_result != PSA_ERROR_NOT_SUPPORTED )
3630 {
3631 TEST_EQUAL( output_size,
3632 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3633 TEST_ASSERT( output_size <=
3634 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3635 }
3636 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003637
Steven Cooremanf49478b2021-02-15 15:19:25 +01003638 status = psa_aead_encrypt( key, alg,
3639 nonce->x, nonce->len,
3640 additional_data->x,
3641 additional_data->len,
3642 input_data->x, input_data->len,
3643 output_data, output_size,
3644 &output_length );
3645
3646 /* If the operation is not supported, just skip and not fail in case the
3647 * encryption involves a common limitation of cryptography hardwares and
3648 * an alternative implementation. */
3649 if( status == PSA_ERROR_NOT_SUPPORTED )
3650 {
3651 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3652 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3653 }
3654
3655 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003656
3657 if( PSA_SUCCESS == expected_result )
3658 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003659 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003660
Gilles Peskine003a4a92019-05-14 16:09:40 +02003661 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3662 * should be exact. */
3663 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003664 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003665
gabor-mezei-armceface22021-01-21 12:26:17 +01003666 TEST_ASSERT( input_data->len <=
3667 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3668
Ronald Cron5425a212020-08-04 14:58:35 +02003669 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003670 nonce->x, nonce->len,
3671 additional_data->x,
3672 additional_data->len,
3673 output_data, output_length,
3674 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003675 &output_length2 ),
3676 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003677
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003678 ASSERT_COMPARE( input_data->x, input_data->len,
3679 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003680 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003681
Gilles Peskinea1cac842018-06-11 19:33:02 +02003682exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003683 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003684 mbedtls_free( output_data );
3685 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003686 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003687}
3688/* END_CASE */
3689
3690/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003691void aead_encrypt( int key_type_arg, data_t *key_data,
3692 int alg_arg,
3693 data_t *nonce,
3694 data_t *additional_data,
3695 data_t *input_data,
3696 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003697{
Ronald Cron5425a212020-08-04 14:58:35 +02003698 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003699 psa_key_type_t key_type = key_type_arg;
3700 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003701 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003702 unsigned char *output_data = NULL;
3703 size_t output_size = 0;
3704 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003705 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003706 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003707
Gilles Peskine8817f612018-12-18 00:18:46 +01003708 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003709
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003710 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3711 psa_set_key_algorithm( &attributes, alg );
3712 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003713
Gilles Peskine049c7532019-05-15 20:22:09 +02003714 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003715 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003716 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3717 key_bits = psa_get_key_bits( &attributes );
3718
3719 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3720 alg );
3721 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3722 * should be exact. */
3723 TEST_EQUAL( output_size,
3724 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3725 TEST_ASSERT( output_size <=
3726 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3727 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003728
Steven Cooremand588ea12021-01-11 19:36:04 +01003729 status = psa_aead_encrypt( key, alg,
3730 nonce->x, nonce->len,
3731 additional_data->x, additional_data->len,
3732 input_data->x, input_data->len,
3733 output_data, output_size,
3734 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003735
Ronald Cron28a45ed2021-02-09 20:35:42 +01003736 /* If the operation is not supported, just skip and not fail in case the
3737 * encryption involves a common limitation of cryptography hardwares and
3738 * an alternative implementation. */
3739 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003740 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003741 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3742 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003743 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003744
3745 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003746 ASSERT_COMPARE( expected_result->x, expected_result->len,
3747 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003748
Gilles Peskinea1cac842018-06-11 19:33:02 +02003749exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003750 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003751 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003752 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003753}
3754/* END_CASE */
3755
3756/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003757void aead_decrypt( int key_type_arg, data_t *key_data,
3758 int alg_arg,
3759 data_t *nonce,
3760 data_t *additional_data,
3761 data_t *input_data,
3762 data_t *expected_data,
3763 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003764{
Ronald Cron5425a212020-08-04 14:58:35 +02003765 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003766 psa_key_type_t key_type = key_type_arg;
3767 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003768 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003769 unsigned char *output_data = NULL;
3770 size_t output_size = 0;
3771 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003772 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003773 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003774 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003775
Gilles Peskine8817f612018-12-18 00:18:46 +01003776 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003777
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003778 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3779 psa_set_key_algorithm( &attributes, alg );
3780 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003781
Gilles Peskine049c7532019-05-15 20:22:09 +02003782 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003783 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003784 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3785 key_bits = psa_get_key_bits( &attributes );
3786
3787 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3788 alg );
3789 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3790 expected_result != PSA_ERROR_NOT_SUPPORTED )
3791 {
3792 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3793 * should be exact. */
3794 TEST_EQUAL( output_size,
3795 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3796 TEST_ASSERT( output_size <=
3797 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3798 }
3799 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003800
Steven Cooremand588ea12021-01-11 19:36:04 +01003801 status = psa_aead_decrypt( key, alg,
3802 nonce->x, nonce->len,
3803 additional_data->x,
3804 additional_data->len,
3805 input_data->x, input_data->len,
3806 output_data, output_size,
3807 &output_length );
3808
Ronald Cron28a45ed2021-02-09 20:35:42 +01003809 /* If the operation is not supported, just skip and not fail in case the
3810 * decryption involves a common limitation of cryptography hardwares and
3811 * an alternative implementation. */
3812 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003813 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003814 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3815 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003816 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003817
3818 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003819
Gilles Peskine2d277862018-06-18 15:41:12 +02003820 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003821 ASSERT_COMPARE( expected_data->x, expected_data->len,
3822 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003823
Gilles Peskinea1cac842018-06-11 19:33:02 +02003824exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003825 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003826 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003827 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003828}
3829/* END_CASE */
3830
3831/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003832void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
3833 int alg_arg,
3834 data_t *nonce,
3835 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003836 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003837 int do_set_lengths,
3838 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003839{
Paul Elliottd3f82412021-06-16 16:52:21 +01003840 size_t ad_part_len = 0;
3841 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01003842 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003843
Paul Elliott32f46ba2021-09-23 18:24:36 +01003844 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003845 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003846 mbedtls_test_set_step( ad_part_len );
3847
3848 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003849 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003850 if( ad_part_len & 0x01 )
3851 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3852 else
3853 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003854 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01003855
3856 /* Split ad into length(ad_part_len) parts. */
3857 if( !aead_multipart_internal_func( key_type_arg, key_data,
3858 alg_arg, nonce,
3859 additional_data,
3860 ad_part_len,
3861 input_data, -1,
3862 set_lengths_method,
3863 expected_output,
3864 1, 0 ) )
3865 break;
3866
3867 /* length(0) part, length(ad_part_len) part, length(0) part... */
3868 mbedtls_test_set_step( 1000 + ad_part_len );
3869
3870 if( !aead_multipart_internal_func( key_type_arg, key_data,
3871 alg_arg, nonce,
3872 additional_data,
3873 ad_part_len,
3874 input_data, -1,
3875 set_lengths_method,
3876 expected_output,
3877 1, 1 ) )
3878 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003879 }
Paul Elliottd3f82412021-06-16 16:52:21 +01003880
Paul Elliott32f46ba2021-09-23 18:24:36 +01003881 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003882 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003883 /* Split data into length(data_part_len) parts. */
3884 mbedtls_test_set_step( 2000 + data_part_len );
3885
3886 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003887 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003888 if( data_part_len & 0x01 )
3889 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3890 else
3891 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003892 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003893
Paul Elliott32f46ba2021-09-23 18:24:36 +01003894 if( !aead_multipart_internal_func( key_type_arg, key_data,
3895 alg_arg, nonce,
3896 additional_data, -1,
3897 input_data, data_part_len,
3898 set_lengths_method,
3899 expected_output,
3900 1, 0 ) )
3901 break;
3902
3903 /* length(0) part, length(data_part_len) part, length(0) part... */
3904 mbedtls_test_set_step( 3000 + data_part_len );
3905
3906 if( !aead_multipart_internal_func( key_type_arg, key_data,
3907 alg_arg, nonce,
3908 additional_data, -1,
3909 input_data, data_part_len,
3910 set_lengths_method,
3911 expected_output,
3912 1, 1 ) )
3913 break;
3914 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01003915
Paul Elliott8fc45162021-06-23 16:06:01 +01003916 /* Goto is required to silence warnings about unused labels, as we
3917 * don't actually do any test assertions in this function. */
3918 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003919}
3920/* END_CASE */
3921
3922/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01003923void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
3924 int alg_arg,
3925 data_t *nonce,
3926 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01003927 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01003928 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01003929 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003930{
Paul Elliottd3f82412021-06-16 16:52:21 +01003931 size_t ad_part_len = 0;
3932 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01003933 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003934
Paul Elliott32f46ba2021-09-23 18:24:36 +01003935 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003936 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003937 /* Split ad into length(ad_part_len) parts. */
3938 mbedtls_test_set_step( ad_part_len );
3939
3940 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003941 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003942 if( ad_part_len & 0x01 )
3943 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3944 else
3945 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003946 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01003947
3948 if( !aead_multipart_internal_func( key_type_arg, key_data,
3949 alg_arg, nonce,
3950 additional_data,
3951 ad_part_len,
3952 input_data, -1,
3953 set_lengths_method,
3954 expected_output,
3955 0, 0 ) )
3956 break;
3957
3958 /* length(0) part, length(ad_part_len) part, length(0) part... */
3959 mbedtls_test_set_step( 1000 + ad_part_len );
3960
3961 if( !aead_multipart_internal_func( key_type_arg, key_data,
3962 alg_arg, nonce,
3963 additional_data,
3964 ad_part_len,
3965 input_data, -1,
3966 set_lengths_method,
3967 expected_output,
3968 0, 1 ) )
3969 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003970 }
3971
Paul Elliott32f46ba2021-09-23 18:24:36 +01003972 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003973 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003974 /* Split data into length(data_part_len) parts. */
3975 mbedtls_test_set_step( 2000 + data_part_len );
3976
3977 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01003978 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01003979 if( data_part_len & 0x01 )
3980 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
3981 else
3982 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01003983 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01003984
3985 if( !aead_multipart_internal_func( key_type_arg, key_data,
3986 alg_arg, nonce,
3987 additional_data, -1,
3988 input_data, data_part_len,
3989 set_lengths_method,
3990 expected_output,
3991 0, 0 ) )
3992 break;
3993
3994 /* length(0) part, length(data_part_len) part, length(0) part... */
3995 mbedtls_test_set_step( 3000 + data_part_len );
3996
3997 if( !aead_multipart_internal_func( key_type_arg, key_data,
3998 alg_arg, nonce,
3999 additional_data, -1,
4000 input_data, data_part_len,
4001 set_lengths_method,
4002 expected_output,
4003 0, 1 ) )
4004 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004005 }
4006
Paul Elliott8fc45162021-06-23 16:06:01 +01004007 /* Goto is required to silence warnings about unused labels, as we
4008 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01004009 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004010}
4011/* END_CASE */
4012
4013/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004014void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
4015 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01004016 int nonce_length,
4017 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004018 data_t *additional_data,
4019 data_t *input_data,
4020 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004021{
4022
4023 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4024 psa_key_type_t key_type = key_type_arg;
4025 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004026 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004027 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4028 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4029 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01004030 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01004031 size_t actual_nonce_length = 0;
4032 size_t expected_nonce_length = expected_nonce_length_arg;
4033 unsigned char *output = NULL;
4034 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004035 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01004036 size_t ciphertext_size = 0;
4037 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004038 size_t tag_length = 0;
4039 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004040
4041 PSA_ASSERT( psa_crypto_init( ) );
4042
4043 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
4044 psa_set_key_algorithm( & attributes, alg );
4045 psa_set_key_type( & attributes, key_type );
4046
4047 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4048 &key ) );
4049
4050 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4051
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004052 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4053
Paul Elliottf1277632021-08-24 18:11:37 +01004054 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004055
Paul Elliottf1277632021-08-24 18:11:37 +01004056 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004057
Paul Elliottf1277632021-08-24 18:11:37 +01004058 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004059
Paul Elliottf1277632021-08-24 18:11:37 +01004060 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004061
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004062 status = psa_aead_encrypt_setup( &operation, key, alg );
4063
4064 /* If the operation is not supported, just skip and not fail in case the
4065 * encryption involves a common limitation of cryptography hardwares and
4066 * an alternative implementation. */
4067 if( status == PSA_ERROR_NOT_SUPPORTED )
4068 {
4069 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01004070 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004071 }
4072
4073 PSA_ASSERT( status );
4074
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004075 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01004076 nonce_length,
4077 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004078
Paul Elliott693bf312021-07-23 17:40:41 +01004079 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004080
Paul Elliottf1277632021-08-24 18:11:37 +01004081 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01004082
Paul Elliott88ecbe12021-09-22 17:23:03 +01004083 if( expected_status == PSA_SUCCESS )
4084 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
4085 alg ) );
4086
Paul Elliottd79c5c52021-10-06 21:49:41 +01004087 TEST_ASSERT( actual_nonce_length <= PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01004088
Paul Elliott693bf312021-07-23 17:40:41 +01004089 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004090 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004091 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01004092 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4093 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004094
4095 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4096 additional_data->len ) );
4097
4098 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01004099 output, output_size,
4100 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004101
Paul Elliottf1277632021-08-24 18:11:37 +01004102 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4103 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004104 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4105 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004106
4107exit:
4108 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01004109 mbedtls_free( output );
4110 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004111 psa_aead_abort( &operation );
4112 PSA_DONE( );
4113}
4114/* END_CASE */
4115
4116/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01004117void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
4118 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01004119 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004120 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01004121 data_t *additional_data,
4122 data_t *input_data,
4123 int expected_status_arg )
4124{
4125
4126 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4127 psa_key_type_t key_type = key_type_arg;
4128 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004129 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01004130 uint8_t *nonce_buffer = NULL;
4131 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4132 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4133 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004134 unsigned char *output = NULL;
4135 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01004136 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01004137 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004138 size_t ciphertext_size = 0;
4139 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01004140 size_t tag_length = 0;
4141 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01004142 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004143 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01004144
4145 PSA_ASSERT( psa_crypto_init( ) );
4146
4147 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4148 psa_set_key_algorithm( &attributes, alg );
4149 psa_set_key_type( &attributes, key_type );
4150
4151 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4152 &key ) );
4153
4154 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4155
4156 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4157
Paul Elliott6f0e7202021-08-25 12:57:18 +01004158 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004159
Paul Elliott6f0e7202021-08-25 12:57:18 +01004160 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01004161
Paul Elliott6f0e7202021-08-25 12:57:18 +01004162 TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01004163
Paul Elliott6f0e7202021-08-25 12:57:18 +01004164 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004165
Paul Elliott863864a2021-07-23 17:28:31 +01004166 status = psa_aead_encrypt_setup( &operation, key, alg );
4167
4168 /* If the operation is not supported, just skip and not fail in case the
4169 * encryption involves a common limitation of cryptography hardwares and
4170 * an alternative implementation. */
4171 if( status == PSA_ERROR_NOT_SUPPORTED )
4172 {
4173 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004174 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01004175 }
4176
4177 PSA_ASSERT( status );
4178
Paul Elliott4023ffd2021-09-10 16:21:22 +01004179 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
4180 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01004181 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01004182 /* Arbitrary size buffer, to test zero length valid buffer. */
4183 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004184 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01004185 }
4186 else
4187 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004188 /* If length is zero, then this will return NULL. */
4189 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004190 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01004191
Paul Elliott4023ffd2021-09-10 16:21:22 +01004192 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01004193 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004194 for( index = 0; index < nonce_length - 1; ++index )
4195 {
4196 nonce_buffer[index] = 'a' + index;
4197 }
Paul Elliott66696b52021-08-16 18:42:41 +01004198 }
Paul Elliott863864a2021-07-23 17:28:31 +01004199 }
4200
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004201 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
4202 {
4203 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4204 input_data->len ) );
4205 }
4206
Paul Elliott6f0e7202021-08-25 12:57:18 +01004207 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01004208
Paul Elliott693bf312021-07-23 17:40:41 +01004209 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004210
4211 if( expected_status == PSA_SUCCESS )
4212 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004213 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
4214 {
4215 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4216 input_data->len ) );
4217 }
4218 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
4219 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01004220
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004221 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
4222 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4223 additional_data->len ),
4224 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004225
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004226 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004227 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004228 &ciphertext_length ),
4229 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004230
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004231 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004232 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004233 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
4234 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004235 }
4236
4237exit:
4238 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01004239 mbedtls_free( output );
4240 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01004241 mbedtls_free( nonce_buffer );
4242 psa_aead_abort( &operation );
4243 PSA_DONE( );
4244}
4245/* END_CASE */
4246
4247/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004248void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
4249 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004250 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01004251 data_t *nonce,
4252 data_t *additional_data,
4253 data_t *input_data,
4254 int expected_status_arg )
4255{
4256
4257 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4258 psa_key_type_t key_type = key_type_arg;
4259 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004260 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01004261 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4262 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4263 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01004264 unsigned char *output = NULL;
4265 unsigned char *ciphertext = NULL;
4266 size_t output_size = output_size_arg;
4267 size_t ciphertext_size = 0;
4268 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01004269 size_t tag_length = 0;
4270 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4271
4272 PSA_ASSERT( psa_crypto_init( ) );
4273
4274 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4275 psa_set_key_algorithm( &attributes, alg );
4276 psa_set_key_type( &attributes, key_type );
4277
4278 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4279 &key ) );
4280
4281 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4282
Paul Elliottc6d11d02021-09-01 12:04:23 +01004283 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004284
Paul Elliottc6d11d02021-09-01 12:04:23 +01004285 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01004286
Paul Elliottc6d11d02021-09-01 12:04:23 +01004287 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004288
Paul Elliott43fbda62021-07-23 18:30:59 +01004289 status = psa_aead_encrypt_setup( &operation, key, alg );
4290
4291 /* If the operation is not supported, just skip and not fail in case the
4292 * encryption involves a common limitation of cryptography hardwares and
4293 * an alternative implementation. */
4294 if( status == PSA_ERROR_NOT_SUPPORTED )
4295 {
4296 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4297 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4298 }
4299
4300 PSA_ASSERT( status );
4301
Paul Elliott47b9a142021-10-07 15:04:57 +01004302 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4303 input_data->len ) );
4304
Paul Elliott43fbda62021-07-23 18:30:59 +01004305 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4306
4307 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4308 additional_data->len ) );
4309
4310 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004311 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01004312
4313 TEST_EQUAL( status, expected_status );
4314
4315 if( expected_status == PSA_SUCCESS )
4316 {
4317 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01004318 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4319 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01004320 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4321 }
4322
4323exit:
4324 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01004325 mbedtls_free( output );
4326 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01004327 psa_aead_abort( &operation );
4328 PSA_DONE( );
4329}
4330/* END_CASE */
4331
Paul Elliott91b021e2021-07-23 18:52:31 +01004332/* BEGIN_CASE */
4333void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
4334 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004335 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01004336 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01004337 data_t *nonce,
4338 data_t *additional_data,
4339 data_t *input_data,
4340 int expected_status_arg )
4341{
4342
4343 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4344 psa_key_type_t key_type = key_type_arg;
4345 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004346 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01004347 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4348 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4349 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004350 unsigned char *ciphertext = NULL;
4351 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004352 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004353 size_t ciphertext_size = 0;
4354 size_t ciphertext_length = 0;
4355 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004356 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004357 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004358
4359 PSA_ASSERT( psa_crypto_init( ) );
4360
4361 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4362 psa_set_key_algorithm( &attributes, alg );
4363 psa_set_key_type( &attributes, key_type );
4364
4365 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4366 &key ) );
4367
4368 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4369
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004370 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004371
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004372 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004373
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004374 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004375
Paul Elliott719c1322021-09-13 18:27:22 +01004376 ASSERT_ALLOC( tag_buffer, tag_size );
4377
Paul Elliott91b021e2021-07-23 18:52:31 +01004378 status = psa_aead_encrypt_setup( &operation, key, alg );
4379
4380 /* If the operation is not supported, just skip and not fail in case the
4381 * encryption involves a common limitation of cryptography hardwares and
4382 * an alternative implementation. */
4383 if( status == PSA_ERROR_NOT_SUPPORTED )
4384 {
4385 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4386 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4387 }
4388
4389 PSA_ASSERT( status );
4390
4391 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4392
Paul Elliott76bda482021-10-07 17:07:23 +01004393 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4394 input_data->len ) );
4395
Paul Elliott91b021e2021-07-23 18:52:31 +01004396 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4397 additional_data->len ) );
4398
4399 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004400 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004401
4402 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004403 status = psa_aead_finish( &operation, finish_ciphertext,
4404 finish_ciphertext_size,
4405 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004406 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004407
4408 TEST_EQUAL( status, expected_status );
4409
4410exit:
4411 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004412 mbedtls_free( ciphertext );
4413 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004414 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004415 psa_aead_abort( &operation );
4416 PSA_DONE( );
4417}
4418/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004419
4420/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004421void aead_multipart_verify( int key_type_arg, data_t *key_data,
4422 int alg_arg,
4423 data_t *nonce,
4424 data_t *additional_data,
4425 data_t *input_data,
4426 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004427 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01004428 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004429 int expected_status_arg )
4430{
4431 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4432 psa_key_type_t key_type = key_type_arg;
4433 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004434 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01004435 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4436 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4437 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004438 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01004439 unsigned char *plaintext = NULL;
4440 unsigned char *finish_plaintext = NULL;
4441 size_t plaintext_size = 0;
4442 size_t plaintext_length = 0;
4443 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004444 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004445 unsigned char *tag_buffer = NULL;
4446 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004447
4448 PSA_ASSERT( psa_crypto_init( ) );
4449
4450 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4451 psa_set_key_algorithm( &attributes, alg );
4452 psa_set_key_type( &attributes, key_type );
4453
4454 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4455 &key ) );
4456
4457 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4458
4459 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4460 input_data->len );
4461
4462 ASSERT_ALLOC( plaintext, plaintext_size );
4463
4464 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4465
4466 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4467
Paul Elliott9961a662021-09-17 19:19:02 +01004468 status = psa_aead_decrypt_setup( &operation, key, alg );
4469
4470 /* If the operation is not supported, just skip and not fail in case the
4471 * encryption involves a common limitation of cryptography hardwares and
4472 * an alternative implementation. */
4473 if( status == PSA_ERROR_NOT_SUPPORTED )
4474 {
4475 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4476 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4477 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01004478 TEST_EQUAL( status, expected_setup_status );
4479
4480 if( status != PSA_SUCCESS )
4481 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01004482
4483 PSA_ASSERT( status );
4484
4485 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4486
Paul Elliottfec6f372021-10-06 17:15:02 +01004487 status = psa_aead_set_lengths( &operation, additional_data->len,
4488 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01004489 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01004490
Paul Elliott9961a662021-09-17 19:19:02 +01004491 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4492 additional_data->len ) );
4493
4494 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
4495 input_data->len,
4496 plaintext, plaintext_size,
4497 &plaintext_length ) );
4498
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004499 if( tag_usage == USE_GIVEN_TAG )
4500 {
4501 tag_buffer = tag->x;
4502 tag_size = tag->len;
4503 }
4504
Paul Elliott9961a662021-09-17 19:19:02 +01004505 status = psa_aead_verify( &operation, finish_plaintext,
4506 verify_plaintext_size,
4507 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004508 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01004509
4510 TEST_EQUAL( status, expected_status );
4511
4512exit:
4513 psa_destroy_key( key );
4514 mbedtls_free( plaintext );
4515 mbedtls_free( finish_plaintext );
4516 psa_aead_abort( &operation );
4517 PSA_DONE( );
4518}
4519/* END_CASE */
4520
Paul Elliott9961a662021-09-17 19:19:02 +01004521/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01004522void aead_multipart_setup( int key_type_arg, data_t *key_data,
4523 int alg_arg, int expected_status_arg )
4524{
4525 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4526 psa_key_type_t key_type = key_type_arg;
4527 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004528 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01004529 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4530 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4531 psa_status_t expected_status = expected_status_arg;
4532
4533 PSA_ASSERT( psa_crypto_init( ) );
4534
4535 psa_set_key_usage_flags( &attributes,
4536 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4537 psa_set_key_algorithm( &attributes, alg );
4538 psa_set_key_type( &attributes, key_type );
4539
4540 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4541 &key ) );
4542
Paul Elliott5221ef62021-09-19 17:33:03 +01004543 status = psa_aead_encrypt_setup( &operation, key, alg );
4544
4545 TEST_EQUAL( status, expected_status );
4546
4547 psa_aead_abort( &operation );
4548
Paul Elliott5221ef62021-09-19 17:33:03 +01004549 status = psa_aead_decrypt_setup( &operation, key, alg );
4550
4551 TEST_EQUAL(status, expected_status );
4552
4553exit:
4554 psa_destroy_key( key );
4555 psa_aead_abort( &operation );
4556 PSA_DONE( );
4557}
4558/* END_CASE */
4559
4560/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01004561void aead_multipart_state_test( int key_type_arg, data_t *key_data,
4562 int alg_arg,
4563 data_t *nonce,
4564 data_t *additional_data,
4565 data_t *input_data )
4566{
4567 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4568 psa_key_type_t key_type = key_type_arg;
4569 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004570 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01004571 unsigned char *output_data = NULL;
4572 unsigned char *final_data = NULL;
4573 size_t output_size = 0;
4574 size_t finish_output_size = 0;
4575 size_t output_length = 0;
4576 size_t key_bits = 0;
4577 size_t tag_length = 0;
4578 size_t tag_size = 0;
4579 size_t nonce_length = 0;
4580 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4581 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4582 size_t output_part_length = 0;
4583 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4584
4585 PSA_ASSERT( psa_crypto_init( ) );
4586
4587 psa_set_key_usage_flags( & attributes,
4588 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4589 psa_set_key_algorithm( & attributes, alg );
4590 psa_set_key_type( & attributes, key_type );
4591
4592 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4593 &key ) );
4594
4595 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4596 key_bits = psa_get_key_bits( &attributes );
4597
4598 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
4599
4600 TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE );
4601
4602 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4603
4604 ASSERT_ALLOC( output_data, output_size );
4605
4606 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
4607
4608 TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
4609
4610 ASSERT_ALLOC( final_data, finish_output_size );
4611
4612 /* Test all operations error without calling setup first. */
4613
Paul Elliottc23a9a02021-06-21 18:32:46 +01004614 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4615 PSA_ERROR_BAD_STATE );
4616
4617 psa_aead_abort( &operation );
4618
Paul Elliottc23a9a02021-06-21 18:32:46 +01004619 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4620 PSA_AEAD_NONCE_MAX_SIZE,
4621 &nonce_length ),
4622 PSA_ERROR_BAD_STATE );
4623
4624 psa_aead_abort( &operation );
4625
Paul Elliott481be342021-07-16 17:38:47 +01004626 /* ------------------------------------------------------- */
4627
Paul Elliottc23a9a02021-06-21 18:32:46 +01004628 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4629 input_data->len ),
4630 PSA_ERROR_BAD_STATE );
4631
4632 psa_aead_abort( &operation );
4633
Paul Elliott481be342021-07-16 17:38:47 +01004634 /* ------------------------------------------------------- */
4635
Paul Elliottc23a9a02021-06-21 18:32:46 +01004636 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4637 additional_data->len ),
4638 PSA_ERROR_BAD_STATE );
4639
4640 psa_aead_abort( &operation );
4641
Paul Elliott481be342021-07-16 17:38:47 +01004642 /* ------------------------------------------------------- */
4643
Paul Elliottc23a9a02021-06-21 18:32:46 +01004644 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4645 input_data->len, output_data,
4646 output_size, &output_length ),
4647 PSA_ERROR_BAD_STATE );
4648
4649 psa_aead_abort( &operation );
4650
Paul Elliott481be342021-07-16 17:38:47 +01004651 /* ------------------------------------------------------- */
4652
Paul Elliottc23a9a02021-06-21 18:32:46 +01004653 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4654 finish_output_size,
4655 &output_part_length,
4656 tag_buffer, tag_length,
4657 &tag_size ),
4658 PSA_ERROR_BAD_STATE );
4659
4660 psa_aead_abort( &operation );
4661
Paul Elliott481be342021-07-16 17:38:47 +01004662 /* ------------------------------------------------------- */
4663
Paul Elliottc23a9a02021-06-21 18:32:46 +01004664 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4665 finish_output_size,
4666 &output_part_length,
4667 tag_buffer,
4668 tag_length ),
4669 PSA_ERROR_BAD_STATE );
4670
4671 psa_aead_abort( &operation );
4672
4673 /* Test for double setups. */
4674
Paul Elliottc23a9a02021-06-21 18:32:46 +01004675 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4676
4677 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4678 PSA_ERROR_BAD_STATE );
4679
4680 psa_aead_abort( &operation );
4681
Paul Elliott481be342021-07-16 17:38:47 +01004682 /* ------------------------------------------------------- */
4683
Paul Elliottc23a9a02021-06-21 18:32:46 +01004684 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4685
4686 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4687 PSA_ERROR_BAD_STATE );
4688
4689 psa_aead_abort( &operation );
4690
Paul Elliott374a2be2021-07-16 17:53:40 +01004691 /* ------------------------------------------------------- */
4692
Paul Elliott374a2be2021-07-16 17:53:40 +01004693 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4694
4695 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
4696 PSA_ERROR_BAD_STATE );
4697
4698 psa_aead_abort( &operation );
4699
4700 /* ------------------------------------------------------- */
4701
Paul Elliott374a2be2021-07-16 17:53:40 +01004702 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4703
4704 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
4705 PSA_ERROR_BAD_STATE );
4706
4707 psa_aead_abort( &operation );
4708
Paul Elliottc23a9a02021-06-21 18:32:46 +01004709 /* Test for not setting a nonce. */
4710
Paul Elliottc23a9a02021-06-21 18:32:46 +01004711 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4712
4713 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4714 additional_data->len ),
4715 PSA_ERROR_BAD_STATE );
4716
4717 psa_aead_abort( &operation );
4718
Paul Elliott7f628422021-09-01 12:08:29 +01004719 /* ------------------------------------------------------- */
4720
Paul Elliott7f628422021-09-01 12:08:29 +01004721 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4722
4723 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
4724 input_data->len, output_data,
4725 output_size, &output_length ),
4726 PSA_ERROR_BAD_STATE );
4727
4728 psa_aead_abort( &operation );
4729
Paul Elliottbdc2c682021-09-21 18:37:10 +01004730 /* ------------------------------------------------------- */
4731
Paul Elliottbdc2c682021-09-21 18:37:10 +01004732 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4733
4734 TEST_EQUAL( psa_aead_finish( &operation, final_data,
4735 finish_output_size,
4736 &output_part_length,
4737 tag_buffer, tag_length,
4738 &tag_size ),
4739 PSA_ERROR_BAD_STATE );
4740
4741 psa_aead_abort( &operation );
4742
4743 /* ------------------------------------------------------- */
4744
Paul Elliottbdc2c682021-09-21 18:37:10 +01004745 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
4746
4747 TEST_EQUAL( psa_aead_verify( &operation, final_data,
4748 finish_output_size,
4749 &output_part_length,
4750 tag_buffer,
4751 tag_length ),
4752 PSA_ERROR_BAD_STATE );
4753
4754 psa_aead_abort( &operation );
4755
Paul Elliottc23a9a02021-06-21 18:32:46 +01004756 /* Test for double setting nonce. */
4757
Paul Elliottc23a9a02021-06-21 18:32:46 +01004758 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4759
4760 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4761
4762 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4763 PSA_ERROR_BAD_STATE );
4764
4765 psa_aead_abort( &operation );
4766
Paul Elliott374a2be2021-07-16 17:53:40 +01004767 /* Test for double generating nonce. */
4768
Paul Elliott374a2be2021-07-16 17:53:40 +01004769 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4770
4771 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4772 PSA_AEAD_NONCE_MAX_SIZE,
4773 &nonce_length ) );
4774
4775 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4776 PSA_AEAD_NONCE_MAX_SIZE,
4777 &nonce_length ),
4778 PSA_ERROR_BAD_STATE );
4779
4780
4781 psa_aead_abort( &operation );
4782
4783 /* Test for generate nonce then set and vice versa */
4784
Paul Elliott374a2be2021-07-16 17:53:40 +01004785 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4786
4787 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4788 PSA_AEAD_NONCE_MAX_SIZE,
4789 &nonce_length ) );
4790
4791 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4792 PSA_ERROR_BAD_STATE );
4793
4794 psa_aead_abort( &operation );
4795
Andrzej Kurekad837522021-12-15 15:28:49 +01004796 /* Test for generating nonce after calling set lengths */
4797
4798 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4799
4800 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4801 input_data->len ) );
4802
4803 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4804 PSA_AEAD_NONCE_MAX_SIZE,
4805 &nonce_length ) );
4806
4807 psa_aead_abort( &operation );
4808
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004809 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01004810
4811 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4812
4813 if( operation.alg == PSA_ALG_CCM )
4814 {
4815 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
4816 input_data->len ),
4817 PSA_ERROR_INVALID_ARGUMENT );
4818 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4819 PSA_AEAD_NONCE_MAX_SIZE,
4820 &nonce_length ),
4821 PSA_ERROR_BAD_STATE );
4822 }
4823 else
4824 {
4825 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
4826 input_data->len ) );
4827 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4828 PSA_AEAD_NONCE_MAX_SIZE,
4829 &nonce_length ) );
4830 }
4831
4832 psa_aead_abort( &operation );
4833
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004834 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01004835#if SIZE_MAX > UINT32_MAX
4836 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4837
4838 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
4839 {
4840 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
4841 input_data->len ),
4842 PSA_ERROR_INVALID_ARGUMENT );
4843 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
4844 PSA_AEAD_NONCE_MAX_SIZE,
4845 &nonce_length ),
4846 PSA_ERROR_BAD_STATE );
4847 }
4848 else
4849 {
4850 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
4851 input_data->len ) );
4852 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4853 PSA_AEAD_NONCE_MAX_SIZE,
4854 &nonce_length ) );
4855 }
4856
4857 psa_aead_abort( &operation );
4858#endif
4859
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004860 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01004861
4862 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4863
4864 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
4865 PSA_AEAD_NONCE_MAX_SIZE,
4866 &nonce_length ) );
4867
4868 if( operation.alg == PSA_ALG_CCM )
4869 {
4870 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
4871 input_data->len ),
4872 PSA_ERROR_INVALID_ARGUMENT );
4873 }
4874 else
4875 {
4876 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
4877 input_data->len ) );
4878 }
4879
4880 psa_aead_abort( &operation );
4881
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01004882 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01004883 /* Test for setting nonce after calling set lengths */
4884
4885 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4886
4887 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4888 input_data->len ) );
4889
4890 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4891
4892 psa_aead_abort( &operation );
4893
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01004894 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01004895
4896 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4897
4898 if( operation.alg == PSA_ALG_CCM )
4899 {
4900 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
4901 input_data->len ),
4902 PSA_ERROR_INVALID_ARGUMENT );
4903 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4904 PSA_ERROR_BAD_STATE );
4905 }
4906 else
4907 {
4908 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
4909 input_data->len ) );
4910 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4911 }
4912
4913 psa_aead_abort( &operation );
4914
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01004915 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01004916#if SIZE_MAX > UINT32_MAX
4917 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4918
4919 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
4920 {
4921 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
4922 input_data->len ),
4923 PSA_ERROR_INVALID_ARGUMENT );
4924 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4925 PSA_ERROR_BAD_STATE );
4926 }
4927 else
4928 {
4929 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
4930 input_data->len ) );
4931 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4932 }
4933
4934 psa_aead_abort( &operation );
4935#endif
4936
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004937 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01004938
4939 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4940
4941 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4942
4943 if( operation.alg == PSA_ALG_CCM )
4944 {
4945 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
4946 input_data->len ),
4947 PSA_ERROR_INVALID_ARGUMENT );
4948 }
4949 else
4950 {
4951 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
4952 input_data->len ) );
4953 }
4954
4955 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01004956
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004957 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01004958#if SIZE_MAX > UINT32_MAX
4959 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4960
4961 if( operation.alg == PSA_ALG_GCM )
4962 {
4963 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4964 SIZE_MAX ),
4965 PSA_ERROR_INVALID_ARGUMENT );
4966 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
4967 PSA_ERROR_BAD_STATE );
4968 }
4969 else if ( operation.alg != PSA_ALG_CCM )
4970 {
4971 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4972 SIZE_MAX ) );
4973 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4974 }
4975
4976 psa_aead_abort( &operation );
4977
Andrzej Kurek031df4a2022-01-19 12:44:49 -05004978 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01004979 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
4980
4981 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4982
4983 if( operation.alg == PSA_ALG_GCM )
4984 {
4985 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
4986 SIZE_MAX ),
4987 PSA_ERROR_INVALID_ARGUMENT );
4988 }
4989 else if ( operation.alg != PSA_ALG_CCM )
4990 {
4991 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4992 SIZE_MAX ) );
4993 }
4994
4995 psa_aead_abort( &operation );
4996#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01004997
4998 /* ------------------------------------------------------- */
4999
Paul Elliott374a2be2021-07-16 17:53:40 +01005000 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5001
5002 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5003
5004 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5005 PSA_AEAD_NONCE_MAX_SIZE,
5006 &nonce_length ),
5007 PSA_ERROR_BAD_STATE );
5008
5009 psa_aead_abort( &operation );
5010
Paul Elliott7220cae2021-06-22 17:25:57 +01005011 /* Test for generating nonce in decrypt setup. */
5012
Paul Elliott7220cae2021-06-22 17:25:57 +01005013 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5014
5015 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5016 PSA_AEAD_NONCE_MAX_SIZE,
5017 &nonce_length ),
5018 PSA_ERROR_BAD_STATE );
5019
5020 psa_aead_abort( &operation );
5021
Paul Elliottc23a9a02021-06-21 18:32:46 +01005022 /* Test for setting lengths twice. */
5023
Paul Elliottc23a9a02021-06-21 18:32:46 +01005024 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5025
5026 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5027
5028 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5029 input_data->len ) );
5030
5031 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5032 input_data->len ),
5033 PSA_ERROR_BAD_STATE );
5034
5035 psa_aead_abort( &operation );
5036
Andrzej Kurekad837522021-12-15 15:28:49 +01005037 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005038
Paul Elliottc23a9a02021-06-21 18:32:46 +01005039 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5040
5041 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5042
Andrzej Kurekad837522021-12-15 15:28:49 +01005043 if( operation.alg == PSA_ALG_CCM )
5044 {
Paul Elliottf94bd992021-09-19 18:15:59 +01005045
Andrzej Kurekad837522021-12-15 15:28:49 +01005046 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5047 additional_data->len ),
5048 PSA_ERROR_BAD_STATE );
5049 }
5050 else
5051 {
5052 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5053 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01005054
Andrzej Kurekad837522021-12-15 15:28:49 +01005055 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5056 input_data->len ),
5057 PSA_ERROR_BAD_STATE );
5058 }
Paul Elliottf94bd992021-09-19 18:15:59 +01005059 psa_aead_abort( &operation );
5060
5061 /* ------------------------------------------------------- */
5062
Paul Elliottf94bd992021-09-19 18:15:59 +01005063 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5064
5065 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5066
Andrzej Kurekad837522021-12-15 15:28:49 +01005067 if( operation.alg == PSA_ALG_CCM )
5068 {
5069 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5070 input_data->len, output_data,
5071 output_size, &output_length ),
5072 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005073
Andrzej Kurekad837522021-12-15 15:28:49 +01005074 }
5075 else
5076 {
5077 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5078 input_data->len, output_data,
5079 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005080
Andrzej Kurekad837522021-12-15 15:28:49 +01005081 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5082 input_data->len ),
5083 PSA_ERROR_BAD_STATE );
5084 }
5085 psa_aead_abort( &operation );
5086
5087 /* ------------------------------------------------------- */
5088
5089 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5090
5091 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5092
5093 if( operation.alg == PSA_ALG_CCM )
5094 {
5095 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5096 finish_output_size,
5097 &output_part_length,
5098 tag_buffer, tag_length,
5099 &tag_size ) );
5100 }
5101 else
5102 {
5103 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5104 finish_output_size,
5105 &output_part_length,
5106 tag_buffer, tag_length,
5107 &tag_size ) );
5108
5109 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5110 input_data->len ),
5111 PSA_ERROR_BAD_STATE );
5112 }
5113 psa_aead_abort( &operation );
5114
5115 /* Test for setting lengths after generating nonce + already starting data. */
5116
5117 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5118
5119 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5120 PSA_AEAD_NONCE_MAX_SIZE,
5121 &nonce_length ) );
5122 if( operation.alg == PSA_ALG_CCM )
5123 {
5124
5125 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5126 additional_data->len ),
5127 PSA_ERROR_BAD_STATE );
5128 }
5129 else
5130 {
5131 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5132 additional_data->len ) );
5133
5134 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5135 input_data->len ),
5136 PSA_ERROR_BAD_STATE );
5137 }
5138 psa_aead_abort( &operation );
5139
5140 /* ------------------------------------------------------- */
5141
5142 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5143
5144 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5145 PSA_AEAD_NONCE_MAX_SIZE,
5146 &nonce_length ) );
5147 if( operation.alg == PSA_ALG_CCM )
5148 {
5149 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5150 input_data->len, output_data,
5151 output_size, &output_length ),
5152 PSA_ERROR_BAD_STATE );
5153
5154 }
5155 else
5156 {
5157 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5158 input_data->len, output_data,
5159 output_size, &output_length ) );
5160
5161 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5162 input_data->len ),
5163 PSA_ERROR_BAD_STATE );
5164 }
5165 psa_aead_abort( &operation );
5166
5167 /* ------------------------------------------------------- */
5168
5169 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5170
5171 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5172 PSA_AEAD_NONCE_MAX_SIZE,
5173 &nonce_length ) );
5174 if( operation.alg == PSA_ALG_CCM )
5175 {
5176 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5177 finish_output_size,
5178 &output_part_length,
5179 tag_buffer, tag_length,
5180 &tag_size ) );
5181 }
5182 else
5183 {
5184 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5185 finish_output_size,
5186 &output_part_length,
5187 tag_buffer, tag_length,
5188 &tag_size ) );
5189
5190 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5191 input_data->len ),
5192 PSA_ERROR_BAD_STATE );
5193 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005194 psa_aead_abort( &operation );
5195
Paul Elliott243080c2021-07-21 19:01:17 +01005196 /* Test for not sending any additional data or data after setting non zero
5197 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005198
Paul Elliottc23a9a02021-06-21 18:32:46 +01005199 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5200
5201 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5202
5203 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5204 input_data->len ) );
5205
5206 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5207 finish_output_size,
5208 &output_part_length,
5209 tag_buffer, tag_length,
5210 &tag_size ),
5211 PSA_ERROR_INVALID_ARGUMENT );
5212
5213 psa_aead_abort( &operation );
5214
Paul Elliott243080c2021-07-21 19:01:17 +01005215 /* Test for not sending any additional data or data after setting non-zero
5216 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005217
Paul Elliottc23a9a02021-06-21 18:32:46 +01005218 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5219
5220 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5221
5222 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5223 input_data->len ) );
5224
5225 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5226 finish_output_size,
5227 &output_part_length,
5228 tag_buffer,
5229 tag_length ),
5230 PSA_ERROR_INVALID_ARGUMENT );
5231
5232 psa_aead_abort( &operation );
5233
Paul Elliott243080c2021-07-21 19:01:17 +01005234 /* Test for not sending any additional data after setting a non-zero length
5235 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005236
Paul Elliottc23a9a02021-06-21 18:32:46 +01005237 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5238
5239 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5240
5241 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5242 input_data->len ) );
5243
5244 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5245 input_data->len, output_data,
5246 output_size, &output_length ),
5247 PSA_ERROR_INVALID_ARGUMENT );
5248
5249 psa_aead_abort( &operation );
5250
Paul Elliottf94bd992021-09-19 18:15:59 +01005251 /* Test for not sending any data after setting a non-zero length for it.*/
5252
Paul Elliottf94bd992021-09-19 18:15:59 +01005253 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5254
5255 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5256
5257 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5258 input_data->len ) );
5259
5260 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5261 additional_data->len ) );
5262
5263 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5264 finish_output_size,
5265 &output_part_length,
5266 tag_buffer, tag_length,
5267 &tag_size ),
5268 PSA_ERROR_INVALID_ARGUMENT );
5269
5270 psa_aead_abort( &operation );
5271
Paul Elliottb0450fe2021-09-01 15:06:26 +01005272 /* Test for sending too much additional data after setting lengths. */
5273
Paul Elliottb0450fe2021-09-01 15:06:26 +01005274 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5275
5276 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5277
5278 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5279
5280
5281 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5282 additional_data->len ),
5283 PSA_ERROR_INVALID_ARGUMENT );
5284
5285 psa_aead_abort( &operation );
5286
Paul Elliotta2a09b02021-09-22 14:56:40 +01005287 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005288
5289 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5290
5291 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5292
5293 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5294 input_data->len ) );
5295
5296 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5297 additional_data->len ) );
5298
5299 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5300 1 ),
5301 PSA_ERROR_INVALID_ARGUMENT );
5302
5303 psa_aead_abort( &operation );
5304
Paul Elliottb0450fe2021-09-01 15:06:26 +01005305 /* Test for sending too much data after setting lengths. */
5306
Paul Elliottb0450fe2021-09-01 15:06:26 +01005307 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5308
5309 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5310
5311 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5312
5313 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5314 input_data->len, output_data,
5315 output_size, &output_length ),
5316 PSA_ERROR_INVALID_ARGUMENT );
5317
5318 psa_aead_abort( &operation );
5319
Paul Elliotta2a09b02021-09-22 14:56:40 +01005320 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005321
5322 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5323
5324 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5325
5326 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5327 input_data->len ) );
5328
5329 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5330 additional_data->len ) );
5331
5332 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5333 input_data->len, output_data,
5334 output_size, &output_length ) );
5335
5336 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5337 1, output_data,
5338 output_size, &output_length ),
5339 PSA_ERROR_INVALID_ARGUMENT );
5340
5341 psa_aead_abort( &operation );
5342
Paul Elliottc23a9a02021-06-21 18:32:46 +01005343 /* Test sending additional data after data. */
5344
Paul Elliottc23a9a02021-06-21 18:32:46 +01005345 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5346
5347 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5348
Andrzej Kurekad837522021-12-15 15:28:49 +01005349 if( operation.alg != PSA_ALG_CCM )
5350 {
5351 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5352 input_data->len, output_data,
5353 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005354
Andrzej Kurekad837522021-12-15 15:28:49 +01005355 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5356 additional_data->len ),
5357 PSA_ERROR_BAD_STATE );
5358 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005359 psa_aead_abort( &operation );
5360
Paul Elliott534d0b42021-06-22 19:15:20 +01005361 /* Test calling finish on decryption. */
5362
Paul Elliott534d0b42021-06-22 19:15:20 +01005363 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5364
5365 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5366
5367 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5368 finish_output_size,
5369 &output_part_length,
5370 tag_buffer, tag_length,
5371 &tag_size ),
5372 PSA_ERROR_BAD_STATE );
5373
5374 psa_aead_abort( &operation );
5375
5376 /* Test calling verify on encryption. */
5377
Paul Elliott534d0b42021-06-22 19:15:20 +01005378 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5379
5380 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5381
5382 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5383 finish_output_size,
5384 &output_part_length,
5385 tag_buffer,
5386 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01005387 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01005388
5389 psa_aead_abort( &operation );
5390
5391
Paul Elliottc23a9a02021-06-21 18:32:46 +01005392exit:
5393 psa_destroy_key( key );
5394 psa_aead_abort( &operation );
5395 mbedtls_free( output_data );
5396 mbedtls_free( final_data );
5397 PSA_DONE( );
5398}
5399/* END_CASE */
5400
5401/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005402void signature_size( int type_arg,
5403 int bits,
5404 int alg_arg,
5405 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005406{
5407 psa_key_type_t type = type_arg;
5408 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005409 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005410
Gilles Peskinefe11b722018-12-18 00:24:04 +01005411 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005412
Gilles Peskinee59236f2018-01-27 23:32:46 +01005413exit:
5414 ;
5415}
5416/* END_CASE */
5417
5418/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005419void sign_hash_deterministic( int key_type_arg, data_t *key_data,
5420 int alg_arg, data_t *input_data,
5421 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005422{
Ronald Cron5425a212020-08-04 14:58:35 +02005423 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005424 psa_key_type_t key_type = key_type_arg;
5425 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005426 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01005427 unsigned char *signature = NULL;
5428 size_t signature_size;
5429 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005430 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005431
Gilles Peskine8817f612018-12-18 00:18:46 +01005432 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005433
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005434 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005435 psa_set_key_algorithm( &attributes, alg );
5436 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005437
Gilles Peskine049c7532019-05-15 20:22:09 +02005438 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005439 &key ) );
5440 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005441 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01005442
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005443 /* Allocate a buffer which has the size advertized by the
5444 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005445 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005446 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01005447 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005448 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005449 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005450
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005451 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005452 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005453 input_data->x, input_data->len,
5454 signature, signature_size,
5455 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005456 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005457 ASSERT_COMPARE( output_data->x, output_data->len,
5458 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01005459
5460exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005461 /*
5462 * Key attributes may have been returned by psa_get_key_attributes()
5463 * thus reset them as required.
5464 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005465 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005466
Ronald Cron5425a212020-08-04 14:58:35 +02005467 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01005468 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005469 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005470}
5471/* END_CASE */
5472
5473/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005474void sign_hash_fail( int key_type_arg, data_t *key_data,
5475 int alg_arg, data_t *input_data,
5476 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01005477{
Ronald Cron5425a212020-08-04 14:58:35 +02005478 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005479 psa_key_type_t key_type = key_type_arg;
5480 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005481 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005482 psa_status_t actual_status;
5483 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01005484 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01005485 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005486 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005487
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005488 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005489
Gilles Peskine8817f612018-12-18 00:18:46 +01005490 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005491
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005492 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005493 psa_set_key_algorithm( &attributes, alg );
5494 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005495
Gilles Peskine049c7532019-05-15 20:22:09 +02005496 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005497 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005498
Ronald Cron5425a212020-08-04 14:58:35 +02005499 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005500 input_data->x, input_data->len,
5501 signature, signature_size,
5502 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005503 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005504 /* The value of *signature_length is unspecified on error, but
5505 * whatever it is, it should be less than signature_size, so that
5506 * if the caller tries to read *signature_length bytes without
5507 * checking the error code then they don't overflow a buffer. */
5508 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005509
5510exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005511 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005512 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01005513 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005514 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005515}
5516/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03005517
5518/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005519void sign_verify_hash( int key_type_arg, data_t *key_data,
5520 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02005521{
Ronald Cron5425a212020-08-04 14:58:35 +02005522 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005523 psa_key_type_t key_type = key_type_arg;
5524 psa_algorithm_t alg = alg_arg;
5525 size_t key_bits;
5526 unsigned char *signature = NULL;
5527 size_t signature_size;
5528 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005529 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02005530
Gilles Peskine8817f612018-12-18 00:18:46 +01005531 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005532
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005533 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005534 psa_set_key_algorithm( &attributes, alg );
5535 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02005536
Gilles Peskine049c7532019-05-15 20:22:09 +02005537 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005538 &key ) );
5539 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005540 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02005541
5542 /* Allocate a buffer which has the size advertized by the
5543 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005544 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02005545 key_bits, alg );
5546 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005547 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005548 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02005549
5550 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005551 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005552 input_data->x, input_data->len,
5553 signature, signature_size,
5554 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005555 /* Check that the signature length looks sensible. */
5556 TEST_ASSERT( signature_length <= signature_size );
5557 TEST_ASSERT( signature_length > 0 );
5558
5559 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02005560 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005561 input_data->x, input_data->len,
5562 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005563
5564 if( input_data->len != 0 )
5565 {
5566 /* Flip a bit in the input and verify that the signature is now
5567 * detected as invalid. Flip a bit at the beginning, not at the end,
5568 * because ECDSA may ignore the last few bits of the input. */
5569 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02005570 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005571 input_data->x, input_data->len,
5572 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005573 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02005574 }
5575
5576exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005577 /*
5578 * Key attributes may have been returned by psa_get_key_attributes()
5579 * thus reset them as required.
5580 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005581 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005582
Ronald Cron5425a212020-08-04 14:58:35 +02005583 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02005584 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005585 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02005586}
5587/* END_CASE */
5588
5589/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005590void verify_hash( int key_type_arg, data_t *key_data,
5591 int alg_arg, data_t *hash_data,
5592 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03005593{
Ronald Cron5425a212020-08-04 14:58:35 +02005594 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005595 psa_key_type_t key_type = key_type_arg;
5596 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005597 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03005598
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005599 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02005600
Gilles Peskine8817f612018-12-18 00:18:46 +01005601 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03005602
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005603 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005604 psa_set_key_algorithm( &attributes, alg );
5605 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03005606
Gilles Peskine049c7532019-05-15 20:22:09 +02005607 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005608 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03005609
Ronald Cron5425a212020-08-04 14:58:35 +02005610 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005611 hash_data->x, hash_data->len,
5612 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01005613
itayzafrir5c753392018-05-08 11:18:38 +03005614exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005615 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005616 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005617 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03005618}
5619/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005620
5621/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005622void verify_hash_fail( int key_type_arg, data_t *key_data,
5623 int alg_arg, data_t *hash_data,
5624 data_t *signature_data,
5625 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005626{
Ronald Cron5425a212020-08-04 14:58:35 +02005627 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005628 psa_key_type_t key_type = key_type_arg;
5629 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005630 psa_status_t actual_status;
5631 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005632 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005633
Gilles Peskine8817f612018-12-18 00:18:46 +01005634 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005635
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005636 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005637 psa_set_key_algorithm( &attributes, alg );
5638 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005639
Gilles Peskine049c7532019-05-15 20:22:09 +02005640 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005641 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005642
Ronald Cron5425a212020-08-04 14:58:35 +02005643 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005644 hash_data->x, hash_data->len,
5645 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005646 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005647
5648exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005649 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02005650 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005651 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005652}
5653/* END_CASE */
5654
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005655/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02005656void sign_message_deterministic( int key_type_arg,
5657 data_t *key_data,
5658 int alg_arg,
5659 data_t *input_data,
5660 data_t *output_data )
5661{
5662 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5663 psa_key_type_t key_type = key_type_arg;
5664 psa_algorithm_t alg = alg_arg;
5665 size_t key_bits;
5666 unsigned char *signature = NULL;
5667 size_t signature_size;
5668 size_t signature_length = 0xdeadbeef;
5669 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5670
5671 PSA_ASSERT( psa_crypto_init( ) );
5672
5673 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5674 psa_set_key_algorithm( &attributes, alg );
5675 psa_set_key_type( &attributes, key_type );
5676
5677 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5678 &key ) );
5679 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5680 key_bits = psa_get_key_bits( &attributes );
5681
5682 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5683 TEST_ASSERT( signature_size != 0 );
5684 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5685 ASSERT_ALLOC( signature, signature_size );
5686
5687 PSA_ASSERT( psa_sign_message( key, alg,
5688 input_data->x, input_data->len,
5689 signature, signature_size,
5690 &signature_length ) );
5691
5692 ASSERT_COMPARE( output_data->x, output_data->len,
5693 signature, signature_length );
5694
5695exit:
5696 psa_reset_key_attributes( &attributes );
5697
5698 psa_destroy_key( key );
5699 mbedtls_free( signature );
5700 PSA_DONE( );
5701
5702}
5703/* END_CASE */
5704
5705/* BEGIN_CASE */
5706void sign_message_fail( int key_type_arg,
5707 data_t *key_data,
5708 int alg_arg,
5709 data_t *input_data,
5710 int signature_size_arg,
5711 int expected_status_arg )
5712{
5713 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5714 psa_key_type_t key_type = key_type_arg;
5715 psa_algorithm_t alg = alg_arg;
5716 size_t signature_size = signature_size_arg;
5717 psa_status_t actual_status;
5718 psa_status_t expected_status = expected_status_arg;
5719 unsigned char *signature = NULL;
5720 size_t signature_length = 0xdeadbeef;
5721 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5722
5723 ASSERT_ALLOC( signature, signature_size );
5724
5725 PSA_ASSERT( psa_crypto_init( ) );
5726
5727 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
5728 psa_set_key_algorithm( &attributes, alg );
5729 psa_set_key_type( &attributes, key_type );
5730
5731 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5732 &key ) );
5733
5734 actual_status = psa_sign_message( key, alg,
5735 input_data->x, input_data->len,
5736 signature, signature_size,
5737 &signature_length );
5738 TEST_EQUAL( actual_status, expected_status );
5739 /* The value of *signature_length is unspecified on error, but
5740 * whatever it is, it should be less than signature_size, so that
5741 * if the caller tries to read *signature_length bytes without
5742 * checking the error code then they don't overflow a buffer. */
5743 TEST_ASSERT( signature_length <= signature_size );
5744
5745exit:
5746 psa_reset_key_attributes( &attributes );
5747 psa_destroy_key( key );
5748 mbedtls_free( signature );
5749 PSA_DONE( );
5750}
5751/* END_CASE */
5752
5753/* BEGIN_CASE */
5754void sign_verify_message( int key_type_arg,
5755 data_t *key_data,
5756 int alg_arg,
5757 data_t *input_data )
5758{
5759 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5760 psa_key_type_t key_type = key_type_arg;
5761 psa_algorithm_t alg = alg_arg;
5762 size_t key_bits;
5763 unsigned char *signature = NULL;
5764 size_t signature_size;
5765 size_t signature_length = 0xdeadbeef;
5766 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5767
5768 PSA_ASSERT( psa_crypto_init( ) );
5769
5770 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
5771 PSA_KEY_USAGE_VERIFY_MESSAGE );
5772 psa_set_key_algorithm( &attributes, alg );
5773 psa_set_key_type( &attributes, key_type );
5774
5775 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5776 &key ) );
5777 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5778 key_bits = psa_get_key_bits( &attributes );
5779
5780 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
5781 TEST_ASSERT( signature_size != 0 );
5782 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
5783 ASSERT_ALLOC( signature, signature_size );
5784
5785 PSA_ASSERT( psa_sign_message( key, alg,
5786 input_data->x, input_data->len,
5787 signature, signature_size,
5788 &signature_length ) );
5789 TEST_ASSERT( signature_length <= signature_size );
5790 TEST_ASSERT( signature_length > 0 );
5791
5792 PSA_ASSERT( psa_verify_message( key, alg,
5793 input_data->x, input_data->len,
5794 signature, signature_length ) );
5795
5796 if( input_data->len != 0 )
5797 {
5798 /* Flip a bit in the input and verify that the signature is now
5799 * detected as invalid. Flip a bit at the beginning, not at the end,
5800 * because ECDSA may ignore the last few bits of the input. */
5801 input_data->x[0] ^= 1;
5802 TEST_EQUAL( psa_verify_message( key, alg,
5803 input_data->x, input_data->len,
5804 signature, signature_length ),
5805 PSA_ERROR_INVALID_SIGNATURE );
5806 }
5807
5808exit:
5809 psa_reset_key_attributes( &attributes );
5810
5811 psa_destroy_key( key );
5812 mbedtls_free( signature );
5813 PSA_DONE( );
5814}
5815/* END_CASE */
5816
5817/* BEGIN_CASE */
5818void verify_message( int key_type_arg,
5819 data_t *key_data,
5820 int alg_arg,
5821 data_t *input_data,
5822 data_t *signature_data )
5823{
5824 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5825 psa_key_type_t key_type = key_type_arg;
5826 psa_algorithm_t alg = alg_arg;
5827 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5828
5829 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
5830
5831 PSA_ASSERT( psa_crypto_init( ) );
5832
5833 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5834 psa_set_key_algorithm( &attributes, alg );
5835 psa_set_key_type( &attributes, key_type );
5836
5837 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5838 &key ) );
5839
5840 PSA_ASSERT( psa_verify_message( key, alg,
5841 input_data->x, input_data->len,
5842 signature_data->x, signature_data->len ) );
5843
5844exit:
5845 psa_reset_key_attributes( &attributes );
5846 psa_destroy_key( key );
5847 PSA_DONE( );
5848}
5849/* END_CASE */
5850
5851/* BEGIN_CASE */
5852void verify_message_fail( int key_type_arg,
5853 data_t *key_data,
5854 int alg_arg,
5855 data_t *hash_data,
5856 data_t *signature_data,
5857 int expected_status_arg )
5858{
5859 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5860 psa_key_type_t key_type = key_type_arg;
5861 psa_algorithm_t alg = alg_arg;
5862 psa_status_t actual_status;
5863 psa_status_t expected_status = expected_status_arg;
5864 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5865
5866 PSA_ASSERT( psa_crypto_init( ) );
5867
5868 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
5869 psa_set_key_algorithm( &attributes, alg );
5870 psa_set_key_type( &attributes, key_type );
5871
5872 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5873 &key ) );
5874
5875 actual_status = psa_verify_message( key, alg,
5876 hash_data->x, hash_data->len,
5877 signature_data->x,
5878 signature_data->len );
5879 TEST_EQUAL( actual_status, expected_status );
5880
5881exit:
5882 psa_reset_key_attributes( &attributes );
5883 psa_destroy_key( key );
5884 PSA_DONE( );
5885}
5886/* END_CASE */
5887
5888/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02005889void asymmetric_encrypt( int key_type_arg,
5890 data_t *key_data,
5891 int alg_arg,
5892 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02005893 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02005894 int expected_output_length_arg,
5895 int expected_status_arg )
5896{
Ronald Cron5425a212020-08-04 14:58:35 +02005897 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005898 psa_key_type_t key_type = key_type_arg;
5899 psa_algorithm_t alg = alg_arg;
5900 size_t expected_output_length = expected_output_length_arg;
5901 size_t key_bits;
5902 unsigned char *output = NULL;
5903 size_t output_size;
5904 size_t output_length = ~0;
5905 psa_status_t actual_status;
5906 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005907 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02005908
Gilles Peskine8817f612018-12-18 00:18:46 +01005909 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005910
Gilles Peskine656896e2018-06-29 19:12:28 +02005911 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005912 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5913 psa_set_key_algorithm( &attributes, alg );
5914 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005915 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005916 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02005917
5918 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02005919 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005920 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005921
Gilles Peskine656896e2018-06-29 19:12:28 +02005922 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005923 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005924 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02005925
5926 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02005927 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02005928 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02005929 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02005930 output, output_size,
5931 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005932 TEST_EQUAL( actual_status, expected_status );
5933 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02005934
Gilles Peskine68428122018-06-30 18:42:41 +02005935 /* If the label is empty, the test framework puts a non-null pointer
5936 * in label->x. Test that a null pointer works as well. */
5937 if( label->len == 0 )
5938 {
5939 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005940 if( output_size != 0 )
5941 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02005942 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02005943 input_data->x, input_data->len,
5944 NULL, label->len,
5945 output, output_size,
5946 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005947 TEST_EQUAL( actual_status, expected_status );
5948 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02005949 }
5950
Gilles Peskine656896e2018-06-29 19:12:28 +02005951exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005952 /*
5953 * Key attributes may have been returned by psa_get_key_attributes()
5954 * thus reset them as required.
5955 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005956 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005957
Ronald Cron5425a212020-08-04 14:58:35 +02005958 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02005959 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005960 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02005961}
5962/* END_CASE */
5963
5964/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02005965void asymmetric_encrypt_decrypt( int key_type_arg,
5966 data_t *key_data,
5967 int alg_arg,
5968 data_t *input_data,
5969 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005970{
Ronald Cron5425a212020-08-04 14:58:35 +02005971 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005972 psa_key_type_t key_type = key_type_arg;
5973 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005974 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005975 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005976 size_t output_size;
5977 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03005978 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005979 size_t output2_size;
5980 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005981 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005982
Gilles Peskine8817f612018-12-18 00:18:46 +01005983 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03005984
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005985 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5986 psa_set_key_algorithm( &attributes, alg );
5987 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03005988
Gilles Peskine049c7532019-05-15 20:22:09 +02005989 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005990 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005991
5992 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02005993 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005994 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01005995
Gilles Peskine55c94dd2018-06-30 18:54:48 +02005996 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01005997 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005998 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01005999
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006000 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01006001 TEST_ASSERT( output2_size <=
6002 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
6003 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006004 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006005
Gilles Peskineeebd7382018-06-08 18:11:54 +02006006 /* We test encryption by checking that encrypt-then-decrypt gives back
6007 * the original plaintext because of the non-optional random
6008 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02006009 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006010 input_data->x, input_data->len,
6011 label->x, label->len,
6012 output, output_size,
6013 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006014 /* We don't know what ciphertext length to expect, but check that
6015 * it looks sensible. */
6016 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006017
Ronald Cron5425a212020-08-04 14:58:35 +02006018 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006019 output, output_length,
6020 label->x, label->len,
6021 output2, output2_size,
6022 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006023 ASSERT_COMPARE( input_data->x, input_data->len,
6024 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006025
6026exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006027 /*
6028 * Key attributes may have been returned by psa_get_key_attributes()
6029 * thus reset them as required.
6030 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006031 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006032
Ronald Cron5425a212020-08-04 14:58:35 +02006033 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006034 mbedtls_free( output );
6035 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006036 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006037}
6038/* END_CASE */
6039
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006040/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006041void asymmetric_decrypt( int key_type_arg,
6042 data_t *key_data,
6043 int alg_arg,
6044 data_t *input_data,
6045 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02006046 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006047{
Ronald Cron5425a212020-08-04 14:58:35 +02006048 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006049 psa_key_type_t key_type = key_type_arg;
6050 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01006051 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006052 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03006053 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006054 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006055 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006056
Gilles Peskine8817f612018-12-18 00:18:46 +01006057 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006058
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006059 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6060 psa_set_key_algorithm( &attributes, alg );
6061 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006062
Gilles Peskine049c7532019-05-15 20:22:09 +02006063 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006064 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006065
gabor-mezei-armceface22021-01-21 12:26:17 +01006066 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6067 key_bits = psa_get_key_bits( &attributes );
6068
6069 /* Determine the maximum ciphertext length */
6070 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
6071 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
6072 ASSERT_ALLOC( output, output_size );
6073
Ronald Cron5425a212020-08-04 14:58:35 +02006074 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006075 input_data->x, input_data->len,
6076 label->x, label->len,
6077 output,
6078 output_size,
6079 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006080 ASSERT_COMPARE( expected_data->x, expected_data->len,
6081 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006082
Gilles Peskine68428122018-06-30 18:42:41 +02006083 /* If the label is empty, the test framework puts a non-null pointer
6084 * in label->x. Test that a null pointer works as well. */
6085 if( label->len == 0 )
6086 {
6087 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006088 if( output_size != 0 )
6089 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006090 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006091 input_data->x, input_data->len,
6092 NULL, label->len,
6093 output,
6094 output_size,
6095 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006096 ASSERT_COMPARE( expected_data->x, expected_data->len,
6097 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006098 }
6099
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006100exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006101 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006102 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006103 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006104 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006105}
6106/* END_CASE */
6107
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006108/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006109void asymmetric_decrypt_fail( int key_type_arg,
6110 data_t *key_data,
6111 int alg_arg,
6112 data_t *input_data,
6113 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00006114 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02006115 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006116{
Ronald Cron5425a212020-08-04 14:58:35 +02006117 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006118 psa_key_type_t key_type = key_type_arg;
6119 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006120 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00006121 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006122 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006123 psa_status_t actual_status;
6124 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006125 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006126
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006127 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006128
Gilles Peskine8817f612018-12-18 00:18:46 +01006129 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006130
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006131 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6132 psa_set_key_algorithm( &attributes, alg );
6133 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006134
Gilles Peskine049c7532019-05-15 20:22:09 +02006135 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006136 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006137
Ronald Cron5425a212020-08-04 14:58:35 +02006138 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006139 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006140 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006141 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02006142 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006143 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006144 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006145
Gilles Peskine68428122018-06-30 18:42:41 +02006146 /* If the label is empty, the test framework puts a non-null pointer
6147 * in label->x. Test that a null pointer works as well. */
6148 if( label->len == 0 )
6149 {
6150 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006151 if( output_size != 0 )
6152 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006153 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006154 input_data->x, input_data->len,
6155 NULL, label->len,
6156 output, output_size,
6157 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006158 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006159 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02006160 }
6161
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006162exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006163 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006164 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02006165 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006166 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006167}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006168/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02006169
6170/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02006171void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00006172{
6173 /* Test each valid way of initializing the object, except for `= {0}`, as
6174 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
6175 * though it's OK by the C standard. We could test for this, but we'd need
6176 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006177 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006178 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
6179 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
6180 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00006181
6182 memset( &zero, 0, sizeof( zero ) );
6183
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006184 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006185 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006186 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006187 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006188 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006189 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006190 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006191
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006192 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006193 PSA_ASSERT( psa_key_derivation_abort(&func) );
6194 PSA_ASSERT( psa_key_derivation_abort(&init) );
6195 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00006196}
6197/* END_CASE */
6198
Janos Follath16de4a42019-06-13 16:32:24 +01006199/* BEGIN_CASE */
6200void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02006201{
Gilles Peskineea0fb492018-07-12 17:17:20 +02006202 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006203 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006204 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006205
Gilles Peskine8817f612018-12-18 00:18:46 +01006206 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006207
Janos Follath16de4a42019-06-13 16:32:24 +01006208 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006209 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006210
6211exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006212 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006213 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006214}
6215/* END_CASE */
6216
Janos Follathaf3c2a02019-06-12 12:34:34 +01006217/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01006218void derive_set_capacity( int alg_arg, int capacity_arg,
6219 int expected_status_arg )
6220{
6221 psa_algorithm_t alg = alg_arg;
6222 size_t capacity = capacity_arg;
6223 psa_status_t expected_status = expected_status_arg;
6224 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6225
6226 PSA_ASSERT( psa_crypto_init( ) );
6227
6228 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6229
6230 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
6231 expected_status );
6232
6233exit:
6234 psa_key_derivation_abort( &operation );
6235 PSA_DONE( );
6236}
6237/* END_CASE */
6238
6239/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01006240void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02006241 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006242 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02006243 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006244 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02006245 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006246 int expected_status_arg3,
6247 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006248{
6249 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02006250 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
6251 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01006252 psa_status_t expected_statuses[] = {expected_status_arg1,
6253 expected_status_arg2,
6254 expected_status_arg3};
6255 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006256 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6257 MBEDTLS_SVC_KEY_ID_INIT,
6258 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01006259 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6260 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6261 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006262 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006263 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006264 psa_status_t expected_output_status = expected_output_status_arg;
6265 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01006266
6267 PSA_ASSERT( psa_crypto_init( ) );
6268
6269 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6270 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006271
6272 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6273
6274 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
6275 {
Gilles Peskine4023c012021-05-27 13:21:20 +02006276 mbedtls_test_set_step( i );
6277 if( steps[i] == 0 )
6278 {
6279 /* Skip this step */
6280 }
6281 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006282 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02006283 psa_set_key_type( &attributes, key_types[i] );
6284 PSA_ASSERT( psa_import_key( &attributes,
6285 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006286 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006287 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
6288 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
6289 {
6290 // When taking a private key as secret input, use key agreement
6291 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006292 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
6293 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006294 expected_statuses[i] );
6295 }
6296 else
6297 {
6298 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02006299 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006300 expected_statuses[i] );
6301 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02006302 }
6303 else
6304 {
6305 TEST_EQUAL( psa_key_derivation_input_bytes(
6306 &operation, steps[i],
6307 inputs[i]->x, inputs[i]->len ),
6308 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006309 }
6310 }
6311
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006312 if( output_key_type != PSA_KEY_TYPE_NONE )
6313 {
6314 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00006315 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006316 psa_set_key_bits( &attributes, 8 );
6317 actual_output_status =
6318 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006319 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006320 }
6321 else
6322 {
6323 uint8_t buffer[1];
6324 actual_output_status =
6325 psa_key_derivation_output_bytes( &operation,
6326 buffer, sizeof( buffer ) );
6327 }
6328 TEST_EQUAL( actual_output_status, expected_output_status );
6329
Janos Follathaf3c2a02019-06-12 12:34:34 +01006330exit:
6331 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006332 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6333 psa_destroy_key( keys[i] );
6334 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006335 PSA_DONE( );
6336}
6337/* END_CASE */
6338
Janos Follathd958bb72019-07-03 15:02:16 +01006339/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006340void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006341{
Janos Follathd958bb72019-07-03 15:02:16 +01006342 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006343 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02006344 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006345 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01006346 unsigned char input1[] = "Input 1";
6347 size_t input1_length = sizeof( input1 );
6348 unsigned char input2[] = "Input 2";
6349 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006350 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02006351 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02006352 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6353 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6354 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006355 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006356
Gilles Peskine8817f612018-12-18 00:18:46 +01006357 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006358
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006359 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6360 psa_set_key_algorithm( &attributes, alg );
6361 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006362
Gilles Peskine73676cb2019-05-15 20:15:10 +02006363 PSA_ASSERT( psa_import_key( &attributes,
6364 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02006365 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006366
6367 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006368 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6369 input1, input1_length,
6370 input2, input2_length,
6371 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01006372 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006373
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006374 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01006375 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006376 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006377
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006378 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006379
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006380 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02006381 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006382
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006383exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006384 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006385 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006386 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006387}
6388/* END_CASE */
6389
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006390/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006391void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006392{
6393 uint8_t output_buffer[16];
6394 size_t buffer_size = 16;
6395 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006396 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006397
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006398 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6399 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006400 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006401
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006402 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006403 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006404
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006405 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006406
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006407 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6408 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006409 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006410
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006411 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006412 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006413
6414exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006415 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006416}
6417/* END_CASE */
6418
6419/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006420void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02006421 int step1_arg, data_t *input1,
6422 int step2_arg, data_t *input2,
6423 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006424 int requested_capacity_arg,
6425 data_t *expected_output1,
6426 data_t *expected_output2 )
6427{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006428 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02006429 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
6430 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006431 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6432 MBEDTLS_SVC_KEY_ID_INIT,
6433 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006434 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006435 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006436 uint8_t *expected_outputs[2] =
6437 {expected_output1->x, expected_output2->x};
6438 size_t output_sizes[2] =
6439 {expected_output1->len, expected_output2->len};
6440 size_t output_buffer_size = 0;
6441 uint8_t *output_buffer = NULL;
6442 size_t expected_capacity;
6443 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006444 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006445 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02006446 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006447
6448 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6449 {
6450 if( output_sizes[i] > output_buffer_size )
6451 output_buffer_size = output_sizes[i];
6452 if( output_sizes[i] == 0 )
6453 expected_outputs[i] = NULL;
6454 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006455 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01006456 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006457
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006458 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6459 psa_set_key_algorithm( &attributes, alg );
6460 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006461
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006462 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02006463 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6464 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
6465 requested_capacity ) );
6466 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006467 {
Gilles Peskine1468da72019-05-29 17:35:49 +02006468 switch( steps[i] )
6469 {
6470 case 0:
6471 break;
6472 case PSA_KEY_DERIVATION_INPUT_SECRET:
6473 PSA_ASSERT( psa_import_key( &attributes,
6474 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006475 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01006476
6477 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
6478 {
6479 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
6480 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
6481 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
6482 }
6483
Gilles Peskine1468da72019-05-29 17:35:49 +02006484 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02006485 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02006486 break;
6487 default:
6488 PSA_ASSERT( psa_key_derivation_input_bytes(
6489 &operation, steps[i],
6490 inputs[i]->x, inputs[i]->len ) );
6491 break;
6492 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006493 }
Gilles Peskine1468da72019-05-29 17:35:49 +02006494
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006495 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006496 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006497 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006498 expected_capacity = requested_capacity;
6499
6500 /* Expansion phase. */
6501 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6502 {
6503 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006504 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006505 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006506 if( expected_capacity == 0 && output_sizes[i] == 0 )
6507 {
6508 /* Reading 0 bytes when 0 bytes are available can go either way. */
6509 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02006510 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006511 continue;
6512 }
6513 else if( expected_capacity == 0 ||
6514 output_sizes[i] > expected_capacity )
6515 {
6516 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02006517 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006518 expected_capacity = 0;
6519 continue;
6520 }
6521 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01006522 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006523 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006524 ASSERT_COMPARE( output_buffer, output_sizes[i],
6525 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006526 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006527 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006528 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006529 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006530 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006531 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006532 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006533
6534exit:
6535 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006536 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006537 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6538 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006539 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006540}
6541/* END_CASE */
6542
6543/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02006544void derive_full( int alg_arg,
6545 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01006546 data_t *input1,
6547 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02006548 int requested_capacity_arg )
6549{
Ronald Cron5425a212020-08-04 14:58:35 +02006550 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006551 psa_algorithm_t alg = alg_arg;
6552 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006553 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006554 unsigned char output_buffer[16];
6555 size_t expected_capacity = requested_capacity;
6556 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006557 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02006558
Gilles Peskine8817f612018-12-18 00:18:46 +01006559 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006560
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006561 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6562 psa_set_key_algorithm( &attributes, alg );
6563 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02006564
Gilles Peskine049c7532019-05-15 20:22:09 +02006565 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006566 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006567
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006568 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6569 input1->x, input1->len,
6570 input2->x, input2->len,
6571 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01006572 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01006573
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006574 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006575 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006576 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006577
6578 /* Expansion phase. */
6579 while( current_capacity > 0 )
6580 {
6581 size_t read_size = sizeof( output_buffer );
6582 if( read_size > current_capacity )
6583 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006584 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006585 output_buffer,
6586 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006587 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006588 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006589 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006590 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02006591 }
6592
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006593 /* Check that the operation refuses to go over capacity. */
6594 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006595 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02006596
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006597 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02006598
6599exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006600 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006601 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006602 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02006603}
6604/* END_CASE */
6605
Janos Follathe60c9052019-07-03 13:51:30 +01006606/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006607void derive_key_exercise( int alg_arg,
6608 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01006609 data_t *input1,
6610 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006611 int derived_type_arg,
6612 int derived_bits_arg,
6613 int derived_usage_arg,
6614 int derived_alg_arg )
6615{
Ronald Cron5425a212020-08-04 14:58:35 +02006616 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6617 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006618 psa_algorithm_t alg = alg_arg;
6619 psa_key_type_t derived_type = derived_type_arg;
6620 size_t derived_bits = derived_bits_arg;
6621 psa_key_usage_t derived_usage = derived_usage_arg;
6622 psa_algorithm_t derived_alg = derived_alg_arg;
6623 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006624 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006625 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006626 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006627
Gilles Peskine8817f612018-12-18 00:18:46 +01006628 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006629
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006630 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6631 psa_set_key_algorithm( &attributes, alg );
6632 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006633 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006634 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006635
6636 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006637 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6638 input1->x, input1->len,
6639 input2->x, input2->len,
6640 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01006641 goto exit;
6642
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006643 psa_set_key_usage_flags( &attributes, derived_usage );
6644 psa_set_key_algorithm( &attributes, derived_alg );
6645 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006646 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006647 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006648 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006649
6650 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02006651 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006652 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
6653 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006654
6655 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006656 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02006657 goto exit;
6658
6659exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006660 /*
6661 * Key attributes may have been returned by psa_get_key_attributes()
6662 * thus reset them as required.
6663 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006664 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006665
6666 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006667 psa_destroy_key( base_key );
6668 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006669 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006670}
6671/* END_CASE */
6672
Janos Follath42fd8882019-07-03 14:17:09 +01006673/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02006674void derive_key_export( int alg_arg,
6675 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01006676 data_t *input1,
6677 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02006678 int bytes1_arg,
6679 int bytes2_arg )
6680{
Ronald Cron5425a212020-08-04 14:58:35 +02006681 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6682 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006683 psa_algorithm_t alg = alg_arg;
6684 size_t bytes1 = bytes1_arg;
6685 size_t bytes2 = bytes2_arg;
6686 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006687 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006688 uint8_t *output_buffer = NULL;
6689 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006690 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6691 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02006692 size_t length;
6693
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006694 ASSERT_ALLOC( output_buffer, capacity );
6695 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01006696 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006697
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006698 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6699 psa_set_key_algorithm( &base_attributes, alg );
6700 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02006701 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006702 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006703
6704 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006705 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6706 input1->x, input1->len,
6707 input2->x, input2->len,
6708 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006709 goto exit;
6710
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006711 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006712 output_buffer,
6713 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006714 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006715
6716 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006717 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6718 input1->x, input1->len,
6719 input2->x, input2->len,
6720 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01006721 goto exit;
6722
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006723 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6724 psa_set_key_algorithm( &derived_attributes, 0 );
6725 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006726 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006727 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006728 &derived_key ) );
6729 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006730 export_buffer, bytes1,
6731 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006732 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02006733 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02006734 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006735 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006736 &derived_key ) );
6737 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01006738 export_buffer + bytes1, bytes2,
6739 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006740 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006741
6742 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01006743 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
6744 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006745
6746exit:
6747 mbedtls_free( output_buffer );
6748 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006749 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006750 psa_destroy_key( base_key );
6751 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006752 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02006753}
6754/* END_CASE */
6755
6756/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006757void derive_key( int alg_arg,
6758 data_t *key_data, data_t *input1, data_t *input2,
6759 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01006760 int expected_status_arg,
6761 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02006762{
Ronald Cron5425a212020-08-04 14:58:35 +02006763 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
6764 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02006765 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006766 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02006767 size_t bits = bits_arg;
6768 psa_status_t expected_status = expected_status_arg;
6769 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6770 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
6771 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
6772
6773 PSA_ASSERT( psa_crypto_init( ) );
6774
6775 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
6776 psa_set_key_algorithm( &base_attributes, alg );
6777 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
6778 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006779 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02006780
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006781 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
6782 input1->x, input1->len,
6783 input2->x, input2->len,
6784 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02006785 goto exit;
6786
6787 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
6788 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02006789 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02006790 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01006791
6792 psa_status_t status =
6793 psa_key_derivation_output_key( &derived_attributes,
6794 &operation,
6795 &derived_key );
6796 if( is_large_output > 0 )
6797 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
6798 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02006799
6800exit:
6801 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006802 psa_destroy_key( base_key );
6803 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02006804 PSA_DONE( );
6805}
6806/* END_CASE */
6807
6808/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006809void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02006810 int our_key_type_arg, int our_key_alg_arg,
6811 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006812 int expected_status_arg )
6813{
Ronald Cron5425a212020-08-04 14:58:35 +02006814 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006815 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006816 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006817 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006818 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006819 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02006820 psa_status_t expected_status = expected_status_arg;
6821 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006822
Gilles Peskine8817f612018-12-18 00:18:46 +01006823 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006824
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006825 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006826 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006827 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006828 PSA_ASSERT( psa_import_key( &attributes,
6829 our_key_data->x, our_key_data->len,
6830 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006831
Gilles Peskine77f40d82019-04-11 21:27:06 +02006832 /* The tests currently include inputs that should fail at either step.
6833 * Test cases that fail at the setup step should be changed to call
6834 * key_derivation_setup instead, and this function should be renamed
6835 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006836 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02006837 if( status == PSA_SUCCESS )
6838 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006839 TEST_EQUAL( psa_key_derivation_key_agreement(
6840 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
6841 our_key,
6842 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02006843 expected_status );
6844 }
6845 else
6846 {
6847 TEST_ASSERT( status == expected_status );
6848 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02006849
6850exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006851 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006852 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006853 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006854}
6855/* END_CASE */
6856
6857/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02006858void raw_key_agreement( int alg_arg,
6859 int our_key_type_arg, data_t *our_key_data,
6860 data_t *peer_key_data,
6861 data_t *expected_output )
6862{
Ronald Cron5425a212020-08-04 14:58:35 +02006863 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006864 psa_algorithm_t alg = alg_arg;
6865 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006866 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006867 unsigned char *output = NULL;
6868 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01006869 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02006870
6871 ASSERT_ALLOC( output, expected_output->len );
6872 PSA_ASSERT( psa_crypto_init( ) );
6873
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006874 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6875 psa_set_key_algorithm( &attributes, alg );
6876 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006877 PSA_ASSERT( psa_import_key( &attributes,
6878 our_key_data->x, our_key_data->len,
6879 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006880
gabor-mezei-armceface22021-01-21 12:26:17 +01006881 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
6882 key_bits = psa_get_key_bits( &attributes );
6883
Gilles Peskinebe697d82019-05-16 18:00:41 +02006884 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
6885 peer_key_data->x, peer_key_data->len,
6886 output, expected_output->len,
6887 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006888 ASSERT_COMPARE( output, output_length,
6889 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01006890 TEST_ASSERT( output_length <=
6891 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
6892 TEST_ASSERT( output_length <=
6893 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006894
6895exit:
6896 mbedtls_free( output );
6897 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006898 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02006899}
6900/* END_CASE */
6901
6902/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02006903void key_agreement_capacity( int alg_arg,
6904 int our_key_type_arg, data_t *our_key_data,
6905 data_t *peer_key_data,
6906 int expected_capacity_arg )
6907{
Ronald Cron5425a212020-08-04 14:58:35 +02006908 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006909 psa_algorithm_t alg = alg_arg;
6910 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006911 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006912 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006913 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02006914 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02006915
Gilles Peskine8817f612018-12-18 00:18:46 +01006916 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006917
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006918 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6919 psa_set_key_algorithm( &attributes, alg );
6920 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006921 PSA_ASSERT( psa_import_key( &attributes,
6922 our_key_data->x, our_key_data->len,
6923 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006924
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006925 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006926 PSA_ASSERT( psa_key_derivation_key_agreement(
6927 &operation,
6928 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6929 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006930 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6931 {
6932 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006933 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006934 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006935 NULL, 0 ) );
6936 }
Gilles Peskine59685592018-09-18 12:11:34 +02006937
Gilles Peskinebf491972018-10-25 22:36:12 +02006938 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006939 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006940 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006941 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02006942
Gilles Peskinebf491972018-10-25 22:36:12 +02006943 /* Test the actual capacity by reading the output. */
6944 while( actual_capacity > sizeof( output ) )
6945 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006946 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006947 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02006948 actual_capacity -= sizeof( output );
6949 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006950 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006951 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006952 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02006953 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02006954
Gilles Peskine59685592018-09-18 12:11:34 +02006955exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006956 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02006957 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006958 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02006959}
6960/* END_CASE */
6961
6962/* BEGIN_CASE */
6963void key_agreement_output( int alg_arg,
6964 int our_key_type_arg, data_t *our_key_data,
6965 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006966 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02006967{
Ronald Cron5425a212020-08-04 14:58:35 +02006968 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02006969 psa_algorithm_t alg = alg_arg;
6970 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006971 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006972 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006973 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02006974
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02006975 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
6976 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006977
Gilles Peskine8817f612018-12-18 00:18:46 +01006978 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006979
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006980 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6981 psa_set_key_algorithm( &attributes, alg );
6982 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006983 PSA_ASSERT( psa_import_key( &attributes,
6984 our_key_data->x, our_key_data->len,
6985 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02006986
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006987 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006988 PSA_ASSERT( psa_key_derivation_key_agreement(
6989 &operation,
6990 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
6991 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006992 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
6993 {
6994 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006995 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02006996 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02006997 NULL, 0 ) );
6998 }
Gilles Peskine59685592018-09-18 12:11:34 +02006999
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007000 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007001 actual_output,
7002 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007003 ASSERT_COMPARE( actual_output, expected_output1->len,
7004 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007005 if( expected_output2->len != 0 )
7006 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007007 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007008 actual_output,
7009 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007010 ASSERT_COMPARE( actual_output, expected_output2->len,
7011 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007012 }
Gilles Peskine59685592018-09-18 12:11:34 +02007013
7014exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007015 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007016 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007017 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007018 mbedtls_free( actual_output );
7019}
7020/* END_CASE */
7021
7022/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02007023void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02007024{
Gilles Peskinea50d7392018-06-21 10:22:13 +02007025 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007026 unsigned char *output = NULL;
7027 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02007028 size_t i;
7029 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02007030
Simon Butcher49f8e312020-03-03 15:51:50 +00007031 TEST_ASSERT( bytes_arg >= 0 );
7032
Gilles Peskine91892022021-02-08 19:50:26 +01007033 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007034 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02007035
Gilles Peskine8817f612018-12-18 00:18:46 +01007036 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02007037
Gilles Peskinea50d7392018-06-21 10:22:13 +02007038 /* Run several times, to ensure that every output byte will be
7039 * nonzero at least once with overwhelming probability
7040 * (2^(-8*number_of_runs)). */
7041 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02007042 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007043 if( bytes != 0 )
7044 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01007045 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007046
Gilles Peskinea50d7392018-06-21 10:22:13 +02007047 for( i = 0; i < bytes; i++ )
7048 {
7049 if( output[i] != 0 )
7050 ++changed[i];
7051 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007052 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02007053
7054 /* Check that every byte was changed to nonzero at least once. This
7055 * validates that psa_generate_random is overwriting every byte of
7056 * the output buffer. */
7057 for( i = 0; i < bytes; i++ )
7058 {
7059 TEST_ASSERT( changed[i] != 0 );
7060 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007061
7062exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007063 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007064 mbedtls_free( output );
7065 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02007066}
7067/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02007068
7069/* BEGIN_CASE */
7070void generate_key( int type_arg,
7071 int bits_arg,
7072 int usage_arg,
7073 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007074 int expected_status_arg,
7075 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02007076{
Ronald Cron5425a212020-08-04 14:58:35 +02007077 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007078 psa_key_type_t type = type_arg;
7079 psa_key_usage_t usage = usage_arg;
7080 size_t bits = bits_arg;
7081 psa_algorithm_t alg = alg_arg;
7082 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007084 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007085
Gilles Peskine8817f612018-12-18 00:18:46 +01007086 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007087
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007088 psa_set_key_usage_flags( &attributes, usage );
7089 psa_set_key_algorithm( &attributes, alg );
7090 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007091 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007092
7093 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01007094 psa_status_t status = psa_generate_key( &attributes, &key );
7095
7096 if( is_large_key > 0 )
7097 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7098 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007099 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007100 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007101
7102 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007103 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007104 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
7105 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007106
Gilles Peskine818ca122018-06-20 18:16:48 +02007107 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007108 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02007109 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007110
7111exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007112 /*
7113 * Key attributes may have been returned by psa_get_key_attributes()
7114 * thus reset them as required.
7115 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007116 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007117
Ronald Cron5425a212020-08-04 14:58:35 +02007118 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007119 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007120}
7121/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03007122
Ronald Cronee414c72021-03-18 18:50:08 +01007123/* 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 +02007124void generate_key_rsa( int bits_arg,
7125 data_t *e_arg,
7126 int expected_status_arg )
7127{
Ronald Cron5425a212020-08-04 14:58:35 +02007128 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02007129 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02007130 size_t bits = bits_arg;
7131 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
7132 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
7133 psa_status_t expected_status = expected_status_arg;
7134 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7135 uint8_t *exported = NULL;
7136 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007137 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007138 size_t exported_length = SIZE_MAX;
7139 uint8_t *e_read_buffer = NULL;
7140 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02007141 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007142 size_t e_read_length = SIZE_MAX;
7143
7144 if( e_arg->len == 0 ||
7145 ( e_arg->len == 3 &&
7146 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
7147 {
7148 is_default_public_exponent = 1;
7149 e_read_size = 0;
7150 }
7151 ASSERT_ALLOC( e_read_buffer, e_read_size );
7152 ASSERT_ALLOC( exported, exported_size );
7153
7154 PSA_ASSERT( psa_crypto_init( ) );
7155
7156 psa_set_key_usage_flags( &attributes, usage );
7157 psa_set_key_algorithm( &attributes, alg );
7158 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
7159 e_arg->x, e_arg->len ) );
7160 psa_set_key_bits( &attributes, bits );
7161
7162 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007163 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007164 if( expected_status != PSA_SUCCESS )
7165 goto exit;
7166
7167 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007168 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007169 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7170 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
7171 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
7172 e_read_buffer, e_read_size,
7173 &e_read_length ) );
7174 if( is_default_public_exponent )
7175 TEST_EQUAL( e_read_length, 0 );
7176 else
7177 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
7178
7179 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007180 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02007181 goto exit;
7182
7183 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02007184 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02007185 exported, exported_size,
7186 &exported_length ) );
7187 {
7188 uint8_t *p = exported;
7189 uint8_t *end = exported + exported_length;
7190 size_t len;
7191 /* RSAPublicKey ::= SEQUENCE {
7192 * modulus INTEGER, -- n
7193 * publicExponent INTEGER } -- e
7194 */
7195 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007196 MBEDTLS_ASN1_SEQUENCE |
7197 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01007198 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007199 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
7200 MBEDTLS_ASN1_INTEGER ) );
7201 if( len >= 1 && p[0] == 0 )
7202 {
7203 ++p;
7204 --len;
7205 }
7206 if( e_arg->len == 0 )
7207 {
7208 TEST_EQUAL( len, 3 );
7209 TEST_EQUAL( p[0], 1 );
7210 TEST_EQUAL( p[1], 0 );
7211 TEST_EQUAL( p[2], 1 );
7212 }
7213 else
7214 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
7215 }
7216
7217exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007218 /*
7219 * Key attributes may have been returned by psa_get_key_attributes() or
7220 * set by psa_set_key_domain_parameters() thus reset them as required.
7221 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02007222 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007223
Ronald Cron5425a212020-08-04 14:58:35 +02007224 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007225 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007226 mbedtls_free( e_read_buffer );
7227 mbedtls_free( exported );
7228}
7229/* END_CASE */
7230
Darryl Greend49a4992018-06-18 17:27:26 +01007231/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007232void persistent_key_load_key_from_storage( data_t *data,
7233 int type_arg, int bits_arg,
7234 int usage_flags_arg, int alg_arg,
7235 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01007236{
Ronald Cron71016a92020-08-28 19:01:50 +02007237 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007238 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02007239 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7240 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007241 psa_key_type_t type = type_arg;
7242 size_t bits = bits_arg;
7243 psa_key_usage_t usage_flags = usage_flags_arg;
7244 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007245 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01007246 unsigned char *first_export = NULL;
7247 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007248 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007249 size_t first_exported_length;
7250 size_t second_exported_length;
7251
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007252 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7253 {
7254 ASSERT_ALLOC( first_export, export_size );
7255 ASSERT_ALLOC( second_export, export_size );
7256 }
Darryl Greend49a4992018-06-18 17:27:26 +01007257
Gilles Peskine8817f612018-12-18 00:18:46 +01007258 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007259
Gilles Peskinec87af662019-05-15 16:12:22 +02007260 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007261 psa_set_key_usage_flags( &attributes, usage_flags );
7262 psa_set_key_algorithm( &attributes, alg );
7263 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007264 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007265
Darryl Green0c6575a2018-11-07 16:05:30 +00007266 switch( generation_method )
7267 {
7268 case IMPORT_KEY:
7269 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02007270 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007271 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007272 break;
Darryl Greend49a4992018-06-18 17:27:26 +01007273
Darryl Green0c6575a2018-11-07 16:05:30 +00007274 case GENERATE_KEY:
7275 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007276 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007277 break;
7278
7279 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01007280#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007281 {
7282 /* Create base key */
7283 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
7284 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7285 psa_set_key_usage_flags( &base_attributes,
7286 PSA_KEY_USAGE_DERIVE );
7287 psa_set_key_algorithm( &base_attributes, derive_alg );
7288 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007289 PSA_ASSERT( psa_import_key( &base_attributes,
7290 data->x, data->len,
7291 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007292 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007293 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007294 PSA_ASSERT( psa_key_derivation_input_key(
7295 &operation,
7296 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007297 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007298 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007299 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007300 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
7301 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007302 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007303 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007304 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02007305 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007306 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01007307#else
7308 TEST_ASSUME( ! "KDF not supported in this configuration" );
7309#endif
7310 break;
7311
7312 default:
7313 TEST_ASSERT( ! "generation_method not implemented in test" );
7314 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00007315 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007316 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01007317
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007318 /* Export the key if permitted by the key policy. */
7319 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7320 {
Ronald Cron5425a212020-08-04 14:58:35 +02007321 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007322 first_export, export_size,
7323 &first_exported_length ) );
7324 if( generation_method == IMPORT_KEY )
7325 ASSERT_COMPARE( data->x, data->len,
7326 first_export, first_exported_length );
7327 }
Darryl Greend49a4992018-06-18 17:27:26 +01007328
7329 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02007330 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007331 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01007332 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007333
Darryl Greend49a4992018-06-18 17:27:26 +01007334 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02007335 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02007336 TEST_ASSERT( mbedtls_svc_key_id_equal(
7337 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007338 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
7339 PSA_KEY_LIFETIME_PERSISTENT );
7340 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7341 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02007342 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02007343 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007344 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01007345
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007346 /* Export the key again if permitted by the key policy. */
7347 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00007348 {
Ronald Cron5425a212020-08-04 14:58:35 +02007349 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007350 second_export, export_size,
7351 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007352 ASSERT_COMPARE( first_export, first_exported_length,
7353 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00007354 }
7355
7356 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007357 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00007358 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01007359
7360exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007361 /*
7362 * Key attributes may have been returned by psa_get_key_attributes()
7363 * thus reset them as required.
7364 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007365 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007366
Darryl Greend49a4992018-06-18 17:27:26 +01007367 mbedtls_free( first_export );
7368 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007369 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007370 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02007371 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007372 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01007373}
7374/* END_CASE */