blob: 1ad0a4a79394639522b56d7e833fb457088db379 [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"
Gilles Peskine42649d92022-11-23 14:15:57 +01007#include "common.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02009/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
10 * uses mbedtls_ctr_drbg internally. */
11#include "mbedtls/ctr_drbg.h"
12
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020013#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020014#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020015
Gilles Peskine8e94efe2021-02-13 00:25:53 +010016#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010017#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010018#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053019#if defined(PSA_CRYPTO_DRIVER_TEST)
20#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053021#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
22#else
23#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053024#endif
Przemek Stekiel8258ea72022-10-19 12:17:19 +020025#include "mbedtls/legacy_or_psa.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010026
Gilles Peskine4023c012021-05-27 13:21:20 +020027/* If this comes up, it's a bug in the test code or in the test data. */
28#define UNUSED 0xdeadbeef
29
Dave Rodgman647791d2021-06-23 12:49:59 +010030/* Assert that an operation is (not) active.
31 * This serves as a proxy for checking if the operation is aborted. */
Gilles Peskine449bd832023-01-11 14:50:10 +010032#define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
33#define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010034
Przemek Stekiel7c795482022-11-15 22:26:12 +010035#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +010036int ecjpake_operation_setup(psa_pake_operation_t *operation,
37 psa_pake_cipher_suite_t *cipher_suite,
38 psa_pake_role_t role,
39 mbedtls_svc_key_id_t key,
40 size_t key_available)
Przemek Stekiel7c795482022-11-15 22:26:12 +010041{
Gilles Peskine449bd832023-01-11 14:50:10 +010042 PSA_ASSERT(psa_pake_abort(operation));
Przemek Stekiel7c795482022-11-15 22:26:12 +010043
Gilles Peskine449bd832023-01-11 14:50:10 +010044 PSA_ASSERT(psa_pake_setup(operation, cipher_suite));
Przemek Stekiel7c795482022-11-15 22:26:12 +010045
Gilles Peskine449bd832023-01-11 14:50:10 +010046 PSA_ASSERT(psa_pake_set_role(operation, role));
Przemek Stekiel7c795482022-11-15 22:26:12 +010047
Gilles Peskine449bd832023-01-11 14:50:10 +010048 if (key_available) {
49 PSA_ASSERT(psa_pake_set_password_key(operation, key));
50 }
Przemek Stekielf82effa2022-11-21 15:10:32 +010051 return 0;
Przemek Stekiel7c795482022-11-15 22:26:12 +010052exit:
Przemek Stekielf82effa2022-11-21 15:10:32 +010053 return 1;
Przemek Stekiel7c795482022-11-15 22:26:12 +010054}
55#endif
56
Jaeden Amerof24c7f82018-06-27 17:20:43 +010057/** An invalid export length that will never be set by psa_export_key(). */
58static const size_t INVALID_EXPORT_LENGTH = ~0U;
59
Gilles Peskinea7aa4422018-08-14 15:17:54 +020060/** Test if a buffer contains a constant byte value.
61 *
62 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020063 *
64 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020065 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020066 * \param size Size of the buffer in bytes.
67 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020068 * \return 1 if the buffer is all-bits-zero.
69 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020070 */
Gilles Peskine449bd832023-01-11 14:50:10 +010071static int mem_is_char(void *buffer, unsigned char c, size_t size)
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020072{
73 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010074 for (i = 0; i < size; i++) {
75 if (((unsigned char *) buffer)[i] != c) {
76 return 0;
77 }
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020078 }
Gilles Peskine449bd832023-01-11 14:50:10 +010079 return 1;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020080}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010081#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020082/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
Gilles Peskine449bd832023-01-11 14:50:10 +010083static int asn1_write_10x(unsigned char **p,
84 unsigned char *start,
85 size_t bits,
86 unsigned char x)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020087{
88 int ret;
89 int len = bits / 8 + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010090 if (bits == 0) {
91 return MBEDTLS_ERR_ASN1_INVALID_DATA;
92 }
93 if (bits <= 8 && x >= 1 << (bits - 1)) {
94 return MBEDTLS_ERR_ASN1_INVALID_DATA;
95 }
96 if (*p < start || *p - start < (ptrdiff_t) len) {
97 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
98 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +020099 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 (*p)[len-1] = x;
101 if (bits % 8 == 0) {
102 (*p)[1] |= 1;
103 } else {
104 (*p)[0] |= 1 << (bits % 8);
105 }
106 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
107 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
108 MBEDTLS_ASN1_INTEGER));
109 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200110}
111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112static int construct_fake_rsa_key(unsigned char *buffer,
113 size_t buffer_size,
114 unsigned char **p,
115 size_t bits,
116 int keypair)
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200117{
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 size_t half_bits = (bits + 1) / 2;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200119 int ret;
120 int len = 0;
121 /* Construct something that looks like a DER encoding of
122 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
123 * RSAPrivateKey ::= SEQUENCE {
124 * version Version,
125 * modulus INTEGER, -- n
126 * publicExponent INTEGER, -- e
127 * privateExponent INTEGER, -- d
128 * prime1 INTEGER, -- p
129 * prime2 INTEGER, -- q
130 * exponent1 INTEGER, -- d mod (p-1)
131 * exponent2 INTEGER, -- d mod (q-1)
132 * coefficient INTEGER, -- (inverse of q) mod p
133 * otherPrimeInfos OtherPrimeInfos OPTIONAL
134 * }
135 * Or, for a public key, the same structure with only
136 * version, modulus and publicExponent.
137 */
138 *p = buffer + buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 if (keypair) {
140 MBEDTLS_ASN1_CHK_ADD(len, /* pq */
141 asn1_write_10x(p, buffer, half_bits, 1));
142 MBEDTLS_ASN1_CHK_ADD(len, /* dq */
143 asn1_write_10x(p, buffer, half_bits, 1));
144 MBEDTLS_ASN1_CHK_ADD(len, /* dp */
145 asn1_write_10x(p, buffer, half_bits, 1));
146 MBEDTLS_ASN1_CHK_ADD(len, /* q */
147 asn1_write_10x(p, buffer, half_bits, 1));
148 MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
149 asn1_write_10x(p, buffer, half_bits, 3));
150 MBEDTLS_ASN1_CHK_ADD(len, /* d */
151 asn1_write_10x(p, buffer, bits, 1));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200152 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
154 asn1_write_10x(p, buffer, 17, 1));
155 MBEDTLS_ASN1_CHK_ADD(len, /* n */
156 asn1_write_10x(p, buffer, bits, 1));
157 if (keypair) {
158 MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
159 mbedtls_asn1_write_int(p, buffer, 0));
160 }
161 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200162 {
163 const unsigned char tag =
164 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200166 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200168}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100169#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171int exercise_mac_setup(psa_key_type_t key_type,
172 const unsigned char *key_bytes,
173 size_t key_length,
174 psa_algorithm_t alg,
175 psa_mac_operation_t *operation,
176 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100177{
Ronald Cron5425a212020-08-04 14:58:35 +0200178 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200179 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
182 psa_set_key_algorithm(&attributes, alg);
183 psa_set_key_type(&attributes, key_type);
184 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 *status = psa_mac_sign_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100187 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 PSA_ASSERT(psa_mac_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100189 /* If setup failed, reproduce the failure, so that the caller can
190 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 if (*status != PSA_SUCCESS) {
192 TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100193 }
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 psa_destroy_key(key);
196 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197
198exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 psa_destroy_key(key);
200 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100201}
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203int exercise_cipher_setup(psa_key_type_t key_type,
204 const unsigned char *key_bytes,
205 size_t key_length,
206 psa_algorithm_t alg,
207 psa_cipher_operation_t *operation,
208 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100209{
Ronald Cron5425a212020-08-04 14:58:35 +0200210 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200211 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
214 psa_set_key_algorithm(&attributes, alg);
215 psa_set_key_type(&attributes, key_type);
216 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 *status = psa_cipher_encrypt_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100219 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 PSA_ASSERT(psa_cipher_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100221 /* If setup failed, reproduce the failure, so that the caller can
222 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 if (*status != PSA_SUCCESS) {
224 TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
225 *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100226 }
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 psa_destroy_key(key);
229 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100230
231exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 psa_destroy_key(key);
233 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100234}
235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237{
238 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 uint8_t buffer[1];
241 size_t length;
242 int ok = 0;
243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 psa_set_key_id(&attributes, key_id);
245 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
246 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
247 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
248 TEST_EQUAL(psa_get_key_attributes(key, &attributes),
249 PSA_ERROR_INVALID_HANDLE);
Ronald Cronecfb2372020-07-23 17:13:42 +0200250 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
Ronald Cronecfb2372020-07-23 17:13:42 +0200252 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
254 TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
255 TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
256 TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
257 TEST_EQUAL(psa_get_key_type(&attributes), 0);
258 TEST_EQUAL(psa_get_key_bits(&attributes), 0);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
261 PSA_ERROR_INVALID_HANDLE);
262 TEST_EQUAL(psa_export_public_key(key,
263 buffer, sizeof(buffer), &length),
264 PSA_ERROR_INVALID_HANDLE);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200265
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200266 ok = 1;
267
268exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100269 /*
270 * Key attributes may have been returned by psa_get_key_attributes()
271 * thus reset them as required.
272 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return ok;
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200276}
277
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200278/* Assert that a key isn't reported as having a slot number. */
279#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100280#define ASSERT_NO_SLOT_NUMBER(attributes) \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200281 do \
282 { \
283 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 TEST_EQUAL(psa_get_key_slot_number( \
285 attributes, \
286 &ASSERT_NO_SLOT_NUMBER_slot_number), \
287 PSA_ERROR_INVALID_ARGUMENT); \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200288 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 while (0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200290#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100291#define ASSERT_NO_SLOT_NUMBER(attributes) \
292 ((void) 0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200293#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
294
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100295/* An overapproximation of the amount of storage needed for a key of the
296 * given type and with the given content. The API doesn't make it easy
297 * to find a good value for the size. The current implementation doesn't
298 * care about the value anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100299#define KEY_BITS_FROM_DATA(type, data) \
300 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100301
Darryl Green0c6575a2018-11-07 16:05:30 +0000302typedef enum {
303 IMPORT_KEY = 0,
304 GENERATE_KEY = 1,
305 DERIVE_KEY = 2
306} generate_method;
307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308typedef enum {
Paul Elliott33746aa2021-09-15 16:40:40 +0100309 DO_NOT_SET_LENGTHS = 0,
310 SET_LENGTHS_BEFORE_NONCE = 1,
311 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100312} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314typedef enum {
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100315 USE_NULL_TAG = 0,
316 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100317} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100318
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100319/*!
320 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100321 * \param key_type_arg Type of key passed in
322 * \param key_data The encryption / decryption key data
323 * \param alg_arg The type of algorithm used
324 * \param nonce Nonce data
325 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100326 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100327 * feed additional data in to be encrypted /
328 * decrypted. If -1, no chunking.
329 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100330 * \param data_part_len_arg If not -1, the length of chunks to feed
331 * the data in to be encrypted / decrypted. If
332 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100333 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100334 * expected here, this controls whether or not
335 * to set lengths, and in what order with
336 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100337 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100338 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100339 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100340 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100341 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100342 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100343static int aead_multipart_internal_func(int key_type_arg, data_t *key_data,
344 int alg_arg,
345 data_t *nonce,
346 data_t *additional_data,
347 int ad_part_len_arg,
348 data_t *input_data,
349 int data_part_len_arg,
350 set_lengths_method_t set_lengths_method,
351 data_t *expected_output,
352 int is_encrypt,
353 int do_zero_parts)
Paul Elliottd3f82412021-06-16 16:52:21 +0100354{
355 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
356 psa_key_type_t key_type = key_type_arg;
357 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100358 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100359 unsigned char *output_data = NULL;
360 unsigned char *part_data = NULL;
361 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100362 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100363 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100364 size_t output_size = 0;
365 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100366 size_t output_length = 0;
367 size_t key_bits = 0;
368 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100369 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100370 size_t part_length = 0;
371 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100372 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100373 size_t ad_part_len = 0;
374 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100375 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100376 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
377 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
378
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100379 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100380 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 PSA_ASSERT(psa_crypto_init());
Paul Elliottd3f82412021-06-16 16:52:21 +0100383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (is_encrypt) {
385 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
386 } else {
387 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100388 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100389
390 psa_set_key_algorithm(&attributes, alg);
391 psa_set_key_type(&attributes, key_type);
392
393 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
394 &key));
395
396 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
397 key_bits = psa_get_key_bits(&attributes);
398
399 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
400
401 if (is_encrypt) {
402 /* Tag gets written at end of buffer. */
403 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
404 (input_data->len +
405 tag_length));
406 data_true_size = input_data->len;
407 } else {
408 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
409 (input_data->len -
410 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100411
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100412 /* Do not want to attempt to decrypt tag. */
413 data_true_size = input_data->len - tag_length;
414 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 ASSERT_ALLOC(output_data, output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if (is_encrypt) {
419 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
420 TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
421 } else {
422 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
423 TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100424 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 ASSERT_ALLOC(final_data, final_output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 if (is_encrypt) {
429 status = psa_aead_encrypt_setup(&operation, key, alg);
430 } else {
431 status = psa_aead_decrypt_setup(&operation, key, alg);
432 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100433
434 /* If the operation is not supported, just skip and not fail in case the
435 * encryption involves a common limitation of cryptography hardwares and
436 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if (status == PSA_ERROR_NOT_SUPPORTED) {
438 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
439 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100440 }
441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 PSA_ASSERT(status);
Paul Elliottd3f82412021-06-16 16:52:21 +0100443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if (set_lengths_method == DO_NOT_SET_LENGTHS) {
445 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
446 } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
447 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
448 data_true_size));
449 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
450 } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
451 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottebf91632021-07-22 17:54:42 +0100452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
454 data_true_size));
Paul Elliottd3f82412021-06-16 16:52:21 +0100455 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if (ad_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100458 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100459 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100462 part_offset < additional_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 part_offset += part_length, part_count++) {
464 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100465 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 } else if (additional_data->len - part_offset < ad_part_len) {
Paul Elliotte49fe452021-09-15 16:52:11 +0100467 part_length = additional_data->len - part_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100469 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100470 }
471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 PSA_ASSERT(psa_aead_update_ad(&operation,
473 additional_data->x + part_offset,
474 part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100475
Paul Elliottd3f82412021-06-16 16:52:21 +0100476 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 } else {
Paul Elliottd3f82412021-06-16 16:52:21 +0100478 /* Pass additional data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
480 additional_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100481 }
482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 if (data_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100484 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 data_part_len = (size_t) data_part_len_arg;
486 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
487 (size_t) data_part_len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 ASSERT_ALLOC(part_data, part_data_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100492 part_offset < data_true_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 part_offset += part_length, part_count++) {
494 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100495 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 } else if ((data_true_size - part_offset) < data_part_len) {
497 part_length = (data_true_size - part_offset);
498 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100499 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100500 }
501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 PSA_ASSERT(psa_aead_update(&operation,
503 (input_data->x + part_offset),
504 part_length, part_data,
505 part_data_size,
506 &output_part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 if (output_data && output_part_length) {
509 memcpy((output_data + output_length), part_data,
510 output_part_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100511 }
512
Paul Elliottd3f82412021-06-16 16:52:21 +0100513 output_length += output_part_length;
514 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 } else {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100516 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
518 data_true_size, output_data,
519 output_size, &output_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 }
521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if (is_encrypt) {
523 PSA_ASSERT(psa_aead_finish(&operation, final_data,
524 final_output_size,
525 &output_part_length,
526 tag_buffer, tag_length,
527 &tag_size));
528 } else {
529 PSA_ASSERT(psa_aead_verify(&operation, final_data,
530 final_output_size,
531 &output_part_length,
532 (input_data->x + data_true_size),
533 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100534 }
535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 if (output_data && output_part_length) {
537 memcpy((output_data + output_length), final_data,
538 output_part_length);
539 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100540
541 output_length += output_part_length;
542
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100543
544 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
545 * should be exact.*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (is_encrypt) {
547 TEST_EQUAL(tag_length, tag_size);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if (output_data && tag_length) {
550 memcpy((output_data + output_length), tag_buffer,
551 tag_length);
552 }
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100553
554 output_length += tag_length;
555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 TEST_EQUAL(output_length,
557 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
558 input_data->len));
559 TEST_LE_U(output_length,
560 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
561 } else {
562 TEST_EQUAL(output_length,
563 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
564 input_data->len));
565 TEST_LE_U(output_length,
566 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100567 }
568
Paul Elliottd3f82412021-06-16 16:52:21 +0100569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 ASSERT_COMPARE(expected_output->x, expected_output->len,
571 output_data, output_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100572
Paul Elliottd3f82412021-06-16 16:52:21 +0100573
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100574 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100575
576exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 psa_destroy_key(key);
578 psa_aead_abort(&operation);
579 mbedtls_free(output_data);
580 mbedtls_free(part_data);
581 mbedtls_free(final_data);
582 PSA_DONE();
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 return test_ok;
Paul Elliottd3f82412021-06-16 16:52:21 +0100585}
586
Neil Armstrong4766f992022-02-28 16:23:59 +0100587/*!
588 * \brief Internal Function for MAC multipart tests.
589 * \param key_type_arg Type of key passed in
590 * \param key_data The encryption / decryption key data
591 * \param alg_arg The type of algorithm used
592 * \param input_data Data to encrypt / decrypt
593 * \param data_part_len_arg If not -1, the length of chunks to feed
594 * the data in to be encrypted / decrypted. If
595 * -1, no chunking
596 * \param expected_output Expected output
Tom Cosgrove1797b052022-12-04 17:19:59 +0000597 * \param is_verify If non-zero this is a verify operation.
Neil Armstrong4766f992022-02-28 16:23:59 +0100598 * \param do_zero_parts If non-zero, interleave zero length chunks
599 * with normal length chunks.
600 * \return int Zero on failure, non-zero on success.
601 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100602static int mac_multipart_internal_func(int key_type_arg, data_t *key_data,
603 int alg_arg,
604 data_t *input_data,
605 int data_part_len_arg,
606 data_t *expected_output,
607 int is_verify,
608 int do_zero_parts)
Neil Armstrong4766f992022-02-28 16:23:59 +0100609{
610 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
611 psa_key_type_t key_type = key_type_arg;
612 psa_algorithm_t alg = alg_arg;
613 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
614 unsigned char mac[PSA_MAC_MAX_SIZE];
615 size_t part_offset = 0;
616 size_t part_length = 0;
617 size_t data_part_len = 0;
618 size_t mac_len = 0;
619 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
620 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
621
622 int test_ok = 0;
623 size_t part_count = 0;
624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 PSA_INIT();
Neil Armstrong4766f992022-02-28 16:23:59 +0100626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 if (is_verify) {
628 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
629 } else {
630 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
631 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 psa_set_key_algorithm(&attributes, alg);
634 psa_set_key_type(&attributes, key_type);
Neil Armstrong4766f992022-02-28 16:23:59 +0100635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
637 &key));
Neil Armstrong4766f992022-02-28 16:23:59 +0100638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (is_verify) {
640 status = psa_mac_verify_setup(&operation, key, alg);
641 } else {
642 status = psa_mac_sign_setup(&operation, key, alg);
643 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 PSA_ASSERT(status);
Neil Armstrong4766f992022-02-28 16:23:59 +0100646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (data_part_len_arg != -1) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100648 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 data_part_len = (size_t) data_part_len_arg;
Neil Armstrong4766f992022-02-28 16:23:59 +0100650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 for (part_offset = 0, part_count = 0;
Neil Armstrong4766f992022-02-28 16:23:59 +0100652 part_offset < input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 part_offset += part_length, part_count++) {
654 if (do_zero_parts && (part_count & 0x01)) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100655 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 } else if ((input_data->len - part_offset) < data_part_len) {
657 part_length = (input_data->len - part_offset);
658 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100659 part_length = data_part_len;
660 }
661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 PSA_ASSERT(psa_mac_update(&operation,
663 (input_data->x + part_offset),
664 part_length));
Neil Armstrong4766f992022-02-28 16:23:59 +0100665 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100667 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 PSA_ASSERT(psa_mac_update(&operation, input_data->x,
669 input_data->len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100670 }
671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if (is_verify) {
673 PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x,
674 expected_output->len));
675 } else {
676 PSA_ASSERT(psa_mac_sign_finish(&operation, mac,
677 PSA_MAC_MAX_SIZE, &mac_len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 ASSERT_COMPARE(expected_output->x, expected_output->len,
680 mac, mac_len);
Neil Armstrong4766f992022-02-28 16:23:59 +0100681 }
682
683 test_ok = 1;
684
685exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 psa_destroy_key(key);
687 psa_mac_abort(&operation);
688 PSA_DONE();
Neil Armstrong4766f992022-02-28 16:23:59 +0100689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return test_ok;
Neil Armstrong4766f992022-02-28 16:23:59 +0100691}
692
Neil Armstrong75673ab2022-06-15 17:39:01 +0200693#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100694static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
695 psa_pake_operation_t *server,
696 psa_pake_operation_t *client,
697 int client_input_first,
698 int round, int inject_error)
Neil Armstrongf983caf2022-06-15 15:27:48 +0200699{
700 unsigned char *buffer0 = NULL, *buffer1 = NULL;
701 size_t buffer_length = (
702 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
703 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
704 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200705 /* The output should be exactly this size according to the spec */
706 const size_t expected_size_key_share =
707 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
708 /* The output should be exactly this size according to the spec */
709 const size_t expected_size_zk_public =
710 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
711 /* The output can be smaller: the spec allows stripping leading zeroes */
712 const size_t max_expected_size_zk_proof =
713 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200714 size_t buffer0_off = 0;
715 size_t buffer1_off = 0;
716 size_t s_g1_len, s_g2_len, s_a_len;
717 size_t s_g1_off, s_g2_off, s_a_off;
718 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
719 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
720 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
721 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
722 size_t c_g1_len, c_g2_len, c_a_len;
723 size_t c_g1_off, c_g2_off, c_a_off;
724 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
725 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
726 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
727 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
728 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200729 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 ASSERT_ALLOC(buffer0, buffer_length);
732 ASSERT_ALLOC(buffer1, buffer_length);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 switch (round) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200735 case 1:
736 /* Server first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
738 buffer0 + buffer0_off,
739 512 - buffer0_off, &s_g1_len));
740 TEST_EQUAL(s_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200741 s_g1_off = buffer0_off;
742 buffer0_off += s_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
744 buffer0 + buffer0_off,
745 512 - buffer0_off, &s_x1_pk_len));
746 TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200747 s_x1_pk_off = buffer0_off;
748 buffer0_off += s_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
750 buffer0 + buffer0_off,
751 512 - buffer0_off, &s_x1_pr_len));
752 TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200753 s_x1_pr_off = buffer0_off;
754 buffer0_off += s_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
756 buffer0 + buffer0_off,
757 512 - buffer0_off, &s_g2_len));
758 TEST_EQUAL(s_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200759 s_g2_off = buffer0_off;
760 buffer0_off += s_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
762 buffer0 + buffer0_off,
763 512 - buffer0_off, &s_x2_pk_len));
764 TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200765 s_x2_pk_off = buffer0_off;
766 buffer0_off += s_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
768 buffer0 + buffer0_off,
769 512 - buffer0_off, &s_x2_pr_len));
770 TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200771 s_x2_pr_off = buffer0_off;
772 buffer0_off += s_x2_pr_len;
773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (inject_error == 1) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500775 buffer0[s_x1_pr_off + 8] ^= 1;
776 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200777 expected_status = PSA_ERROR_DATA_INVALID;
778 }
779
Neil Armstrong51009d72022-09-05 17:59:54 +0200780 /*
781 * When injecting errors in inputs, the implementation is
782 * free to detect it right away of with a delay.
783 * This permits delaying the error until the end of the input
784 * sequence, if no error appears then, this will be treated
785 * as an error.
786 */
787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200789 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
791 buffer0 + s_g1_off, s_g1_len);
792 if (inject_error == 1 && status != PSA_SUCCESS) {
793 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200794 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 } else {
796 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200797 }
798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
800 buffer0 + s_x1_pk_off,
801 s_x1_pk_len);
802 if (inject_error == 1 && status != PSA_SUCCESS) {
803 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200804 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 } else {
806 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200807 }
808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
810 buffer0 + s_x1_pr_off,
811 s_x1_pr_len);
812 if (inject_error == 1 && status != PSA_SUCCESS) {
813 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200814 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 } else {
816 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200817 }
818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
820 buffer0 + s_g2_off,
821 s_g2_len);
822 if (inject_error == 1 && status != PSA_SUCCESS) {
823 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200824 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 } else {
826 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200827 }
828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
830 buffer0 + s_x2_pk_off,
831 s_x2_pk_len);
832 if (inject_error == 1 && status != PSA_SUCCESS) {
833 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200834 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 } else {
836 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200837 }
838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
840 buffer0 + s_x2_pr_off,
841 s_x2_pr_len);
842 if (inject_error == 1 && status != PSA_SUCCESS) {
843 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200844 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 } else {
846 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200847 }
848
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200849 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (inject_error == 1) {
851 TEST_ASSERT(
852 !"One of the last psa_pake_input() calls should have returned the expected error.");
853 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200854 }
855
856 /* Client first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
858 buffer1 + buffer1_off,
859 512 - buffer1_off, &c_g1_len));
860 TEST_EQUAL(c_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200861 c_g1_off = buffer1_off;
862 buffer1_off += c_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
864 buffer1 + buffer1_off,
865 512 - buffer1_off, &c_x1_pk_len));
866 TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200867 c_x1_pk_off = buffer1_off;
868 buffer1_off += c_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
870 buffer1 + buffer1_off,
871 512 - buffer1_off, &c_x1_pr_len));
872 TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200873 c_x1_pr_off = buffer1_off;
874 buffer1_off += c_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
876 buffer1 + buffer1_off,
877 512 - buffer1_off, &c_g2_len));
878 TEST_EQUAL(c_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200879 c_g2_off = buffer1_off;
880 buffer1_off += c_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
882 buffer1 + buffer1_off,
883 512 - buffer1_off, &c_x2_pk_len));
884 TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200885 c_x2_pk_off = buffer1_off;
886 buffer1_off += c_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
888 buffer1 + buffer1_off,
889 512 - buffer1_off, &c_x2_pr_len));
890 TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200891 c_x2_pr_off = buffer1_off;
892 buffer1_off += c_x2_pr_len;
893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200895 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
897 buffer0 + s_g1_off, s_g1_len);
898 if (inject_error == 1 && status != PSA_SUCCESS) {
899 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200900 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 } else {
902 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200903 }
904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
906 buffer0 + s_x1_pk_off,
907 s_x1_pk_len);
908 if (inject_error == 1 && status != PSA_SUCCESS) {
909 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200910 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 } else {
912 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200913 }
914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
916 buffer0 + s_x1_pr_off,
917 s_x1_pr_len);
918 if (inject_error == 1 && status != PSA_SUCCESS) {
919 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200920 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 } else {
922 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200923 }
924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
926 buffer0 + s_g2_off,
927 s_g2_len);
928 if (inject_error == 1 && status != PSA_SUCCESS) {
929 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200930 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 } else {
932 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200933 }
934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
936 buffer0 + s_x2_pk_off,
937 s_x2_pk_len);
938 if (inject_error == 1 && status != PSA_SUCCESS) {
939 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200940 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 } else {
942 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200943 }
944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
946 buffer0 + s_x2_pr_off,
947 s_x2_pr_len);
948 if (inject_error == 1 && status != PSA_SUCCESS) {
949 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200950 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 } else {
952 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200953 }
954
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200955 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 if (inject_error == 1) {
957 TEST_ASSERT(
958 !"One of the last psa_pake_input() calls should have returned the expected error.");
959 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200960 }
961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 if (inject_error == 2) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500963 buffer1[c_x1_pr_off + 12] ^= 1;
964 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200965 expected_status = PSA_ERROR_DATA_INVALID;
966 }
967
968 /* Server first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
970 buffer1 + c_g1_off, c_g1_len);
971 if (inject_error == 2 && status != PSA_SUCCESS) {
972 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200973 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 } else {
975 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200976 }
977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
979 buffer1 + c_x1_pk_off, c_x1_pk_len);
980 if (inject_error == 2 && status != PSA_SUCCESS) {
981 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200982 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 } else {
984 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200985 }
986
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
988 buffer1 + c_x1_pr_off, c_x1_pr_len);
989 if (inject_error == 2 && status != PSA_SUCCESS) {
990 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200991 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 } else {
993 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200994 }
995
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
997 buffer1 + c_g2_off, c_g2_len);
998 if (inject_error == 2 && status != PSA_SUCCESS) {
999 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001000 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 } else {
1002 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001003 }
1004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1006 buffer1 + c_x2_pk_off, c_x2_pk_len);
1007 if (inject_error == 2 && status != PSA_SUCCESS) {
1008 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001009 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 } else {
1011 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001012 }
1013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1015 buffer1 + c_x2_pr_off, c_x2_pr_len);
1016 if (inject_error == 2 && status != PSA_SUCCESS) {
1017 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001018 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 } else {
1020 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001021 }
1022
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001023 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 if (inject_error == 2) {
1025 TEST_ASSERT(
1026 !"One of the last psa_pake_input() calls should have returned the expected error.");
1027 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001028
1029 break;
1030
1031 case 2:
1032 /* Server second round Output */
1033 buffer0_off = 0;
1034
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
1036 buffer0 + buffer0_off,
1037 512 - buffer0_off, &s_a_len));
1038 TEST_EQUAL(s_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001039 s_a_off = buffer0_off;
1040 buffer0_off += s_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
1042 buffer0 + buffer0_off,
1043 512 - buffer0_off, &s_x2s_pk_len));
1044 TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001045 s_x2s_pk_off = buffer0_off;
1046 buffer0_off += s_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
1048 buffer0 + buffer0_off,
1049 512 - buffer0_off, &s_x2s_pr_len));
1050 TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001051 s_x2s_pr_off = buffer0_off;
1052 buffer0_off += s_x2s_pr_len;
1053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if (inject_error == 3) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001055 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001056 expected_status = PSA_ERROR_DATA_INVALID;
1057 }
1058
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001060 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1062 buffer0 + s_a_off, s_a_len);
1063 if (inject_error == 3 && status != PSA_SUCCESS) {
1064 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001065 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 } else {
1067 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001068 }
1069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1071 buffer0 + s_x2s_pk_off,
1072 s_x2s_pk_len);
1073 if (inject_error == 3 && status != PSA_SUCCESS) {
1074 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001075 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 } else {
1077 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001078 }
1079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1081 buffer0 + s_x2s_pr_off,
1082 s_x2s_pr_len);
1083 if (inject_error == 3 && status != PSA_SUCCESS) {
1084 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001085 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 } else {
1087 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001088 }
1089
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001090 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (inject_error == 3) {
1092 TEST_ASSERT(
1093 !"One of the last psa_pake_input() calls should have returned the expected error.");
1094 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001095 }
1096
1097 /* Client second round Output */
1098 buffer1_off = 0;
1099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
1101 buffer1 + buffer1_off,
1102 512 - buffer1_off, &c_a_len));
1103 TEST_EQUAL(c_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001104 c_a_off = buffer1_off;
1105 buffer1_off += c_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
1107 buffer1 + buffer1_off,
1108 512 - buffer1_off, &c_x2s_pk_len));
1109 TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001110 c_x2s_pk_off = buffer1_off;
1111 buffer1_off += c_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
1113 buffer1 + buffer1_off,
1114 512 - buffer1_off, &c_x2s_pr_len));
1115 TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001116 c_x2s_pr_off = buffer1_off;
1117 buffer1_off += c_x2s_pr_len;
1118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001120 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1122 buffer0 + s_a_off, s_a_len);
1123 if (inject_error == 3 && status != PSA_SUCCESS) {
1124 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001125 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 } else {
1127 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001128 }
1129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1131 buffer0 + s_x2s_pk_off,
1132 s_x2s_pk_len);
1133 if (inject_error == 3 && status != PSA_SUCCESS) {
1134 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001135 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 } else {
1137 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001138 }
1139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1141 buffer0 + s_x2s_pr_off,
1142 s_x2s_pr_len);
1143 if (inject_error == 3 && status != PSA_SUCCESS) {
1144 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001145 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 } else {
1147 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001148 }
1149
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001150 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 if (inject_error == 3) {
1152 TEST_ASSERT(
1153 !"One of the last psa_pake_input() calls should have returned the expected error.");
1154 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001155 }
1156
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 if (inject_error == 4) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001158 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001159 expected_status = PSA_ERROR_DATA_INVALID;
1160 }
1161
1162 /* Server second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1164 buffer1 + c_a_off, c_a_len);
1165 if (inject_error == 4 && status != PSA_SUCCESS) {
1166 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001167 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 } else {
1169 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001170 }
1171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1173 buffer1 + c_x2s_pk_off, c_x2s_pk_len);
1174 if (inject_error == 4 && status != PSA_SUCCESS) {
1175 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001176 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 } else {
1178 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001179 }
1180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1182 buffer1 + c_x2s_pr_off, c_x2s_pr_len);
1183 if (inject_error == 4 && status != PSA_SUCCESS) {
1184 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001185 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 } else {
1187 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001188 }
1189
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001190 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if (inject_error == 4) {
1192 TEST_ASSERT(
1193 !"One of the last psa_pake_input() calls should have returned the expected error.");
1194 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001195
1196 break;
1197
1198 }
1199
Neil Armstrongf983caf2022-06-15 15:27:48 +02001200exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 mbedtls_free(buffer0);
1202 mbedtls_free(buffer1);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001203}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001204#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206typedef enum {
Valerio Setti1070aed2022-11-11 19:37:31 +01001207 INJECT_ERR_NONE = 0,
1208 INJECT_ERR_UNINITIALIZED_ACCESS,
1209 INJECT_ERR_DUPLICATE_SETUP,
1210 INJECT_ERR_INVALID_USER,
1211 INJECT_ERR_INVALID_PEER,
1212 INJECT_ERR_SET_USER,
1213 INJECT_ERR_SET_PEER,
1214 INJECT_EMPTY_IO_BUFFER,
1215 INJECT_UNKNOWN_STEP,
1216 INJECT_INVALID_FIRST_STEP,
1217 INJECT_WRONG_BUFFER_SIZE,
1218 INJECT_VALID_OPERATION_AFTER_FAILURE,
1219 INJECT_ANTICIPATE_KEY_DERIVATION_1,
1220 INJECT_ANTICIPATE_KEY_DERIVATION_2,
1221} ecjpake_injected_failure_t;
1222
Gilles Peskinee59236f2018-01-27 23:32:46 +01001223/* END_HEADER */
1224
1225/* BEGIN_DEPENDENCIES
1226 * depends_on:MBEDTLS_PSA_CRYPTO_C
1227 * END_DEPENDENCIES
1228 */
1229
1230/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001231void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001232{
1233 size_t max_truncated_mac_size =
1234 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1235
1236 /* Check that the length for a truncated MAC always fits in the algorithm
1237 * encoding. The shifted mask is the maximum truncated value. The
1238 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001240}
1241/* END_CASE */
1242
1243/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001244void import_with_policy(int type_arg,
1245 int usage_arg, int alg_arg,
1246 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001247{
1248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1249 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001250 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001251 psa_key_type_t type = type_arg;
1252 psa_key_usage_t usage = usage_arg;
1253 psa_algorithm_t alg = alg_arg;
1254 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001256 psa_status_t status;
1257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 psa_set_key_type(&attributes, type);
1261 psa_set_key_usage_flags(&attributes, usage);
1262 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 status = psa_import_key(&attributes,
1265 key_material, sizeof(key_material),
1266 &key);
1267 TEST_EQUAL(status, expected_status);
1268 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001269 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1273 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1274 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1275 mbedtls_test_update_key_usage_flags(usage));
1276 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1277 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 PSA_ASSERT(psa_destroy_key(key));
1280 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001281
1282exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001283 /*
1284 * Key attributes may have been returned by psa_get_key_attributes()
1285 * thus reset them as required.
1286 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001288
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 psa_destroy_key(key);
1290 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001291}
1292/* END_CASE */
1293
1294/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001295void import_with_data(data_t *data, int type_arg,
1296 int attr_bits_arg,
1297 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001298{
1299 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1300 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001301 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001302 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001303 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001304 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001305 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 psa_set_key_type(&attributes, type);
1310 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001311
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 status = psa_import_key(&attributes, data->x, data->len, &key);
1313 TEST_EQUAL(status, expected_status);
1314 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001315 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1319 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1320 if (attr_bits != 0) {
1321 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1322 }
1323 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 PSA_ASSERT(psa_destroy_key(key));
1326 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001327
1328exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001329 /*
1330 * Key attributes may have been returned by psa_get_key_attributes()
1331 * thus reset them as required.
1332 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 psa_destroy_key(key);
1336 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001337}
1338/* END_CASE */
1339
1340/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001341/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001342void import_large_key(int type_arg, int byte_size_arg,
1343 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001344{
1345 psa_key_type_t type = type_arg;
1346 size_t byte_size = byte_size_arg;
1347 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1348 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001349 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001350 psa_status_t status;
1351 uint8_t *buffer = NULL;
1352 size_t buffer_size = byte_size + 1;
1353 size_t n;
1354
Steven Cooreman69967ce2021-01-18 18:01:08 +01001355 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001356 * accommodate large keys due to heap size constraints */
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 ASSERT_ALLOC_WEAK(buffer, buffer_size);
1358 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001361
1362 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1364 psa_set_key_type(&attributes, type);
1365 status = psa_import_key(&attributes, buffer, byte_size, &key);
1366 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1367 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 if (status == PSA_SUCCESS) {
1370 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1371 TEST_EQUAL(psa_get_key_type(&attributes), type);
1372 TEST_EQUAL(psa_get_key_bits(&attributes),
1373 PSA_BYTES_TO_BITS(byte_size));
1374 ASSERT_NO_SLOT_NUMBER(&attributes);
1375 memset(buffer, 0, byte_size + 1);
1376 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1377 for (n = 0; n < byte_size; n++) {
1378 TEST_EQUAL(buffer[n], 'K');
1379 }
1380 for (n = byte_size; n < buffer_size; n++) {
1381 TEST_EQUAL(buffer[n], 0);
1382 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001383 }
1384
1385exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001386 /*
1387 * Key attributes may have been returned by psa_get_key_attributes()
1388 * thus reset them as required.
1389 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001391
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 psa_destroy_key(key);
1393 PSA_DONE();
1394 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001395}
1396/* END_CASE */
1397
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001398/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001399/* Import an RSA key with a valid structure (but not valid numbers
1400 * inside, beyond having sensible size and parity). This is expected to
1401 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001402void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001403{
Ronald Cron5425a212020-08-04 14:58:35 +02001404 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001405 size_t bits = bits_arg;
1406 psa_status_t expected_status = expected_status_arg;
1407 psa_status_t status;
1408 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001409 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001410 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001412 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001413 unsigned char *p;
1414 int ret;
1415 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001416 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 PSA_ASSERT(psa_crypto_init());
1419 ASSERT_ALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1422 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001423 length = ret;
1424
1425 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 psa_set_key_type(&attributes, type);
1427 status = psa_import_key(&attributes, p, length, &key);
1428 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 if (status == PSA_SUCCESS) {
1431 PSA_ASSERT(psa_destroy_key(key));
1432 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001433
1434exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 mbedtls_free(buffer);
1436 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001437}
1438/* END_CASE */
1439
1440/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001441void import_export(data_t *data,
1442 int type_arg,
1443 int usage_arg, int alg_arg,
1444 int lifetime_arg,
1445 int expected_bits,
1446 int export_size_delta,
1447 int expected_export_status_arg,
1448 /*whether reexport must give the original input exactly*/
1449 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001450{
Ronald Cron5425a212020-08-04 14:58:35 +02001451 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001452 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001453 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001454 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001455 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301456 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001457 unsigned char *exported = NULL;
1458 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001459 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001460 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001461 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001462 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001463 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001464
Moran Pekercb088e72018-07-17 17:36:59 +03001465 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 ASSERT_ALLOC(exported, export_size);
1467 if (!canonical_input) {
1468 ASSERT_ALLOC(reexported, export_size);
1469 }
1470 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 psa_set_key_lifetime(&attributes, lifetime);
1473 psa_set_key_usage_flags(&attributes, usage_arg);
1474 psa_set_key_algorithm(&attributes, alg);
1475 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001476
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001477 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001479
1480 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1482 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1483 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1484 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001485
1486 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 status = psa_export_key(key, exported, export_size, &exported_length);
1488 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001489
1490 /* The exported length must be set by psa_export_key() to a value between 0
1491 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1493 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1494 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1497 export_size - exported_length));
1498 if (status != PSA_SUCCESS) {
1499 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001500 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001501 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001502
Gilles Peskineea38a922021-02-13 00:05:16 +01001503 /* Run sanity checks on the exported key. For non-canonical inputs,
1504 * this validates the canonical representations. For canonical inputs,
1505 * this doesn't directly validate the implementation, but it still helps
1506 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 if (!psa_key_lifetime_is_external(lifetime)) {
1508 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301509 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 }
Archana4d7ae1d2021-07-07 02:50:22 +05301511 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001512
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 if (canonical_input) {
1514 ASSERT_COMPARE(data->x, data->len, exported, exported_length);
1515 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001516 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1518 &key2));
1519 PSA_ASSERT(psa_export_key(key2,
1520 reexported,
1521 export_size,
1522 &reexported_length));
1523 ASSERT_COMPARE(exported, exported_length,
1524 reexported, reexported_length);
1525 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001526 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 TEST_LE_U(exported_length,
1528 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1529 psa_get_key_bits(&got_attributes)));
1530 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001531
1532destroy:
1533 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 PSA_ASSERT(psa_destroy_key(key));
1535 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001536
1537exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001538 /*
1539 * Key attributes may have been returned by psa_get_key_attributes()
1540 * thus reset them as required.
1541 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 psa_reset_key_attributes(&got_attributes);
1543 psa_destroy_key(key);
1544 mbedtls_free(exported);
1545 mbedtls_free(reexported);
1546 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001547}
1548/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001549
Moran Pekerf709f4a2018-06-06 17:26:04 +03001550/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001551void import_export_public_key(data_t *data,
1552 int type_arg, // key pair or public key
1553 int alg_arg,
1554 int lifetime_arg,
1555 int export_size_delta,
1556 int expected_export_status_arg,
1557 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001558{
Ronald Cron5425a212020-08-04 14:58:35 +02001559 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001560 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001561 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001562 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001563 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301564 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001565 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001566 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001567 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001568 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 psa_set_key_lifetime(&attributes, lifetime);
1573 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1574 psa_set_key_algorithm(&attributes, alg);
1575 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001576
1577 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001579
Gilles Peskine49c25912018-10-29 15:15:31 +01001580 /* Export the public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 ASSERT_ALLOC(exported, export_size);
1582 status = psa_export_public_key(key,
1583 exported, export_size,
1584 &exported_length);
1585 TEST_EQUAL(status, expected_export_status);
1586 if (status == PSA_SUCCESS) {
1587 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001588 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1590 bits = psa_get_key_bits(&attributes);
1591 TEST_LE_U(expected_public_key->len,
1592 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1593 TEST_LE_U(expected_public_key->len,
1594 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1595 TEST_LE_U(expected_public_key->len,
1596 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1597 ASSERT_COMPARE(expected_public_key->x, expected_public_key->len,
1598 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001599 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001600exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001601 /*
1602 * Key attributes may have been returned by psa_get_key_attributes()
1603 * thus reset them as required.
1604 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001606
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 mbedtls_free(exported);
1608 psa_destroy_key(key);
1609 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001610}
1611/* END_CASE */
1612
Gilles Peskine20035e32018-02-03 22:44:14 +01001613/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001614void import_and_exercise_key(data_t *data,
1615 int type_arg,
1616 int bits_arg,
1617 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001618{
Ronald Cron5425a212020-08-04 14:58:35 +02001619 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001620 psa_key_type_t type = type_arg;
1621 size_t bits = bits_arg;
1622 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001625 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 psa_set_key_usage_flags(&attributes, usage);
1630 psa_set_key_algorithm(&attributes, alg);
1631 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001632
1633 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001635
1636 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1638 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1639 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001640
1641 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001643 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001645
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 PSA_ASSERT(psa_destroy_key(key));
1647 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001648
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001649exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001650 /*
1651 * Key attributes may have been returned by psa_get_key_attributes()
1652 * thus reset them as required.
1653 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001655
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 psa_reset_key_attributes(&attributes);
1657 psa_destroy_key(key);
1658 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001659}
1660/* END_CASE */
1661
1662/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001663void effective_key_attributes(int type_arg, int expected_type_arg,
1664 int bits_arg, int expected_bits_arg,
1665 int usage_arg, int expected_usage_arg,
1666 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001667{
Ronald Cron5425a212020-08-04 14:58:35 +02001668 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001669 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001670 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001671 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001672 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001673 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001674 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001675 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001676 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001677 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001678
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001680
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 psa_set_key_usage_flags(&attributes, usage);
1682 psa_set_key_algorithm(&attributes, alg);
1683 psa_set_key_type(&attributes, key_type);
1684 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001685
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 PSA_ASSERT(psa_generate_key(&attributes, &key));
1687 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1690 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1691 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1692 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1693 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001694
1695exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001696 /*
1697 * Key attributes may have been returned by psa_get_key_attributes()
1698 * thus reset them as required.
1699 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001701
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 psa_destroy_key(key);
1703 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001704}
1705/* END_CASE */
1706
1707/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001708void check_key_policy(int type_arg, int bits_arg,
1709 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001710{
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1712 usage_arg,
1713 mbedtls_test_update_key_usage_flags(usage_arg),
1714 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001715 goto exit;
1716}
1717/* END_CASE */
1718
1719/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001720void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001721{
1722 /* Test each valid way of initializing the object, except for `= {0}`, as
1723 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1724 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001725 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001727 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1728 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001729
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1733 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1734 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001735
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 TEST_EQUAL(psa_get_key_type(&func), 0);
1737 TEST_EQUAL(psa_get_key_type(&init), 0);
1738 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001739
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 TEST_EQUAL(psa_get_key_bits(&func), 0);
1741 TEST_EQUAL(psa_get_key_bits(&init), 0);
1742 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001743
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1745 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1746 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1749 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1750 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001751}
1752/* END_CASE */
1753
1754/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001755void mac_key_policy(int policy_usage_arg,
1756 int policy_alg_arg,
1757 int key_type_arg,
1758 data_t *key_data,
1759 int exercise_alg_arg,
1760 int expected_status_sign_arg,
1761 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001762{
Ronald Cron5425a212020-08-04 14:58:35 +02001763 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001764 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001765 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001766 psa_key_type_t key_type = key_type_arg;
1767 psa_algorithm_t policy_alg = policy_alg_arg;
1768 psa_algorithm_t exercise_alg = exercise_alg_arg;
1769 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001770 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001771 psa_status_t expected_status_sign = expected_status_sign_arg;
1772 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001773 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 psa_set_key_usage_flags(&attributes, policy_usage);
1778 psa_set_key_algorithm(&attributes, policy_alg);
1779 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1782 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001783
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1785 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001786
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1788 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001789
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001790 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001792 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1794 input, 128,
1795 mac, PSA_MAC_MAX_SIZE, &mac_len),
1796 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001797
Neil Armstrong3af9b972022-02-07 12:20:21 +01001798 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 PSA_ASSERT(psa_mac_abort(&operation));
1800 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1801 if (status == PSA_SUCCESS) {
1802 status = psa_mac_update(&operation, input, 128);
1803 if (status == PSA_SUCCESS) {
1804 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1805 &mac_len),
1806 expected_status_sign);
1807 } else {
1808 TEST_EQUAL(status, expected_status_sign);
1809 }
1810 } else {
1811 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001812 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001814
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001815 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 status = psa_mac_verify(key, exercise_alg, input, 128,
1817 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001818
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1820 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1821 } else {
1822 TEST_EQUAL(status, expected_status_verify);
1823 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001824
Neil Armstrong3af9b972022-02-07 12:20:21 +01001825 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1827 if (status == PSA_SUCCESS) {
1828 status = psa_mac_update(&operation, input, 128);
1829 if (status == PSA_SUCCESS) {
1830 status = psa_mac_verify_finish(&operation, mac, mac_len);
1831 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1832 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1833 } else {
1834 TEST_EQUAL(status, expected_status_verify);
1835 }
1836 } else {
1837 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001838 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 } else {
1840 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001841 }
1842
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001844
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 memset(mac, 0, sizeof(mac));
1846 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1847 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001848
1849exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 psa_mac_abort(&operation);
1851 psa_destroy_key(key);
1852 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001853}
1854/* END_CASE */
1855
1856/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001857void cipher_key_policy(int policy_usage_arg,
1858 int policy_alg,
1859 int key_type,
1860 data_t *key_data,
1861 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001862{
Ronald Cron5425a212020-08-04 14:58:35 +02001863 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001864 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001865 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001866 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001867 size_t output_buffer_size = 0;
1868 size_t input_buffer_size = 0;
1869 size_t output_length = 0;
1870 uint8_t *output = NULL;
1871 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001872 psa_status_t status;
1873
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1875 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1876 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001877
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 ASSERT_ALLOC(input, input_buffer_size);
1879 ASSERT_ALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001880
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001882
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 psa_set_key_usage_flags(&attributes, policy_usage);
1884 psa_set_key_algorithm(&attributes, policy_alg);
1885 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1888 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001889
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001890 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 TEST_EQUAL(policy_usage,
1892 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001893
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001894 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1896 output, output_buffer_size,
1897 &output_length);
1898 if (policy_alg == exercise_alg &&
1899 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1900 PSA_ASSERT(status);
1901 } else {
1902 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1903 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001904
1905 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1907 if (policy_alg == exercise_alg &&
1908 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1909 PSA_ASSERT(status);
1910 } else {
1911 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1912 }
1913 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001914
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001915 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1917 input, input_buffer_size,
1918 &output_length);
1919 if (policy_alg == exercise_alg &&
1920 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1921 PSA_ASSERT(status);
1922 } else {
1923 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1924 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001925
1926 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
1928 if (policy_alg == exercise_alg &&
1929 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1930 PSA_ASSERT(status);
1931 } else {
1932 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1933 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001934
1935exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 psa_cipher_abort(&operation);
1937 mbedtls_free(input);
1938 mbedtls_free(output);
1939 psa_destroy_key(key);
1940 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001941}
1942/* END_CASE */
1943
1944/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001945void aead_key_policy(int policy_usage_arg,
1946 int policy_alg,
1947 int key_type,
1948 data_t *key_data,
1949 int nonce_length_arg,
1950 int tag_length_arg,
1951 int exercise_alg,
1952 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001953{
Ronald Cron5425a212020-08-04 14:58:35 +02001954 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001955 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01001956 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001957 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001958 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001959 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001961 size_t nonce_length = nonce_length_arg;
1962 unsigned char tag[16];
1963 size_t tag_length = tag_length_arg;
1964 size_t output_length;
1965
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 TEST_LE_U(nonce_length, sizeof(nonce));
1967 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001968
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001970
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 psa_set_key_usage_flags(&attributes, policy_usage);
1972 psa_set_key_algorithm(&attributes, policy_alg);
1973 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001974
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1976 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001977
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001978 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 TEST_EQUAL(policy_usage,
1980 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001981
Neil Armstrong752d8112022-02-07 14:51:11 +01001982 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 status = psa_aead_encrypt(key, exercise_alg,
1984 nonce, nonce_length,
1985 NULL, 0,
1986 NULL, 0,
1987 tag, tag_length,
1988 &output_length);
1989 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1990 TEST_EQUAL(status, expected_status);
1991 } else {
1992 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1993 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001994
Neil Armstrong752d8112022-02-07 14:51:11 +01001995 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
1997 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1998 TEST_EQUAL(status, expected_status);
1999 } else {
2000 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2001 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002002
2003 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 memset(tag, 0, sizeof(tag));
2005 status = psa_aead_decrypt(key, exercise_alg,
2006 nonce, nonce_length,
2007 NULL, 0,
2008 tag, tag_length,
2009 NULL, 0,
2010 &output_length);
2011 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2012 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2013 } else if (expected_status == PSA_SUCCESS) {
2014 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2015 } else {
2016 TEST_EQUAL(status, expected_status);
2017 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002018
Neil Armstrong752d8112022-02-07 14:51:11 +01002019 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 PSA_ASSERT(psa_aead_abort(&operation));
2021 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2022 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2023 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2024 } else {
2025 TEST_EQUAL(status, expected_status);
2026 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002027
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002028exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 PSA_ASSERT(psa_aead_abort(&operation));
2030 psa_destroy_key(key);
2031 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002032}
2033/* END_CASE */
2034
2035/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002036void asymmetric_encryption_key_policy(int policy_usage_arg,
2037 int policy_alg,
2038 int key_type,
2039 data_t *key_data,
2040 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002041{
Ronald Cron5425a212020-08-04 14:58:35 +02002042 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002043 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002044 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002045 psa_status_t status;
2046 size_t key_bits;
2047 size_t buffer_length;
2048 unsigned char *buffer = NULL;
2049 size_t output_length;
2050
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002052
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 psa_set_key_usage_flags(&attributes, policy_usage);
2054 psa_set_key_algorithm(&attributes, policy_alg);
2055 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2058 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002059
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002060 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 TEST_EQUAL(policy_usage,
2062 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2065 key_bits = psa_get_key_bits(&attributes);
2066 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2067 exercise_alg);
2068 ASSERT_ALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002069
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 status = psa_asymmetric_encrypt(key, exercise_alg,
2071 NULL, 0,
2072 NULL, 0,
2073 buffer, buffer_length,
2074 &output_length);
2075 if (policy_alg == exercise_alg &&
2076 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2077 PSA_ASSERT(status);
2078 } else {
2079 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2080 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002081
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 if (buffer_length != 0) {
2083 memset(buffer, 0, buffer_length);
2084 }
2085 status = psa_asymmetric_decrypt(key, exercise_alg,
2086 buffer, buffer_length,
2087 NULL, 0,
2088 buffer, buffer_length,
2089 &output_length);
2090 if (policy_alg == exercise_alg &&
2091 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2092 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2093 } else {
2094 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2095 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002096
2097exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002098 /*
2099 * Key attributes may have been returned by psa_get_key_attributes()
2100 * thus reset them as required.
2101 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002103
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 psa_destroy_key(key);
2105 PSA_DONE();
2106 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002107}
2108/* END_CASE */
2109
2110/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002111void asymmetric_signature_key_policy(int policy_usage_arg,
2112 int policy_alg,
2113 int key_type,
2114 data_t *key_data,
2115 int exercise_alg,
2116 int payload_length_arg,
2117 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002118{
Ronald Cron5425a212020-08-04 14:58:35 +02002119 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002120 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002121 psa_key_usage_t policy_usage = policy_usage_arg;
2122 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002123 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002125 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2126 * compatible with the policy and `payload_length_arg` is supposed to be
2127 * a valid input length to sign. If `payload_length_arg <= 0`,
2128 * `exercise_alg` is supposed to be forbidden by the policy. */
2129 int compatible_alg = payload_length_arg > 0;
2130 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002132 size_t signature_length;
2133
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002134 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002135 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 TEST_EQUAL(expected_usage,
2137 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002140
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 psa_set_key_usage_flags(&attributes, policy_usage);
2142 psa_set_key_algorithm(&attributes, policy_alg);
2143 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002144
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2146 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002147
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002149
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 status = psa_sign_hash(key, exercise_alg,
2151 payload, payload_length,
2152 signature, sizeof(signature),
2153 &signature_length);
2154 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2155 PSA_ASSERT(status);
2156 } else {
2157 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2158 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 memset(signature, 0, sizeof(signature));
2161 status = psa_verify_hash(key, exercise_alg,
2162 payload, payload_length,
2163 signature, sizeof(signature));
2164 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2165 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2166 } else {
2167 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2168 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2171 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2172 status = psa_sign_message(key, exercise_alg,
2173 payload, payload_length,
2174 signature, sizeof(signature),
2175 &signature_length);
2176 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2177 PSA_ASSERT(status);
2178 } else {
2179 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2180 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002181
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 memset(signature, 0, sizeof(signature));
2183 status = psa_verify_message(key, exercise_alg,
2184 payload, payload_length,
2185 signature, sizeof(signature));
2186 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2187 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2188 } else {
2189 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2190 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002191 }
2192
Gilles Peskined5b33222018-06-18 22:20:03 +02002193exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 psa_destroy_key(key);
2195 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002196}
2197/* END_CASE */
2198
Janos Follathba3fab92019-06-11 14:50:16 +01002199/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002200void derive_key_policy(int policy_usage,
2201 int policy_alg,
2202 int key_type,
2203 data_t *key_data,
2204 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002205{
Ronald Cron5425a212020-08-04 14:58:35 +02002206 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002208 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002209 psa_status_t status;
2210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 psa_set_key_usage_flags(&attributes, policy_usage);
2214 psa_set_key_algorithm(&attributes, policy_alg);
2215 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2218 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002221
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2223 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2224 PSA_ASSERT(psa_key_derivation_input_bytes(
2225 &operation,
2226 PSA_KEY_DERIVATION_INPUT_SEED,
2227 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002228 }
Janos Follathba3fab92019-06-11 14:50:16 +01002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 status = psa_key_derivation_input_key(&operation,
2231 PSA_KEY_DERIVATION_INPUT_SECRET,
2232 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 if (policy_alg == exercise_alg &&
2235 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2236 PSA_ASSERT(status);
2237 } else {
2238 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2239 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002240
2241exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 psa_key_derivation_abort(&operation);
2243 psa_destroy_key(key);
2244 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002245}
2246/* END_CASE */
2247
2248/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002249void agreement_key_policy(int policy_usage,
2250 int policy_alg,
2251 int key_type_arg,
2252 data_t *key_data,
2253 int exercise_alg,
2254 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002255{
Ronald Cron5425a212020-08-04 14:58:35 +02002256 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002257 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002258 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002259 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002260 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002261 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002264
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 psa_set_key_usage_flags(&attributes, policy_usage);
2266 psa_set_key_algorithm(&attributes, policy_alg);
2267 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2270 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002271
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2273 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002274
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002276
2277exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 psa_key_derivation_abort(&operation);
2279 psa_destroy_key(key);
2280 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002281}
2282/* END_CASE */
2283
2284/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002285void key_policy_alg2(int key_type_arg, data_t *key_data,
2286 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002287{
Ronald Cron5425a212020-08-04 14:58:35 +02002288 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002289 psa_key_type_t key_type = key_type_arg;
2290 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2291 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2292 psa_key_usage_t usage = usage_arg;
2293 psa_algorithm_t alg = alg_arg;
2294 psa_algorithm_t alg2 = alg2_arg;
2295
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 psa_set_key_usage_flags(&attributes, usage);
2299 psa_set_key_algorithm(&attributes, alg);
2300 psa_set_key_enrollment_algorithm(&attributes, alg2);
2301 psa_set_key_type(&attributes, key_type);
2302 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2303 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002304
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002305 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 usage = mbedtls_test_update_key_usage_flags(usage);
2307 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2308 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2309 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2310 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002313 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 }
2315 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002316 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002318
2319exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002320 /*
2321 * Key attributes may have been returned by psa_get_key_attributes()
2322 * thus reset them as required.
2323 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 psa_destroy_key(key);
2327 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002328}
2329/* END_CASE */
2330
2331/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002332void raw_agreement_key_policy(int policy_usage,
2333 int policy_alg,
2334 int key_type_arg,
2335 data_t *key_data,
2336 int exercise_alg,
2337 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002338{
Ronald Cron5425a212020-08-04 14:58:35 +02002339 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002340 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002341 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002342 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002343 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002344 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002347
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 psa_set_key_usage_flags(&attributes, policy_usage);
2349 psa_set_key_algorithm(&attributes, policy_alg);
2350 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002351
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2353 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002358
2359exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 psa_key_derivation_abort(&operation);
2361 psa_destroy_key(key);
2362 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002363}
2364/* END_CASE */
2365
2366/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002367void copy_success(int source_usage_arg,
2368 int source_alg_arg, int source_alg2_arg,
2369 unsigned int source_lifetime_arg,
2370 int type_arg, data_t *material,
2371 int copy_attributes,
2372 int target_usage_arg,
2373 int target_alg_arg, int target_alg2_arg,
2374 unsigned int target_lifetime_arg,
2375 int expected_usage_arg,
2376 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002377{
Gilles Peskineca25db92019-04-19 11:43:08 +02002378 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2379 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002380 psa_key_usage_t expected_usage = expected_usage_arg;
2381 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002382 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302383 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2384 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002385 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2386 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002387 uint8_t *export_buffer = NULL;
2388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002390
Gilles Peskineca25db92019-04-19 11:43:08 +02002391 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2393 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2394 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2395 psa_set_key_type(&source_attributes, type_arg);
2396 psa_set_key_lifetime(&source_attributes, source_lifetime);
2397 PSA_ASSERT(psa_import_key(&source_attributes,
2398 material->x, material->len,
2399 &source_key));
2400 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002401
Gilles Peskineca25db92019-04-19 11:43:08 +02002402 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002404 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002405 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002407
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 if (target_usage_arg != -1) {
2409 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2410 }
2411 if (target_alg_arg != -1) {
2412 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2413 }
2414 if (target_alg2_arg != -1) {
2415 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2416 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002417
Archana8a180362021-07-05 02:18:48 +05302418
Gilles Peskine57ab7212019-01-28 13:03:09 +01002419 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 PSA_ASSERT(psa_copy_key(source_key,
2421 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002422
2423 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002425
2426 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2428 TEST_EQUAL(psa_get_key_type(&source_attributes),
2429 psa_get_key_type(&target_attributes));
2430 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2431 psa_get_key_bits(&target_attributes));
2432 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2433 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2434 TEST_EQUAL(expected_alg2,
2435 psa_get_key_enrollment_algorithm(&target_attributes));
2436 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002437 size_t length;
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 ASSERT_ALLOC(export_buffer, material->len);
2439 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2440 material->len, &length));
2441 ASSERT_COMPARE(material->x, material->len,
2442 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002443 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 if (!psa_key_lifetime_is_external(target_lifetime)) {
2446 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302447 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 }
2449 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302450 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 }
Archana8a180362021-07-05 02:18:48 +05302452 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002453
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002455
2456exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002457 /*
2458 * Source and target key attributes may have been returned by
2459 * psa_get_key_attributes() thus reset them as required.
2460 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 psa_reset_key_attributes(&source_attributes);
2462 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002463
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 PSA_DONE();
2465 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002466}
2467/* END_CASE */
2468
2469/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002470void copy_fail(int source_usage_arg,
2471 int source_alg_arg, int source_alg2_arg,
2472 int source_lifetime_arg,
2473 int type_arg, data_t *material,
2474 int target_type_arg, int target_bits_arg,
2475 int target_usage_arg,
2476 int target_alg_arg, int target_alg2_arg,
2477 int target_id_arg, int target_lifetime_arg,
2478 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002479{
2480 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2481 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002482 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2483 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002487
2488 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2490 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2491 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2492 psa_set_key_type(&source_attributes, type_arg);
2493 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2494 PSA_ASSERT(psa_import_key(&source_attributes,
2495 material->x, material->len,
2496 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002497
2498 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 psa_set_key_id(&target_attributes, key_id);
2500 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2501 psa_set_key_type(&target_attributes, target_type_arg);
2502 psa_set_key_bits(&target_attributes, target_bits_arg);
2503 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2504 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2505 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002506
2507 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 TEST_EQUAL(psa_copy_key(source_key,
2509 &target_attributes, &target_key),
2510 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002513
Gilles Peskine4a644642019-05-03 17:14:08 +02002514exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 psa_reset_key_attributes(&source_attributes);
2516 psa_reset_key_attributes(&target_attributes);
2517 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002518}
2519/* END_CASE */
2520
2521/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002522void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002523{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002524 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002525 /* Test each valid way of initializing the object, except for `= {0}`, as
2526 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2527 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002528 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002530 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2531 psa_hash_operation_t zero;
2532
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002534
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002535 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2537 PSA_ERROR_BAD_STATE);
2538 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2539 PSA_ERROR_BAD_STATE);
2540 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2541 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002542
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002543 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 PSA_ASSERT(psa_hash_abort(&func));
2545 PSA_ASSERT(psa_hash_abort(&init));
2546 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002547}
2548/* END_CASE */
2549
2550/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002551void hash_setup(int alg_arg,
2552 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002553{
2554 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002555 uint8_t *output = NULL;
2556 size_t output_size = 0;
2557 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002558 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002559 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002560 psa_status_t status;
2561
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002563
Neil Armstrongedb20862022-02-07 15:47:44 +01002564 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 output_size = PSA_HASH_LENGTH(alg);
2566 ASSERT_ALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 status = psa_hash_compute(alg, NULL, 0,
2569 output, output_size, &output_length);
2570 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002571
2572 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 status = psa_hash_setup(&operation, alg);
2574 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002575
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002576 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002578
2579 /* If setup failed, reproduce the failure, so as to
2580 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 if (status != PSA_SUCCESS) {
2582 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2583 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002584
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002585 /* Now the operation object should be reusable. */
2586#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2588 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002589#endif
2590
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002591exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 mbedtls_free(output);
2593 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002594}
2595/* END_CASE */
2596
2597/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002598void hash_compute_fail(int alg_arg, data_t *input,
2599 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002600{
2601 psa_algorithm_t alg = alg_arg;
2602 uint8_t *output = NULL;
2603 size_t output_size = output_size_arg;
2604 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002605 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002606 psa_status_t expected_status = expected_status_arg;
2607 psa_status_t status;
2608
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 ASSERT_ALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002612
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002613 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 status = psa_hash_compute(alg, input->x, input->len,
2615 output, output_size, &output_length);
2616 TEST_EQUAL(status, expected_status);
2617 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002618
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002619 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 status = psa_hash_setup(&operation, alg);
2621 if (status == PSA_SUCCESS) {
2622 status = psa_hash_update(&operation, input->x, input->len);
2623 if (status == PSA_SUCCESS) {
2624 status = psa_hash_finish(&operation, output, output_size,
2625 &output_length);
2626 if (status == PSA_SUCCESS) {
2627 TEST_LE_U(output_length, output_size);
2628 } else {
2629 TEST_EQUAL(status, expected_status);
2630 }
2631 } else {
2632 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002633 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 } else {
2635 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002636 }
2637
Gilles Peskine0a749c82019-11-28 19:33:58 +01002638exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 PSA_ASSERT(psa_hash_abort(&operation));
2640 mbedtls_free(output);
2641 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002642}
2643/* END_CASE */
2644
2645/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002646void hash_compare_fail(int alg_arg, data_t *input,
2647 data_t *reference_hash,
2648 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002649{
2650 psa_algorithm_t alg = alg_arg;
2651 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002652 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002653 psa_status_t status;
2654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002656
Neil Armstrong55a1be12022-02-07 11:23:20 +01002657 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 status = psa_hash_compare(alg, input->x, input->len,
2659 reference_hash->x, reference_hash->len);
2660 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002661
Neil Armstrong55a1be12022-02-07 11:23:20 +01002662 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 status = psa_hash_setup(&operation, alg);
2664 if (status == PSA_SUCCESS) {
2665 status = psa_hash_update(&operation, input->x, input->len);
2666 if (status == PSA_SUCCESS) {
2667 status = psa_hash_verify(&operation, reference_hash->x,
2668 reference_hash->len);
2669 TEST_EQUAL(status, expected_status);
2670 } else {
2671 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002672 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 } else {
2674 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002675 }
2676
Gilles Peskine88e08462020-01-28 20:43:00 +01002677exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 PSA_ASSERT(psa_hash_abort(&operation));
2679 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002680}
2681/* END_CASE */
2682
2683/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002684void hash_compute_compare(int alg_arg, data_t *input,
2685 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002686{
2687 psa_algorithm_t alg = alg_arg;
2688 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2689 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002690 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002691 size_t i;
2692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002694
Neil Armstrongca30a002022-02-07 11:40:23 +01002695 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2697 output, PSA_HASH_LENGTH(alg),
2698 &output_length));
2699 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2700 ASSERT_COMPARE(output, output_length,
2701 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002702
Neil Armstrongca30a002022-02-07 11:40:23 +01002703 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 PSA_ASSERT(psa_hash_setup(&operation, alg));
2705 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2706 PSA_ASSERT(psa_hash_finish(&operation, output,
2707 PSA_HASH_LENGTH(alg),
2708 &output_length));
2709 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2710 ASSERT_COMPARE(output, output_length,
2711 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002712
2713 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2715 output, sizeof(output),
2716 &output_length));
2717 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2718 ASSERT_COMPARE(output, output_length,
2719 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002720
Neil Armstrongca30a002022-02-07 11:40:23 +01002721 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 PSA_ASSERT(psa_hash_setup(&operation, alg));
2723 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2724 PSA_ASSERT(psa_hash_finish(&operation, output,
2725 sizeof(output), &output_length));
2726 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2727 ASSERT_COMPARE(output, output_length,
2728 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002729
2730 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2732 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002733
Neil Armstrongca30a002022-02-07 11:40:23 +01002734 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 PSA_ASSERT(psa_hash_setup(&operation, alg));
2736 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2737 PSA_ASSERT(psa_hash_verify(&operation, output,
2738 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002739
2740 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2742 output, output_length + 1),
2743 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002744
Neil Armstrongca30a002022-02-07 11:40:23 +01002745 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 PSA_ASSERT(psa_hash_setup(&operation, alg));
2747 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2748 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2749 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002750
2751 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2753 output, output_length - 1),
2754 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002755
Neil Armstrongca30a002022-02-07 11:40:23 +01002756 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 PSA_ASSERT(psa_hash_setup(&operation, alg));
2758 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2759 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2760 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002761
Gilles Peskine0a749c82019-11-28 19:33:58 +01002762 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 for (i = 0; i < output_length; i++) {
2764 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002765 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002766
2767 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2769 output, output_length),
2770 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002771
2772 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 PSA_ASSERT(psa_hash_setup(&operation, alg));
2774 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2775 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2776 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002777
Gilles Peskine0a749c82019-11-28 19:33:58 +01002778 output[i] ^= 1;
2779 }
2780
2781exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 PSA_ASSERT(psa_hash_abort(&operation));
2783 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002784}
2785/* END_CASE */
2786
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002787/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002788void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002789{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002790 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002791 unsigned char input[] = "";
2792 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002793 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002794 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2795 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2797 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002798 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002799 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002800 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002801
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002803
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002804 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 PSA_ASSERT(psa_hash_setup(&operation, alg));
2806 ASSERT_OPERATION_IS_ACTIVE(operation);
2807 TEST_EQUAL(psa_hash_setup(&operation, alg),
2808 PSA_ERROR_BAD_STATE);
2809 ASSERT_OPERATION_IS_INACTIVE(operation);
2810 PSA_ASSERT(psa_hash_abort(&operation));
2811 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002812
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002813 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2815 PSA_ERROR_BAD_STATE);
2816 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002817
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002818 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002820 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 ASSERT_OPERATION_IS_ACTIVE(operation);
2822 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2823 PSA_ERROR_BAD_STATE);
2824 ASSERT_OPERATION_IS_INACTIVE(operation);
2825 PSA_ASSERT(psa_hash_abort(&operation));
2826 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002827
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002828 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 PSA_ASSERT(psa_hash_setup(&operation, alg));
2830 PSA_ASSERT(psa_hash_finish(&operation,
2831 hash, sizeof(hash), &hash_len));
2832 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2833 PSA_ERROR_BAD_STATE);
2834 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002835
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002836 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 TEST_EQUAL(psa_hash_verify(&operation,
2838 valid_hash, sizeof(valid_hash)),
2839 PSA_ERROR_BAD_STATE);
2840 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002841
2842 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 PSA_ASSERT(psa_hash_setup(&operation, alg));
2844 PSA_ASSERT(psa_hash_finish(&operation,
2845 hash, sizeof(hash), &hash_len));
2846 TEST_EQUAL(psa_hash_verify(&operation,
2847 valid_hash, sizeof(valid_hash)),
2848 PSA_ERROR_BAD_STATE);
2849 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002850
2851 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 PSA_ASSERT(psa_hash_setup(&operation, alg));
2853 ASSERT_OPERATION_IS_ACTIVE(operation);
2854 PSA_ASSERT(psa_hash_verify(&operation,
2855 valid_hash, sizeof(valid_hash)));
2856 ASSERT_OPERATION_IS_INACTIVE(operation);
2857 TEST_EQUAL(psa_hash_verify(&operation,
2858 valid_hash, sizeof(valid_hash)),
2859 PSA_ERROR_BAD_STATE);
2860 ASSERT_OPERATION_IS_INACTIVE(operation);
2861 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002862
2863 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 TEST_EQUAL(psa_hash_finish(&operation,
2865 hash, sizeof(hash), &hash_len),
2866 PSA_ERROR_BAD_STATE);
2867 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002868
2869 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 PSA_ASSERT(psa_hash_setup(&operation, alg));
2871 PSA_ASSERT(psa_hash_finish(&operation,
2872 hash, sizeof(hash), &hash_len));
2873 TEST_EQUAL(psa_hash_finish(&operation,
2874 hash, sizeof(hash), &hash_len),
2875 PSA_ERROR_BAD_STATE);
2876 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002877
2878 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 PSA_ASSERT(psa_hash_setup(&operation, alg));
2880 PSA_ASSERT(psa_hash_verify(&operation,
2881 valid_hash, sizeof(valid_hash)));
2882 TEST_EQUAL(psa_hash_finish(&operation,
2883 hash, sizeof(hash), &hash_len),
2884 PSA_ERROR_BAD_STATE);
2885 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002886
2887exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002889}
2890/* END_CASE */
2891
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002892/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002893void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002894{
2895 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002896 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2897 * appended to it */
2898 unsigned char hash[] = {
2899 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2900 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2902 };
2903 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002904 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002905
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002907
itayzafrir27e69452018-11-01 14:26:34 +02002908 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 PSA_ASSERT(psa_hash_setup(&operation, alg));
2910 ASSERT_OPERATION_IS_ACTIVE(operation);
2911 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2912 PSA_ERROR_INVALID_SIGNATURE);
2913 ASSERT_OPERATION_IS_INACTIVE(operation);
2914 PSA_ASSERT(psa_hash_abort(&operation));
2915 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002916
itayzafrir27e69452018-11-01 14:26:34 +02002917 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 PSA_ASSERT(psa_hash_setup(&operation, alg));
2919 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
2920 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03002921
itayzafrir27e69452018-11-01 14:26:34 +02002922 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 PSA_ASSERT(psa_hash_setup(&operation, alg));
2924 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
2925 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03002926
itayzafrirec93d302018-10-18 18:01:10 +03002927exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03002929}
2930/* END_CASE */
2931
Ronald Cronee414c72021-03-18 18:50:08 +01002932/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002933void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03002934{
2935 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002936 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002938 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002939 size_t hash_len;
2940
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03002942
itayzafrir58028322018-10-25 10:22:01 +03002943 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 PSA_ASSERT(psa_hash_setup(&operation, alg));
2945 TEST_EQUAL(psa_hash_finish(&operation,
2946 hash, expected_size - 1, &hash_len),
2947 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03002948
2949exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03002951}
2952/* END_CASE */
2953
Ronald Cronee414c72021-03-18 18:50:08 +01002954/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002955void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002956{
2957 psa_algorithm_t alg = PSA_ALG_SHA_256;
2958 unsigned char hash[PSA_HASH_MAX_SIZE];
2959 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2960 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2961 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2962 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2963 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2964 size_t hash_len;
2965
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 PSA_ASSERT(psa_crypto_init());
2967 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002968
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
2970 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
2971 PSA_ASSERT(psa_hash_finish(&op_finished,
2972 hash, sizeof(hash), &hash_len));
2973 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
2974 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002975
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
2977 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002978
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
2980 PSA_ASSERT(psa_hash_finish(&op_init,
2981 hash, sizeof(hash), &hash_len));
2982 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
2983 PSA_ASSERT(psa_hash_finish(&op_finished,
2984 hash, sizeof(hash), &hash_len));
2985 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
2986 PSA_ASSERT(psa_hash_finish(&op_aborted,
2987 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002988
2989exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 psa_hash_abort(&op_source);
2991 psa_hash_abort(&op_init);
2992 psa_hash_abort(&op_setup);
2993 psa_hash_abort(&op_finished);
2994 psa_hash_abort(&op_aborted);
2995 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002996}
2997/* END_CASE */
2998
Ronald Cronee414c72021-03-18 18:50:08 +01002999/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003000void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003001{
3002 psa_algorithm_t alg = PSA_ALG_SHA_256;
3003 unsigned char hash[PSA_HASH_MAX_SIZE];
3004 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3005 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3006 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3007 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3008 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3009 size_t hash_len;
3010
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003012
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3014 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3015 PSA_ASSERT(psa_hash_finish(&op_finished,
3016 hash, sizeof(hash), &hash_len));
3017 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3018 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003019
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3021 PSA_ASSERT(psa_hash_finish(&op_target,
3022 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003023
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3025 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3026 PSA_ERROR_BAD_STATE);
3027 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3028 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003029
3030exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 psa_hash_abort(&op_target);
3032 psa_hash_abort(&op_init);
3033 psa_hash_abort(&op_setup);
3034 psa_hash_abort(&op_finished);
3035 psa_hash_abort(&op_aborted);
3036 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003037}
3038/* END_CASE */
3039
itayzafrir58028322018-10-25 10:22:01 +03003040/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003041void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003042{
Jaeden Amero252ef282019-02-15 14:05:35 +00003043 const uint8_t input[1] = { 0 };
3044
Jaeden Amero769ce272019-01-04 11:48:03 +00003045 /* Test each valid way of initializing the object, except for `= {0}`, as
3046 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3047 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003048 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003050 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3051 psa_mac_operation_t zero;
3052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003054
Jaeden Amero252ef282019-02-15 14:05:35 +00003055 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 TEST_EQUAL(psa_mac_update(&func,
3057 input, sizeof(input)),
3058 PSA_ERROR_BAD_STATE);
3059 TEST_EQUAL(psa_mac_update(&init,
3060 input, sizeof(input)),
3061 PSA_ERROR_BAD_STATE);
3062 TEST_EQUAL(psa_mac_update(&zero,
3063 input, sizeof(input)),
3064 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003065
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003066 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 PSA_ASSERT(psa_mac_abort(&func));
3068 PSA_ASSERT(psa_mac_abort(&init));
3069 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003070}
3071/* END_CASE */
3072
3073/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003074void mac_setup(int key_type_arg,
3075 data_t *key,
3076 int alg_arg,
3077 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003078{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003079 psa_key_type_t key_type = key_type_arg;
3080 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003081 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003082 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003083 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3084#if defined(KNOWN_SUPPORTED_MAC_ALG)
3085 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3086#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003087
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003089
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3091 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003092 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 }
3094 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003095
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003096 /* The operation object should be reusable. */
3097#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3099 smoke_test_key_data,
3100 sizeof(smoke_test_key_data),
3101 KNOWN_SUPPORTED_MAC_ALG,
3102 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003103 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003104 }
3105 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003106#endif
3107
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003108exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003110}
3111/* END_CASE */
3112
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003113/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003114void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003115{
Ronald Cron5425a212020-08-04 14:58:35 +02003116 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003117 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3118 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003119 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003120 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3121 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3123 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003125 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3126 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3127 size_t sign_mac_length = 0;
3128 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3129 const uint8_t verify_mac[] = {
3130 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3131 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3133 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003134
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 PSA_ASSERT(psa_crypto_init());
3136 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3137 psa_set_key_algorithm(&attributes, alg);
3138 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003139
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3141 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003142
Jaeden Amero252ef282019-02-15 14:05:35 +00003143 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3145 PSA_ERROR_BAD_STATE);
3146 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003147
3148 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3150 &sign_mac_length),
3151 PSA_ERROR_BAD_STATE);
3152 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003153
3154 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003155 TEST_EQUAL(psa_mac_verify_finish(&operation,
3156 verify_mac, sizeof(verify_mac)),
3157 PSA_ERROR_BAD_STATE);
3158 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003159
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003160 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3162 ASSERT_OPERATION_IS_ACTIVE(operation);
3163 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3164 PSA_ERROR_BAD_STATE);
3165 ASSERT_OPERATION_IS_INACTIVE(operation);
3166 PSA_ASSERT(psa_mac_abort(&operation));
3167 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003168
Jaeden Amero252ef282019-02-15 14:05:35 +00003169 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3171 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3172 PSA_ASSERT(psa_mac_sign_finish(&operation,
3173 sign_mac, sizeof(sign_mac),
3174 &sign_mac_length));
3175 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3176 PSA_ERROR_BAD_STATE);
3177 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003178
3179 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3181 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3182 PSA_ASSERT(psa_mac_verify_finish(&operation,
3183 verify_mac, sizeof(verify_mac)));
3184 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3185 PSA_ERROR_BAD_STATE);
3186 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003187
3188 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3190 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3191 PSA_ASSERT(psa_mac_sign_finish(&operation,
3192 sign_mac, sizeof(sign_mac),
3193 &sign_mac_length));
3194 TEST_EQUAL(psa_mac_sign_finish(&operation,
3195 sign_mac, sizeof(sign_mac),
3196 &sign_mac_length),
3197 PSA_ERROR_BAD_STATE);
3198 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003199
3200 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3202 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3203 PSA_ASSERT(psa_mac_verify_finish(&operation,
3204 verify_mac, sizeof(verify_mac)));
3205 TEST_EQUAL(psa_mac_verify_finish(&operation,
3206 verify_mac, sizeof(verify_mac)),
3207 PSA_ERROR_BAD_STATE);
3208 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003209
3210 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3212 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3213 ASSERT_OPERATION_IS_ACTIVE(operation);
3214 TEST_EQUAL(psa_mac_verify_finish(&operation,
3215 verify_mac, sizeof(verify_mac)),
3216 PSA_ERROR_BAD_STATE);
3217 ASSERT_OPERATION_IS_INACTIVE(operation);
3218 PSA_ASSERT(psa_mac_abort(&operation));
3219 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003220
3221 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003222 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3223 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3224 ASSERT_OPERATION_IS_ACTIVE(operation);
3225 TEST_EQUAL(psa_mac_sign_finish(&operation,
3226 sign_mac, sizeof(sign_mac),
3227 &sign_mac_length),
3228 PSA_ERROR_BAD_STATE);
3229 ASSERT_OPERATION_IS_INACTIVE(operation);
3230 PSA_ASSERT(psa_mac_abort(&operation));
3231 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003232
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003234
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003235exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003237}
3238/* END_CASE */
3239
3240/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003241void mac_sign_verify_multi(int key_type_arg,
3242 data_t *key_data,
3243 int alg_arg,
3244 data_t *input,
3245 int is_verify,
3246 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003247{
3248 size_t data_part_len = 0;
3249
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003251 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003253
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 if (mac_multipart_internal_func(key_type_arg, key_data,
3255 alg_arg,
3256 input, data_part_len,
3257 expected_mac,
3258 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003259 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003261
3262 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003264
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 if (mac_multipart_internal_func(key_type_arg, key_data,
3266 alg_arg,
3267 input, data_part_len,
3268 expected_mac,
3269 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003270 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003272 }
3273
3274 /* Goto is required to silence warnings about unused labels, as we
3275 * don't actually do any test assertions in this function. */
3276 goto exit;
3277}
3278/* END_CASE */
3279
3280/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003281void mac_sign(int key_type_arg,
3282 data_t *key_data,
3283 int alg_arg,
3284 data_t *input,
3285 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003286{
Ronald Cron5425a212020-08-04 14:58:35 +02003287 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003288 psa_key_type_t key_type = key_type_arg;
3289 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003290 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003291 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003292 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003293 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003295 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003296 const size_t output_sizes_to_test[] = {
3297 0,
3298 1,
3299 expected_mac->len - 1,
3300 expected_mac->len,
3301 expected_mac->len + 1,
3302 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003303
Gilles Peskine449bd832023-01-11 14:50:10 +01003304 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003305 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003307
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003309
Gilles Peskine449bd832023-01-11 14:50:10 +01003310 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3311 psa_set_key_algorithm(&attributes, alg);
3312 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003313
Gilles Peskine449bd832023-01-11 14:50:10 +01003314 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3315 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003316
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003318 const size_t output_size = output_sizes_to_test[i];
3319 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 (output_size >= expected_mac->len ? PSA_SUCCESS :
3321 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003322
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 mbedtls_test_set_step(output_size);
3324 ASSERT_ALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003325
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003326 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 TEST_EQUAL(psa_mac_compute(key, alg,
3328 input->x, input->len,
3329 actual_mac, output_size, &mac_length),
3330 expected_status);
3331 if (expected_status == PSA_SUCCESS) {
3332 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3333 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003334 }
3335
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 if (output_size > 0) {
3337 memset(actual_mac, 0, output_size);
3338 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003339
3340 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3342 PSA_ASSERT(psa_mac_update(&operation,
3343 input->x, input->len));
3344 TEST_EQUAL(psa_mac_sign_finish(&operation,
3345 actual_mac, output_size,
3346 &mac_length),
3347 expected_status);
3348 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003349
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 if (expected_status == PSA_SUCCESS) {
3351 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3352 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003353 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003355 actual_mac = NULL;
3356 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003357
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003358exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003359 psa_mac_abort(&operation);
3360 psa_destroy_key(key);
3361 PSA_DONE();
3362 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003363}
3364/* END_CASE */
3365
3366/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003367void mac_verify(int key_type_arg,
3368 data_t *key_data,
3369 int alg_arg,
3370 data_t *input,
3371 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003372{
Ronald Cron5425a212020-08-04 14:58:35 +02003373 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003374 psa_key_type_t key_type = key_type_arg;
3375 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003376 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003377 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003378 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003379
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003381
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003383
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3385 psa_set_key_algorithm(&attributes, alg);
3386 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003387
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3389 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003390
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003391 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003392 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3393 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003394
3395 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003396 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3397 PSA_ASSERT(psa_mac_update(&operation,
3398 input->x, input->len));
3399 PSA_ASSERT(psa_mac_verify_finish(&operation,
3400 expected_mac->x,
3401 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003402
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003403 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003404 TEST_EQUAL(psa_mac_verify(key, alg,
3405 input->x, input->len,
3406 expected_mac->x,
3407 expected_mac->len - 1),
3408 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003409
3410 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003411 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3412 PSA_ASSERT(psa_mac_update(&operation,
3413 input->x, input->len));
3414 TEST_EQUAL(psa_mac_verify_finish(&operation,
3415 expected_mac->x,
3416 expected_mac->len - 1),
3417 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003418
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003419 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1);
3421 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3422 TEST_EQUAL(psa_mac_verify(key, alg,
3423 input->x, input->len,
3424 perturbed_mac, expected_mac->len + 1),
3425 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003426
3427 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3429 PSA_ASSERT(psa_mac_update(&operation,
3430 input->x, input->len));
3431 TEST_EQUAL(psa_mac_verify_finish(&operation,
3432 perturbed_mac,
3433 expected_mac->len + 1),
3434 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003435
3436 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003437 for (size_t i = 0; i < expected_mac->len; i++) {
3438 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003439 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003440
Gilles Peskine449bd832023-01-11 14:50:10 +01003441 TEST_EQUAL(psa_mac_verify(key, alg,
3442 input->x, input->len,
3443 perturbed_mac, expected_mac->len),
3444 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003445
Gilles Peskine449bd832023-01-11 14:50:10 +01003446 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3447 PSA_ASSERT(psa_mac_update(&operation,
3448 input->x, input->len));
3449 TEST_EQUAL(psa_mac_verify_finish(&operation,
3450 perturbed_mac,
3451 expected_mac->len),
3452 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003453 perturbed_mac[i] ^= 1;
3454 }
3455
Gilles Peskine8c9def32018-02-08 10:02:12 +01003456exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003457 psa_mac_abort(&operation);
3458 psa_destroy_key(key);
3459 PSA_DONE();
3460 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003461}
3462/* END_CASE */
3463
3464/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003465void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003466{
Jaeden Ameroab439972019-02-15 14:12:05 +00003467 const uint8_t input[1] = { 0 };
3468 unsigned char output[1] = { 0 };
3469 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003470 /* Test each valid way of initializing the object, except for `= {0}`, as
3471 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3472 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003473 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003474 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003475 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3476 psa_cipher_operation_t zero;
3477
Gilles Peskine449bd832023-01-11 14:50:10 +01003478 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003479
Jaeden Ameroab439972019-02-15 14:12:05 +00003480 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 TEST_EQUAL(psa_cipher_update(&func,
3482 input, sizeof(input),
3483 output, sizeof(output),
3484 &output_length),
3485 PSA_ERROR_BAD_STATE);
3486 TEST_EQUAL(psa_cipher_update(&init,
3487 input, sizeof(input),
3488 output, sizeof(output),
3489 &output_length),
3490 PSA_ERROR_BAD_STATE);
3491 TEST_EQUAL(psa_cipher_update(&zero,
3492 input, sizeof(input),
3493 output, sizeof(output),
3494 &output_length),
3495 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003496
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003497 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 PSA_ASSERT(psa_cipher_abort(&func));
3499 PSA_ASSERT(psa_cipher_abort(&init));
3500 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003501}
3502/* END_CASE */
3503
3504/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003505void cipher_setup(int key_type_arg,
3506 data_t *key,
3507 int alg_arg,
3508 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003509{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003510 psa_key_type_t key_type = key_type_arg;
3511 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003512 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003513 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003514 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003515#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003516 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3517#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003518
Gilles Peskine449bd832023-01-11 14:50:10 +01003519 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003520
Gilles Peskine449bd832023-01-11 14:50:10 +01003521 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3522 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003523 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003524 }
3525 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003526
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003527 /* The operation object should be reusable. */
3528#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003529 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3530 smoke_test_key_data,
3531 sizeof(smoke_test_key_data),
3532 KNOWN_SUPPORTED_CIPHER_ALG,
3533 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003534 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003535 }
3536 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003537#endif
3538
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003539exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003540 psa_cipher_abort(&operation);
3541 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003542}
3543/* END_CASE */
3544
Ronald Cronee414c72021-03-18 18:50:08 +01003545/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003546void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003547{
Ronald Cron5425a212020-08-04 14:58:35 +02003548 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003549 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3550 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003551 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003552 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003553 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003554 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003555 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003556 0xaa, 0xaa, 0xaa, 0xaa
3557 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003558 const uint8_t text[] = {
3559 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003560 0xbb, 0xbb, 0xbb, 0xbb
3561 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003562 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003563 size_t length = 0;
3564
Gilles Peskine449bd832023-01-11 14:50:10 +01003565 PSA_ASSERT(psa_crypto_init());
3566 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3567 psa_set_key_algorithm(&attributes, alg);
3568 psa_set_key_type(&attributes, key_type);
3569 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3570 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003571
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003572 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003573 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3574 ASSERT_OPERATION_IS_ACTIVE(operation);
3575 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3576 PSA_ERROR_BAD_STATE);
3577 ASSERT_OPERATION_IS_INACTIVE(operation);
3578 PSA_ASSERT(psa_cipher_abort(&operation));
3579 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003580
3581 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003582 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3583 ASSERT_OPERATION_IS_ACTIVE(operation);
3584 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3585 PSA_ERROR_BAD_STATE);
3586 ASSERT_OPERATION_IS_INACTIVE(operation);
3587 PSA_ASSERT(psa_cipher_abort(&operation));
3588 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003589
Jaeden Ameroab439972019-02-15 14:12:05 +00003590 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003591 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3592 buffer, sizeof(buffer),
3593 &length),
3594 PSA_ERROR_BAD_STATE);
3595 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003596
3597 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003598 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3599 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3600 buffer, sizeof(buffer),
3601 &length));
3602 ASSERT_OPERATION_IS_ACTIVE(operation);
3603 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3604 buffer, sizeof(buffer),
3605 &length),
3606 PSA_ERROR_BAD_STATE);
3607 ASSERT_OPERATION_IS_INACTIVE(operation);
3608 PSA_ASSERT(psa_cipher_abort(&operation));
3609 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003610
3611 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003612 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3613 PSA_ASSERT(psa_cipher_set_iv(&operation,
3614 iv, sizeof(iv)));
3615 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3616 buffer, sizeof(buffer),
3617 &length),
3618 PSA_ERROR_BAD_STATE);
3619 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003620
3621 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003622 TEST_EQUAL(psa_cipher_set_iv(&operation,
3623 iv, sizeof(iv)),
3624 PSA_ERROR_BAD_STATE);
3625 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003626
3627 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003628 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3629 PSA_ASSERT(psa_cipher_set_iv(&operation,
3630 iv, sizeof(iv)));
3631 ASSERT_OPERATION_IS_ACTIVE(operation);
3632 TEST_EQUAL(psa_cipher_set_iv(&operation,
3633 iv, sizeof(iv)),
3634 PSA_ERROR_BAD_STATE);
3635 ASSERT_OPERATION_IS_INACTIVE(operation);
3636 PSA_ASSERT(psa_cipher_abort(&operation));
3637 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003638
3639 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003640 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3641 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3642 buffer, sizeof(buffer),
3643 &length));
3644 TEST_EQUAL(psa_cipher_set_iv(&operation,
3645 iv, sizeof(iv)),
3646 PSA_ERROR_BAD_STATE);
3647 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003648
3649 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003650 TEST_EQUAL(psa_cipher_update(&operation,
3651 text, sizeof(text),
3652 buffer, sizeof(buffer),
3653 &length),
3654 PSA_ERROR_BAD_STATE);
3655 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003656
3657 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003658 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3659 ASSERT_OPERATION_IS_ACTIVE(operation);
3660 TEST_EQUAL(psa_cipher_update(&operation,
3661 text, sizeof(text),
3662 buffer, sizeof(buffer),
3663 &length),
3664 PSA_ERROR_BAD_STATE);
3665 ASSERT_OPERATION_IS_INACTIVE(operation);
3666 PSA_ASSERT(psa_cipher_abort(&operation));
3667 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003668
3669 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003670 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3671 PSA_ASSERT(psa_cipher_set_iv(&operation,
3672 iv, sizeof(iv)));
3673 PSA_ASSERT(psa_cipher_finish(&operation,
3674 buffer, sizeof(buffer), &length));
3675 TEST_EQUAL(psa_cipher_update(&operation,
3676 text, sizeof(text),
3677 buffer, sizeof(buffer),
3678 &length),
3679 PSA_ERROR_BAD_STATE);
3680 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003681
3682 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003683 TEST_EQUAL(psa_cipher_finish(&operation,
3684 buffer, sizeof(buffer), &length),
3685 PSA_ERROR_BAD_STATE);
3686 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003687
3688 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003689 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003690 /* Not calling update means we are encrypting an empty buffer, which is OK
3691 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003692 ASSERT_OPERATION_IS_ACTIVE(operation);
3693 TEST_EQUAL(psa_cipher_finish(&operation,
3694 buffer, sizeof(buffer), &length),
3695 PSA_ERROR_BAD_STATE);
3696 ASSERT_OPERATION_IS_INACTIVE(operation);
3697 PSA_ASSERT(psa_cipher_abort(&operation));
3698 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003699
3700 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003701 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3702 PSA_ASSERT(psa_cipher_set_iv(&operation,
3703 iv, sizeof(iv)));
3704 PSA_ASSERT(psa_cipher_finish(&operation,
3705 buffer, sizeof(buffer), &length));
3706 TEST_EQUAL(psa_cipher_finish(&operation,
3707 buffer, sizeof(buffer), &length),
3708 PSA_ERROR_BAD_STATE);
3709 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003710
Gilles Peskine449bd832023-01-11 14:50:10 +01003711 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003712
Jaeden Ameroab439972019-02-15 14:12:05 +00003713exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003714 psa_cipher_abort(&operation);
3715 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003716}
3717/* END_CASE */
3718
3719/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003720void cipher_encrypt_fail(int alg_arg,
3721 int key_type_arg,
3722 data_t *key_data,
3723 data_t *input,
3724 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003725{
Ronald Cron5425a212020-08-04 14:58:35 +02003726 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003727 psa_status_t status;
3728 psa_key_type_t key_type = key_type_arg;
3729 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003730 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003731 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003732 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3733 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003734 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003735 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003736 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003737 size_t function_output_length;
3738 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003739 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3740
Gilles Peskine449bd832023-01-11 14:50:10 +01003741 if (PSA_ERROR_BAD_STATE != expected_status) {
3742 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003743
Gilles Peskine449bd832023-01-11 14:50:10 +01003744 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3745 psa_set_key_algorithm(&attributes, alg);
3746 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003747
Gilles Peskine449bd832023-01-11 14:50:10 +01003748 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3749 input->len);
3750 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003751
Gilles Peskine449bd832023-01-11 14:50:10 +01003752 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3753 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003754 }
3755
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003756 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003757 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3758 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003759
Gilles Peskine449bd832023-01-11 14:50:10 +01003760 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003761
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003762 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003763 status = psa_cipher_encrypt_setup(&operation, key, alg);
3764 if (status == PSA_SUCCESS) {
3765 if (alg != PSA_ALG_ECB_NO_PADDING) {
3766 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3767 iv, iv_size,
3768 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003769 }
3770
Gilles Peskine449bd832023-01-11 14:50:10 +01003771 status = psa_cipher_update(&operation, input->x, input->len,
3772 output, output_buffer_size,
3773 &function_output_length);
3774 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003775 output_length += function_output_length;
3776
Gilles Peskine449bd832023-01-11 14:50:10 +01003777 status = psa_cipher_finish(&operation, output + output_length,
3778 output_buffer_size - output_length,
3779 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003780
Gilles Peskine449bd832023-01-11 14:50:10 +01003781 TEST_EQUAL(status, expected_status);
3782 } else {
3783 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003784 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003785 } else {
3786 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003787 }
3788
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003789exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003790 psa_cipher_abort(&operation);
3791 mbedtls_free(output);
3792 psa_destroy_key(key);
3793 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003794}
3795/* END_CASE */
3796
3797/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003798void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3799 data_t *input, int iv_length,
3800 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003801{
3802 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3803 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3804 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3805 size_t output_buffer_size = 0;
3806 unsigned char *output = NULL;
3807
Gilles Peskine449bd832023-01-11 14:50:10 +01003808 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
3809 ASSERT_ALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003810
Gilles Peskine449bd832023-01-11 14:50:10 +01003811 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003812
Gilles Peskine449bd832023-01-11 14:50:10 +01003813 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3814 psa_set_key_algorithm(&attributes, alg);
3815 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003816
Gilles Peskine449bd832023-01-11 14:50:10 +01003817 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3818 &key));
3819 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3820 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3821 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003822
3823exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003824 psa_cipher_abort(&operation);
3825 mbedtls_free(output);
3826 psa_destroy_key(key);
3827 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003828}
3829/* END_CASE */
3830
3831/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003832void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3833 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003834{
3835 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3836 psa_key_type_t key_type = key_type_arg;
3837 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003838 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3839 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003840 unsigned char *output = NULL;
3841 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003842 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003843 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3844
Gilles Peskine449bd832023-01-11 14:50:10 +01003845 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003846
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003847 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003848 TEST_LE_U(ciphertext->len,
3849 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3850 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3851 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3852 TEST_LE_U(plaintext->len,
3853 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3854 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3855 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003856
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003857
3858 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003859 psa_set_key_usage_flags(&attributes,
3860 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3861 psa_set_key_algorithm(&attributes, alg);
3862 psa_set_key_type(&attributes, key_type);
3863 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3864 &key));
3865 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3866 plaintext->len);
3867 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003868
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003869 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003870 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3871 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3872 PSA_ERROR_BAD_STATE);
3873 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3874 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3875 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003876
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003877 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003878 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3879 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3880 &length),
3881 PSA_ERROR_BAD_STATE);
3882 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3883 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3884 &length),
3885 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003886
Gilles Peskine286c3142022-04-20 17:09:38 +02003887 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003888 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003889 output_length = 0;
3890 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003891 PSA_ASSERT(psa_cipher_update(&operation,
3892 plaintext->x, plaintext->len,
3893 output, output_buffer_size,
3894 &length));
3895 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003896 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003897 PSA_ASSERT(psa_cipher_finish(&operation,
3898 mbedtls_buffer_offset(output, output_length),
3899 output_buffer_size - output_length,
3900 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003901 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003902 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3903 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003904
Gilles Peskine286c3142022-04-20 17:09:38 +02003905 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003906 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003907 output_length = 0;
3908 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003909 PSA_ASSERT(psa_cipher_update(&operation,
3910 ciphertext->x, ciphertext->len,
3911 output, output_buffer_size,
3912 &length));
3913 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003914 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003915 PSA_ASSERT(psa_cipher_finish(&operation,
3916 mbedtls_buffer_offset(output, output_length),
3917 output_buffer_size - output_length,
3918 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003919 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003920 ASSERT_COMPARE(plaintext->x, plaintext->len,
3921 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003922
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003923 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003924 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003925 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
3926 output, output_buffer_size,
3927 &output_length));
3928 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3929 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003930
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003931 /* One-shot decryption */
3932 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003933 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
3934 output, output_buffer_size,
3935 &output_length));
3936 ASSERT_COMPARE(plaintext->x, plaintext->len,
3937 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003938
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003939exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003940 PSA_ASSERT(psa_cipher_abort(&operation));
3941 mbedtls_free(output);
3942 psa_cipher_abort(&operation);
3943 psa_destroy_key(key);
3944 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003945}
3946/* END_CASE */
3947
3948/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003949void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01003950{
3951 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3952 psa_algorithm_t alg = alg_arg;
3953 psa_key_type_t key_type = key_type_arg;
3954 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3955 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3956 psa_status_t status;
3957
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01003959
Gilles Peskine449bd832023-01-11 14:50:10 +01003960 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3961 psa_set_key_algorithm(&attributes, alg);
3962 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01003963
3964 /* Usage of either of these two size macros would cause divide by zero
3965 * with incorrect key types previously. Input length should be irrelevant
3966 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003967 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
3968 0);
3969 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01003970
3971
Gilles Peskine449bd832023-01-11 14:50:10 +01003972 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3973 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01003974
3975 /* Should fail due to invalid alg type (to support invalid key type).
3976 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003977 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01003978
Gilles Peskine449bd832023-01-11 14:50:10 +01003979 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01003980
3981exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003982 psa_cipher_abort(&operation);
3983 psa_destroy_key(key);
3984 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01003985}
3986/* END_CASE */
3987
3988/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003989void cipher_encrypt_validation(int alg_arg,
3990 int key_type_arg,
3991 data_t *key_data,
3992 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003993{
3994 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3995 psa_key_type_t key_type = key_type_arg;
3996 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003997 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003998 unsigned char *output1 = NULL;
3999 size_t output1_buffer_size = 0;
4000 size_t output1_length = 0;
4001 unsigned char *output2 = NULL;
4002 size_t output2_buffer_size = 0;
4003 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004004 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004005 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004006 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004007
Gilles Peskine449bd832023-01-11 14:50:10 +01004008 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004009
Gilles Peskine449bd832023-01-11 14:50:10 +01004010 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4011 psa_set_key_algorithm(&attributes, alg);
4012 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004013
Gilles Peskine449bd832023-01-11 14:50:10 +01004014 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4015 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4016 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4017 ASSERT_ALLOC(output1, output1_buffer_size);
4018 ASSERT_ALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004019
Gilles Peskine449bd832023-01-11 14:50:10 +01004020 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4021 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004022
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004023 /* The one-shot cipher encryption uses generated iv so validating
4024 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004025 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4026 output1_buffer_size, &output1_length));
4027 TEST_LE_U(output1_length,
4028 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4029 TEST_LE_U(output1_length,
4030 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004031
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4033 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004034
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 PSA_ASSERT(psa_cipher_update(&operation,
4036 input->x, input->len,
4037 output2, output2_buffer_size,
4038 &function_output_length));
4039 TEST_LE_U(function_output_length,
4040 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4041 TEST_LE_U(function_output_length,
4042 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004043 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004044
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 PSA_ASSERT(psa_cipher_finish(&operation,
4046 output2 + output2_length,
4047 output2_buffer_size - output2_length,
4048 &function_output_length));
4049 TEST_LE_U(function_output_length,
4050 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4051 TEST_LE_U(function_output_length,
4052 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004053 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004054
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 PSA_ASSERT(psa_cipher_abort(&operation));
4056 ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size,
4057 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004058
Gilles Peskine50e586b2018-06-08 14:28:46 +02004059exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004060 psa_cipher_abort(&operation);
4061 mbedtls_free(output1);
4062 mbedtls_free(output2);
4063 psa_destroy_key(key);
4064 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004065}
4066/* END_CASE */
4067
4068/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004069void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4070 data_t *key_data, data_t *iv,
4071 data_t *input,
4072 int first_part_size_arg,
4073 int output1_length_arg, int output2_length_arg,
4074 data_t *expected_output,
4075 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004076{
Ronald Cron5425a212020-08-04 14:58:35 +02004077 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004078 psa_key_type_t key_type = key_type_arg;
4079 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004080 psa_status_t status;
4081 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004082 size_t first_part_size = first_part_size_arg;
4083 size_t output1_length = output1_length_arg;
4084 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004085 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004086 size_t output_buffer_size = 0;
4087 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004088 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004089 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004090 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004091
Gilles Peskine449bd832023-01-11 14:50:10 +01004092 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004093
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4095 psa_set_key_algorithm(&attributes, alg);
4096 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004097
Gilles Peskine449bd832023-01-11 14:50:10 +01004098 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4099 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004100
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004102
Gilles Peskine449bd832023-01-11 14:50:10 +01004103 if (iv->len > 0) {
4104 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004105 }
4106
Gilles Peskine449bd832023-01-11 14:50:10 +01004107 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4108 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4109 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004110
Gilles Peskine449bd832023-01-11 14:50:10 +01004111 TEST_LE_U(first_part_size, input->len);
4112 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4113 output, output_buffer_size,
4114 &function_output_length));
4115 TEST_ASSERT(function_output_length == output1_length);
4116 TEST_LE_U(function_output_length,
4117 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4118 TEST_LE_U(function_output_length,
4119 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004120 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004121
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 if (first_part_size < input->len) {
4123 PSA_ASSERT(psa_cipher_update(&operation,
4124 input->x + first_part_size,
4125 input->len - first_part_size,
4126 (output_buffer_size == 0 ? NULL :
4127 output + total_output_length),
4128 output_buffer_size - total_output_length,
4129 &function_output_length));
4130 TEST_ASSERT(function_output_length == output2_length);
4131 TEST_LE_U(function_output_length,
4132 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4133 alg,
4134 input->len - first_part_size));
4135 TEST_LE_U(function_output_length,
4136 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004137 total_output_length += function_output_length;
4138 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004139
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 status = psa_cipher_finish(&operation,
4141 (output_buffer_size == 0 ? NULL :
4142 output + total_output_length),
4143 output_buffer_size - total_output_length,
4144 &function_output_length);
4145 TEST_LE_U(function_output_length,
4146 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4147 TEST_LE_U(function_output_length,
4148 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004149 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004151
Gilles Peskine449bd832023-01-11 14:50:10 +01004152 if (expected_status == PSA_SUCCESS) {
4153 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004154
Gilles Peskine449bd832023-01-11 14:50:10 +01004155 ASSERT_COMPARE(expected_output->x, expected_output->len,
4156 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004157 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004158
4159exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 psa_cipher_abort(&operation);
4161 mbedtls_free(output);
4162 psa_destroy_key(key);
4163 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004164}
4165/* END_CASE */
4166
4167/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004168void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4169 data_t *key_data, data_t *iv,
4170 data_t *input,
4171 int first_part_size_arg,
4172 int output1_length_arg, int output2_length_arg,
4173 data_t *expected_output,
4174 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004175{
Ronald Cron5425a212020-08-04 14:58:35 +02004176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004177 psa_key_type_t key_type = key_type_arg;
4178 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004179 psa_status_t status;
4180 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004181 size_t first_part_size = first_part_size_arg;
4182 size_t output1_length = output1_length_arg;
4183 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004184 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004185 size_t output_buffer_size = 0;
4186 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004187 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004188 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004189 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004190
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004192
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4194 psa_set_key_algorithm(&attributes, alg);
4195 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004196
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4198 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004199
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004201
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 if (iv->len > 0) {
4203 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004204 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004205
Gilles Peskine449bd832023-01-11 14:50:10 +01004206 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4207 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4208 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004209
Gilles Peskine449bd832023-01-11 14:50:10 +01004210 TEST_LE_U(first_part_size, input->len);
4211 PSA_ASSERT(psa_cipher_update(&operation,
4212 input->x, first_part_size,
4213 output, output_buffer_size,
4214 &function_output_length));
4215 TEST_ASSERT(function_output_length == output1_length);
4216 TEST_LE_U(function_output_length,
4217 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4218 TEST_LE_U(function_output_length,
4219 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004220 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004221
Gilles Peskine449bd832023-01-11 14:50:10 +01004222 if (first_part_size < input->len) {
4223 PSA_ASSERT(psa_cipher_update(&operation,
4224 input->x + first_part_size,
4225 input->len - first_part_size,
4226 (output_buffer_size == 0 ? NULL :
4227 output + total_output_length),
4228 output_buffer_size - total_output_length,
4229 &function_output_length));
4230 TEST_ASSERT(function_output_length == output2_length);
4231 TEST_LE_U(function_output_length,
4232 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4233 alg,
4234 input->len - first_part_size));
4235 TEST_LE_U(function_output_length,
4236 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004237 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004238 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004239
Gilles Peskine449bd832023-01-11 14:50:10 +01004240 status = psa_cipher_finish(&operation,
4241 (output_buffer_size == 0 ? NULL :
4242 output + total_output_length),
4243 output_buffer_size - total_output_length,
4244 &function_output_length);
4245 TEST_LE_U(function_output_length,
4246 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4247 TEST_LE_U(function_output_length,
4248 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004249 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004250 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004251
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 if (expected_status == PSA_SUCCESS) {
4253 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004254
Gilles Peskine449bd832023-01-11 14:50:10 +01004255 ASSERT_COMPARE(expected_output->x, expected_output->len,
4256 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004257 }
4258
Gilles Peskine50e586b2018-06-08 14:28:46 +02004259exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 psa_cipher_abort(&operation);
4261 mbedtls_free(output);
4262 psa_destroy_key(key);
4263 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004264}
4265/* END_CASE */
4266
Gilles Peskine50e586b2018-06-08 14:28:46 +02004267/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004268void cipher_decrypt_fail(int alg_arg,
4269 int key_type_arg,
4270 data_t *key_data,
4271 data_t *iv,
4272 data_t *input_arg,
4273 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004274{
4275 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4276 psa_status_t status;
4277 psa_key_type_t key_type = key_type_arg;
4278 psa_algorithm_t alg = alg_arg;
4279 psa_status_t expected_status = expected_status_arg;
4280 unsigned char *input = NULL;
4281 size_t input_buffer_size = 0;
4282 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004283 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004284 size_t output_buffer_size = 0;
4285 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004286 size_t function_output_length;
4287 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004288 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4289
Gilles Peskine449bd832023-01-11 14:50:10 +01004290 if (PSA_ERROR_BAD_STATE != expected_status) {
4291 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004292
Gilles Peskine449bd832023-01-11 14:50:10 +01004293 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4294 psa_set_key_algorithm(&attributes, alg);
4295 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004296
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4298 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004299 }
4300
4301 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004302 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4303 if (input_buffer_size > 0) {
4304 ASSERT_ALLOC(input, input_buffer_size);
4305 memcpy(input, iv->x, iv->len);
4306 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004307 }
4308
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4310 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004311
Neil Armstrong66a479f2022-02-07 15:41:19 +01004312 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004313 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4314 output_buffer_size, &output_length);
4315 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004316
Neil Armstrong66a479f2022-02-07 15:41:19 +01004317 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 status = psa_cipher_decrypt_setup(&operation, key, alg);
4319 if (status == PSA_SUCCESS) {
4320 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4321 input_arg->len) +
4322 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4323 ASSERT_ALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004324
Gilles Peskine449bd832023-01-11 14:50:10 +01004325 if (iv->len > 0) {
4326 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004327
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 if (status != PSA_SUCCESS) {
4329 TEST_EQUAL(status, expected_status);
4330 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004331 }
4332
Gilles Peskine449bd832023-01-11 14:50:10 +01004333 if (status == PSA_SUCCESS) {
4334 status = psa_cipher_update(&operation,
4335 input_arg->x, input_arg->len,
4336 output_multi, output_buffer_size,
4337 &function_output_length);
4338 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004339 output_length = function_output_length;
4340
Gilles Peskine449bd832023-01-11 14:50:10 +01004341 status = psa_cipher_finish(&operation,
4342 output_multi + output_length,
4343 output_buffer_size - output_length,
4344 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004345
Gilles Peskine449bd832023-01-11 14:50:10 +01004346 TEST_EQUAL(status, expected_status);
4347 } else {
4348 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004349 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004350 } else {
4351 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004352 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 } else {
4354 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004355 }
4356
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004357exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 psa_cipher_abort(&operation);
4359 mbedtls_free(input);
4360 mbedtls_free(output);
4361 mbedtls_free(output_multi);
4362 psa_destroy_key(key);
4363 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004364}
4365/* END_CASE */
4366
4367/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004368void cipher_decrypt(int alg_arg,
4369 int key_type_arg,
4370 data_t *key_data,
4371 data_t *iv,
4372 data_t *input_arg,
4373 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004374{
4375 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4376 psa_key_type_t key_type = key_type_arg;
4377 psa_algorithm_t alg = alg_arg;
4378 unsigned char *input = NULL;
4379 size_t input_buffer_size = 0;
4380 unsigned char *output = NULL;
4381 size_t output_buffer_size = 0;
4382 size_t output_length = 0;
4383 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4384
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004386
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4388 psa_set_key_algorithm(&attributes, alg);
4389 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004390
4391 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4393 if (input_buffer_size > 0) {
4394 ASSERT_ALLOC(input, input_buffer_size);
4395 memcpy(input, iv->x, iv->len);
4396 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004397 }
4398
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4400 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004401
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4403 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004404
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4406 output_buffer_size, &output_length));
4407 TEST_LE_U(output_length,
4408 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4409 TEST_LE_U(output_length,
4410 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004411
Gilles Peskine449bd832023-01-11 14:50:10 +01004412 ASSERT_COMPARE(expected_output->x, expected_output->len,
4413 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004414exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 mbedtls_free(input);
4416 mbedtls_free(output);
4417 psa_destroy_key(key);
4418 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004419}
4420/* END_CASE */
4421
4422/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004423void cipher_verify_output(int alg_arg,
4424 int key_type_arg,
4425 data_t *key_data,
4426 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004427{
Ronald Cron5425a212020-08-04 14:58:35 +02004428 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004429 psa_key_type_t key_type = key_type_arg;
4430 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004431 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004432 size_t output1_size = 0;
4433 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004434 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004435 size_t output2_size = 0;
4436 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004437 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004438
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004440
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4442 psa_set_key_algorithm(&attributes, alg);
4443 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004444
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4446 &key));
4447 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4448 ASSERT_ALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004449
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4451 output1, output1_size,
4452 &output1_length));
4453 TEST_LE_U(output1_length,
4454 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4455 TEST_LE_U(output1_length,
4456 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004457
4458 output2_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 ASSERT_ALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004460
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4462 output2, output2_size,
4463 &output2_length));
4464 TEST_LE_U(output2_length,
4465 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4466 TEST_LE_U(output2_length,
4467 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004468
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004470
4471exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004472 mbedtls_free(output1);
4473 mbedtls_free(output2);
4474 psa_destroy_key(key);
4475 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004476}
4477/* END_CASE */
4478
4479/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004480void cipher_verify_output_multipart(int alg_arg,
4481 int key_type_arg,
4482 data_t *key_data,
4483 data_t *input,
4484 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004485{
Ronald Cron5425a212020-08-04 14:58:35 +02004486 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004487 psa_key_type_t key_type = key_type_arg;
4488 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004489 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004491 size_t iv_size = 16;
4492 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004493 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004494 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004495 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004496 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004497 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004498 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004499 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004500 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4501 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004503
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004505
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4507 psa_set_key_algorithm(&attributes, alg);
4508 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004509
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4511 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004512
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4514 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004515
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 if (alg != PSA_ALG_ECB_NO_PADDING) {
4517 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4518 iv, iv_size,
4519 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004520 }
4521
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4523 TEST_LE_U(output1_buffer_size,
4524 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
4525 ASSERT_ALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004526
Gilles Peskine449bd832023-01-11 14:50:10 +01004527 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004528
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4530 output1, output1_buffer_size,
4531 &function_output_length));
4532 TEST_LE_U(function_output_length,
4533 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4534 TEST_LE_U(function_output_length,
4535 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004536 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004537
Gilles Peskine449bd832023-01-11 14:50:10 +01004538 PSA_ASSERT(psa_cipher_update(&operation1,
4539 input->x + first_part_size,
4540 input->len - first_part_size,
4541 output1, output1_buffer_size,
4542 &function_output_length));
4543 TEST_LE_U(function_output_length,
4544 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4545 alg,
4546 input->len - first_part_size));
4547 TEST_LE_U(function_output_length,
4548 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004549 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004550
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 PSA_ASSERT(psa_cipher_finish(&operation1,
4552 output1 + output1_length,
4553 output1_buffer_size - output1_length,
4554 &function_output_length));
4555 TEST_LE_U(function_output_length,
4556 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4557 TEST_LE_U(function_output_length,
4558 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004559 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004560
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004562
Gilles Peskine048b7f02018-06-08 14:20:49 +02004563 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 TEST_LE_U(output2_buffer_size,
4565 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4566 TEST_LE_U(output2_buffer_size,
4567 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
4568 ASSERT_ALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004569
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 if (iv_length > 0) {
4571 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4572 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004573 }
Moran Pekerded84402018-06-06 16:36:50 +03004574
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4576 output2, output2_buffer_size,
4577 &function_output_length));
4578 TEST_LE_U(function_output_length,
4579 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4580 TEST_LE_U(function_output_length,
4581 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004582 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004583
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 PSA_ASSERT(psa_cipher_update(&operation2,
4585 output1 + first_part_size,
4586 output1_length - first_part_size,
4587 output2, output2_buffer_size,
4588 &function_output_length));
4589 TEST_LE_U(function_output_length,
4590 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4591 alg,
4592 output1_length - first_part_size));
4593 TEST_LE_U(function_output_length,
4594 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004595 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004596
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 PSA_ASSERT(psa_cipher_finish(&operation2,
4598 output2 + output2_length,
4599 output2_buffer_size - output2_length,
4600 &function_output_length));
4601 TEST_LE_U(function_output_length,
4602 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4603 TEST_LE_U(function_output_length,
4604 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004605 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004606
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004608
Gilles Peskine449bd832023-01-11 14:50:10 +01004609 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004610
4611exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 psa_cipher_abort(&operation1);
4613 psa_cipher_abort(&operation2);
4614 mbedtls_free(output1);
4615 mbedtls_free(output2);
4616 psa_destroy_key(key);
4617 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004618}
4619/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004620
Gilles Peskine20035e32018-02-03 22:44:14 +01004621/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004622void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4623 int alg_arg,
4624 data_t *nonce,
4625 data_t *additional_data,
4626 data_t *input_data,
4627 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004628{
Ronald Cron5425a212020-08-04 14:58:35 +02004629 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004630 psa_key_type_t key_type = key_type_arg;
4631 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004632 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004633 unsigned char *output_data = NULL;
4634 size_t output_size = 0;
4635 size_t output_length = 0;
4636 unsigned char *output_data2 = NULL;
4637 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004638 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004639 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004640 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004641
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004643
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4645 psa_set_key_algorithm(&attributes, alg);
4646 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004647
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4649 &key));
4650 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4651 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004652
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4654 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004655 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4656 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4658 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4659 TEST_EQUAL(output_size,
4660 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4661 TEST_LE_U(output_size,
4662 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004663 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004665
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 status = psa_aead_encrypt(key, alg,
4667 nonce->x, nonce->len,
4668 additional_data->x,
4669 additional_data->len,
4670 input_data->x, input_data->len,
4671 output_data, output_size,
4672 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004673
4674 /* If the operation is not supported, just skip and not fail in case the
4675 * encryption involves a common limitation of cryptography hardwares and
4676 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004677 if (status == PSA_ERROR_NOT_SUPPORTED) {
4678 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4679 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004680 }
4681
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004683
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 if (PSA_SUCCESS == expected_result) {
4685 ASSERT_ALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004686
Gilles Peskine003a4a92019-05-14 16:09:40 +02004687 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4688 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 TEST_EQUAL(input_data->len,
4690 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004691
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 TEST_LE_U(input_data->len,
4693 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004694
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 TEST_EQUAL(psa_aead_decrypt(key, alg,
4696 nonce->x, nonce->len,
4697 additional_data->x,
4698 additional_data->len,
4699 output_data, output_length,
4700 output_data2, output_length,
4701 &output_length2),
4702 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004703
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 ASSERT_COMPARE(input_data->x, input_data->len,
4705 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004706 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004707
Gilles Peskinea1cac842018-06-11 19:33:02 +02004708exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 psa_destroy_key(key);
4710 mbedtls_free(output_data);
4711 mbedtls_free(output_data2);
4712 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004713}
4714/* END_CASE */
4715
4716/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004717void aead_encrypt(int key_type_arg, data_t *key_data,
4718 int alg_arg,
4719 data_t *nonce,
4720 data_t *additional_data,
4721 data_t *input_data,
4722 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004723{
Ronald Cron5425a212020-08-04 14:58:35 +02004724 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004725 psa_key_type_t key_type = key_type_arg;
4726 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004727 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004728 unsigned char *output_data = NULL;
4729 size_t output_size = 0;
4730 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004731 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004732 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004733
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004735
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4737 psa_set_key_algorithm(&attributes, alg);
4738 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004739
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4741 &key));
4742 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4743 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004744
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4746 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004747 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4748 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 TEST_EQUAL(output_size,
4750 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4751 TEST_LE_U(output_size,
4752 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
4753 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004754
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 status = psa_aead_encrypt(key, alg,
4756 nonce->x, nonce->len,
4757 additional_data->x, additional_data->len,
4758 input_data->x, input_data->len,
4759 output_data, output_size,
4760 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004761
Ronald Cron28a45ed2021-02-09 20:35:42 +01004762 /* If the operation is not supported, just skip and not fail in case the
4763 * encryption involves a common limitation of cryptography hardwares and
4764 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 if (status == PSA_ERROR_NOT_SUPPORTED) {
4766 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4767 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004768 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004769
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 PSA_ASSERT(status);
4771 ASSERT_COMPARE(expected_result->x, expected_result->len,
4772 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004773
Gilles Peskinea1cac842018-06-11 19:33:02 +02004774exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004775 psa_destroy_key(key);
4776 mbedtls_free(output_data);
4777 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004778}
4779/* END_CASE */
4780
4781/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004782void aead_decrypt(int key_type_arg, data_t *key_data,
4783 int alg_arg,
4784 data_t *nonce,
4785 data_t *additional_data,
4786 data_t *input_data,
4787 data_t *expected_data,
4788 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004789{
Ronald Cron5425a212020-08-04 14:58:35 +02004790 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004791 psa_key_type_t key_type = key_type_arg;
4792 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004793 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004794 unsigned char *output_data = NULL;
4795 size_t output_size = 0;
4796 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004797 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004798 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004799 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004800
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004802
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4804 psa_set_key_algorithm(&attributes, alg);
4805 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004806
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4808 &key));
4809 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4810 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004811
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4813 alg);
4814 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4815 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004816 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4817 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 TEST_EQUAL(output_size,
4819 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4820 TEST_LE_U(output_size,
4821 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004822 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004824
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 status = psa_aead_decrypt(key, alg,
4826 nonce->x, nonce->len,
4827 additional_data->x,
4828 additional_data->len,
4829 input_data->x, input_data->len,
4830 output_data, output_size,
4831 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004832
Ronald Cron28a45ed2021-02-09 20:35:42 +01004833 /* If the operation is not supported, just skip and not fail in case the
4834 * decryption involves a common limitation of cryptography hardwares and
4835 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 if (status == PSA_ERROR_NOT_SUPPORTED) {
4837 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4838 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004839 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004840
Gilles Peskine449bd832023-01-11 14:50:10 +01004841 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004842
Gilles Peskine449bd832023-01-11 14:50:10 +01004843 if (expected_result == PSA_SUCCESS) {
4844 ASSERT_COMPARE(expected_data->x, expected_data->len,
4845 output_data, output_length);
4846 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004847
Gilles Peskinea1cac842018-06-11 19:33:02 +02004848exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 psa_destroy_key(key);
4850 mbedtls_free(output_data);
4851 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004852}
4853/* END_CASE */
4854
4855/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004856void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4857 int alg_arg,
4858 data_t *nonce,
4859 data_t *additional_data,
4860 data_t *input_data,
4861 int do_set_lengths,
4862 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004863{
Paul Elliottd3f82412021-06-16 16:52:21 +01004864 size_t ad_part_len = 0;
4865 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004866 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4869 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 if (do_set_lengths) {
4872 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004873 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004875 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004877 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004878
4879 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 if (!aead_multipart_internal_func(key_type_arg, key_data,
4881 alg_arg, nonce,
4882 additional_data,
4883 ad_part_len,
4884 input_data, -1,
4885 set_lengths_method,
4886 expected_output,
4887 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004888 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004889 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004890
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 /* length(0) part, length(ad_part_len) part, length(0) part... */
4892 mbedtls_test_set_step(1000 + ad_part_len);
4893
4894 if (!aead_multipart_internal_func(key_type_arg, key_data,
4895 alg_arg, nonce,
4896 additional_data,
4897 ad_part_len,
4898 input_data, -1,
4899 set_lengths_method,
4900 expected_output,
4901 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004902 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 }
4904 }
4905
4906 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4907 /* Split data into length(data_part_len) parts. */
4908 mbedtls_test_set_step(2000 + data_part_len);
4909
4910 if (do_set_lengths) {
4911 if (data_part_len & 0x01) {
4912 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4913 } else {
4914 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4915 }
4916 }
4917
4918 if (!aead_multipart_internal_func(key_type_arg, key_data,
4919 alg_arg, nonce,
4920 additional_data, -1,
4921 input_data, data_part_len,
4922 set_lengths_method,
4923 expected_output,
4924 1, 0)) {
4925 break;
4926 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004927
4928 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004930
Gilles Peskine449bd832023-01-11 14:50:10 +01004931 if (!aead_multipart_internal_func(key_type_arg, key_data,
4932 alg_arg, nonce,
4933 additional_data, -1,
4934 input_data, data_part_len,
4935 set_lengths_method,
4936 expected_output,
4937 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004938 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004939 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004940 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004941
Paul Elliott8fc45162021-06-23 16:06:01 +01004942 /* Goto is required to silence warnings about unused labels, as we
4943 * don't actually do any test assertions in this function. */
4944 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004945}
4946/* END_CASE */
4947
4948/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004949void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
4950 int alg_arg,
4951 data_t *nonce,
4952 data_t *additional_data,
4953 data_t *input_data,
4954 int do_set_lengths,
4955 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004956{
Paul Elliottd3f82412021-06-16 16:52:21 +01004957 size_t ad_part_len = 0;
4958 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004959 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004960
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004962 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004964
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 if (do_set_lengths) {
4966 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004967 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004969 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004971 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004972
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 if (!aead_multipart_internal_func(key_type_arg, key_data,
4974 alg_arg, nonce,
4975 additional_data,
4976 ad_part_len,
4977 input_data, -1,
4978 set_lengths_method,
4979 expected_output,
4980 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004981 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004983
4984 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01004985 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004986
Gilles Peskine449bd832023-01-11 14:50:10 +01004987 if (!aead_multipart_internal_func(key_type_arg, key_data,
4988 alg_arg, nonce,
4989 additional_data,
4990 ad_part_len,
4991 input_data, -1,
4992 set_lengths_method,
4993 expected_output,
4994 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004995 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004997 }
4998
Gilles Peskine449bd832023-01-11 14:50:10 +01004999 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005000 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005001 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005002
Gilles Peskine449bd832023-01-11 14:50:10 +01005003 if (do_set_lengths) {
5004 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005005 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005007 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005009 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005010
Gilles Peskine449bd832023-01-11 14:50:10 +01005011 if (!aead_multipart_internal_func(key_type_arg, key_data,
5012 alg_arg, nonce,
5013 additional_data, -1,
5014 input_data, data_part_len,
5015 set_lengths_method,
5016 expected_output,
5017 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005018 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005020
5021 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005023
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 if (!aead_multipart_internal_func(key_type_arg, key_data,
5025 alg_arg, nonce,
5026 additional_data, -1,
5027 input_data, data_part_len,
5028 set_lengths_method,
5029 expected_output,
5030 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005031 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005033 }
5034
Paul Elliott8fc45162021-06-23 16:06:01 +01005035 /* Goto is required to silence warnings about unused labels, as we
5036 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005037 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005038}
5039/* END_CASE */
5040
5041/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005042void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5043 int alg_arg,
5044 int nonce_length,
5045 int expected_nonce_length_arg,
5046 data_t *additional_data,
5047 data_t *input_data,
5048 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005049{
5050
5051 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5052 psa_key_type_t key_type = key_type_arg;
5053 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005054 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005055 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5056 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5057 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005058 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005059 size_t actual_nonce_length = 0;
5060 size_t expected_nonce_length = expected_nonce_length_arg;
5061 unsigned char *output = NULL;
5062 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005063 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005064 size_t ciphertext_size = 0;
5065 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005066 size_t tag_length = 0;
5067 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005068
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005070
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5072 psa_set_key_algorithm(&attributes, alg);
5073 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005074
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5076 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005077
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005079
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005081
Gilles Peskine449bd832023-01-11 14:50:10 +01005082 ASSERT_ALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005083
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005085
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005087
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005089
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005091
5092 /* If the operation is not supported, just skip and not fail in case the
5093 * encryption involves a common limitation of cryptography hardwares and
5094 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 if (status == PSA_ERROR_NOT_SUPPORTED) {
5096 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5097 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005098 }
5099
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005101
Gilles Peskine449bd832023-01-11 14:50:10 +01005102 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5103 nonce_length,
5104 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005105
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005107
Gilles Peskine449bd832023-01-11 14:50:10 +01005108 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005109
Gilles Peskine449bd832023-01-11 14:50:10 +01005110 if (expected_status == PSA_SUCCESS) {
5111 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5112 alg));
5113 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005114
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005116
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005118 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5120 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005121
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5123 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005124
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5126 output, output_size,
5127 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005128
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5130 &ciphertext_length, tag_buffer,
5131 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005132 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005133
5134exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 psa_destroy_key(key);
5136 mbedtls_free(output);
5137 mbedtls_free(ciphertext);
5138 psa_aead_abort(&operation);
5139 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005140}
5141/* END_CASE */
5142
5143/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005144void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5145 int alg_arg,
5146 int nonce_length_arg,
5147 int set_lengths_method_arg,
5148 data_t *additional_data,
5149 data_t *input_data,
5150 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005151{
5152
5153 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5154 psa_key_type_t key_type = key_type_arg;
5155 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005156 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005157 uint8_t *nonce_buffer = NULL;
5158 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5159 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5160 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005161 unsigned char *output = NULL;
5162 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005163 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005164 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005165 size_t ciphertext_size = 0;
5166 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005167 size_t tag_length = 0;
5168 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005169 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005170 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005171
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005173
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5175 psa_set_key_algorithm(&attributes, alg);
5176 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005177
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5179 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005180
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005182
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005184
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 ASSERT_ALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005186
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005188
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005190
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005192
Gilles Peskine449bd832023-01-11 14:50:10 +01005193 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005194
5195 /* If the operation is not supported, just skip and not fail in case the
5196 * encryption involves a common limitation of cryptography hardwares and
5197 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 if (status == PSA_ERROR_NOT_SUPPORTED) {
5199 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5200 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005201 }
5202
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005204
Paul Elliott4023ffd2021-09-10 16:21:22 +01005205 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 if (nonce_length_arg == -1) {
5207 /* Arbitrary size buffer, to test zero length valid buffer. */
5208 ASSERT_ALLOC(nonce_buffer, 4);
5209 nonce_length = 0;
5210 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005211 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 nonce_length = (size_t) nonce_length_arg;
5213 ASSERT_ALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005214
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 if (nonce_buffer) {
5216 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005217 nonce_buffer[index] = 'a' + index;
5218 }
Paul Elliott66696b52021-08-16 18:42:41 +01005219 }
Paul Elliott863864a2021-07-23 17:28:31 +01005220 }
5221
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5223 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5224 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005225 }
5226
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005228
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005230
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 if (expected_status == PSA_SUCCESS) {
5232 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5233 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5234 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005235 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005237 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005238 }
Paul Elliott863864a2021-07-23 17:28:31 +01005239
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005240 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5242 additional_data->len),
5243 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005244
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5246 output, output_size,
5247 &ciphertext_length),
5248 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005249
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5251 &ciphertext_length, tag_buffer,
5252 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5253 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005254 }
5255
5256exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 psa_destroy_key(key);
5258 mbedtls_free(output);
5259 mbedtls_free(ciphertext);
5260 mbedtls_free(nonce_buffer);
5261 psa_aead_abort(&operation);
5262 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005263}
5264/* END_CASE */
5265
5266/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005267void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005268 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005269 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005270 data_t *nonce,
5271 data_t *additional_data,
5272 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005274{
5275
5276 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5277 psa_key_type_t key_type = key_type_arg;
5278 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005279 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005280 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5281 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5282 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005283 unsigned char *output = NULL;
5284 unsigned char *ciphertext = NULL;
5285 size_t output_size = output_size_arg;
5286 size_t ciphertext_size = 0;
5287 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005288 size_t tag_length = 0;
5289 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5290
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005292
Gilles Peskine449bd832023-01-11 14:50:10 +01005293 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5294 psa_set_key_algorithm(&attributes, alg);
5295 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005296
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5298 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005299
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005301
Gilles Peskine449bd832023-01-11 14:50:10 +01005302 ASSERT_ALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005303
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005305
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005307
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005309
5310 /* If the operation is not supported, just skip and not fail in case the
5311 * encryption involves a common limitation of cryptography hardwares and
5312 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 if (status == PSA_ERROR_NOT_SUPPORTED) {
5314 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5315 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005316 }
5317
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005319
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5321 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005322
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005324
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5326 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005327
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 status = psa_aead_update(&operation, input_data->x, input_data->len,
5329 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005330
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005332
Gilles Peskine449bd832023-01-11 14:50:10 +01005333 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005334 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5336 &ciphertext_length, tag_buffer,
5337 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005338 }
5339
5340exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 psa_destroy_key(key);
5342 mbedtls_free(output);
5343 mbedtls_free(ciphertext);
5344 psa_aead_abort(&operation);
5345 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005346}
5347/* END_CASE */
5348
Paul Elliott91b021e2021-07-23 18:52:31 +01005349/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005350void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5351 int alg_arg,
5352 int finish_ciphertext_size_arg,
5353 int tag_size_arg,
5354 data_t *nonce,
5355 data_t *additional_data,
5356 data_t *input_data,
5357 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005358{
5359
5360 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5361 psa_key_type_t key_type = key_type_arg;
5362 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005363 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005364 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5365 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5366 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005367 unsigned char *ciphertext = NULL;
5368 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005369 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005370 size_t ciphertext_size = 0;
5371 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5373 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005374 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005375
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005377
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5379 psa_set_key_algorithm(&attributes, alg);
5380 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005381
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5383 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005384
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005386
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005388
Gilles Peskine449bd832023-01-11 14:50:10 +01005389 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005390
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005392
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 ASSERT_ALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005394
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005396
5397 /* If the operation is not supported, just skip and not fail in case the
5398 * encryption involves a common limitation of cryptography hardwares and
5399 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 if (status == PSA_ERROR_NOT_SUPPORTED) {
5401 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5402 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005403 }
5404
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005406
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005408
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5410 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005411
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5413 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005414
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5416 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005417
5418 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 status = psa_aead_finish(&operation, finish_ciphertext,
5420 finish_ciphertext_size,
5421 &ciphertext_length, tag_buffer,
5422 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005423
Gilles Peskine449bd832023-01-11 14:50:10 +01005424 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005425
5426exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 psa_destroy_key(key);
5428 mbedtls_free(ciphertext);
5429 mbedtls_free(finish_ciphertext);
5430 mbedtls_free(tag_buffer);
5431 psa_aead_abort(&operation);
5432 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005433}
5434/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005435
5436/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005437void aead_multipart_verify(int key_type_arg, data_t *key_data,
5438 int alg_arg,
5439 data_t *nonce,
5440 data_t *additional_data,
5441 data_t *input_data,
5442 data_t *tag,
5443 int tag_usage_arg,
5444 int expected_setup_status_arg,
5445 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005446{
5447 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5448 psa_key_type_t key_type = key_type_arg;
5449 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005450 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005451 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5452 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5453 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005454 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005455 unsigned char *plaintext = NULL;
5456 unsigned char *finish_plaintext = NULL;
5457 size_t plaintext_size = 0;
5458 size_t plaintext_length = 0;
5459 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005460 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005461 unsigned char *tag_buffer = NULL;
5462 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005463
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005465
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5467 psa_set_key_algorithm(&attributes, alg);
5468 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005469
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5471 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005472
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005474
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5476 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005477
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 ASSERT_ALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005479
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005481
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 ASSERT_ALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005483
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005485
5486 /* If the operation is not supported, just skip and not fail in case the
5487 * encryption involves a common limitation of cryptography hardwares and
5488 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 if (status == PSA_ERROR_NOT_SUPPORTED) {
5490 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5491 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005492 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005494
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005496 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 }
Paul Elliott9961a662021-09-17 19:19:02 +01005498
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005500
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005502
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 status = psa_aead_set_lengths(&operation, additional_data->len,
5504 input_data->len);
5505 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005506
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5508 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005509
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5511 input_data->len,
5512 plaintext, plaintext_size,
5513 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005514
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005516 tag_buffer = tag->x;
5517 tag_size = tag->len;
5518 }
5519
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 status = psa_aead_verify(&operation, finish_plaintext,
5521 verify_plaintext_size,
5522 &plaintext_length,
5523 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005524
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005526
5527exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 psa_destroy_key(key);
5529 mbedtls_free(plaintext);
5530 mbedtls_free(finish_plaintext);
5531 psa_aead_abort(&operation);
5532 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005533}
5534/* END_CASE */
5535
Paul Elliott9961a662021-09-17 19:19:02 +01005536/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005537void aead_multipart_setup(int key_type_arg, data_t *key_data,
5538 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005539{
5540 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5541 psa_key_type_t key_type = key_type_arg;
5542 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005543 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005544 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5545 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5546 psa_status_t expected_status = expected_status_arg;
5547
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005549
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 psa_set_key_usage_flags(&attributes,
5551 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5552 psa_set_key_algorithm(&attributes, alg);
5553 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005554
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5556 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005557
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005559
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005561
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005563
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005565
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005567
5568exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 psa_destroy_key(key);
5570 psa_aead_abort(&operation);
5571 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005572}
5573/* END_CASE */
5574
5575/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005576void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5577 int alg_arg,
5578 data_t *nonce,
5579 data_t *additional_data,
5580 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005581{
5582 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5583 psa_key_type_t key_type = key_type_arg;
5584 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005585 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005586 unsigned char *output_data = NULL;
5587 unsigned char *final_data = NULL;
5588 size_t output_size = 0;
5589 size_t finish_output_size = 0;
5590 size_t output_length = 0;
5591 size_t key_bits = 0;
5592 size_t tag_length = 0;
5593 size_t tag_size = 0;
5594 size_t nonce_length = 0;
5595 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5596 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5597 size_t output_part_length = 0;
5598 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5599
Gilles Peskine449bd832023-01-11 14:50:10 +01005600 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005601
Gilles Peskine449bd832023-01-11 14:50:10 +01005602 psa_set_key_usage_flags(&attributes,
5603 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5604 psa_set_key_algorithm(&attributes, alg);
5605 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005606
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5608 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005609
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5611 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005612
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005614
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005616
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005618
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 ASSERT_ALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005620
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005622
Gilles Peskine449bd832023-01-11 14:50:10 +01005623 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005624
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 ASSERT_ALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005626
5627 /* Test all operations error without calling setup first. */
5628
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5630 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005631
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005633
Gilles Peskine449bd832023-01-11 14:50:10 +01005634 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5635 PSA_AEAD_NONCE_MAX_SIZE,
5636 &nonce_length),
5637 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005638
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005640
Paul Elliott481be342021-07-16 17:38:47 +01005641 /* ------------------------------------------------------- */
5642
Gilles Peskine449bd832023-01-11 14:50:10 +01005643 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5644 input_data->len),
5645 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005646
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005648
Paul Elliott481be342021-07-16 17:38:47 +01005649 /* ------------------------------------------------------- */
5650
Gilles Peskine449bd832023-01-11 14:50:10 +01005651 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5652 additional_data->len),
5653 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005654
Gilles Peskine449bd832023-01-11 14:50:10 +01005655 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005656
Paul Elliott481be342021-07-16 17:38:47 +01005657 /* ------------------------------------------------------- */
5658
Gilles Peskine449bd832023-01-11 14:50:10 +01005659 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5660 input_data->len, output_data,
5661 output_size, &output_length),
5662 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005663
Gilles Peskine449bd832023-01-11 14:50:10 +01005664 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005665
Paul Elliott481be342021-07-16 17:38:47 +01005666 /* ------------------------------------------------------- */
5667
Gilles Peskine449bd832023-01-11 14:50:10 +01005668 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5669 finish_output_size,
5670 &output_part_length,
5671 tag_buffer, tag_length,
5672 &tag_size),
5673 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005674
Gilles Peskine449bd832023-01-11 14:50:10 +01005675 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005676
Paul Elliott481be342021-07-16 17:38:47 +01005677 /* ------------------------------------------------------- */
5678
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5680 finish_output_size,
5681 &output_part_length,
5682 tag_buffer,
5683 tag_length),
5684 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005685
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005687
5688 /* Test for double setups. */
5689
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005691
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5693 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005694
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005696
Paul Elliott481be342021-07-16 17:38:47 +01005697 /* ------------------------------------------------------- */
5698
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005700
Gilles Peskine449bd832023-01-11 14:50:10 +01005701 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5702 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005703
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005705
Paul Elliott374a2be2021-07-16 17:53:40 +01005706 /* ------------------------------------------------------- */
5707
Gilles Peskine449bd832023-01-11 14:50:10 +01005708 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005709
Gilles Peskine449bd832023-01-11 14:50:10 +01005710 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5711 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005712
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005714
5715 /* ------------------------------------------------------- */
5716
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005718
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5720 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005721
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005723
Paul Elliottc23a9a02021-06-21 18:32:46 +01005724 /* Test for not setting a nonce. */
5725
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005727
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5729 additional_data->len),
5730 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005731
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005733
Paul Elliott7f628422021-09-01 12:08:29 +01005734 /* ------------------------------------------------------- */
5735
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005737
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5739 input_data->len, output_data,
5740 output_size, &output_length),
5741 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005742
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005744
Paul Elliottbdc2c682021-09-21 18:37:10 +01005745 /* ------------------------------------------------------- */
5746
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005748
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5750 finish_output_size,
5751 &output_part_length,
5752 tag_buffer, tag_length,
5753 &tag_size),
5754 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005755
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005757
5758 /* ------------------------------------------------------- */
5759
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005761
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5763 finish_output_size,
5764 &output_part_length,
5765 tag_buffer,
5766 tag_length),
5767 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005768
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005770
Paul Elliottc23a9a02021-06-21 18:32:46 +01005771 /* Test for double setting nonce. */
5772
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005774
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005776
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5778 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005779
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005781
Paul Elliott374a2be2021-07-16 17:53:40 +01005782 /* Test for double generating nonce. */
5783
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005785
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5787 PSA_AEAD_NONCE_MAX_SIZE,
5788 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005789
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5791 PSA_AEAD_NONCE_MAX_SIZE,
5792 &nonce_length),
5793 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005794
5795
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005797
5798 /* Test for generate nonce then set and vice versa */
5799
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005801
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5803 PSA_AEAD_NONCE_MAX_SIZE,
5804 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005805
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5807 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005808
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005810
Andrzej Kurekad837522021-12-15 15:28:49 +01005811 /* Test for generating nonce after calling set lengths */
5812
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005814
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5816 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005817
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5819 PSA_AEAD_NONCE_MAX_SIZE,
5820 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005821
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005823
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005824 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005825
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005827
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 if (operation.alg == PSA_ALG_CCM) {
5829 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5830 input_data->len),
5831 PSA_ERROR_INVALID_ARGUMENT);
5832 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5833 PSA_AEAD_NONCE_MAX_SIZE,
5834 &nonce_length),
5835 PSA_ERROR_BAD_STATE);
5836 } else {
5837 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5838 input_data->len));
5839 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5840 PSA_AEAD_NONCE_MAX_SIZE,
5841 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005842 }
5843
Gilles Peskine449bd832023-01-11 14:50:10 +01005844 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005845
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005846 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005847#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005849
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5851 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5852 input_data->len),
5853 PSA_ERROR_INVALID_ARGUMENT);
5854 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5855 PSA_AEAD_NONCE_MAX_SIZE,
5856 &nonce_length),
5857 PSA_ERROR_BAD_STATE);
5858 } else {
5859 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5860 input_data->len));
5861 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5862 PSA_AEAD_NONCE_MAX_SIZE,
5863 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005864 }
5865
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005867#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005868
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005869 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005870
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005872
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5874 PSA_AEAD_NONCE_MAX_SIZE,
5875 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005876
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 if (operation.alg == PSA_ALG_CCM) {
5878 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5879 input_data->len),
5880 PSA_ERROR_INVALID_ARGUMENT);
5881 } else {
5882 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5883 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005884 }
5885
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005887
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005888 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005889 /* Test for setting nonce after calling set lengths */
5890
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005892
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5894 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005895
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005897
Gilles Peskine449bd832023-01-11 14:50:10 +01005898 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005899
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005900 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005901
Gilles Peskine449bd832023-01-11 14:50:10 +01005902 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005903
Gilles Peskine449bd832023-01-11 14:50:10 +01005904 if (operation.alg == PSA_ALG_CCM) {
5905 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5906 input_data->len),
5907 PSA_ERROR_INVALID_ARGUMENT);
5908 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5909 PSA_ERROR_BAD_STATE);
5910 } else {
5911 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5912 input_data->len));
5913 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005914 }
5915
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005917
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005918 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005919#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005921
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5923 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5924 input_data->len),
5925 PSA_ERROR_INVALID_ARGUMENT);
5926 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5927 PSA_ERROR_BAD_STATE);
5928 } else {
5929 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5930 input_data->len));
5931 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005932 }
5933
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00005935#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005936
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005937 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005938
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005940
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005942
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 if (operation.alg == PSA_ALG_CCM) {
5944 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5945 input_data->len),
5946 PSA_ERROR_INVALID_ARGUMENT);
5947 } else {
5948 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5949 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005950 }
5951
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005953
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005954 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Gilles Peskine449bd832023-01-11 14:50:10 +01005955 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005956
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 if (operation.alg == PSA_ALG_GCM) {
5958 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5959 SIZE_MAX),
5960 PSA_ERROR_INVALID_ARGUMENT);
5961 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5962 PSA_ERROR_BAD_STATE);
5963 } else if (operation.alg != PSA_ALG_CCM) {
5964 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5965 SIZE_MAX));
5966 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005967 }
5968
Gilles Peskine449bd832023-01-11 14:50:10 +01005969 psa_aead_abort(&operation);
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005970
Tom Cosgrove1797b052022-12-04 17:19:59 +00005971 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01005972 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005973
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005975
Gilles Peskine449bd832023-01-11 14:50:10 +01005976 if (operation.alg == PSA_ALG_GCM) {
5977 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5978 SIZE_MAX),
5979 PSA_ERROR_INVALID_ARGUMENT);
5980 } else if (operation.alg != PSA_ALG_CCM) {
5981 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5982 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005983 }
5984
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005986
5987 /* ------------------------------------------------------- */
5988
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005990
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01005992
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5994 PSA_AEAD_NONCE_MAX_SIZE,
5995 &nonce_length),
5996 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005997
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005999
Paul Elliott7220cae2021-06-22 17:25:57 +01006000 /* Test for generating nonce in decrypt setup. */
6001
Gilles Peskine449bd832023-01-11 14:50:10 +01006002 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006003
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6005 PSA_AEAD_NONCE_MAX_SIZE,
6006 &nonce_length),
6007 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006008
Gilles Peskine449bd832023-01-11 14:50:10 +01006009 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006010
Paul Elliottc23a9a02021-06-21 18:32:46 +01006011 /* Test for setting lengths twice. */
6012
Gilles Peskine449bd832023-01-11 14:50:10 +01006013 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006014
Gilles Peskine449bd832023-01-11 14:50:10 +01006015 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006016
Gilles Peskine449bd832023-01-11 14:50:10 +01006017 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6018 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006019
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6021 input_data->len),
6022 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006023
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006025
Andrzej Kurekad837522021-12-15 15:28:49 +01006026 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006027
Gilles Peskine449bd832023-01-11 14:50:10 +01006028 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006029
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006031
Gilles Peskine449bd832023-01-11 14:50:10 +01006032 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006033
Gilles Peskine449bd832023-01-11 14:50:10 +01006034 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6035 additional_data->len),
6036 PSA_ERROR_BAD_STATE);
6037 } else {
6038 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6039 additional_data->len));
6040
6041 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6042 input_data->len),
6043 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006044 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006046
6047 /* ------------------------------------------------------- */
6048
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006050
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006052
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 if (operation.alg == PSA_ALG_CCM) {
6054 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6055 input_data->len, output_data,
6056 output_size, &output_length),
6057 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006058
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 } else {
6060 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6061 input_data->len, output_data,
6062 output_size, &output_length));
6063
6064 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6065 input_data->len),
6066 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006067 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006069
6070 /* ------------------------------------------------------- */
6071
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006073
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006075
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 if (operation.alg == PSA_ALG_CCM) {
6077 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6078 finish_output_size,
6079 &output_part_length,
6080 tag_buffer, tag_length,
6081 &tag_size));
6082 } else {
6083 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6084 finish_output_size,
6085 &output_part_length,
6086 tag_buffer, tag_length,
6087 &tag_size));
6088
6089 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6090 input_data->len),
6091 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006092 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006094
6095 /* Test for setting lengths after generating nonce + already starting data. */
6096
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006098
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6100 PSA_AEAD_NONCE_MAX_SIZE,
6101 &nonce_length));
6102 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006103
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6105 additional_data->len),
6106 PSA_ERROR_BAD_STATE);
6107 } else {
6108 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6109 additional_data->len));
6110
6111 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6112 input_data->len),
6113 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006114 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006116
6117 /* ------------------------------------------------------- */
6118
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006120
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6122 PSA_AEAD_NONCE_MAX_SIZE,
6123 &nonce_length));
6124 if (operation.alg == PSA_ALG_CCM) {
6125 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6126 input_data->len, output_data,
6127 output_size, &output_length),
6128 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006129
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 } else {
6131 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6132 input_data->len, output_data,
6133 output_size, &output_length));
6134
6135 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6136 input_data->len),
6137 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006138 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006140
6141 /* ------------------------------------------------------- */
6142
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006144
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6146 PSA_AEAD_NONCE_MAX_SIZE,
6147 &nonce_length));
6148 if (operation.alg == PSA_ALG_CCM) {
6149 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6150 finish_output_size,
6151 &output_part_length,
6152 tag_buffer, tag_length,
6153 &tag_size));
6154 } else {
6155 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6156 finish_output_size,
6157 &output_part_length,
6158 tag_buffer, tag_length,
6159 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006160
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6162 input_data->len),
6163 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006164 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006166
Paul Elliott243080c2021-07-21 19:01:17 +01006167 /* Test for not sending any additional data or data after setting non zero
6168 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006169
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006171
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006173
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6175 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006176
Gilles Peskine449bd832023-01-11 14:50:10 +01006177 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6178 finish_output_size,
6179 &output_part_length,
6180 tag_buffer, tag_length,
6181 &tag_size),
6182 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006183
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006185
Paul Elliott243080c2021-07-21 19:01:17 +01006186 /* Test for not sending any additional data or data after setting non-zero
6187 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006188
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006190
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006192
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6194 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006195
Gilles Peskine449bd832023-01-11 14:50:10 +01006196 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6197 finish_output_size,
6198 &output_part_length,
6199 tag_buffer,
6200 tag_length),
6201 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006202
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006204
Paul Elliott243080c2021-07-21 19:01:17 +01006205 /* Test for not sending any additional data after setting a non-zero length
6206 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006207
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006209
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006211
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6213 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006214
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6216 input_data->len, output_data,
6217 output_size, &output_length),
6218 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006219
Gilles Peskine449bd832023-01-11 14:50:10 +01006220 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006221
Paul Elliottf94bd992021-09-19 18:15:59 +01006222 /* Test for not sending any data after setting a non-zero length for it.*/
6223
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006225
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006227
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6229 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006230
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6232 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006233
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6235 finish_output_size,
6236 &output_part_length,
6237 tag_buffer, tag_length,
6238 &tag_size),
6239 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006240
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006242
Paul Elliottb0450fe2021-09-01 15:06:26 +01006243 /* Test for sending too much additional data after setting lengths. */
6244
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006246
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006248
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006250
6251
Gilles Peskine449bd832023-01-11 14:50:10 +01006252 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6253 additional_data->len),
6254 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006257
Paul Elliotta2a09b02021-09-22 14:56:40 +01006258 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006261
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006263
Gilles Peskine449bd832023-01-11 14:50:10 +01006264 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6265 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006266
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6268 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006269
Gilles Peskine449bd832023-01-11 14:50:10 +01006270 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6271 1),
6272 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006273
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006275
Paul Elliottb0450fe2021-09-01 15:06:26 +01006276 /* Test for sending too much data after setting lengths. */
6277
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006279
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006281
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006283
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6285 input_data->len, output_data,
6286 output_size, &output_length),
6287 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006288
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006290
Paul Elliotta2a09b02021-09-22 14:56:40 +01006291 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006292
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006294
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006296
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6298 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006299
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6301 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006302
Gilles Peskine449bd832023-01-11 14:50:10 +01006303 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6304 input_data->len, output_data,
6305 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006306
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6308 1, output_data,
6309 output_size, &output_length),
6310 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006311
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006313
Paul Elliottc23a9a02021-06-21 18:32:46 +01006314 /* Test sending additional data after data. */
6315
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006317
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006319
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 if (operation.alg != PSA_ALG_CCM) {
6321 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6322 input_data->len, output_data,
6323 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006324
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6326 additional_data->len),
6327 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006328 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006330
Paul Elliott534d0b42021-06-22 19:15:20 +01006331 /* Test calling finish on decryption. */
6332
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006334
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006336
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6338 finish_output_size,
6339 &output_part_length,
6340 tag_buffer, tag_length,
6341 &tag_size),
6342 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006343
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006345
6346 /* Test calling verify on encryption. */
6347
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006349
Gilles Peskine449bd832023-01-11 14:50:10 +01006350 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006351
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6353 finish_output_size,
6354 &output_part_length,
6355 tag_buffer,
6356 tag_length),
6357 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006358
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006360
6361
Paul Elliottc23a9a02021-06-21 18:32:46 +01006362exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 psa_destroy_key(key);
6364 psa_aead_abort(&operation);
6365 mbedtls_free(output_data);
6366 mbedtls_free(final_data);
6367 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006368}
6369/* END_CASE */
6370
6371/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006372void signature_size(int type_arg,
6373 int bits,
6374 int alg_arg,
6375 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006376{
6377 psa_key_type_t type = type_arg;
6378 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006380
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006382
Gilles Peskinee59236f2018-01-27 23:32:46 +01006383exit:
6384 ;
6385}
6386/* END_CASE */
6387
6388/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006389void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6390 int alg_arg, data_t *input_data,
6391 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006392{
Ronald Cron5425a212020-08-04 14:58:35 +02006393 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006394 psa_key_type_t key_type = key_type_arg;
6395 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006396 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006397 unsigned char *signature = NULL;
6398 size_t signature_size;
6399 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006400 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006401
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006403
Gilles Peskine449bd832023-01-11 14:50:10 +01006404 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6405 psa_set_key_algorithm(&attributes, alg);
6406 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006407
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6409 &key));
6410 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6411 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006412
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006413 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006414 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6416 key_bits, alg);
6417 TEST_ASSERT(signature_size != 0);
6418 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6419 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006420
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006421 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 PSA_ASSERT(psa_sign_hash(key, alg,
6423 input_data->x, input_data->len,
6424 signature, signature_size,
6425 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006426 /* Verify that the signature is what is expected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 ASSERT_COMPARE(output_data->x, output_data->len,
6428 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006429
6430exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006431 /*
6432 * Key attributes may have been returned by psa_get_key_attributes()
6433 * thus reset them as required.
6434 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006436
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 psa_destroy_key(key);
6438 mbedtls_free(signature);
6439 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006440}
6441/* END_CASE */
6442
6443/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006444void sign_hash_fail(int key_type_arg, data_t *key_data,
6445 int alg_arg, data_t *input_data,
6446 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006447{
Ronald Cron5425a212020-08-04 14:58:35 +02006448 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006449 psa_key_type_t key_type = key_type_arg;
6450 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006451 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006452 psa_status_t actual_status;
6453 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006454 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006455 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006456 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006457
Gilles Peskine449bd832023-01-11 14:50:10 +01006458 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006459
Gilles Peskine449bd832023-01-11 14:50:10 +01006460 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006461
Gilles Peskine449bd832023-01-11 14:50:10 +01006462 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6463 psa_set_key_algorithm(&attributes, alg);
6464 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006465
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6467 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006468
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 actual_status = psa_sign_hash(key, alg,
6470 input_data->x, input_data->len,
6471 signature, signature_size,
6472 &signature_length);
6473 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006474 /* The value of *signature_length is unspecified on error, but
6475 * whatever it is, it should be less than signature_size, so that
6476 * if the caller tries to read *signature_length bytes without
6477 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006478 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006479
6480exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 psa_reset_key_attributes(&attributes);
6482 psa_destroy_key(key);
6483 mbedtls_free(signature);
6484 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006485}
6486/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006487
6488/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006489void sign_verify_hash(int key_type_arg, data_t *key_data,
6490 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006491{
Ronald Cron5425a212020-08-04 14:58:35 +02006492 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006493 psa_key_type_t key_type = key_type_arg;
6494 psa_algorithm_t alg = alg_arg;
6495 size_t key_bits;
6496 unsigned char *signature = NULL;
6497 size_t signature_size;
6498 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006499 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006500
Gilles Peskine449bd832023-01-11 14:50:10 +01006501 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006502
Gilles Peskine449bd832023-01-11 14:50:10 +01006503 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6504 psa_set_key_algorithm(&attributes, alg);
6505 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006506
Gilles Peskine449bd832023-01-11 14:50:10 +01006507 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6508 &key));
6509 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6510 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006511
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006512 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006513 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006514 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6515 key_bits, alg);
6516 TEST_ASSERT(signature_size != 0);
6517 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6518 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006519
6520 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 PSA_ASSERT(psa_sign_hash(key, alg,
6522 input_data->x, input_data->len,
6523 signature, signature_size,
6524 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006525 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006526 TEST_LE_U(signature_length, signature_size);
6527 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006528
6529 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006530 PSA_ASSERT(psa_verify_hash(key, alg,
6531 input_data->x, input_data->len,
6532 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006533
Gilles Peskine449bd832023-01-11 14:50:10 +01006534 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006535 /* Flip a bit in the input and verify that the signature is now
6536 * detected as invalid. Flip a bit at the beginning, not at the end,
6537 * because ECDSA may ignore the last few bits of the input. */
6538 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006539 TEST_EQUAL(psa_verify_hash(key, alg,
6540 input_data->x, input_data->len,
6541 signature, signature_length),
6542 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006543 }
6544
6545exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006546 /*
6547 * Key attributes may have been returned by psa_get_key_attributes()
6548 * thus reset them as required.
6549 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006550 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006551
Gilles Peskine449bd832023-01-11 14:50:10 +01006552 psa_destroy_key(key);
6553 mbedtls_free(signature);
6554 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006555}
6556/* END_CASE */
6557
6558/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006559void verify_hash(int key_type_arg, data_t *key_data,
6560 int alg_arg, data_t *hash_data,
6561 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03006562{
Ronald Cron5425a212020-08-04 14:58:35 +02006563 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006564 psa_key_type_t key_type = key_type_arg;
6565 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006566 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006567
Gilles Peskine449bd832023-01-11 14:50:10 +01006568 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02006569
Gilles Peskine449bd832023-01-11 14:50:10 +01006570 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03006571
Gilles Peskine449bd832023-01-11 14:50:10 +01006572 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
6573 psa_set_key_algorithm(&attributes, alg);
6574 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03006575
Gilles Peskine449bd832023-01-11 14:50:10 +01006576 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6577 &key));
itayzafrir5c753392018-05-08 11:18:38 +03006578
Gilles Peskine449bd832023-01-11 14:50:10 +01006579 PSA_ASSERT(psa_verify_hash(key, alg,
6580 hash_data->x, hash_data->len,
6581 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01006582
itayzafrir5c753392018-05-08 11:18:38 +03006583exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006584 psa_reset_key_attributes(&attributes);
6585 psa_destroy_key(key);
6586 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03006587}
6588/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006589
6590/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006591void verify_hash_fail(int key_type_arg, data_t *key_data,
6592 int alg_arg, data_t *hash_data,
6593 data_t *signature_data,
6594 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006595{
Ronald Cron5425a212020-08-04 14:58:35 +02006596 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006597 psa_key_type_t key_type = key_type_arg;
6598 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006599 psa_status_t actual_status;
6600 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006601 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006602
Gilles Peskine449bd832023-01-11 14:50:10 +01006603 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006604
Gilles Peskine449bd832023-01-11 14:50:10 +01006605 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
6606 psa_set_key_algorithm(&attributes, alg);
6607 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006608
Gilles Peskine449bd832023-01-11 14:50:10 +01006609 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6610 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006611
Gilles Peskine449bd832023-01-11 14:50:10 +01006612 actual_status = psa_verify_hash(key, alg,
6613 hash_data->x, hash_data->len,
6614 signature_data->x, signature_data->len);
6615 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006616
6617exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006618 psa_reset_key_attributes(&attributes);
6619 psa_destroy_key(key);
6620 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006621}
6622/* END_CASE */
6623
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006624/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006625void sign_message_deterministic(int key_type_arg,
6626 data_t *key_data,
6627 int alg_arg,
6628 data_t *input_data,
6629 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02006630{
6631 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6632 psa_key_type_t key_type = key_type_arg;
6633 psa_algorithm_t alg = alg_arg;
6634 size_t key_bits;
6635 unsigned char *signature = NULL;
6636 size_t signature_size;
6637 size_t signature_length = 0xdeadbeef;
6638 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6639
Gilles Peskine449bd832023-01-11 14:50:10 +01006640 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02006641
Gilles Peskine449bd832023-01-11 14:50:10 +01006642 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
6643 psa_set_key_algorithm(&attributes, alg);
6644 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02006645
Gilles Peskine449bd832023-01-11 14:50:10 +01006646 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6647 &key));
6648 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6649 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02006650
Gilles Peskine449bd832023-01-11 14:50:10 +01006651 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
6652 TEST_ASSERT(signature_size != 0);
6653 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6654 ASSERT_ALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02006655
Gilles Peskine449bd832023-01-11 14:50:10 +01006656 PSA_ASSERT(psa_sign_message(key, alg,
6657 input_data->x, input_data->len,
6658 signature, signature_size,
6659 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02006660
Gilles Peskine449bd832023-01-11 14:50:10 +01006661 ASSERT_COMPARE(output_data->x, output_data->len,
6662 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02006663
6664exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006665 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02006666
Gilles Peskine449bd832023-01-11 14:50:10 +01006667 psa_destroy_key(key);
6668 mbedtls_free(signature);
6669 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02006670
6671}
6672/* END_CASE */
6673
6674/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006675void sign_message_fail(int key_type_arg,
6676 data_t *key_data,
6677 int alg_arg,
6678 data_t *input_data,
6679 int signature_size_arg,
6680 int expected_status_arg)
6681{
6682 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6683 psa_key_type_t key_type = key_type_arg;
6684 psa_algorithm_t alg = alg_arg;
6685 size_t signature_size = signature_size_arg;
6686 psa_status_t actual_status;
6687 psa_status_t expected_status = expected_status_arg;
6688 unsigned char *signature = NULL;
6689 size_t signature_length = 0xdeadbeef;
6690 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6691
6692 ASSERT_ALLOC(signature, signature_size);
6693
6694 PSA_ASSERT(psa_crypto_init());
6695
6696 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
6697 psa_set_key_algorithm(&attributes, alg);
6698 psa_set_key_type(&attributes, key_type);
6699
6700 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6701 &key));
6702
6703 actual_status = psa_sign_message(key, alg,
6704 input_data->x, input_data->len,
6705 signature, signature_size,
6706 &signature_length);
6707 TEST_EQUAL(actual_status, expected_status);
6708 /* The value of *signature_length is unspecified on error, but
6709 * whatever it is, it should be less than signature_size, so that
6710 * if the caller tries to read *signature_length bytes without
6711 * checking the error code then they don't overflow a buffer. */
6712 TEST_LE_U(signature_length, signature_size);
6713
6714exit:
6715 psa_reset_key_attributes(&attributes);
6716 psa_destroy_key(key);
6717 mbedtls_free(signature);
6718 PSA_DONE();
6719}
6720/* END_CASE */
6721
6722/* BEGIN_CASE */
6723void sign_verify_message(int key_type_arg,
6724 data_t *key_data,
6725 int alg_arg,
6726 data_t *input_data)
6727{
6728 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6729 psa_key_type_t key_type = key_type_arg;
6730 psa_algorithm_t alg = alg_arg;
6731 size_t key_bits;
6732 unsigned char *signature = NULL;
6733 size_t signature_size;
6734 size_t signature_length = 0xdeadbeef;
6735 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6736
6737 PSA_ASSERT(psa_crypto_init());
6738
6739 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6740 PSA_KEY_USAGE_VERIFY_MESSAGE);
6741 psa_set_key_algorithm(&attributes, alg);
6742 psa_set_key_type(&attributes, key_type);
6743
6744 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6745 &key));
6746 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6747 key_bits = psa_get_key_bits(&attributes);
6748
6749 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
6750 TEST_ASSERT(signature_size != 0);
6751 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6752 ASSERT_ALLOC(signature, signature_size);
6753
6754 PSA_ASSERT(psa_sign_message(key, alg,
6755 input_data->x, input_data->len,
6756 signature, signature_size,
6757 &signature_length));
6758 TEST_LE_U(signature_length, signature_size);
6759 TEST_ASSERT(signature_length > 0);
6760
6761 PSA_ASSERT(psa_verify_message(key, alg,
6762 input_data->x, input_data->len,
6763 signature, signature_length));
6764
6765 if (input_data->len != 0) {
6766 /* Flip a bit in the input and verify that the signature is now
6767 * detected as invalid. Flip a bit at the beginning, not at the end,
6768 * because ECDSA may ignore the last few bits of the input. */
6769 input_data->x[0] ^= 1;
6770 TEST_EQUAL(psa_verify_message(key, alg,
6771 input_data->x, input_data->len,
6772 signature, signature_length),
6773 PSA_ERROR_INVALID_SIGNATURE);
6774 }
6775
6776exit:
6777 psa_reset_key_attributes(&attributes);
6778
6779 psa_destroy_key(key);
6780 mbedtls_free(signature);
6781 PSA_DONE();
6782}
6783/* END_CASE */
6784
6785/* BEGIN_CASE */
6786void verify_message(int key_type_arg,
6787 data_t *key_data,
6788 int alg_arg,
6789 data_t *input_data,
6790 data_t *signature_data)
6791{
6792 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6793 psa_key_type_t key_type = key_type_arg;
6794 psa_algorithm_t alg = alg_arg;
6795 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6796
6797 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
6798
6799 PSA_ASSERT(psa_crypto_init());
6800
6801 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
6802 psa_set_key_algorithm(&attributes, alg);
6803 psa_set_key_type(&attributes, key_type);
6804
6805 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6806 &key));
6807
6808 PSA_ASSERT(psa_verify_message(key, alg,
6809 input_data->x, input_data->len,
6810 signature_data->x, signature_data->len));
6811
6812exit:
6813 psa_reset_key_attributes(&attributes);
6814 psa_destroy_key(key);
6815 PSA_DONE();
6816}
6817/* END_CASE */
6818
6819/* BEGIN_CASE */
6820void verify_message_fail(int key_type_arg,
6821 data_t *key_data,
6822 int alg_arg,
6823 data_t *hash_data,
6824 data_t *signature_data,
6825 int expected_status_arg)
6826{
6827 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6828 psa_key_type_t key_type = key_type_arg;
6829 psa_algorithm_t alg = alg_arg;
6830 psa_status_t actual_status;
6831 psa_status_t expected_status = expected_status_arg;
6832 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6833
6834 PSA_ASSERT(psa_crypto_init());
6835
6836 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
6837 psa_set_key_algorithm(&attributes, alg);
6838 psa_set_key_type(&attributes, key_type);
6839
6840 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6841 &key));
6842
6843 actual_status = psa_verify_message(key, alg,
6844 hash_data->x, hash_data->len,
6845 signature_data->x,
6846 signature_data->len);
6847 TEST_EQUAL(actual_status, expected_status);
6848
6849exit:
6850 psa_reset_key_attributes(&attributes);
6851 psa_destroy_key(key);
6852 PSA_DONE();
6853}
6854/* END_CASE */
6855
6856/* BEGIN_CASE */
6857void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02006858 data_t *key_data,
6859 int alg_arg,
6860 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01006861 data_t *label,
6862 int expected_output_length_arg,
6863 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02006864{
Ronald Cron5425a212020-08-04 14:58:35 +02006865 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006866 psa_key_type_t key_type = key_type_arg;
6867 psa_algorithm_t alg = alg_arg;
6868 size_t expected_output_length = expected_output_length_arg;
6869 size_t key_bits;
6870 unsigned char *output = NULL;
6871 size_t output_size;
6872 size_t output_length = ~0;
6873 psa_status_t actual_status;
6874 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006875 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006876
Gilles Peskine449bd832023-01-11 14:50:10 +01006877 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01006878
Gilles Peskine656896e2018-06-29 19:12:28 +02006879 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01006880 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
6881 psa_set_key_algorithm(&attributes, alg);
6882 psa_set_key_type(&attributes, key_type);
6883 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6884 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02006885
6886 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01006887 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6888 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01006889
Gilles Peskine449bd832023-01-11 14:50:10 +01006890 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
6891 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
6892 ASSERT_ALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02006893
6894 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01006895 actual_status = psa_asymmetric_encrypt(key, alg,
6896 input_data->x, input_data->len,
6897 label->x, label->len,
6898 output, output_size,
6899 &output_length);
6900 TEST_EQUAL(actual_status, expected_status);
6901 TEST_EQUAL(output_length, expected_output_length);
Gilles Peskine656896e2018-06-29 19:12:28 +02006902
Gilles Peskine68428122018-06-30 18:42:41 +02006903 /* If the label is empty, the test framework puts a non-null pointer
6904 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006905 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02006906 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006907 if (output_size != 0) {
6908 memset(output, 0, output_size);
6909 }
6910 actual_status = psa_asymmetric_encrypt(key, alg,
6911 input_data->x, input_data->len,
6912 NULL, label->len,
6913 output, output_size,
6914 &output_length);
6915 TEST_EQUAL(actual_status, expected_status);
6916 TEST_EQUAL(output_length, expected_output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02006917 }
6918
Gilles Peskine656896e2018-06-29 19:12:28 +02006919exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006920 /*
6921 * Key attributes may have been returned by psa_get_key_attributes()
6922 * thus reset them as required.
6923 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006924 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006925
Gilles Peskine449bd832023-01-11 14:50:10 +01006926 psa_destroy_key(key);
6927 mbedtls_free(output);
6928 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02006929}
6930/* END_CASE */
6931
6932/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006933void asymmetric_encrypt_decrypt(int key_type_arg,
6934 data_t *key_data,
6935 int alg_arg,
6936 data_t *input_data,
6937 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006938{
Ronald Cron5425a212020-08-04 14:58:35 +02006939 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006940 psa_key_type_t key_type = key_type_arg;
6941 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006942 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006943 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006944 size_t output_size;
6945 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006946 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006947 size_t output2_size;
6948 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006949 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006950
Gilles Peskine449bd832023-01-11 14:50:10 +01006951 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006952
Gilles Peskine449bd832023-01-11 14:50:10 +01006953 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
6954 psa_set_key_algorithm(&attributes, alg);
6955 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006956
Gilles Peskine449bd832023-01-11 14:50:10 +01006957 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6958 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006959
6960 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6962 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01006963
Gilles Peskine449bd832023-01-11 14:50:10 +01006964 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
6965 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
6966 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01006967
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006968 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006969 TEST_LE_U(output2_size,
6970 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
6971 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
6972 ASSERT_ALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006973
Gilles Peskineeebd7382018-06-08 18:11:54 +02006974 /* We test encryption by checking that encrypt-then-decrypt gives back
6975 * the original plaintext because of the non-optional random
6976 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006977 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
6978 input_data->x, input_data->len,
6979 label->x, label->len,
6980 output, output_size,
6981 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006982 /* We don't know what ciphertext length to expect, but check that
6983 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006984 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006985
Gilles Peskine449bd832023-01-11 14:50:10 +01006986 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
6987 output, output_length,
6988 label->x, label->len,
6989 output2, output2_size,
6990 &output2_length));
6991 ASSERT_COMPARE(input_data->x, input_data->len,
6992 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006993
6994exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006995 /*
6996 * Key attributes may have been returned by psa_get_key_attributes()
6997 * thus reset them as required.
6998 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006999 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007000
Gilles Peskine449bd832023-01-11 14:50:10 +01007001 psa_destroy_key(key);
7002 mbedtls_free(output);
7003 mbedtls_free(output2);
7004 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007005}
7006/* END_CASE */
7007
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007008/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007009void asymmetric_decrypt(int key_type_arg,
7010 data_t *key_data,
7011 int alg_arg,
7012 data_t *input_data,
7013 data_t *label,
7014 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007015{
Ronald Cron5425a212020-08-04 14:58:35 +02007016 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007017 psa_key_type_t key_type = key_type_arg;
7018 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01007019 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007020 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03007021 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007022 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007023 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007024
Gilles Peskine449bd832023-01-11 14:50:10 +01007025 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007026
Gilles Peskine449bd832023-01-11 14:50:10 +01007027 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
7028 psa_set_key_algorithm(&attributes, alg);
7029 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007030
Gilles Peskine449bd832023-01-11 14:50:10 +01007031 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7032 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007033
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7035 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01007036
7037 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01007038 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
7039 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
7040 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01007041
Gilles Peskine449bd832023-01-11 14:50:10 +01007042 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
7043 input_data->x, input_data->len,
7044 label->x, label->len,
7045 output,
7046 output_size,
7047 &output_length));
7048 ASSERT_COMPARE(expected_data->x, expected_data->len,
7049 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007050
Gilles Peskine68428122018-06-30 18:42:41 +02007051 /* If the label is empty, the test framework puts a non-null pointer
7052 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007053 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02007054 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007055 if (output_size != 0) {
7056 memset(output, 0, output_size);
7057 }
7058 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
7059 input_data->x, input_data->len,
7060 NULL, label->len,
7061 output,
7062 output_size,
7063 &output_length));
7064 ASSERT_COMPARE(expected_data->x, expected_data->len,
7065 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02007066 }
7067
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007068exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007069 psa_reset_key_attributes(&attributes);
7070 psa_destroy_key(key);
7071 mbedtls_free(output);
7072 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007073}
7074/* END_CASE */
7075
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007076/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007077void asymmetric_decrypt_fail(int key_type_arg,
7078 data_t *key_data,
7079 int alg_arg,
7080 data_t *input_data,
7081 data_t *label,
7082 int output_size_arg,
7083 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007084{
Ronald Cron5425a212020-08-04 14:58:35 +02007085 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007086 psa_key_type_t key_type = key_type_arg;
7087 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007088 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00007089 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007090 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007091 psa_status_t actual_status;
7092 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007093 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007094
Gilles Peskine449bd832023-01-11 14:50:10 +01007095 ASSERT_ALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007096
Gilles Peskine449bd832023-01-11 14:50:10 +01007097 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007098
Gilles Peskine449bd832023-01-11 14:50:10 +01007099 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
7100 psa_set_key_algorithm(&attributes, alg);
7101 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007102
Gilles Peskine449bd832023-01-11 14:50:10 +01007103 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7104 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007105
Gilles Peskine449bd832023-01-11 14:50:10 +01007106 actual_status = psa_asymmetric_decrypt(key, alg,
7107 input_data->x, input_data->len,
7108 label->x, label->len,
7109 output, output_size,
7110 &output_length);
7111 TEST_EQUAL(actual_status, expected_status);
7112 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007113
Gilles Peskine68428122018-06-30 18:42:41 +02007114 /* If the label is empty, the test framework puts a non-null pointer
7115 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007116 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02007117 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007118 if (output_size != 0) {
7119 memset(output, 0, output_size);
7120 }
7121 actual_status = psa_asymmetric_decrypt(key, alg,
7122 input_data->x, input_data->len,
7123 NULL, label->len,
7124 output, output_size,
7125 &output_length);
7126 TEST_EQUAL(actual_status, expected_status);
7127 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02007128 }
7129
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007130exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007131 psa_reset_key_attributes(&attributes);
7132 psa_destroy_key(key);
7133 mbedtls_free(output);
7134 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007135}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007136/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02007137
7138/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007139void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00007140{
7141 /* Test each valid way of initializing the object, except for `= {0}`, as
7142 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
7143 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007144 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007145 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01007146 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007147 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
7148 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00007149
Gilles Peskine449bd832023-01-11 14:50:10 +01007150 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00007151
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007152 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007153 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
7154 PSA_ERROR_BAD_STATE);
7155 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
7156 PSA_ERROR_BAD_STATE);
7157 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
7158 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007159
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007160 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007161 PSA_ASSERT(psa_key_derivation_abort(&func));
7162 PSA_ASSERT(psa_key_derivation_abort(&init));
7163 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00007164}
7165/* END_CASE */
7166
Janos Follath16de4a42019-06-13 16:32:24 +01007167/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007168void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02007169{
Gilles Peskineea0fb492018-07-12 17:17:20 +02007170 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007171 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007172 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007173
Gilles Peskine449bd832023-01-11 14:50:10 +01007174 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02007175
Gilles Peskine449bd832023-01-11 14:50:10 +01007176 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
7177 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02007178
7179exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007180 psa_key_derivation_abort(&operation);
7181 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02007182}
7183/* END_CASE */
7184
Janos Follathaf3c2a02019-06-12 12:34:34 +01007185/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007186void derive_set_capacity(int alg_arg, int capacity_arg,
7187 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01007188{
7189 psa_algorithm_t alg = alg_arg;
7190 size_t capacity = capacity_arg;
7191 psa_status_t expected_status = expected_status_arg;
7192 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7193
Gilles Peskine449bd832023-01-11 14:50:10 +01007194 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01007195
Gilles Peskine449bd832023-01-11 14:50:10 +01007196 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01007197
Gilles Peskine449bd832023-01-11 14:50:10 +01007198 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
7199 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01007200
7201exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007202 psa_key_derivation_abort(&operation);
7203 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01007204}
7205/* END_CASE */
7206
7207/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007208void derive_input(int alg_arg,
7209 int step_arg1, int key_type_arg1, data_t *input1,
7210 int expected_status_arg1,
7211 int step_arg2, int key_type_arg2, data_t *input2,
7212 int expected_status_arg2,
7213 int step_arg3, int key_type_arg3, data_t *input3,
7214 int expected_status_arg3,
7215 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01007216{
7217 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01007218 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
7219 psa_key_type_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
7220 psa_status_t expected_statuses[] = { expected_status_arg1,
7221 expected_status_arg2,
7222 expected_status_arg3 };
7223 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02007224 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7225 MBEDTLS_SVC_KEY_ID_INIT,
7226 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01007227 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7228 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7229 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007230 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007231 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007232 psa_status_t expected_output_status = expected_output_status_arg;
7233 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01007234
Gilles Peskine449bd832023-01-11 14:50:10 +01007235 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01007236
Gilles Peskine449bd832023-01-11 14:50:10 +01007237 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
7238 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01007239
Gilles Peskine449bd832023-01-11 14:50:10 +01007240 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01007241
Gilles Peskine449bd832023-01-11 14:50:10 +01007242 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
7243 mbedtls_test_set_step(i);
7244 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02007245 /* Skip this step */
Gilles Peskine449bd832023-01-11 14:50:10 +01007246 } else if (key_types[i] != PSA_KEY_TYPE_NONE) {
7247 psa_set_key_type(&attributes, key_types[i]);
7248 PSA_ASSERT(psa_import_key(&attributes,
7249 inputs[i]->x, inputs[i]->len,
7250 &keys[i]));
7251 if (PSA_KEY_TYPE_IS_KEY_PAIR(key_types[i]) &&
7252 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007253 // When taking a private key as secret input, use key agreement
7254 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01007255 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
7256 &operation, keys[i]),
7257 expected_statuses[i]);
7258 } else {
7259 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
7260 keys[i]),
7261 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007262 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007263 } else {
7264 TEST_EQUAL(psa_key_derivation_input_bytes(
7265 &operation, steps[i],
7266 inputs[i]->x, inputs[i]->len),
7267 expected_statuses[i]);
Janos Follathaf3c2a02019-06-12 12:34:34 +01007268 }
7269 }
7270
Gilles Peskine449bd832023-01-11 14:50:10 +01007271 if (output_key_type != PSA_KEY_TYPE_NONE) {
7272 psa_reset_key_attributes(&attributes);
7273 psa_set_key_type(&attributes, output_key_type);
7274 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007275 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01007276 psa_key_derivation_output_key(&attributes, &operation,
7277 &output_key);
7278 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007279 uint8_t buffer[1];
7280 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01007281 psa_key_derivation_output_bytes(&operation,
7282 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007283 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007284 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007285
Janos Follathaf3c2a02019-06-12 12:34:34 +01007286exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007287 psa_key_derivation_abort(&operation);
7288 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
7289 psa_destroy_key(keys[i]);
7290 }
7291 psa_destroy_key(output_key);
7292 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01007293}
7294/* END_CASE */
7295
Janos Follathd958bb72019-07-03 15:02:16 +01007296/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007297void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007298{
Janos Follathd958bb72019-07-03 15:02:16 +01007299 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007300 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02007301 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007302 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01007303 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01007304 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01007305 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01007306 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007307 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01007308 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02007309 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7310 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01007311 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007312 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007313
Gilles Peskine449bd832023-01-11 14:50:10 +01007314 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007315
Gilles Peskine449bd832023-01-11 14:50:10 +01007316 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
7317 psa_set_key_algorithm(&attributes, alg);
7318 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007319
Gilles Peskine449bd832023-01-11 14:50:10 +01007320 PSA_ASSERT(psa_import_key(&attributes,
7321 key_data, sizeof(key_data),
7322 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007323
7324 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01007325 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
7326 input1, input1_length,
7327 input2, input2_length,
7328 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01007329 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007330 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007331
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007332 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01007333 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
7334 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007335
Gilles Peskine449bd832023-01-11 14:50:10 +01007336 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007337
Gilles Peskine449bd832023-01-11 14:50:10 +01007338 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
7339 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007340
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007341exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007342 psa_key_derivation_abort(&operation);
7343 psa_destroy_key(key);
7344 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007345}
7346/* END_CASE */
7347
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007348/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007349void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007350{
7351 uint8_t output_buffer[16];
7352 size_t buffer_size = 16;
7353 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007354 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007355
Gilles Peskine449bd832023-01-11 14:50:10 +01007356 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
7357 output_buffer, buffer_size)
7358 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007359
Gilles Peskine449bd832023-01-11 14:50:10 +01007360 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
7361 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007362
Gilles Peskine449bd832023-01-11 14:50:10 +01007363 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007364
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
7366 output_buffer, buffer_size)
7367 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007368
Gilles Peskine449bd832023-01-11 14:50:10 +01007369 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
7370 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007371
7372exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007373 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007374}
7375/* END_CASE */
7376
7377/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007378void derive_output(int alg_arg,
7379 int step1_arg, data_t *input1, int expected_status_arg1,
7380 int step2_arg, data_t *input2, int expected_status_arg2,
7381 int step3_arg, data_t *input3, int expected_status_arg3,
7382 int step4_arg, data_t *input4, int expected_status_arg4,
7383 data_t *key_agreement_peer_key,
7384 int requested_capacity_arg,
7385 data_t *expected_output1,
7386 data_t *expected_output2,
7387 int other_key_input_type,
7388 int key_input_type,
7389 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007390{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007391 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01007392 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
7393 data_t *inputs[] = { input1, input2, input3, input4 };
7394 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7395 MBEDTLS_SVC_KEY_ID_INIT,
7396 MBEDTLS_SVC_KEY_ID_INIT,
7397 MBEDTLS_SVC_KEY_ID_INIT };
7398 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
7399 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007400 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007401 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007402 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01007403 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007404 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01007405 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007406 size_t output_buffer_size = 0;
7407 uint8_t *output_buffer = NULL;
7408 size_t expected_capacity;
7409 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007410 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
7411 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
7412 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
7413 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007414 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007415 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02007416 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007417
Gilles Peskine449bd832023-01-11 14:50:10 +01007418 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
7419 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007420 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01007421 }
7422 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007423 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01007424 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007425 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007426 ASSERT_ALLOC(output_buffer, output_buffer_size);
7427 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007428
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007429 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007430 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
7431 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
7432 requested_capacity));
7433 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
7434 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02007435 case 0:
7436 break;
7437 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007438 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007439 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01007440 TEST_EQUAL(psa_key_derivation_input_bytes(
7441 &operation, steps[i],
7442 inputs[i]->x, inputs[i]->len),
7443 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007444
Gilles Peskine449bd832023-01-11 14:50:10 +01007445 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007446 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007447 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007448 break;
7449 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01007450 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
7451 psa_set_key_algorithm(&attributes1, alg);
7452 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007453
Gilles Peskine449bd832023-01-11 14:50:10 +01007454 PSA_ASSERT(psa_import_key(&attributes1,
7455 inputs[i]->x, inputs[i]->len,
7456 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007457
Gilles Peskine449bd832023-01-11 14:50:10 +01007458 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
7459 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
7460 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
7461 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007462 }
7463
Gilles Peskine449bd832023-01-11 14:50:10 +01007464 PSA_ASSERT(psa_key_derivation_input_key(&operation,
7465 steps[i],
7466 keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007467 break;
7468 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007469 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007470 break;
7471 }
7472 break;
7473 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01007474 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007475 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01007476 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
7477 steps[i],
7478 inputs[i]->x,
7479 inputs[i]->len),
7480 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007481 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02007482 case 1: // input key, type DERIVE
7483 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01007484 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
7485 psa_set_key_algorithm(&attributes2, alg);
7486 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007487
7488 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01007489 if (other_key_input_type == 11) {
7490 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
7491 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007492
Gilles Peskine449bd832023-01-11 14:50:10 +01007493 PSA_ASSERT(psa_import_key(&attributes2,
7494 inputs[i]->x, inputs[i]->len,
7495 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007496
Gilles Peskine449bd832023-01-11 14:50:10 +01007497 TEST_EQUAL(psa_key_derivation_input_key(&operation,
7498 steps[i],
7499 keys[i]),
7500 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007501 break;
7502 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01007503 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
7504 psa_set_key_algorithm(&attributes3, alg);
7505 psa_set_key_type(&attributes3,
7506 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007507
Gilles Peskine449bd832023-01-11 14:50:10 +01007508 PSA_ASSERT(psa_import_key(&attributes3,
7509 inputs[i]->x, inputs[i]->len,
7510 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007511
Gilles Peskine449bd832023-01-11 14:50:10 +01007512 TEST_EQUAL(psa_key_derivation_key_agreement(
7513 &operation,
7514 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
7515 keys[i], key_agreement_peer_key->x,
7516 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007517 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007518 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007519 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007520 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01007521 }
7522
Gilles Peskine449bd832023-01-11 14:50:10 +01007523 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007524 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007525 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007526 break;
7527 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007528 TEST_EQUAL(psa_key_derivation_input_bytes(
7529 &operation, steps[i],
7530 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02007531
Gilles Peskine449bd832023-01-11 14:50:10 +01007532 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02007533 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007534 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007535 break;
7536 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007537 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007538
Gilles Peskine449bd832023-01-11 14:50:10 +01007539 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
7540 &current_capacity));
7541 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007542 expected_capacity = requested_capacity;
7543
Gilles Peskine449bd832023-01-11 14:50:10 +01007544 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007545 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
7546
7547 /* For output key derivation secret must be provided using
7548 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007549 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007550 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01007551 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007552
Gilles Peskine449bd832023-01-11 14:50:10 +01007553 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
7554 psa_set_key_algorithm(&attributes4, alg);
7555 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
7556 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007557
Gilles Peskine449bd832023-01-11 14:50:10 +01007558 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
7559 &derived_key), expected_status);
7560 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007561 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007562 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007563 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007564 status = psa_key_derivation_output_bytes(&operation,
7565 output_buffer, output_sizes[i]);
7566 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007567 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007568 TEST_ASSERT(status == PSA_SUCCESS ||
7569 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007570 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01007571 } else if (expected_capacity == 0 ||
7572 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007573 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007574 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007575 expected_capacity = 0;
7576 continue;
7577 }
7578 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007579 PSA_ASSERT(status);
7580 if (output_sizes[i] != 0) {
7581 ASSERT_COMPARE(output_buffer, output_sizes[i],
7582 expected_outputs[i], output_sizes[i]);
7583 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007584 /* Check the operation status. */
7585 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01007586 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
7587 &current_capacity));
7588 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007589 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007590 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007591 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007592
7593exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007594 mbedtls_free(output_buffer);
7595 psa_key_derivation_abort(&operation);
7596 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
7597 psa_destroy_key(keys[i]);
7598 }
7599 psa_destroy_key(derived_key);
7600 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007601}
7602/* END_CASE */
7603
7604/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007605void derive_full(int alg_arg,
7606 data_t *key_data,
7607 data_t *input1,
7608 data_t *input2,
7609 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02007610{
Ronald Cron5425a212020-08-04 14:58:35 +02007611 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007612 psa_algorithm_t alg = alg_arg;
7613 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007614 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007615 unsigned char output_buffer[16];
7616 size_t expected_capacity = requested_capacity;
7617 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007618 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007619
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02007621
Gilles Peskine449bd832023-01-11 14:50:10 +01007622 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
7623 psa_set_key_algorithm(&attributes, alg);
7624 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02007625
Gilles Peskine449bd832023-01-11 14:50:10 +01007626 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7627 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02007628
Gilles Peskine449bd832023-01-11 14:50:10 +01007629 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
7630 input1->x, input1->len,
7631 input2->x, input2->len,
7632 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01007633 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007634 }
Janos Follath47f27ed2019-06-25 13:24:52 +01007635
Gilles Peskine449bd832023-01-11 14:50:10 +01007636 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
7637 &current_capacity));
7638 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02007639
7640 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007641 while (current_capacity > 0) {
7642 size_t read_size = sizeof(output_buffer);
7643 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02007644 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01007645 }
7646 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
7647 output_buffer,
7648 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02007649 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01007650 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
7651 &current_capacity));
7652 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02007653 }
7654
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007655 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007656 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
7657 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02007658
Gilles Peskine449bd832023-01-11 14:50:10 +01007659 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02007660
7661exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007662 psa_key_derivation_abort(&operation);
7663 psa_destroy_key(key);
7664 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02007665}
7666/* END_CASE */
7667
Przemek Stekiel8258ea72022-10-19 12:17:19 +02007668/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskine449bd832023-01-11 14:50:10 +01007669void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
7670 int derivation_step,
7671 int capacity, int expected_capacity_status_arg,
7672 data_t *expected_output,
7673 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007674{
7675 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7676 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04007677 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007678 uint8_t *output_buffer = NULL;
7679 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007680 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
7681 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
7682 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007683
Gilles Peskine449bd832023-01-11 14:50:10 +01007684 ASSERT_ALLOC(output_buffer, expected_output->len);
7685 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007686
Gilles Peskine449bd832023-01-11 14:50:10 +01007687 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
7688 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
7689 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007690
Gilles Peskine449bd832023-01-11 14:50:10 +01007691 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
7692 step, input->x, input->len),
7693 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007694
Gilles Peskine449bd832023-01-11 14:50:10 +01007695 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007696 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007697 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007698
Gilles Peskine449bd832023-01-11 14:50:10 +01007699 status = psa_key_derivation_output_bytes(&operation, output_buffer,
7700 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007701
Gilles Peskine449bd832023-01-11 14:50:10 +01007702 TEST_EQUAL(status, expected_output_status);
7703 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
7704 ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x,
7705 expected_output->len);
7706 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007707
7708exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007709 mbedtls_free(output_buffer);
7710 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007711 PSA_DONE();
7712}
7713/* END_CASE */
7714
Janos Follathe60c9052019-07-03 13:51:30 +01007715/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007716void derive_key_exercise(int alg_arg,
7717 data_t *key_data,
7718 data_t *input1,
7719 data_t *input2,
7720 int derived_type_arg,
7721 int derived_bits_arg,
7722 int derived_usage_arg,
7723 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02007724{
Ronald Cron5425a212020-08-04 14:58:35 +02007725 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7726 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007727 psa_algorithm_t alg = alg_arg;
7728 psa_key_type_t derived_type = derived_type_arg;
7729 size_t derived_bits = derived_bits_arg;
7730 psa_key_usage_t derived_usage = derived_usage_arg;
7731 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01007732 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007733 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007734 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007735 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007736
Gilles Peskine449bd832023-01-11 14:50:10 +01007737 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02007738
Gilles Peskine449bd832023-01-11 14:50:10 +01007739 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
7740 psa_set_key_algorithm(&attributes, alg);
7741 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
7742 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7743 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02007744
7745 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007746 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
7747 input1->x, input1->len,
7748 input2->x, input2->len,
7749 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01007750 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007751 }
Janos Follathe60c9052019-07-03 13:51:30 +01007752
Gilles Peskine449bd832023-01-11 14:50:10 +01007753 psa_set_key_usage_flags(&attributes, derived_usage);
7754 psa_set_key_algorithm(&attributes, derived_alg);
7755 psa_set_key_type(&attributes, derived_type);
7756 psa_set_key_bits(&attributes, derived_bits);
7757 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
7758 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02007759
7760 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01007761 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
7762 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
7763 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02007764
7765 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007766 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02007767 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007768 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02007769
7770exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007771 /*
7772 * Key attributes may have been returned by psa_get_key_attributes()
7773 * thus reset them as required.
7774 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007775 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007776
Gilles Peskine449bd832023-01-11 14:50:10 +01007777 psa_key_derivation_abort(&operation);
7778 psa_destroy_key(base_key);
7779 psa_destroy_key(derived_key);
7780 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02007781}
7782/* END_CASE */
7783
Janos Follath42fd8882019-07-03 14:17:09 +01007784/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007785void derive_key_export(int alg_arg,
7786 data_t *key_data,
7787 data_t *input1,
7788 data_t *input2,
7789 int bytes1_arg,
7790 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02007791{
Ronald Cron5425a212020-08-04 14:58:35 +02007792 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7793 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007794 psa_algorithm_t alg = alg_arg;
7795 size_t bytes1 = bytes1_arg;
7796 size_t bytes2 = bytes2_arg;
7797 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007798 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007799 uint8_t *output_buffer = NULL;
7800 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007801 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7802 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007803 size_t length;
7804
Gilles Peskine449bd832023-01-11 14:50:10 +01007805 ASSERT_ALLOC(output_buffer, capacity);
7806 ASSERT_ALLOC(export_buffer, capacity);
7807 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02007808
Gilles Peskine449bd832023-01-11 14:50:10 +01007809 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
7810 psa_set_key_algorithm(&base_attributes, alg);
7811 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
7812 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
7813 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02007814
7815 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007816 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
7817 input1->x, input1->len,
7818 input2->x, input2->len,
7819 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01007820 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007821 }
Janos Follath42fd8882019-07-03 14:17:09 +01007822
Gilles Peskine449bd832023-01-11 14:50:10 +01007823 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
7824 output_buffer,
7825 capacity));
7826 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02007827
7828 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007829 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
7830 input1->x, input1->len,
7831 input2->x, input2->len,
7832 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01007833 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007834 }
Janos Follath42fd8882019-07-03 14:17:09 +01007835
Gilles Peskine449bd832023-01-11 14:50:10 +01007836 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
7837 psa_set_key_algorithm(&derived_attributes, 0);
7838 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
7839 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
7840 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
7841 &derived_key));
7842 PSA_ASSERT(psa_export_key(derived_key,
7843 export_buffer, bytes1,
7844 &length));
7845 TEST_EQUAL(length, bytes1);
7846 PSA_ASSERT(psa_destroy_key(derived_key));
7847 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
7848 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
7849 &derived_key));
7850 PSA_ASSERT(psa_export_key(derived_key,
7851 export_buffer + bytes1, bytes2,
7852 &length));
7853 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02007854
7855 /* Compare the outputs from the two runs. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007856 ASSERT_COMPARE(output_buffer, bytes1 + bytes2,
7857 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02007858
7859exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007860 mbedtls_free(output_buffer);
7861 mbedtls_free(export_buffer);
7862 psa_key_derivation_abort(&operation);
7863 psa_destroy_key(base_key);
7864 psa_destroy_key(derived_key);
7865 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02007866}
7867/* END_CASE */
7868
7869/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007870void derive_key_type(int alg_arg,
7871 data_t *key_data,
7872 data_t *input1,
7873 data_t *input2,
7874 int key_type_arg, int bits_arg,
7875 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007876{
7877 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7878 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
7879 const psa_algorithm_t alg = alg_arg;
7880 const psa_key_type_t key_type = key_type_arg;
7881 const size_t bits = bits_arg;
7882 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7883 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01007884 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007885 uint8_t *export_buffer = NULL;
7886 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7887 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
7888 size_t export_length;
7889
Gilles Peskine449bd832023-01-11 14:50:10 +01007890 ASSERT_ALLOC(export_buffer, export_buffer_size);
7891 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007892
Gilles Peskine449bd832023-01-11 14:50:10 +01007893 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
7894 psa_set_key_algorithm(&base_attributes, alg);
7895 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
7896 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
7897 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007898
Gilles Peskine449bd832023-01-11 14:50:10 +01007899 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007900 &operation, base_key, alg,
7901 input1->x, input1->len,
7902 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01007903 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007904 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007905 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007906
Gilles Peskine449bd832023-01-11 14:50:10 +01007907 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
7908 psa_set_key_algorithm(&derived_attributes, 0);
7909 psa_set_key_type(&derived_attributes, key_type);
7910 psa_set_key_bits(&derived_attributes, bits);
7911 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
7912 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007913
Gilles Peskine449bd832023-01-11 14:50:10 +01007914 PSA_ASSERT(psa_export_key(derived_key,
7915 export_buffer, export_buffer_size,
7916 &export_length));
7917 ASSERT_COMPARE(export_buffer, export_length,
7918 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007919
7920exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007921 mbedtls_free(export_buffer);
7922 psa_key_derivation_abort(&operation);
7923 psa_destroy_key(base_key);
7924 psa_destroy_key(derived_key);
7925 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007926}
7927/* END_CASE */
7928
7929/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007930void derive_key(int alg_arg,
7931 data_t *key_data, data_t *input1, data_t *input2,
7932 int type_arg, int bits_arg,
7933 int expected_status_arg,
7934 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02007935{
Ronald Cron5425a212020-08-04 14:58:35 +02007936 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7937 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02007938 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007939 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02007940 size_t bits = bits_arg;
7941 psa_status_t expected_status = expected_status_arg;
7942 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7943 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7944 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
7945
Gilles Peskine449bd832023-01-11 14:50:10 +01007946 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02007947
Gilles Peskine449bd832023-01-11 14:50:10 +01007948 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
7949 psa_set_key_algorithm(&base_attributes, alg);
7950 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
7951 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
7952 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02007953
Gilles Peskine449bd832023-01-11 14:50:10 +01007954 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
7955 input1->x, input1->len,
7956 input2->x, input2->len,
7957 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02007958 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007959 }
Gilles Peskinec744d992019-07-30 17:26:54 +02007960
Gilles Peskine449bd832023-01-11 14:50:10 +01007961 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
7962 psa_set_key_algorithm(&derived_attributes, 0);
7963 psa_set_key_type(&derived_attributes, type);
7964 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01007965
7966 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01007967 psa_key_derivation_output_key(&derived_attributes,
7968 &operation,
7969 &derived_key);
7970 if (is_large_output > 0) {
7971 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
7972 }
7973 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02007974
7975exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007976 psa_key_derivation_abort(&operation);
7977 psa_destroy_key(base_key);
7978 psa_destroy_key(derived_key);
7979 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02007980}
7981/* END_CASE */
7982
7983/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007984void key_agreement_setup(int alg_arg,
7985 int our_key_type_arg, int our_key_alg_arg,
7986 data_t *our_key_data, data_t *peer_key_data,
7987 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02007988{
Ronald Cron5425a212020-08-04 14:58:35 +02007989 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007990 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007991 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007992 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007993 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007994 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02007995 psa_status_t expected_status = expected_status_arg;
7996 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007997
Gilles Peskine449bd832023-01-11 14:50:10 +01007998 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02007999
Gilles Peskine449bd832023-01-11 14:50:10 +01008000 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8001 psa_set_key_algorithm(&attributes, our_key_alg);
8002 psa_set_key_type(&attributes, our_key_type);
8003 PSA_ASSERT(psa_import_key(&attributes,
8004 our_key_data->x, our_key_data->len,
8005 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02008006
Gilles Peskine77f40d82019-04-11 21:27:06 +02008007 /* The tests currently include inputs that should fail at either step.
8008 * Test cases that fail at the setup step should be changed to call
8009 * key_derivation_setup instead, and this function should be renamed
8010 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008011 status = psa_key_derivation_setup(&operation, alg);
8012 if (status == PSA_SUCCESS) {
8013 TEST_EQUAL(psa_key_derivation_key_agreement(
8014 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
8015 our_key,
8016 peer_key_data->x, peer_key_data->len),
8017 expected_status);
8018 } else {
8019 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02008020 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02008021
8022exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008023 psa_key_derivation_abort(&operation);
8024 psa_destroy_key(our_key);
8025 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02008026}
8027/* END_CASE */
8028
8029/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008030void raw_key_agreement(int alg_arg,
8031 int our_key_type_arg, data_t *our_key_data,
8032 data_t *peer_key_data,
8033 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02008034{
Ronald Cron5425a212020-08-04 14:58:35 +02008035 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008036 psa_algorithm_t alg = alg_arg;
8037 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008038 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008039 unsigned char *output = NULL;
8040 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01008041 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008042
Gilles Peskine449bd832023-01-11 14:50:10 +01008043 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02008044
Gilles Peskine449bd832023-01-11 14:50:10 +01008045 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8046 psa_set_key_algorithm(&attributes, alg);
8047 psa_set_key_type(&attributes, our_key_type);
8048 PSA_ASSERT(psa_import_key(&attributes,
8049 our_key_data->x, our_key_data->len,
8050 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02008051
Gilles Peskine449bd832023-01-11 14:50:10 +01008052 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
8053 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008054
Gilles Peskine992bee82022-04-13 23:25:52 +02008055 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01008056 TEST_LE_U(expected_output->len,
8057 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
8058 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
8059 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02008060
8061 /* Good case with exact output size */
Gilles Peskine449bd832023-01-11 14:50:10 +01008062 ASSERT_ALLOC(output, expected_output->len);
8063 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
8064 peer_key_data->x, peer_key_data->len,
8065 output, expected_output->len,
8066 &output_length));
8067 ASSERT_COMPARE(output, output_length,
8068 expected_output->x, expected_output->len);
8069 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02008070 output = NULL;
8071 output_length = ~0;
8072
8073 /* Larger buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01008074 ASSERT_ALLOC(output, expected_output->len + 1);
8075 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
8076 peer_key_data->x, peer_key_data->len,
8077 output, expected_output->len + 1,
8078 &output_length));
8079 ASSERT_COMPARE(output, output_length,
8080 expected_output->x, expected_output->len);
8081 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02008082 output = NULL;
8083 output_length = ~0;
8084
8085 /* Buffer too small */
Gilles Peskine449bd832023-01-11 14:50:10 +01008086 ASSERT_ALLOC(output, expected_output->len - 1);
8087 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
8088 peer_key_data->x, peer_key_data->len,
8089 output, expected_output->len - 1,
8090 &output_length),
8091 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02008092 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01008093 TEST_LE_U(output_length, expected_output->len - 1);
8094 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02008095 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008096
8097exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008098 mbedtls_free(output);
8099 psa_destroy_key(our_key);
8100 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02008101}
8102/* END_CASE */
8103
8104/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008105void key_agreement_capacity(int alg_arg,
8106 int our_key_type_arg, data_t *our_key_data,
8107 data_t *peer_key_data,
8108 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02008109{
Ronald Cron5425a212020-08-04 14:58:35 +02008110 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008111 psa_algorithm_t alg = alg_arg;
8112 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008113 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008114 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008115 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02008116 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02008117
Gilles Peskine449bd832023-01-11 14:50:10 +01008118 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02008119
Gilles Peskine449bd832023-01-11 14:50:10 +01008120 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8121 psa_set_key_algorithm(&attributes, alg);
8122 psa_set_key_type(&attributes, our_key_type);
8123 PSA_ASSERT(psa_import_key(&attributes,
8124 our_key_data->x, our_key_data->len,
8125 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02008126
Gilles Peskine449bd832023-01-11 14:50:10 +01008127 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8128 PSA_ASSERT(psa_key_derivation_key_agreement(
8129 &operation,
8130 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8131 peer_key_data->x, peer_key_data->len));
8132 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008133 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01008134 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
8135 PSA_KEY_DERIVATION_INPUT_INFO,
8136 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008137 }
Gilles Peskine59685592018-09-18 12:11:34 +02008138
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008139 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008140 PSA_ASSERT(psa_key_derivation_get_capacity(
8141 &operation, &actual_capacity));
8142 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02008143
Gilles Peskinebf491972018-10-25 22:36:12 +02008144 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008145 while (actual_capacity > sizeof(output)) {
8146 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8147 output, sizeof(output)));
8148 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02008149 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008150 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8151 output, actual_capacity));
8152 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
8153 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02008154
Gilles Peskine59685592018-09-18 12:11:34 +02008155exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008156 psa_key_derivation_abort(&operation);
8157 psa_destroy_key(our_key);
8158 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02008159}
8160/* END_CASE */
8161
8162/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008163void key_agreement_output(int alg_arg,
8164 int our_key_type_arg, data_t *our_key_data,
8165 data_t *peer_key_data,
8166 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02008167{
Ronald Cron5425a212020-08-04 14:58:35 +02008168 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008169 psa_algorithm_t alg = alg_arg;
8170 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008171 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008172 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008173 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02008174
Gilles Peskine449bd832023-01-11 14:50:10 +01008175 ASSERT_ALLOC(actual_output, MAX(expected_output1->len,
8176 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02008177
Gilles Peskine449bd832023-01-11 14:50:10 +01008178 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02008179
Gilles Peskine449bd832023-01-11 14:50:10 +01008180 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8181 psa_set_key_algorithm(&attributes, alg);
8182 psa_set_key_type(&attributes, our_key_type);
8183 PSA_ASSERT(psa_import_key(&attributes,
8184 our_key_data->x, our_key_data->len,
8185 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02008186
Gilles Peskine449bd832023-01-11 14:50:10 +01008187 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8188 PSA_ASSERT(psa_key_derivation_key_agreement(
8189 &operation,
8190 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8191 peer_key_data->x, peer_key_data->len));
8192 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008193 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01008194 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
8195 PSA_KEY_DERIVATION_INPUT_INFO,
8196 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008197 }
Gilles Peskine59685592018-09-18 12:11:34 +02008198
Gilles Peskine449bd832023-01-11 14:50:10 +01008199 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8200 actual_output,
8201 expected_output1->len));
8202 ASSERT_COMPARE(actual_output, expected_output1->len,
8203 expected_output1->x, expected_output1->len);
8204 if (expected_output2->len != 0) {
8205 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8206 actual_output,
8207 expected_output2->len));
8208 ASSERT_COMPARE(actual_output, expected_output2->len,
8209 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008210 }
Gilles Peskine59685592018-09-18 12:11:34 +02008211
8212exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008213 psa_key_derivation_abort(&operation);
8214 psa_destroy_key(our_key);
8215 PSA_DONE();
8216 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02008217}
8218/* END_CASE */
8219
8220/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008221void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02008222{
Gilles Peskinea50d7392018-06-21 10:22:13 +02008223 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008224 unsigned char *output = NULL;
8225 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02008226 size_t i;
8227 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02008228
Gilles Peskine449bd832023-01-11 14:50:10 +01008229 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00008230
Gilles Peskine449bd832023-01-11 14:50:10 +01008231 ASSERT_ALLOC(output, bytes);
8232 ASSERT_ALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02008233
Gilles Peskine449bd832023-01-11 14:50:10 +01008234 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02008235
Gilles Peskinea50d7392018-06-21 10:22:13 +02008236 /* Run several times, to ensure that every output byte will be
8237 * nonzero at least once with overwhelming probability
8238 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01008239 for (run = 0; run < 10; run++) {
8240 if (bytes != 0) {
8241 memset(output, 0, bytes);
8242 }
8243 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02008244
Gilles Peskine449bd832023-01-11 14:50:10 +01008245 for (i = 0; i < bytes; i++) {
8246 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02008247 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008248 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008249 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008250 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008251
8252 /* Check that every byte was changed to nonzero at least once. This
8253 * validates that psa_generate_random is overwriting every byte of
8254 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008255 for (i = 0; i < bytes; i++) {
8256 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02008257 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008258
8259exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008260 PSA_DONE();
8261 mbedtls_free(output);
8262 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02008263}
8264/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02008265
8266/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008267void generate_key(int type_arg,
8268 int bits_arg,
8269 int usage_arg,
8270 int alg_arg,
8271 int expected_status_arg,
8272 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02008273{
Ronald Cron5425a212020-08-04 14:58:35 +02008274 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008275 psa_key_type_t type = type_arg;
8276 psa_key_usage_t usage = usage_arg;
8277 size_t bits = bits_arg;
8278 psa_algorithm_t alg = alg_arg;
8279 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008280 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008281 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008282
Gilles Peskine449bd832023-01-11 14:50:10 +01008283 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02008284
Gilles Peskine449bd832023-01-11 14:50:10 +01008285 psa_set_key_usage_flags(&attributes, usage);
8286 psa_set_key_algorithm(&attributes, alg);
8287 psa_set_key_type(&attributes, type);
8288 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02008289
8290 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008291 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01008292
Gilles Peskine449bd832023-01-11 14:50:10 +01008293 if (is_large_key > 0) {
8294 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
8295 }
8296 TEST_EQUAL(status, expected_status);
8297 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008298 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008299 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02008300
8301 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01008302 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
8303 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
8304 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02008305
Gilles Peskine818ca122018-06-20 18:16:48 +02008306 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008307 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02008308 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008309 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02008310
8311exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008312 /*
8313 * Key attributes may have been returned by psa_get_key_attributes()
8314 * thus reset them as required.
8315 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008316 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008317
Gilles Peskine449bd832023-01-11 14:50:10 +01008318 psa_destroy_key(key);
8319 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02008320}
8321/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03008322
Ronald Cronee414c72021-03-18 18:50:08 +01008323/* 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 Peskine449bd832023-01-11 14:50:10 +01008324void generate_key_rsa(int bits_arg,
8325 data_t *e_arg,
8326 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02008327{
Ronald Cron5425a212020-08-04 14:58:35 +02008328 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02008329 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02008330 size_t bits = bits_arg;
8331 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
8332 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
8333 psa_status_t expected_status = expected_status_arg;
8334 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8335 uint8_t *exported = NULL;
8336 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01008337 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02008338 size_t exported_length = SIZE_MAX;
8339 uint8_t *e_read_buffer = NULL;
8340 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008341 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02008342 size_t e_read_length = SIZE_MAX;
8343
Gilles Peskine449bd832023-01-11 14:50:10 +01008344 if (e_arg->len == 0 ||
8345 (e_arg->len == 3 &&
8346 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02008347 is_default_public_exponent = 1;
8348 e_read_size = 0;
8349 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008350 ASSERT_ALLOC(e_read_buffer, e_read_size);
8351 ASSERT_ALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02008352
Gilles Peskine449bd832023-01-11 14:50:10 +01008353 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02008354
Gilles Peskine449bd832023-01-11 14:50:10 +01008355 psa_set_key_usage_flags(&attributes, usage);
8356 psa_set_key_algorithm(&attributes, alg);
8357 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
8358 e_arg->x, e_arg->len));
8359 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02008360
8361 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008362 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
8363 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02008364 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008365 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02008366
8367 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01008368 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8369 TEST_EQUAL(psa_get_key_type(&attributes), type);
8370 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
8371 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
8372 e_read_buffer, e_read_size,
8373 &e_read_length));
8374 if (is_default_public_exponent) {
8375 TEST_EQUAL(e_read_length, 0);
8376 } else {
8377 ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
8378 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02008379
8380 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008381 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02008382 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008383 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02008384
8385 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008386 PSA_ASSERT(psa_export_public_key(key,
8387 exported, exported_size,
8388 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02008389 {
8390 uint8_t *p = exported;
8391 uint8_t *end = exported + exported_length;
8392 size_t len;
8393 /* RSAPublicKey ::= SEQUENCE {
8394 * modulus INTEGER, -- n
8395 * publicExponent INTEGER } -- e
8396 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008397 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
8398 MBEDTLS_ASN1_SEQUENCE |
8399 MBEDTLS_ASN1_CONSTRUCTED));
8400 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
8401 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
8402 MBEDTLS_ASN1_INTEGER));
8403 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02008404 ++p;
8405 --len;
8406 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008407 if (e_arg->len == 0) {
8408 TEST_EQUAL(len, 3);
8409 TEST_EQUAL(p[0], 1);
8410 TEST_EQUAL(p[1], 0);
8411 TEST_EQUAL(p[2], 1);
8412 } else {
8413 ASSERT_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02008414 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02008415 }
8416
8417exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008418 /*
8419 * Key attributes may have been returned by psa_get_key_attributes() or
8420 * set by psa_set_key_domain_parameters() thus reset them as required.
8421 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008422 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008423
Gilles Peskine449bd832023-01-11 14:50:10 +01008424 psa_destroy_key(key);
8425 PSA_DONE();
8426 mbedtls_free(e_read_buffer);
8427 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02008428}
8429/* END_CASE */
8430
Darryl Greend49a4992018-06-18 17:27:26 +01008431/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01008432void persistent_key_load_key_from_storage(data_t *data,
8433 int type_arg, int bits_arg,
8434 int usage_flags_arg, int alg_arg,
8435 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01008436{
Gilles Peskine449bd832023-01-11 14:50:10 +01008437 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008438 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02008439 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8440 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008441 psa_key_type_t type = type_arg;
8442 size_t bits = bits_arg;
8443 psa_key_usage_t usage_flags = usage_flags_arg;
8444 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008445 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01008446 unsigned char *first_export = NULL;
8447 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008448 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01008449 size_t first_exported_length;
8450 size_t second_exported_length;
8451
Gilles Peskine449bd832023-01-11 14:50:10 +01008452 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
8453 ASSERT_ALLOC(first_export, export_size);
8454 ASSERT_ALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008455 }
Darryl Greend49a4992018-06-18 17:27:26 +01008456
Gilles Peskine449bd832023-01-11 14:50:10 +01008457 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01008458
Gilles Peskine449bd832023-01-11 14:50:10 +01008459 psa_set_key_id(&attributes, key_id);
8460 psa_set_key_usage_flags(&attributes, usage_flags);
8461 psa_set_key_algorithm(&attributes, alg);
8462 psa_set_key_type(&attributes, type);
8463 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01008464
Gilles Peskine449bd832023-01-11 14:50:10 +01008465 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00008466 case IMPORT_KEY:
8467 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008468 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
8469 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00008470 break;
Darryl Greend49a4992018-06-18 17:27:26 +01008471
Darryl Green0c6575a2018-11-07 16:05:30 +00008472 case GENERATE_KEY:
8473 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008474 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00008475 break;
8476
8477 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01008478#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01008479 {
8480 /* Create base key */
8481 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
8482 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8483 psa_set_key_usage_flags(&base_attributes,
8484 PSA_KEY_USAGE_DERIVE);
8485 psa_set_key_algorithm(&base_attributes, derive_alg);
8486 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
8487 PSA_ASSERT(psa_import_key(&base_attributes,
8488 data->x, data->len,
8489 &base_key));
8490 /* Derive a key. */
8491 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
8492 PSA_ASSERT(psa_key_derivation_input_key(
8493 &operation,
8494 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
8495 PSA_ASSERT(psa_key_derivation_input_bytes(
8496 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
8497 NULL, 0));
8498 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
8499 &operation,
8500 &key));
8501 PSA_ASSERT(psa_key_derivation_abort(&operation));
8502 PSA_ASSERT(psa_destroy_key(base_key));
8503 base_key = MBEDTLS_SVC_KEY_ID_INIT;
8504 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008505#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008506 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008507#endif
8508 break;
8509
8510 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008511 TEST_ASSERT(!"generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008512 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00008513 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008514 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01008515
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008516 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008517 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
8518 PSA_ASSERT(psa_export_key(key,
8519 first_export, export_size,
8520 &first_exported_length));
8521 if (generation_method == IMPORT_KEY) {
8522 ASSERT_COMPARE(data->x, data->len,
8523 first_export, first_exported_length);
8524 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008525 }
Darryl Greend49a4992018-06-18 17:27:26 +01008526
8527 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01008528 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008529 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01008530 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01008531
Darryl Greend49a4992018-06-18 17:27:26 +01008532 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01008533 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8534 TEST_ASSERT(mbedtls_svc_key_id_equal(
8535 psa_get_key_id(&attributes), key_id));
8536 TEST_EQUAL(psa_get_key_lifetime(&attributes),
8537 PSA_KEY_LIFETIME_PERSISTENT);
8538 TEST_EQUAL(psa_get_key_type(&attributes), type);
8539 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
8540 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
8541 mbedtls_test_update_key_usage_flags(usage_flags));
8542 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01008543
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008544 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008545 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
8546 PSA_ASSERT(psa_export_key(key,
8547 second_export, export_size,
8548 &second_exported_length));
8549 ASSERT_COMPARE(first_export, first_exported_length,
8550 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00008551 }
8552
8553 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008554 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00008555 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008556 }
Darryl Greend49a4992018-06-18 17:27:26 +01008557
8558exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008559 /*
8560 * Key attributes may have been returned by psa_get_key_attributes()
8561 * thus reset them as required.
8562 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008563 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008564
Gilles Peskine449bd832023-01-11 14:50:10 +01008565 mbedtls_free(first_export);
8566 mbedtls_free(second_export);
8567 psa_key_derivation_abort(&operation);
8568 psa_destroy_key(base_key);
8569 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008570 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01008571}
8572/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008573
Neil Armstronga557cb82022-06-10 08:58:32 +02008574/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008575void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
8576 int primitive_arg, int hash_arg, int role_arg,
8577 int test_input, data_t *pw_data,
8578 int inj_err_type_arg,
8579 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02008580{
8581 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8582 psa_pake_operation_t operation = psa_pake_operation_init();
8583 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008584 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02008585 psa_key_type_t key_type_pw = key_type_pw_arg;
8586 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008587 psa_algorithm_t hash_alg = hash_arg;
8588 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008589 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8590 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01008591 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
8592 psa_status_t expected_error = expected_error_arg;
8593 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008594 unsigned char *output_buffer = NULL;
8595 size_t output_len = 0;
8596
Gilles Peskine449bd832023-01-11 14:50:10 +01008597 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02008598
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008599 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01008600 PSA_PAKE_STEP_KEY_SHARE);
8601 ASSERT_ALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02008602
Gilles Peskine449bd832023-01-11 14:50:10 +01008603 if (pw_data->len > 0) {
8604 psa_set_key_usage_flags(&attributes, key_usage_pw);
8605 psa_set_key_algorithm(&attributes, alg);
8606 psa_set_key_type(&attributes, key_type_pw);
8607 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
8608 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02008609 }
8610
Gilles Peskine449bd832023-01-11 14:50:10 +01008611 psa_pake_cs_set_algorithm(&cipher_suite, alg);
8612 psa_pake_cs_set_primitive(&cipher_suite, primitive);
8613 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02008614
Gilles Peskine449bd832023-01-11 14:50:10 +01008615 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02008616
Gilles Peskine449bd832023-01-11 14:50:10 +01008617 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
8618 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
8619 expected_error);
8620 PSA_ASSERT(psa_pake_abort(&operation));
8621 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
8622 expected_error);
8623 PSA_ASSERT(psa_pake_abort(&operation));
8624 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
8625 expected_error);
8626 PSA_ASSERT(psa_pake_abort(&operation));
8627 TEST_EQUAL(psa_pake_set_role(&operation, role),
8628 expected_error);
8629 PSA_ASSERT(psa_pake_abort(&operation));
8630 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
8631 NULL, 0, NULL),
8632 expected_error);
8633 PSA_ASSERT(psa_pake_abort(&operation));
8634 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
8635 expected_error);
8636 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02008637 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01008638 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008639
Gilles Peskine449bd832023-01-11 14:50:10 +01008640 status = psa_pake_setup(&operation, &cipher_suite);
8641 if (status != PSA_SUCCESS) {
8642 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02008643 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01008644 }
8645
Gilles Peskine449bd832023-01-11 14:50:10 +01008646 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
8647 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
8648 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01008649 goto exit;
8650 }
8651
Gilles Peskine449bd832023-01-11 14:50:10 +01008652 status = psa_pake_set_role(&operation, role);
8653 if (status != PSA_SUCCESS) {
8654 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01008655 goto exit;
8656 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008657
Gilles Peskine449bd832023-01-11 14:50:10 +01008658 if (pw_data->len > 0) {
8659 status = psa_pake_set_password_key(&operation, key);
8660 if (status != PSA_SUCCESS) {
8661 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02008662 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01008663 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008664 }
8665
Gilles Peskine449bd832023-01-11 14:50:10 +01008666 if (inj_err_type == INJECT_ERR_INVALID_USER) {
8667 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
8668 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01008669 goto exit;
8670 }
Neil Armstrong707d9572022-06-08 17:31:49 +02008671
Gilles Peskine449bd832023-01-11 14:50:10 +01008672 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
8673 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
8674 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01008675 goto exit;
8676 }
Neil Armstrong707d9572022-06-08 17:31:49 +02008677
Gilles Peskine449bd832023-01-11 14:50:10 +01008678 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01008679 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01008680 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
8681 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01008682 goto exit;
8683 }
8684
Gilles Peskine449bd832023-01-11 14:50:10 +01008685 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01008686 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01008687 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
8688 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01008689 goto exit;
8690 }
Neil Armstrong707d9572022-06-08 17:31:49 +02008691
Gilles Peskine449bd832023-01-11 14:50:10 +01008692 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
8693 PSA_PAKE_STEP_KEY_SHARE);
8694 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
8695 PSA_PAKE_STEP_ZK_PUBLIC);
8696 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
8697 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008698
Gilles Peskine449bd832023-01-11 14:50:10 +01008699 if (test_input) {
8700 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
8701 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
8702 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01008703 goto exit;
8704 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008705
Gilles Peskine449bd832023-01-11 14:50:10 +01008706 if (inj_err_type == INJECT_UNKNOWN_STEP) {
8707 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8708 output_buffer, size_zk_proof),
8709 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01008710 goto exit;
8711 }
8712
Gilles Peskine449bd832023-01-11 14:50:10 +01008713 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
8714 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
8715 output_buffer, size_zk_proof),
8716 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01008717 goto exit;
8718 }
8719
Gilles Peskine449bd832023-01-11 14:50:10 +01008720 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
8721 output_buffer, size_key_share);
8722 if (status != PSA_SUCCESS) {
8723 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01008724 goto exit;
8725 }
8726
Gilles Peskine449bd832023-01-11 14:50:10 +01008727 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
8728 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
8729 output_buffer, size_zk_public + 1),
8730 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01008731 goto exit;
8732 }
8733
Gilles Peskine449bd832023-01-11 14:50:10 +01008734 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01008735 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01008736 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
8737 output_buffer, size_zk_public + 1);
8738 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
8739 output_buffer, size_zk_public),
8740 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01008741 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008742 }
Valerio Setti1070aed2022-11-11 19:37:31 +01008743 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01008744 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
8745 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
8746 NULL, 0, NULL),
8747 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01008748 goto exit;
8749 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008750
Gilles Peskine449bd832023-01-11 14:50:10 +01008751 if (inj_err_type == INJECT_UNKNOWN_STEP) {
8752 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8753 output_buffer, buf_size, &output_len),
8754 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01008755 goto exit;
8756 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008757
Gilles Peskine449bd832023-01-11 14:50:10 +01008758 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
8759 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
8760 output_buffer, buf_size, &output_len),
8761 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01008762 goto exit;
8763 }
8764
Gilles Peskine449bd832023-01-11 14:50:10 +01008765 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
8766 output_buffer, buf_size, &output_len);
8767 if (status != PSA_SUCCESS) {
8768 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01008769 goto exit;
8770 }
8771
Gilles Peskine449bd832023-01-11 14:50:10 +01008772 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +01008773
Gilles Peskine449bd832023-01-11 14:50:10 +01008774 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
8775 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
8776 output_buffer, size_zk_public - 1, &output_len),
8777 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +01008778 goto exit;
8779 }
8780
Gilles Peskine449bd832023-01-11 14:50:10 +01008781 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01008782 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01008783 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
8784 output_buffer, size_zk_public - 1, &output_len);
8785 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
8786 output_buffer, buf_size, &output_len),
8787 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01008788 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008789 }
8790 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008791
8792exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008793 PSA_ASSERT(psa_destroy_key(key));
8794 PSA_ASSERT(psa_pake_abort(&operation));
8795 mbedtls_free(output_buffer);
8796 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +02008797}
8798/* END_CASE */
8799
Neil Armstronga557cb82022-06-10 08:58:32 +02008800/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008801void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
8802 int client_input_first, int inject_error,
8803 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008804{
8805 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8806 psa_pake_operation_t server = psa_pake_operation_init();
8807 psa_pake_operation_t client = psa_pake_operation_init();
8808 psa_algorithm_t alg = alg_arg;
8809 psa_algorithm_t hash_alg = hash_arg;
8810 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8811 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8812
Gilles Peskine449bd832023-01-11 14:50:10 +01008813 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008814
Gilles Peskine449bd832023-01-11 14:50:10 +01008815 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8816 psa_set_key_algorithm(&attributes, alg);
8817 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
8818 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
8819 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008820
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 psa_pake_cs_set_algorithm(&cipher_suite, alg);
8822 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
8823 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008824
8825
Gilles Peskine449bd832023-01-11 14:50:10 +01008826 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
8827 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008828
Gilles Peskine449bd832023-01-11 14:50:10 +01008829 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
8830 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008831
Gilles Peskine449bd832023-01-11 14:50:10 +01008832 PSA_ASSERT(psa_pake_set_password_key(&server, key));
8833 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008834
Gilles Peskine449bd832023-01-11 14:50:10 +01008835 ecjpake_do_round(alg, primitive_arg, &server, &client,
8836 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008837
Gilles Peskine449bd832023-01-11 14:50:10 +01008838 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008839 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008840 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008841
Gilles Peskine449bd832023-01-11 14:50:10 +01008842 ecjpake_do_round(alg, primitive_arg, &server, &client,
8843 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008844
8845exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008846 psa_destroy_key(key);
8847 psa_pake_abort(&server);
8848 psa_pake_abort(&client);
8849 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008850}
8851/* END_CASE */
8852
8853/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008854void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
8855 int derive_alg_arg, data_t *pw_data,
8856 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02008857{
8858 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8859 psa_pake_operation_t server = psa_pake_operation_init();
8860 psa_pake_operation_t client = psa_pake_operation_init();
8861 psa_algorithm_t alg = alg_arg;
8862 psa_algorithm_t hash_alg = hash_arg;
8863 psa_algorithm_t derive_alg = derive_alg_arg;
8864 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8865 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8866 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +01008867 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008868 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +01008869 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01008870 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008871
Gilles Peskine449bd832023-01-11 14:50:10 +01008872 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02008873
Gilles Peskine449bd832023-01-11 14:50:10 +01008874 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8875 psa_set_key_algorithm(&attributes, alg);
8876 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
8877 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
8878 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02008879
Gilles Peskine449bd832023-01-11 14:50:10 +01008880 psa_pake_cs_set_algorithm(&cipher_suite, alg);
8881 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
8882 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02008883
Neil Armstrong1e855602022-06-15 11:32:11 +02008884 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008885 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
8886 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +02008887
Gilles Peskine449bd832023-01-11 14:50:10 +01008888 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
8889 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
8890 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
8891 PSA_KEY_DERIVATION_INPUT_SEED,
8892 (const uint8_t *) "", 0));
8893 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
8894 PSA_KEY_DERIVATION_INPUT_SEED,
8895 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +02008896 }
8897
Gilles Peskine449bd832023-01-11 14:50:10 +01008898 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
8899 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +02008900
Gilles Peskine449bd832023-01-11 14:50:10 +01008901 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
8902 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +02008903
Gilles Peskine449bd832023-01-11 14:50:10 +01008904 PSA_ASSERT(psa_pake_set_password_key(&server, key));
8905 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02008906
Gilles Peskine449bd832023-01-11 14:50:10 +01008907 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
8908 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
8909 PSA_ERROR_BAD_STATE);
8910 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
8911 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01008912 goto exit;
8913 }
Neil Armstrong1e855602022-06-15 11:32:11 +02008914
Neil Armstrongf983caf2022-06-15 15:27:48 +02008915 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +01008916 ecjpake_do_round(alg, primitive_arg, &server, &client,
8917 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +02008918
Gilles Peskine449bd832023-01-11 14:50:10 +01008919 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
8920 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
8921 PSA_ERROR_BAD_STATE);
8922 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
8923 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01008924 goto exit;
8925 }
Neil Armstrong1e855602022-06-15 11:32:11 +02008926
Neil Armstrongf983caf2022-06-15 15:27:48 +02008927 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +01008928 ecjpake_do_round(alg, primitive_arg, &server, &client,
8929 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +02008930
Gilles Peskine449bd832023-01-11 14:50:10 +01008931 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
8932 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +02008933
8934exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008935 psa_key_derivation_abort(&server_derive);
8936 psa_key_derivation_abort(&client_derive);
8937 psa_destroy_key(key);
8938 psa_pake_abort(&server);
8939 psa_pake_abort(&client);
8940 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +02008941}
8942/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02008943
8944/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008945void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02008946{
8947 const psa_algorithm_t alg = PSA_ALG_JPAKE;
8948 const size_t bits = 256;
8949 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +01008950 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02008951 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +01008952 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02008953
8954 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
8955 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008956 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
8957 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
8958 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
8959 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02008960 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +01008961 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
8962 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02008963
8964 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +01008965 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
8966 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
8967 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
8968 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
8969 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
8970 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02008971
8972 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +01008973 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
8974 PSA_PAKE_OUTPUT_MAX_SIZE);
8975 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
8976 PSA_PAKE_OUTPUT_MAX_SIZE);
8977 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
8978 PSA_PAKE_OUTPUT_MAX_SIZE);
8979 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
8980 PSA_PAKE_INPUT_MAX_SIZE);
8981 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
8982 PSA_PAKE_INPUT_MAX_SIZE);
8983 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
8984 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02008985}
8986/* END_CASE */