blob: 02cc5efe3daf06ba77ac869817714ce68a1ce40f [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
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +010016/* For psa_can_do_hash() */
17#include "psa_crypto_core.h"
18
Gilles Peskine8e94efe2021-02-13 00:25:53 +010019#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010020#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010021#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053022#if defined(PSA_CRYPTO_DRIVER_TEST)
23#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053024#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
25#else
26#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053027#endif
Ronald Cron28a45ed2021-02-09 20:35:42 +010028
Gilles Peskine4023c012021-05-27 13:21:20 +020029/* If this comes up, it's a bug in the test code or in the test data. */
30#define UNUSED 0xdeadbeef
31
Dave Rodgman647791d2021-06-23 12:49:59 +010032/* Assert that an operation is (not) active.
33 * This serves as a proxy for checking if the operation is aborted. */
Gilles Peskine449bd832023-01-11 14:50:10 +010034#define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
35#define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010036
Przemek Stekiel7c795482022-11-15 22:26:12 +010037#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +010038int ecjpake_operation_setup(psa_pake_operation_t *operation,
39 psa_pake_cipher_suite_t *cipher_suite,
40 psa_pake_role_t role,
41 mbedtls_svc_key_id_t key,
42 size_t key_available)
Przemek Stekiel7c795482022-11-15 22:26:12 +010043{
Gilles Peskine449bd832023-01-11 14:50:10 +010044 PSA_ASSERT(psa_pake_abort(operation));
Przemek Stekiel7c795482022-11-15 22:26:12 +010045
Gilles Peskine449bd832023-01-11 14:50:10 +010046 PSA_ASSERT(psa_pake_setup(operation, cipher_suite));
Przemek Stekiel7c795482022-11-15 22:26:12 +010047
Gilles Peskine449bd832023-01-11 14:50:10 +010048 PSA_ASSERT(psa_pake_set_role(operation, role));
Przemek Stekiel7c795482022-11-15 22:26:12 +010049
Gilles Peskine449bd832023-01-11 14:50:10 +010050 if (key_available) {
51 PSA_ASSERT(psa_pake_set_password_key(operation, key));
52 }
Przemek Stekielf82effa2022-11-21 15:10:32 +010053 return 0;
Przemek Stekiel7c795482022-11-15 22:26:12 +010054exit:
Przemek Stekielf82effa2022-11-21 15:10:32 +010055 return 1;
Przemek Stekiel7c795482022-11-15 22:26:12 +010056}
57#endif
58
Jaeden Amerof24c7f82018-06-27 17:20:43 +010059/** An invalid export length that will never be set by psa_export_key(). */
60static const size_t INVALID_EXPORT_LENGTH = ~0U;
61
Gilles Peskinea7aa4422018-08-14 15:17:54 +020062/** Test if a buffer contains a constant byte value.
63 *
64 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020065 *
66 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020067 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020068 * \param size Size of the buffer in bytes.
69 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020070 * \return 1 if the buffer is all-bits-zero.
71 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020072 */
Gilles Peskine449bd832023-01-11 14:50:10 +010073static int mem_is_char(void *buffer, unsigned char c, size_t size)
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020074{
75 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010076 for (i = 0; i < size; i++) {
77 if (((unsigned char *) buffer)[i] != c) {
78 return 0;
79 }
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020080 }
Gilles Peskine449bd832023-01-11 14:50:10 +010081 return 1;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020082}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010083#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020084/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
Gilles Peskine449bd832023-01-11 14:50:10 +010085static int asn1_write_10x(unsigned char **p,
86 unsigned char *start,
87 size_t bits,
88 unsigned char x)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020089{
90 int ret;
91 int len = bits / 8 + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (bits == 0) {
93 return MBEDTLS_ERR_ASN1_INVALID_DATA;
94 }
95 if (bits <= 8 && x >= 1 << (bits - 1)) {
96 return MBEDTLS_ERR_ASN1_INVALID_DATA;
97 }
98 if (*p < start || *p - start < (ptrdiff_t) len) {
99 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
100 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200101 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 (*p)[len-1] = x;
103 if (bits % 8 == 0) {
104 (*p)[1] |= 1;
105 } else {
106 (*p)[0] |= 1 << (bits % 8);
107 }
108 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
109 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
110 MBEDTLS_ASN1_INTEGER));
111 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200112}
113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114static int construct_fake_rsa_key(unsigned char *buffer,
115 size_t buffer_size,
116 unsigned char **p,
117 size_t bits,
118 int keypair)
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200119{
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 size_t half_bits = (bits + 1) / 2;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200121 int ret;
122 int len = 0;
123 /* Construct something that looks like a DER encoding of
124 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
125 * RSAPrivateKey ::= SEQUENCE {
126 * version Version,
127 * modulus INTEGER, -- n
128 * publicExponent INTEGER, -- e
129 * privateExponent INTEGER, -- d
130 * prime1 INTEGER, -- p
131 * prime2 INTEGER, -- q
132 * exponent1 INTEGER, -- d mod (p-1)
133 * exponent2 INTEGER, -- d mod (q-1)
134 * coefficient INTEGER, -- (inverse of q) mod p
135 * otherPrimeInfos OtherPrimeInfos OPTIONAL
136 * }
137 * Or, for a public key, the same structure with only
138 * version, modulus and publicExponent.
139 */
140 *p = buffer + buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 if (keypair) {
142 MBEDTLS_ASN1_CHK_ADD(len, /* pq */
143 asn1_write_10x(p, buffer, half_bits, 1));
144 MBEDTLS_ASN1_CHK_ADD(len, /* dq */
145 asn1_write_10x(p, buffer, half_bits, 1));
146 MBEDTLS_ASN1_CHK_ADD(len, /* dp */
147 asn1_write_10x(p, buffer, half_bits, 1));
148 MBEDTLS_ASN1_CHK_ADD(len, /* q */
149 asn1_write_10x(p, buffer, half_bits, 1));
150 MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
151 asn1_write_10x(p, buffer, half_bits, 3));
152 MBEDTLS_ASN1_CHK_ADD(len, /* d */
153 asn1_write_10x(p, buffer, bits, 1));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200154 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
156 asn1_write_10x(p, buffer, 17, 1));
157 MBEDTLS_ASN1_CHK_ADD(len, /* n */
158 asn1_write_10x(p, buffer, bits, 1));
159 if (keypair) {
160 MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
161 mbedtls_asn1_write_int(p, buffer, 0));
162 }
163 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200164 {
165 const unsigned char tag =
166 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200168 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200170}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100171#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173int exercise_mac_setup(psa_key_type_t key_type,
174 const unsigned char *key_bytes,
175 size_t key_length,
176 psa_algorithm_t alg,
177 psa_mac_operation_t *operation,
178 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100179{
Ronald Cron5425a212020-08-04 14:58:35 +0200180 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200181 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
184 psa_set_key_algorithm(&attributes, alg);
185 psa_set_key_type(&attributes, key_type);
186 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 *status = psa_mac_sign_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100189 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 PSA_ASSERT(psa_mac_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100191 /* If setup failed, reproduce the failure, so that the caller can
192 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 if (*status != PSA_SUCCESS) {
194 TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100195 }
196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 psa_destroy_key(key);
198 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100199
200exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 psa_destroy_key(key);
202 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100203}
204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205int exercise_cipher_setup(psa_key_type_t key_type,
206 const unsigned char *key_bytes,
207 size_t key_length,
208 psa_algorithm_t alg,
209 psa_cipher_operation_t *operation,
210 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100211{
Ronald Cron5425a212020-08-04 14:58:35 +0200212 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200213 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
216 psa_set_key_algorithm(&attributes, alg);
217 psa_set_key_type(&attributes, key_type);
218 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 *status = psa_cipher_encrypt_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100221 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 PSA_ASSERT(psa_cipher_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100223 /* If setup failed, reproduce the failure, so that the caller can
224 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if (*status != PSA_SUCCESS) {
226 TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
227 *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100228 }
229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 psa_destroy_key(key);
231 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100232
233exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 psa_destroy_key(key);
235 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100236}
237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200239{
240 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200242 uint8_t buffer[1];
243 size_t length;
244 int ok = 0;
245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 psa_set_key_id(&attributes, key_id);
247 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
248 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
249 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
250 TEST_EQUAL(psa_get_key_attributes(key, &attributes),
251 PSA_ERROR_INVALID_HANDLE);
Ronald Cronecfb2372020-07-23 17:13:42 +0200252 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
Ronald Cronecfb2372020-07-23 17:13:42 +0200254 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
256 TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
257 TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
258 TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
259 TEST_EQUAL(psa_get_key_type(&attributes), 0);
260 TEST_EQUAL(psa_get_key_bits(&attributes), 0);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
263 PSA_ERROR_INVALID_HANDLE);
264 TEST_EQUAL(psa_export_public_key(key,
265 buffer, sizeof(buffer), &length),
266 PSA_ERROR_INVALID_HANDLE);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200267
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200268 ok = 1;
269
270exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100271 /*
272 * Key attributes may have been returned by psa_get_key_attributes()
273 * thus reset them as required.
274 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return ok;
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200278}
279
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200280/* Assert that a key isn't reported as having a slot number. */
281#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100282#define ASSERT_NO_SLOT_NUMBER(attributes) \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200283 do \
284 { \
285 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 TEST_EQUAL(psa_get_key_slot_number( \
287 attributes, \
288 &ASSERT_NO_SLOT_NUMBER_slot_number), \
289 PSA_ERROR_INVALID_ARGUMENT); \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200290 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 while (0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200292#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100293#define ASSERT_NO_SLOT_NUMBER(attributes) \
294 ((void) 0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200295#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
296
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +0530297#define INPUT_INTEGER 0x10000 /* Out of range of psa_key_type_t */
298
299uint64_t parse_binary_string(data_t *bin_string)
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +0530300{
301 uint64_t result = 0;
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +0530302 TEST_LE_U(bin_string->len, 8);
303 for (size_t i = 0; i < bin_string->len; i++) {
Kusumit Ghoderao0f2f9962023-04-28 10:07:10 +0530304 result = result << 8 | bin_string->x[i];
Kusumit Ghoderao3b27a7f2023-04-19 17:20:25 +0530305 }
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +0530306exit:
307 return result; /* returns 0 if len > 8 */
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +0530308}
309
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100310/* An overapproximation of the amount of storage needed for a key of the
311 * given type and with the given content. The API doesn't make it easy
312 * to find a good value for the size. The current implementation doesn't
313 * care about the value anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100314#define KEY_BITS_FROM_DATA(type, data) \
315 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100316
Darryl Green0c6575a2018-11-07 16:05:30 +0000317typedef enum {
318 IMPORT_KEY = 0,
319 GENERATE_KEY = 1,
320 DERIVE_KEY = 2
321} generate_method;
322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323typedef enum {
Paul Elliott33746aa2021-09-15 16:40:40 +0100324 DO_NOT_SET_LENGTHS = 0,
325 SET_LENGTHS_BEFORE_NONCE = 1,
326 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100327} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329typedef enum {
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100330 USE_NULL_TAG = 0,
331 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100332} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100333
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +0530334
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100335/*!
336 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100337 * \param key_type_arg Type of key passed in
338 * \param key_data The encryption / decryption key data
339 * \param alg_arg The type of algorithm used
340 * \param nonce Nonce data
341 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100342 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100343 * feed additional data in to be encrypted /
344 * decrypted. If -1, no chunking.
345 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100346 * \param data_part_len_arg If not -1, the length of chunks to feed
347 * the data in to be encrypted / decrypted. If
348 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100349 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100350 * expected here, this controls whether or not
351 * to set lengths, and in what order with
352 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100353 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100354 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100355 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100356 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100357 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100358 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100359static int aead_multipart_internal_func(int key_type_arg, data_t *key_data,
360 int alg_arg,
361 data_t *nonce,
362 data_t *additional_data,
363 int ad_part_len_arg,
364 data_t *input_data,
365 int data_part_len_arg,
366 set_lengths_method_t set_lengths_method,
367 data_t *expected_output,
368 int is_encrypt,
369 int do_zero_parts)
Paul Elliottd3f82412021-06-16 16:52:21 +0100370{
371 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
372 psa_key_type_t key_type = key_type_arg;
373 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100374 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100375 unsigned char *output_data = NULL;
376 unsigned char *part_data = NULL;
377 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100378 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100379 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100380 size_t output_size = 0;
381 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100382 size_t output_length = 0;
383 size_t key_bits = 0;
384 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100385 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100386 size_t part_length = 0;
387 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100388 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100389 size_t ad_part_len = 0;
390 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100391 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100392 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
393 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
394
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100395 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100396 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 PSA_ASSERT(psa_crypto_init());
Paul Elliottd3f82412021-06-16 16:52:21 +0100399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (is_encrypt) {
401 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
402 } else {
403 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100404 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100405
406 psa_set_key_algorithm(&attributes, alg);
407 psa_set_key_type(&attributes, key_type);
408
409 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
410 &key));
411
412 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
413 key_bits = psa_get_key_bits(&attributes);
414
415 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
416
417 if (is_encrypt) {
418 /* Tag gets written at end of buffer. */
419 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
420 (input_data->len +
421 tag_length));
422 data_true_size = input_data->len;
423 } else {
424 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
425 (input_data->len -
426 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100427
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100428 /* Do not want to attempt to decrypt tag. */
429 data_true_size = input_data->len - tag_length;
430 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100431
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100432 TEST_CALLOC(output_data, output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 if (is_encrypt) {
435 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
436 TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
437 } else {
438 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
439 TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100440 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100441
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100442 TEST_CALLOC(final_data, final_output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if (is_encrypt) {
445 status = psa_aead_encrypt_setup(&operation, key, alg);
446 } else {
447 status = psa_aead_decrypt_setup(&operation, key, alg);
448 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100449
450 /* If the operation is not supported, just skip and not fail in case the
451 * encryption involves a common limitation of cryptography hardwares and
452 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 if (status == PSA_ERROR_NOT_SUPPORTED) {
454 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
455 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100456 }
457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 PSA_ASSERT(status);
Paul Elliottd3f82412021-06-16 16:52:21 +0100459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if (set_lengths_method == DO_NOT_SET_LENGTHS) {
461 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
462 } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
463 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
464 data_true_size));
465 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
466 } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
467 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottebf91632021-07-22 17:54:42 +0100468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
470 data_true_size));
Paul Elliottd3f82412021-06-16 16:52:21 +0100471 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if (ad_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100474 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100475 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100478 part_offset < additional_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 part_offset += part_length, part_count++) {
480 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100481 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 } else if (additional_data->len - part_offset < ad_part_len) {
Paul Elliotte49fe452021-09-15 16:52:11 +0100483 part_length = additional_data->len - part_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100485 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100486 }
487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 PSA_ASSERT(psa_aead_update_ad(&operation,
489 additional_data->x + part_offset,
490 part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100491
Paul Elliottd3f82412021-06-16 16:52:21 +0100492 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 } else {
Paul Elliottd3f82412021-06-16 16:52:21 +0100494 /* Pass additional data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
496 additional_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100497 }
498
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 if (data_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100500 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 data_part_len = (size_t) data_part_len_arg;
502 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
503 (size_t) data_part_len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100504
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100505 TEST_CALLOC(part_data, part_data_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100508 part_offset < data_true_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 part_offset += part_length, part_count++) {
510 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100511 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 } else if ((data_true_size - part_offset) < data_part_len) {
513 part_length = (data_true_size - part_offset);
514 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100515 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100516 }
517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 PSA_ASSERT(psa_aead_update(&operation,
519 (input_data->x + part_offset),
520 part_length, part_data,
521 part_data_size,
522 &output_part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (output_data && output_part_length) {
525 memcpy((output_data + output_length), part_data,
526 output_part_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100527 }
528
Paul Elliottd3f82412021-06-16 16:52:21 +0100529 output_length += output_part_length;
530 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 } else {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100532 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
534 data_true_size, output_data,
535 output_size, &output_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100536 }
537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 if (is_encrypt) {
539 PSA_ASSERT(psa_aead_finish(&operation, final_data,
540 final_output_size,
541 &output_part_length,
542 tag_buffer, tag_length,
543 &tag_size));
544 } else {
545 PSA_ASSERT(psa_aead_verify(&operation, final_data,
546 final_output_size,
547 &output_part_length,
548 (input_data->x + data_true_size),
549 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100550 }
551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 if (output_data && output_part_length) {
553 memcpy((output_data + output_length), final_data,
554 output_part_length);
555 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100556
557 output_length += output_part_length;
558
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100559
560 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
561 * should be exact.*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 if (is_encrypt) {
563 TEST_EQUAL(tag_length, tag_size);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 if (output_data && tag_length) {
566 memcpy((output_data + output_length), tag_buffer,
567 tag_length);
568 }
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100569
570 output_length += tag_length;
571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 TEST_EQUAL(output_length,
573 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
574 input_data->len));
575 TEST_LE_U(output_length,
576 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
577 } else {
578 TEST_EQUAL(output_length,
579 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
580 input_data->len));
581 TEST_LE_U(output_length,
582 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100583 }
584
Paul Elliottd3f82412021-06-16 16:52:21 +0100585
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100586 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100587 output_data, output_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100588
Paul Elliottd3f82412021-06-16 16:52:21 +0100589
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100590 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100591
592exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 psa_destroy_key(key);
594 psa_aead_abort(&operation);
595 mbedtls_free(output_data);
596 mbedtls_free(part_data);
597 mbedtls_free(final_data);
598 PSA_DONE();
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 return test_ok;
Paul Elliottd3f82412021-06-16 16:52:21 +0100601}
602
Neil Armstrong4766f992022-02-28 16:23:59 +0100603/*!
604 * \brief Internal Function for MAC multipart tests.
605 * \param key_type_arg Type of key passed in
606 * \param key_data The encryption / decryption key data
607 * \param alg_arg The type of algorithm used
608 * \param input_data Data to encrypt / decrypt
609 * \param data_part_len_arg If not -1, the length of chunks to feed
610 * the data in to be encrypted / decrypted. If
611 * -1, no chunking
612 * \param expected_output Expected output
Tom Cosgrove1797b052022-12-04 17:19:59 +0000613 * \param is_verify If non-zero this is a verify operation.
Neil Armstrong4766f992022-02-28 16:23:59 +0100614 * \param do_zero_parts If non-zero, interleave zero length chunks
615 * with normal length chunks.
616 * \return int Zero on failure, non-zero on success.
617 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100618static int mac_multipart_internal_func(int key_type_arg, data_t *key_data,
619 int alg_arg,
620 data_t *input_data,
621 int data_part_len_arg,
622 data_t *expected_output,
623 int is_verify,
624 int do_zero_parts)
Neil Armstrong4766f992022-02-28 16:23:59 +0100625{
626 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
627 psa_key_type_t key_type = key_type_arg;
628 psa_algorithm_t alg = alg_arg;
629 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
630 unsigned char mac[PSA_MAC_MAX_SIZE];
631 size_t part_offset = 0;
632 size_t part_length = 0;
633 size_t data_part_len = 0;
634 size_t mac_len = 0;
635 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
636 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
637
638 int test_ok = 0;
639 size_t part_count = 0;
640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 PSA_INIT();
Neil Armstrong4766f992022-02-28 16:23:59 +0100642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 if (is_verify) {
644 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
645 } else {
646 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
647 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100648
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 psa_set_key_algorithm(&attributes, alg);
650 psa_set_key_type(&attributes, key_type);
Neil Armstrong4766f992022-02-28 16:23:59 +0100651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
653 &key));
Neil Armstrong4766f992022-02-28 16:23:59 +0100654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (is_verify) {
656 status = psa_mac_verify_setup(&operation, key, alg);
657 } else {
658 status = psa_mac_sign_setup(&operation, key, alg);
659 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100660
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 PSA_ASSERT(status);
Neil Armstrong4766f992022-02-28 16:23:59 +0100662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 if (data_part_len_arg != -1) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100664 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 data_part_len = (size_t) data_part_len_arg;
Neil Armstrong4766f992022-02-28 16:23:59 +0100666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 for (part_offset = 0, part_count = 0;
Neil Armstrong4766f992022-02-28 16:23:59 +0100668 part_offset < input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 part_offset += part_length, part_count++) {
670 if (do_zero_parts && (part_count & 0x01)) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100671 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 } else if ((input_data->len - part_offset) < data_part_len) {
673 part_length = (input_data->len - part_offset);
674 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100675 part_length = data_part_len;
676 }
677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 PSA_ASSERT(psa_mac_update(&operation,
679 (input_data->x + part_offset),
680 part_length));
Neil Armstrong4766f992022-02-28 16:23:59 +0100681 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100683 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 PSA_ASSERT(psa_mac_update(&operation, input_data->x,
685 input_data->len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100686 }
687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 if (is_verify) {
689 PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x,
690 expected_output->len));
691 } else {
692 PSA_ASSERT(psa_mac_sign_finish(&operation, mac,
693 PSA_MAC_MAX_SIZE, &mac_len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100694
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100695 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100696 mac, mac_len);
Neil Armstrong4766f992022-02-28 16:23:59 +0100697 }
698
699 test_ok = 1;
700
701exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 psa_destroy_key(key);
703 psa_mac_abort(&operation);
704 PSA_DONE();
Neil Armstrong4766f992022-02-28 16:23:59 +0100705
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 return test_ok;
Neil Armstrong4766f992022-02-28 16:23:59 +0100707}
708
Neil Armstrong75673ab2022-06-15 17:39:01 +0200709#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100710static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
711 psa_pake_operation_t *server,
712 psa_pake_operation_t *client,
713 int client_input_first,
714 int round, int inject_error)
Neil Armstrongf983caf2022-06-15 15:27:48 +0200715{
716 unsigned char *buffer0 = NULL, *buffer1 = NULL;
717 size_t buffer_length = (
718 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
719 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
720 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200721 /* The output should be exactly this size according to the spec */
722 const size_t expected_size_key_share =
723 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
724 /* The output should be exactly this size according to the spec */
725 const size_t expected_size_zk_public =
726 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
727 /* The output can be smaller: the spec allows stripping leading zeroes */
728 const size_t max_expected_size_zk_proof =
729 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200730 size_t buffer0_off = 0;
731 size_t buffer1_off = 0;
732 size_t s_g1_len, s_g2_len, s_a_len;
733 size_t s_g1_off, s_g2_off, s_a_off;
734 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
735 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
736 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
737 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
738 size_t c_g1_len, c_g2_len, c_a_len;
739 size_t c_g1_off, c_g2_off, c_a_off;
740 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
741 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
742 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
743 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
744 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200745 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200746
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100747 TEST_CALLOC(buffer0, buffer_length);
748 TEST_CALLOC(buffer1, buffer_length);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 switch (round) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200751 case 1:
752 /* Server first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
754 buffer0 + buffer0_off,
755 512 - buffer0_off, &s_g1_len));
756 TEST_EQUAL(s_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200757 s_g1_off = buffer0_off;
758 buffer0_off += s_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
760 buffer0 + buffer0_off,
761 512 - buffer0_off, &s_x1_pk_len));
762 TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200763 s_x1_pk_off = buffer0_off;
764 buffer0_off += s_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
766 buffer0 + buffer0_off,
767 512 - buffer0_off, &s_x1_pr_len));
768 TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200769 s_x1_pr_off = buffer0_off;
770 buffer0_off += s_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
772 buffer0 + buffer0_off,
773 512 - buffer0_off, &s_g2_len));
774 TEST_EQUAL(s_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200775 s_g2_off = buffer0_off;
776 buffer0_off += s_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
778 buffer0 + buffer0_off,
779 512 - buffer0_off, &s_x2_pk_len));
780 TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200781 s_x2_pk_off = buffer0_off;
782 buffer0_off += s_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
784 buffer0 + buffer0_off,
785 512 - buffer0_off, &s_x2_pr_len));
786 TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200787 s_x2_pr_off = buffer0_off;
788 buffer0_off += s_x2_pr_len;
789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if (inject_error == 1) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500791 buffer0[s_x1_pr_off + 8] ^= 1;
792 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200793 expected_status = PSA_ERROR_DATA_INVALID;
794 }
795
Neil Armstrong51009d72022-09-05 17:59:54 +0200796 /*
797 * When injecting errors in inputs, the implementation is
798 * free to detect it right away of with a delay.
799 * This permits delaying the error until the end of the input
800 * sequence, if no error appears then, this will be treated
801 * as an error.
802 */
803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200805 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
807 buffer0 + s_g1_off, s_g1_len);
808 if (inject_error == 1 && status != PSA_SUCCESS) {
809 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200810 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 } else {
812 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200813 }
814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
816 buffer0 + s_x1_pk_off,
817 s_x1_pk_len);
818 if (inject_error == 1 && status != PSA_SUCCESS) {
819 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200820 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 } else {
822 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200823 }
824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
826 buffer0 + s_x1_pr_off,
827 s_x1_pr_len);
828 if (inject_error == 1 && status != PSA_SUCCESS) {
829 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200830 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 } else {
832 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200833 }
834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
836 buffer0 + s_g2_off,
837 s_g2_len);
838 if (inject_error == 1 && status != PSA_SUCCESS) {
839 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200840 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 } else {
842 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200843 }
844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
846 buffer0 + s_x2_pk_off,
847 s_x2_pk_len);
848 if (inject_error == 1 && status != PSA_SUCCESS) {
849 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200850 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 } else {
852 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200853 }
854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
856 buffer0 + s_x2_pr_off,
857 s_x2_pr_len);
858 if (inject_error == 1 && status != PSA_SUCCESS) {
859 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200860 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 } else {
862 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200863 }
864
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200865 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (inject_error == 1) {
867 TEST_ASSERT(
868 !"One of the last psa_pake_input() calls should have returned the expected error.");
869 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200870 }
871
872 /* Client first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
874 buffer1 + buffer1_off,
875 512 - buffer1_off, &c_g1_len));
876 TEST_EQUAL(c_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200877 c_g1_off = buffer1_off;
878 buffer1_off += c_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
880 buffer1 + buffer1_off,
881 512 - buffer1_off, &c_x1_pk_len));
882 TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200883 c_x1_pk_off = buffer1_off;
884 buffer1_off += c_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
886 buffer1 + buffer1_off,
887 512 - buffer1_off, &c_x1_pr_len));
888 TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200889 c_x1_pr_off = buffer1_off;
890 buffer1_off += c_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
892 buffer1 + buffer1_off,
893 512 - buffer1_off, &c_g2_len));
894 TEST_EQUAL(c_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200895 c_g2_off = buffer1_off;
896 buffer1_off += c_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
898 buffer1 + buffer1_off,
899 512 - buffer1_off, &c_x2_pk_len));
900 TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200901 c_x2_pk_off = buffer1_off;
902 buffer1_off += c_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
904 buffer1 + buffer1_off,
905 512 - buffer1_off, &c_x2_pr_len));
906 TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200907 c_x2_pr_off = buffer1_off;
908 buffer1_off += c_x2_pr_len;
909
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200911 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
913 buffer0 + s_g1_off, s_g1_len);
914 if (inject_error == 1 && status != PSA_SUCCESS) {
915 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200916 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 } else {
918 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200919 }
920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
922 buffer0 + s_x1_pk_off,
923 s_x1_pk_len);
924 if (inject_error == 1 && status != PSA_SUCCESS) {
925 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200926 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 } else {
928 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200929 }
930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
932 buffer0 + s_x1_pr_off,
933 s_x1_pr_len);
934 if (inject_error == 1 && status != PSA_SUCCESS) {
935 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200936 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 } else {
938 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200939 }
940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
942 buffer0 + s_g2_off,
943 s_g2_len);
944 if (inject_error == 1 && status != PSA_SUCCESS) {
945 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200946 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 } else {
948 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200949 }
950
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
952 buffer0 + s_x2_pk_off,
953 s_x2_pk_len);
954 if (inject_error == 1 && status != PSA_SUCCESS) {
955 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200956 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 } else {
958 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200959 }
960
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
962 buffer0 + s_x2_pr_off,
963 s_x2_pr_len);
964 if (inject_error == 1 && status != PSA_SUCCESS) {
965 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200966 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 } else {
968 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200969 }
970
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200971 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 if (inject_error == 1) {
973 TEST_ASSERT(
974 !"One of the last psa_pake_input() calls should have returned the expected error.");
975 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200976 }
977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 if (inject_error == 2) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500979 buffer1[c_x1_pr_off + 12] ^= 1;
980 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200981 expected_status = PSA_ERROR_DATA_INVALID;
982 }
983
984 /* Server first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
986 buffer1 + c_g1_off, c_g1_len);
987 if (inject_error == 2 && status != PSA_SUCCESS) {
988 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200989 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 } else {
991 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200992 }
993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
995 buffer1 + c_x1_pk_off, c_x1_pk_len);
996 if (inject_error == 2 && status != PSA_SUCCESS) {
997 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200998 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 } else {
1000 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001001 }
1002
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1004 buffer1 + c_x1_pr_off, c_x1_pr_len);
1005 if (inject_error == 2 && status != PSA_SUCCESS) {
1006 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001007 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 } else {
1009 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001010 }
1011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1013 buffer1 + c_g2_off, c_g2_len);
1014 if (inject_error == 2 && status != PSA_SUCCESS) {
1015 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001016 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 } else {
1018 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001019 }
1020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1022 buffer1 + c_x2_pk_off, c_x2_pk_len);
1023 if (inject_error == 2 && status != PSA_SUCCESS) {
1024 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001025 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 } else {
1027 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001028 }
1029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1031 buffer1 + c_x2_pr_off, c_x2_pr_len);
1032 if (inject_error == 2 && status != PSA_SUCCESS) {
1033 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001034 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 } else {
1036 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001037 }
1038
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001039 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 if (inject_error == 2) {
1041 TEST_ASSERT(
1042 !"One of the last psa_pake_input() calls should have returned the expected error.");
1043 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001044
1045 break;
1046
1047 case 2:
1048 /* Server second round Output */
1049 buffer0_off = 0;
1050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
1052 buffer0 + buffer0_off,
1053 512 - buffer0_off, &s_a_len));
1054 TEST_EQUAL(s_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001055 s_a_off = buffer0_off;
1056 buffer0_off += s_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
1058 buffer0 + buffer0_off,
1059 512 - buffer0_off, &s_x2s_pk_len));
1060 TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001061 s_x2s_pk_off = buffer0_off;
1062 buffer0_off += s_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
1064 buffer0 + buffer0_off,
1065 512 - buffer0_off, &s_x2s_pr_len));
1066 TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001067 s_x2s_pr_off = buffer0_off;
1068 buffer0_off += s_x2s_pr_len;
1069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 if (inject_error == 3) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001071 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001072 expected_status = PSA_ERROR_DATA_INVALID;
1073 }
1074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001076 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1078 buffer0 + s_a_off, s_a_len);
1079 if (inject_error == 3 && status != PSA_SUCCESS) {
1080 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001081 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 } else {
1083 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001084 }
1085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1087 buffer0 + s_x2s_pk_off,
1088 s_x2s_pk_len);
1089 if (inject_error == 3 && status != PSA_SUCCESS) {
1090 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001091 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 } else {
1093 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001094 }
1095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1097 buffer0 + s_x2s_pr_off,
1098 s_x2s_pr_len);
1099 if (inject_error == 3 && status != PSA_SUCCESS) {
1100 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001101 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 } else {
1103 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001104 }
1105
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001106 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 if (inject_error == 3) {
1108 TEST_ASSERT(
1109 !"One of the last psa_pake_input() calls should have returned the expected error.");
1110 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001111 }
1112
1113 /* Client second round Output */
1114 buffer1_off = 0;
1115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
1117 buffer1 + buffer1_off,
1118 512 - buffer1_off, &c_a_len));
1119 TEST_EQUAL(c_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001120 c_a_off = buffer1_off;
1121 buffer1_off += c_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
1123 buffer1 + buffer1_off,
1124 512 - buffer1_off, &c_x2s_pk_len));
1125 TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001126 c_x2s_pk_off = buffer1_off;
1127 buffer1_off += c_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
1129 buffer1 + buffer1_off,
1130 512 - buffer1_off, &c_x2s_pr_len));
1131 TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001132 c_x2s_pr_off = buffer1_off;
1133 buffer1_off += c_x2s_pr_len;
1134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001136 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1138 buffer0 + s_a_off, s_a_len);
1139 if (inject_error == 3 && status != PSA_SUCCESS) {
1140 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001141 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 } else {
1143 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001144 }
1145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1147 buffer0 + s_x2s_pk_off,
1148 s_x2s_pk_len);
1149 if (inject_error == 3 && status != PSA_SUCCESS) {
1150 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001151 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 } else {
1153 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001154 }
1155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1157 buffer0 + s_x2s_pr_off,
1158 s_x2s_pr_len);
1159 if (inject_error == 3 && status != PSA_SUCCESS) {
1160 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001161 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 } else {
1163 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001164 }
1165
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001166 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 if (inject_error == 3) {
1168 TEST_ASSERT(
1169 !"One of the last psa_pake_input() calls should have returned the expected error.");
1170 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001171 }
1172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if (inject_error == 4) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001174 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001175 expected_status = PSA_ERROR_DATA_INVALID;
1176 }
1177
1178 /* Server second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1180 buffer1 + c_a_off, c_a_len);
1181 if (inject_error == 4 && status != PSA_SUCCESS) {
1182 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001183 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 } else {
1185 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001186 }
1187
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1189 buffer1 + c_x2s_pk_off, c_x2s_pk_len);
1190 if (inject_error == 4 && status != PSA_SUCCESS) {
1191 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001192 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 } else {
1194 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001195 }
1196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1198 buffer1 + c_x2s_pr_off, c_x2s_pr_len);
1199 if (inject_error == 4 && status != PSA_SUCCESS) {
1200 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001201 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 } else {
1203 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001204 }
1205
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001206 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if (inject_error == 4) {
1208 TEST_ASSERT(
1209 !"One of the last psa_pake_input() calls should have returned the expected error.");
1210 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001211
1212 break;
1213
1214 }
1215
Neil Armstrongf983caf2022-06-15 15:27:48 +02001216exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 mbedtls_free(buffer0);
1218 mbedtls_free(buffer1);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001219}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001220#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001221
Gilles Peskine449bd832023-01-11 14:50:10 +01001222typedef enum {
Valerio Setti1070aed2022-11-11 19:37:31 +01001223 INJECT_ERR_NONE = 0,
1224 INJECT_ERR_UNINITIALIZED_ACCESS,
1225 INJECT_ERR_DUPLICATE_SETUP,
1226 INJECT_ERR_INVALID_USER,
1227 INJECT_ERR_INVALID_PEER,
1228 INJECT_ERR_SET_USER,
1229 INJECT_ERR_SET_PEER,
1230 INJECT_EMPTY_IO_BUFFER,
1231 INJECT_UNKNOWN_STEP,
1232 INJECT_INVALID_FIRST_STEP,
1233 INJECT_WRONG_BUFFER_SIZE,
1234 INJECT_VALID_OPERATION_AFTER_FAILURE,
1235 INJECT_ANTICIPATE_KEY_DERIVATION_1,
1236 INJECT_ANTICIPATE_KEY_DERIVATION_2,
1237} ecjpake_injected_failure_t;
1238
Paul Elliott01885fa2023-02-09 12:07:30 +00001239#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott1243f932023-02-07 11:21:10 +00001240
Paul Elliott6f600372023-02-06 18:41:05 +00001241static void interruptible_signverify_get_minmax_completes(uint32_t max_ops,
1242 psa_status_t expected_status,
1243 size_t *min_completes,
1244 size_t *max_completes)
1245{
1246
1247 /* This is slightly contrived, but we only really know that with a minimum
1248 value of max_ops that a successful operation should take more than one op
1249 to complete, and likewise that with a max_ops of
1250 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */
1251 if (max_ops == 0 || max_ops == 1) {
Paul Elliottc86d45e2023-02-15 17:38:05 +00001252
Paul Elliott6f600372023-02-06 18:41:05 +00001253 if (expected_status == PSA_SUCCESS) {
1254 *min_completes = 2;
1255 } else {
1256 *min_completes = 1;
1257 }
1258
1259 *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
1260 } else {
1261 *min_completes = 1;
1262 *max_completes = 1;
1263 }
1264}
Paul Elliott01885fa2023-02-09 12:07:30 +00001265#endif /* MBEDTLS_ECP_RESTARTABLE */
Paul Elliott6f600372023-02-06 18:41:05 +00001266
Gilles Peskinee59236f2018-01-27 23:32:46 +01001267/* END_HEADER */
1268
1269/* BEGIN_DEPENDENCIES
1270 * depends_on:MBEDTLS_PSA_CRYPTO_C
1271 * END_DEPENDENCIES
1272 */
1273
1274/* BEGIN_CASE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01001275void psa_can_do_hash()
1276{
1277 /* We can't test that this is specific to drivers until partial init has
1278 * been implemented, but we can at least test before/after full init. */
1279 TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE));
1280 PSA_INIT();
1281 TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE));
1282 PSA_DONE();
1283}
1284/* END_CASE */
1285
1286/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001287void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001288{
1289 size_t max_truncated_mac_size =
1290 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1291
1292 /* Check that the length for a truncated MAC always fits in the algorithm
1293 * encoding. The shifted mask is the maximum truncated value. The
1294 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001296}
1297/* END_CASE */
1298
1299/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001300void import_with_policy(int type_arg,
1301 int usage_arg, int alg_arg,
1302 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001303{
1304 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1305 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001306 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001307 psa_key_type_t type = type_arg;
1308 psa_key_usage_t usage = usage_arg;
1309 psa_algorithm_t alg = alg_arg;
1310 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001312 psa_status_t status;
1313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001315
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 psa_set_key_type(&attributes, type);
1317 psa_set_key_usage_flags(&attributes, usage);
1318 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001319
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 status = psa_import_key(&attributes,
1321 key_material, sizeof(key_material),
1322 &key);
1323 TEST_EQUAL(status, expected_status);
1324 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001325 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1329 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1330 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1331 mbedtls_test_update_key_usage_flags(usage));
1332 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1333 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 PSA_ASSERT(psa_destroy_key(key));
1336 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001337
1338exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001339 /*
1340 * Key attributes may have been returned by psa_get_key_attributes()
1341 * thus reset them as required.
1342 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 psa_destroy_key(key);
1346 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001347}
1348/* END_CASE */
1349
1350/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001351void import_with_data(data_t *data, int type_arg,
1352 int attr_bits_arg,
1353 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001354{
1355 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1356 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001357 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001358 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001359 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001360 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001361 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001364
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 psa_set_key_type(&attributes, type);
1366 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 status = psa_import_key(&attributes, data->x, data->len, &key);
Manuel Pégourié-Gonnard0509b582023-08-08 12:47:56 +02001369 /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED.
1370 *
1371 * This can happen with a type supported only by a driver:
1372 * - the driver sees the invalid data (for example wrong size) and thinks
1373 * "well perhaps this is a key size I don't support" so it returns
1374 * NOT_SUPPORTED which is correct at this point;
1375 * - we fallback to built-ins, which don't support this type, so return
1376 * NOT_SUPPORTED which again is correct at this point.
1377 */
1378 if (expected_status == PSA_ERROR_INVALID_ARGUMENT &&
1379 status == PSA_ERROR_NOT_SUPPORTED) {
1380 ; // OK
1381 } else {
1382 TEST_EQUAL(status, expected_status);
1383 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001385 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1389 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1390 if (attr_bits != 0) {
1391 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1392 }
1393 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001394
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 PSA_ASSERT(psa_destroy_key(key));
1396 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001397
1398exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001399 /*
1400 * Key attributes may have been returned by psa_get_key_attributes()
1401 * thus reset them as required.
1402 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 psa_destroy_key(key);
1406 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001407}
1408/* END_CASE */
1409
1410/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001411/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001412void import_large_key(int type_arg, int byte_size_arg,
1413 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001414{
1415 psa_key_type_t type = type_arg;
1416 size_t byte_size = byte_size_arg;
1417 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1418 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001419 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001420 psa_status_t status;
1421 uint8_t *buffer = NULL;
1422 size_t buffer_size = byte_size + 1;
1423 size_t n;
1424
Steven Cooreman69967ce2021-01-18 18:01:08 +01001425 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001426 * accommodate large keys due to heap size constraints */
Tom Cosgrove412a8132023-07-20 16:55:14 +01001427 TEST_CALLOC_OR_SKIP(buffer, buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001431
1432 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1434 psa_set_key_type(&attributes, type);
1435 status = psa_import_key(&attributes, buffer, byte_size, &key);
1436 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1437 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001438
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 if (status == PSA_SUCCESS) {
1440 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1441 TEST_EQUAL(psa_get_key_type(&attributes), type);
1442 TEST_EQUAL(psa_get_key_bits(&attributes),
1443 PSA_BYTES_TO_BITS(byte_size));
1444 ASSERT_NO_SLOT_NUMBER(&attributes);
1445 memset(buffer, 0, byte_size + 1);
1446 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1447 for (n = 0; n < byte_size; n++) {
1448 TEST_EQUAL(buffer[n], 'K');
1449 }
1450 for (n = byte_size; n < buffer_size; n++) {
1451 TEST_EQUAL(buffer[n], 0);
1452 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001453 }
1454
1455exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001456 /*
1457 * Key attributes may have been returned by psa_get_key_attributes()
1458 * thus reset them as required.
1459 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 psa_destroy_key(key);
1463 PSA_DONE();
1464 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001465}
1466/* END_CASE */
1467
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001468/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001469/* Import an RSA key with a valid structure (but not valid numbers
1470 * inside, beyond having sensible size and parity). This is expected to
1471 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001472void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001473{
Ronald Cron5425a212020-08-04 14:58:35 +02001474 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001475 size_t bits = bits_arg;
1476 psa_status_t expected_status = expected_status_arg;
1477 psa_status_t status;
1478 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001479 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001480 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001482 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001483 unsigned char *p;
1484 int ret;
1485 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001486 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 PSA_ASSERT(psa_crypto_init());
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001489 TEST_CALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1492 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001493 length = ret;
1494
1495 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 psa_set_key_type(&attributes, type);
1497 status = psa_import_key(&attributes, p, length, &key);
1498 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 if (status == PSA_SUCCESS) {
1501 PSA_ASSERT(psa_destroy_key(key));
1502 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001503
1504exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 mbedtls_free(buffer);
1506 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001507}
1508/* END_CASE */
1509
1510/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001511void import_export(data_t *data,
1512 int type_arg,
1513 int usage_arg, int alg_arg,
1514 int lifetime_arg,
1515 int expected_bits,
1516 int export_size_delta,
1517 int expected_export_status_arg,
1518 /*whether reexport must give the original input exactly*/
1519 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001520{
Ronald Cron5425a212020-08-04 14:58:35 +02001521 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001522 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001523 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001524 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001525 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301526 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001527 unsigned char *exported = NULL;
1528 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001529 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001530 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001531 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001532 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001533 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001534
Moran Pekercb088e72018-07-17 17:36:59 +03001535 export_size = (ptrdiff_t) data->len + export_size_delta;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001536 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 if (!canonical_input) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001538 TEST_CALLOC(reexported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 }
1540 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001541
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 psa_set_key_lifetime(&attributes, lifetime);
1543 psa_set_key_usage_flags(&attributes, usage_arg);
1544 psa_set_key_algorithm(&attributes, alg);
1545 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001546
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001547 if (PSA_KEY_TYPE_IS_DH(type) &&
1548 expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) {
Przemek Stekiel654bef02022-12-15 13:28:02 +01001549 /* Simulate that buffer is too small, by decreasing its size by 1 byte. */
1550 export_size -= 1;
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001551 }
1552
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001553 /* Import the key */
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001554 TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key),
Przemek Stekiel2e7c33d2023-04-27 12:29:45 +02001555 PSA_SUCCESS);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001556
1557 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1559 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1560 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1561 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001562
1563 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 status = psa_export_key(key, exported, export_size, &exported_length);
1565 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001566
1567 /* The exported length must be set by psa_export_key() to a value between 0
1568 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1570 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1571 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001572
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1574 export_size - exported_length));
1575 if (status != PSA_SUCCESS) {
1576 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001577 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001578 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001579
Gilles Peskineea38a922021-02-13 00:05:16 +01001580 /* Run sanity checks on the exported key. For non-canonical inputs,
1581 * this validates the canonical representations. For canonical inputs,
1582 * this doesn't directly validate the implementation, but it still helps
1583 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 if (!psa_key_lifetime_is_external(lifetime)) {
1585 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301586 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 }
Archana4d7ae1d2021-07-07 02:50:22 +05301588 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001589
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 if (canonical_input) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001591 TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001593 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1595 &key2));
1596 PSA_ASSERT(psa_export_key(key2,
1597 reexported,
1598 export_size,
1599 &reexported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001600 TEST_MEMORY_COMPARE(exported, exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001601 reexported, reexported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001603 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 TEST_LE_U(exported_length,
1605 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1606 psa_get_key_bits(&got_attributes)));
Valerio Setti1eacae82023-07-28 16:07:03 +02001607 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
1608 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
1609 } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
1610 TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1611 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001612
1613destroy:
1614 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 PSA_ASSERT(psa_destroy_key(key));
1616 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001617
1618exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001619 /*
1620 * Key attributes may have been returned by psa_get_key_attributes()
1621 * thus reset them as required.
1622 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 psa_reset_key_attributes(&got_attributes);
1624 psa_destroy_key(key);
1625 mbedtls_free(exported);
1626 mbedtls_free(reexported);
1627 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001628}
1629/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001630
Moran Pekerf709f4a2018-06-06 17:26:04 +03001631/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001632void import_export_public_key(data_t *data,
1633 int type_arg, // key pair or public key
1634 int alg_arg,
1635 int lifetime_arg,
1636 int export_size_delta,
1637 int expected_export_status_arg,
1638 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001639{
Ronald Cron5425a212020-08-04 14:58:35 +02001640 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001641 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001642 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001643 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001644 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301645 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001646 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001647 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001648 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001649 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001650
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 psa_set_key_lifetime(&attributes, lifetime);
1654 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1655 psa_set_key_algorithm(&attributes, alg);
1656 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001657
1658 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001660
Gilles Peskine49c25912018-10-29 15:15:31 +01001661 /* Export the public key */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001662 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 status = psa_export_public_key(key,
1664 exported, export_size,
1665 &exported_length);
1666 TEST_EQUAL(status, expected_export_status);
1667 if (status == PSA_SUCCESS) {
1668 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001669 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1671 bits = psa_get_key_bits(&attributes);
1672 TEST_LE_U(expected_public_key->len,
1673 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1674 TEST_LE_U(expected_public_key->len,
1675 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1676 TEST_LE_U(expected_public_key->len,
1677 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001678 TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001679 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001680 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001681exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001682 /*
1683 * Key attributes may have been returned by psa_get_key_attributes()
1684 * thus reset them as required.
1685 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 mbedtls_free(exported);
1689 psa_destroy_key(key);
1690 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001691}
1692/* END_CASE */
1693
Gilles Peskine20035e32018-02-03 22:44:14 +01001694/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001695void import_and_exercise_key(data_t *data,
1696 int type_arg,
1697 int bits_arg,
1698 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001699{
Ronald Cron5425a212020-08-04 14:58:35 +02001700 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001701 psa_key_type_t type = type_arg;
1702 size_t bits = bits_arg;
1703 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001705 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001706 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001707
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001709
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 psa_set_key_usage_flags(&attributes, usage);
1711 psa_set_key_algorithm(&attributes, alg);
1712 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001713
1714 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001716
1717 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1719 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1720 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001721
1722 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001724 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001726
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 PSA_ASSERT(psa_destroy_key(key));
1728 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001729
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001730exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001731 /*
1732 * Key attributes may have been returned by psa_get_key_attributes()
1733 * thus reset them as required.
1734 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001736
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 psa_reset_key_attributes(&attributes);
1738 psa_destroy_key(key);
1739 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001740}
1741/* END_CASE */
1742
1743/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001744void effective_key_attributes(int type_arg, int expected_type_arg,
1745 int bits_arg, int expected_bits_arg,
1746 int usage_arg, int expected_usage_arg,
1747 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001748{
Ronald Cron5425a212020-08-04 14:58:35 +02001749 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001750 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001751 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001752 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001753 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001754 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001755 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001756 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001757 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001758 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001759
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001761
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 psa_set_key_usage_flags(&attributes, usage);
1763 psa_set_key_algorithm(&attributes, alg);
1764 psa_set_key_type(&attributes, key_type);
1765 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001766
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 PSA_ASSERT(psa_generate_key(&attributes, &key));
1768 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001769
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1771 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1772 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1773 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1774 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001775
1776exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001777 /*
1778 * Key attributes may have been returned by psa_get_key_attributes()
1779 * thus reset them as required.
1780 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001782
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 psa_destroy_key(key);
1784 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001785}
1786/* END_CASE */
1787
1788/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001789void check_key_policy(int type_arg, int bits_arg,
1790 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001791{
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1793 usage_arg,
1794 mbedtls_test_update_key_usage_flags(usage_arg),
1795 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001796 goto exit;
1797}
1798/* END_CASE */
1799
1800/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001801void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001802{
1803 /* Test each valid way of initializing the object, except for `= {0}`, as
1804 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1805 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001806 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001808 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1809 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001810
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001812
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1814 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1815 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001816
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 TEST_EQUAL(psa_get_key_type(&func), 0);
1818 TEST_EQUAL(psa_get_key_type(&init), 0);
1819 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001820
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 TEST_EQUAL(psa_get_key_bits(&func), 0);
1822 TEST_EQUAL(psa_get_key_bits(&init), 0);
1823 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001824
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1826 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1827 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001828
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1830 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1831 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001832}
1833/* END_CASE */
1834
1835/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001836void mac_key_policy(int policy_usage_arg,
1837 int policy_alg_arg,
1838 int key_type_arg,
1839 data_t *key_data,
1840 int exercise_alg_arg,
1841 int expected_status_sign_arg,
1842 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001843{
Ronald Cron5425a212020-08-04 14:58:35 +02001844 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001845 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001846 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001847 psa_key_type_t key_type = key_type_arg;
1848 psa_algorithm_t policy_alg = policy_alg_arg;
1849 psa_algorithm_t exercise_alg = exercise_alg_arg;
1850 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001851 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001852 psa_status_t expected_status_sign = expected_status_sign_arg;
1853 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001854 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001855
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001857
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 psa_set_key_usage_flags(&attributes, policy_usage);
1859 psa_set_key_algorithm(&attributes, policy_alg);
1860 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001861
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1863 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001864
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1866 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001867
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1869 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001870
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001871 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001873 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1875 input, 128,
1876 mac, PSA_MAC_MAX_SIZE, &mac_len),
1877 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001878
Neil Armstrong3af9b972022-02-07 12:20:21 +01001879 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 PSA_ASSERT(psa_mac_abort(&operation));
1881 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1882 if (status == PSA_SUCCESS) {
1883 status = psa_mac_update(&operation, input, 128);
1884 if (status == PSA_SUCCESS) {
1885 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1886 &mac_len),
1887 expected_status_sign);
1888 } else {
1889 TEST_EQUAL(status, expected_status_sign);
1890 }
1891 } else {
1892 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001893 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001895
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001896 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 status = psa_mac_verify(key, exercise_alg, input, 128,
1898 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001899
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1901 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1902 } else {
1903 TEST_EQUAL(status, expected_status_verify);
1904 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001905
Neil Armstrong3af9b972022-02-07 12:20:21 +01001906 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1908 if (status == PSA_SUCCESS) {
1909 status = psa_mac_update(&operation, input, 128);
1910 if (status == PSA_SUCCESS) {
1911 status = psa_mac_verify_finish(&operation, mac, mac_len);
1912 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1913 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1914 } else {
1915 TEST_EQUAL(status, expected_status_verify);
1916 }
1917 } else {
1918 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001919 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 } else {
1921 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001922 }
1923
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 memset(mac, 0, sizeof(mac));
1927 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1928 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001929
1930exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 psa_mac_abort(&operation);
1932 psa_destroy_key(key);
1933 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001934}
1935/* END_CASE */
1936
1937/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001938void cipher_key_policy(int policy_usage_arg,
1939 int policy_alg,
1940 int key_type,
1941 data_t *key_data,
1942 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001943{
Ronald Cron5425a212020-08-04 14:58:35 +02001944 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001945 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001946 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001947 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001948 size_t output_buffer_size = 0;
1949 size_t input_buffer_size = 0;
1950 size_t output_length = 0;
1951 uint8_t *output = NULL;
1952 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001953 psa_status_t status;
1954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1956 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1957 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001958
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001959 TEST_CALLOC(input, input_buffer_size);
1960 TEST_CALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001961
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001963
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 psa_set_key_usage_flags(&attributes, policy_usage);
1965 psa_set_key_algorithm(&attributes, policy_alg);
1966 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001967
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1969 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001970
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001971 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 TEST_EQUAL(policy_usage,
1973 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001974
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001975 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1977 output, output_buffer_size,
1978 &output_length);
1979 if (policy_alg == exercise_alg &&
1980 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1981 PSA_ASSERT(status);
1982 } else {
1983 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1984 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001985
1986 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1988 if (policy_alg == exercise_alg &&
1989 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1990 PSA_ASSERT(status);
1991 } else {
1992 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1993 }
1994 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001995
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001996 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1998 input, input_buffer_size,
1999 &output_length);
2000 if (policy_alg == exercise_alg &&
2001 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2002 PSA_ASSERT(status);
2003 } else {
2004 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2005 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002006
2007 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
2009 if (policy_alg == exercise_alg &&
2010 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2011 PSA_ASSERT(status);
2012 } else {
2013 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2014 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002015
2016exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 psa_cipher_abort(&operation);
2018 mbedtls_free(input);
2019 mbedtls_free(output);
2020 psa_destroy_key(key);
2021 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002022}
2023/* END_CASE */
2024
2025/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002026void aead_key_policy(int policy_usage_arg,
2027 int policy_alg,
2028 int key_type,
2029 data_t *key_data,
2030 int nonce_length_arg,
2031 int tag_length_arg,
2032 int exercise_alg,
2033 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002034{
Ronald Cron5425a212020-08-04 14:58:35 +02002035 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002036 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002037 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002038 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002039 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002040 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002042 size_t nonce_length = nonce_length_arg;
2043 unsigned char tag[16];
2044 size_t tag_length = tag_length_arg;
2045 size_t output_length;
2046
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 TEST_LE_U(nonce_length, sizeof(nonce));
2048 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002049
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002051
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 psa_set_key_usage_flags(&attributes, policy_usage);
2053 psa_set_key_algorithm(&attributes, policy_alg);
2054 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002055
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2057 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002058
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002059 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 TEST_EQUAL(policy_usage,
2061 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002062
Neil Armstrong752d8112022-02-07 14:51:11 +01002063 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 status = psa_aead_encrypt(key, exercise_alg,
2065 nonce, nonce_length,
2066 NULL, 0,
2067 NULL, 0,
2068 tag, tag_length,
2069 &output_length);
2070 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2071 TEST_EQUAL(status, expected_status);
2072 } else {
2073 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2074 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002075
Neil Armstrong752d8112022-02-07 14:51:11 +01002076 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2078 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2079 TEST_EQUAL(status, expected_status);
2080 } else {
2081 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2082 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002083
2084 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 memset(tag, 0, sizeof(tag));
2086 status = psa_aead_decrypt(key, exercise_alg,
2087 nonce, nonce_length,
2088 NULL, 0,
2089 tag, tag_length,
2090 NULL, 0,
2091 &output_length);
2092 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2093 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2094 } else if (expected_status == PSA_SUCCESS) {
2095 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2096 } else {
2097 TEST_EQUAL(status, expected_status);
2098 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002099
Neil Armstrong752d8112022-02-07 14:51:11 +01002100 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 PSA_ASSERT(psa_aead_abort(&operation));
2102 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2103 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2104 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2105 } else {
2106 TEST_EQUAL(status, expected_status);
2107 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002108
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002109exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 PSA_ASSERT(psa_aead_abort(&operation));
2111 psa_destroy_key(key);
2112 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002113}
2114/* END_CASE */
2115
2116/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002117void asymmetric_encryption_key_policy(int policy_usage_arg,
2118 int policy_alg,
2119 int key_type,
2120 data_t *key_data,
2121 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002122{
Ronald Cron5425a212020-08-04 14:58:35 +02002123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002124 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002125 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002126 psa_status_t status;
2127 size_t key_bits;
2128 size_t buffer_length;
2129 unsigned char *buffer = NULL;
2130 size_t output_length;
2131
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002133
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 psa_set_key_usage_flags(&attributes, policy_usage);
2135 psa_set_key_algorithm(&attributes, policy_alg);
2136 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2139 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002140
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002141 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 TEST_EQUAL(policy_usage,
2143 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002144
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2146 key_bits = psa_get_key_bits(&attributes);
2147 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2148 exercise_alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002149 TEST_CALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002150
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 status = psa_asymmetric_encrypt(key, exercise_alg,
2152 NULL, 0,
2153 NULL, 0,
2154 buffer, buffer_length,
2155 &output_length);
2156 if (policy_alg == exercise_alg &&
2157 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2158 PSA_ASSERT(status);
2159 } else {
2160 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2161 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 if (buffer_length != 0) {
2164 memset(buffer, 0, buffer_length);
2165 }
2166 status = psa_asymmetric_decrypt(key, exercise_alg,
2167 buffer, buffer_length,
2168 NULL, 0,
2169 buffer, buffer_length,
2170 &output_length);
2171 if (policy_alg == exercise_alg &&
2172 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2173 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2174 } else {
2175 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2176 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002177
2178exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002179 /*
2180 * Key attributes may have been returned by psa_get_key_attributes()
2181 * thus reset them as required.
2182 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002184
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 psa_destroy_key(key);
2186 PSA_DONE();
2187 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002188}
2189/* END_CASE */
2190
2191/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002192void asymmetric_signature_key_policy(int policy_usage_arg,
2193 int policy_alg,
2194 int key_type,
2195 data_t *key_data,
2196 int exercise_alg,
2197 int payload_length_arg,
2198 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002199{
Ronald Cron5425a212020-08-04 14:58:35 +02002200 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002201 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002202 psa_key_usage_t policy_usage = policy_usage_arg;
2203 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002204 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002206 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2207 * compatible with the policy and `payload_length_arg` is supposed to be
2208 * a valid input length to sign. If `payload_length_arg <= 0`,
2209 * `exercise_alg` is supposed to be forbidden by the policy. */
2210 int compatible_alg = payload_length_arg > 0;
2211 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002213 size_t signature_length;
2214
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002215 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002216 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 TEST_EQUAL(expected_usage,
2218 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002221
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 psa_set_key_usage_flags(&attributes, policy_usage);
2223 psa_set_key_algorithm(&attributes, policy_alg);
2224 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002225
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2227 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002228
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002230
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 status = psa_sign_hash(key, exercise_alg,
2232 payload, payload_length,
2233 signature, sizeof(signature),
2234 &signature_length);
2235 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2236 PSA_ASSERT(status);
2237 } else {
2238 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2239 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 memset(signature, 0, sizeof(signature));
2242 status = psa_verify_hash(key, exercise_alg,
2243 payload, payload_length,
2244 signature, sizeof(signature));
2245 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2246 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2247 } else {
2248 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2249 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2252 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2253 status = psa_sign_message(key, exercise_alg,
2254 payload, payload_length,
2255 signature, sizeof(signature),
2256 &signature_length);
2257 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2258 PSA_ASSERT(status);
2259 } else {
2260 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2261 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 memset(signature, 0, sizeof(signature));
2264 status = psa_verify_message(key, exercise_alg,
2265 payload, payload_length,
2266 signature, sizeof(signature));
2267 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2268 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2269 } else {
2270 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2271 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002272 }
2273
Gilles Peskined5b33222018-06-18 22:20:03 +02002274exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 psa_destroy_key(key);
2276 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002277}
2278/* END_CASE */
2279
Janos Follathba3fab92019-06-11 14:50:16 +01002280/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002281void derive_key_policy(int policy_usage,
2282 int policy_alg,
2283 int key_type,
2284 data_t *key_data,
2285 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002286{
Ronald Cron5425a212020-08-04 14:58:35 +02002287 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002288 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002289 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002290 psa_status_t status;
2291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 psa_set_key_usage_flags(&attributes, policy_usage);
2295 psa_set_key_algorithm(&attributes, policy_alg);
2296 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2299 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2304 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2305 PSA_ASSERT(psa_key_derivation_input_bytes(
2306 &operation,
2307 PSA_KEY_DERIVATION_INPUT_SEED,
2308 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002309 }
Janos Follathba3fab92019-06-11 14:50:16 +01002310
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 status = psa_key_derivation_input_key(&operation,
2312 PSA_KEY_DERIVATION_INPUT_SECRET,
2313 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 if (policy_alg == exercise_alg &&
2316 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2317 PSA_ASSERT(status);
2318 } else {
2319 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2320 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002321
2322exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 psa_key_derivation_abort(&operation);
2324 psa_destroy_key(key);
2325 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002326}
2327/* END_CASE */
2328
2329/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002330void agreement_key_policy(int policy_usage,
2331 int policy_alg,
2332 int key_type_arg,
2333 data_t *key_data,
2334 int exercise_alg,
2335 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002336{
Ronald Cron5425a212020-08-04 14:58:35 +02002337 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002338 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002339 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002340 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002341 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002342 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 psa_set_key_usage_flags(&attributes, policy_usage);
2347 psa_set_key_algorithm(&attributes, policy_alg);
2348 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002349
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2351 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002352
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2354 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002355
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002357
2358exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 psa_key_derivation_abort(&operation);
2360 psa_destroy_key(key);
2361 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002362}
2363/* END_CASE */
2364
2365/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002366void key_policy_alg2(int key_type_arg, data_t *key_data,
2367 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002368{
Ronald Cron5425a212020-08-04 14:58:35 +02002369 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002370 psa_key_type_t key_type = key_type_arg;
2371 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2372 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2373 psa_key_usage_t usage = usage_arg;
2374 psa_algorithm_t alg = alg_arg;
2375 psa_algorithm_t alg2 = alg2_arg;
2376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002378
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 psa_set_key_usage_flags(&attributes, usage);
2380 psa_set_key_algorithm(&attributes, alg);
2381 psa_set_key_enrollment_algorithm(&attributes, alg2);
2382 psa_set_key_type(&attributes, key_type);
2383 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2384 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002385
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002386 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 usage = mbedtls_test_update_key_usage_flags(usage);
2388 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2389 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2390 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2391 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002392
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002394 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 }
2396 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002397 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002399
2400exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002401 /*
2402 * Key attributes may have been returned by psa_get_key_attributes()
2403 * thus reset them as required.
2404 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002406
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 psa_destroy_key(key);
2408 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002409}
2410/* END_CASE */
2411
2412/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002413void raw_agreement_key_policy(int policy_usage,
2414 int policy_alg,
2415 int key_type_arg,
2416 data_t *key_data,
2417 int exercise_alg,
2418 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002419{
Ronald Cron5425a212020-08-04 14:58:35 +02002420 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002421 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002422 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002423 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002424 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002425 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002426
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 psa_set_key_usage_flags(&attributes, policy_usage);
2430 psa_set_key_algorithm(&attributes, policy_alg);
2431 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002432
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2434 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002435
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002437
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002439
2440exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 psa_key_derivation_abort(&operation);
2442 psa_destroy_key(key);
2443 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002444}
2445/* END_CASE */
2446
2447/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002448void copy_success(int source_usage_arg,
2449 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002450 int source_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 int type_arg, data_t *material,
2452 int copy_attributes,
2453 int target_usage_arg,
2454 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002455 int target_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 int expected_usage_arg,
2457 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002458{
Gilles Peskineca25db92019-04-19 11:43:08 +02002459 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2460 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002461 psa_key_usage_t expected_usage = expected_usage_arg;
2462 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002463 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302464 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2465 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002466 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2467 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002468 uint8_t *export_buffer = NULL;
2469
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002471
Gilles Peskineca25db92019-04-19 11:43:08 +02002472 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2474 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2475 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2476 psa_set_key_type(&source_attributes, type_arg);
2477 psa_set_key_lifetime(&source_attributes, source_lifetime);
2478 PSA_ASSERT(psa_import_key(&source_attributes,
2479 material->x, material->len,
2480 &source_key));
2481 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002482
Gilles Peskineca25db92019-04-19 11:43:08 +02002483 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002485 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002486 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002488
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 if (target_usage_arg != -1) {
2490 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2491 }
2492 if (target_alg_arg != -1) {
2493 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2494 }
2495 if (target_alg2_arg != -1) {
2496 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2497 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002498
Archana8a180362021-07-05 02:18:48 +05302499
Gilles Peskine57ab7212019-01-28 13:03:09 +01002500 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 PSA_ASSERT(psa_copy_key(source_key,
2502 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002503
2504 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002506
2507 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2509 TEST_EQUAL(psa_get_key_type(&source_attributes),
2510 psa_get_key_type(&target_attributes));
2511 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2512 psa_get_key_bits(&target_attributes));
2513 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2514 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2515 TEST_EQUAL(expected_alg2,
2516 psa_get_key_enrollment_algorithm(&target_attributes));
2517 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002518 size_t length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002519 TEST_CALLOC(export_buffer, material->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2521 material->len, &length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002522 TEST_MEMORY_COMPARE(material->x, material->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002523 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002524 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002525
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 if (!psa_key_lifetime_is_external(target_lifetime)) {
2527 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302528 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 }
2530 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302531 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 }
Archana8a180362021-07-05 02:18:48 +05302533 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002534
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002536
2537exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002538 /*
2539 * Source and target key attributes may have been returned by
2540 * psa_get_key_attributes() thus reset them as required.
2541 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 psa_reset_key_attributes(&source_attributes);
2543 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 PSA_DONE();
2546 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002547}
2548/* END_CASE */
2549
2550/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002551void copy_fail(int source_usage_arg,
2552 int source_alg_arg, int source_alg2_arg,
2553 int source_lifetime_arg,
2554 int type_arg, data_t *material,
2555 int target_type_arg, int target_bits_arg,
2556 int target_usage_arg,
2557 int target_alg_arg, int target_alg2_arg,
2558 int target_id_arg, int target_lifetime_arg,
2559 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002560{
2561 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2562 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002563 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2564 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002566
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002568
2569 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2571 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2572 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2573 psa_set_key_type(&source_attributes, type_arg);
2574 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2575 PSA_ASSERT(psa_import_key(&source_attributes,
2576 material->x, material->len,
2577 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002578
2579 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 psa_set_key_id(&target_attributes, key_id);
2581 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2582 psa_set_key_type(&target_attributes, target_type_arg);
2583 psa_set_key_bits(&target_attributes, target_bits_arg);
2584 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2585 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2586 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002587
2588 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 TEST_EQUAL(psa_copy_key(source_key,
2590 &target_attributes, &target_key),
2591 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002594
Gilles Peskine4a644642019-05-03 17:14:08 +02002595exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 psa_reset_key_attributes(&source_attributes);
2597 psa_reset_key_attributes(&target_attributes);
2598 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002599}
2600/* END_CASE */
2601
2602/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002603void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002604{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002605 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002606 /* Test each valid way of initializing the object, except for `= {0}`, as
2607 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2608 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002609 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002611 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2612 psa_hash_operation_t zero;
2613
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002615
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002616 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2618 PSA_ERROR_BAD_STATE);
2619 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2620 PSA_ERROR_BAD_STATE);
2621 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2622 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002623
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002624 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 PSA_ASSERT(psa_hash_abort(&func));
2626 PSA_ASSERT(psa_hash_abort(&init));
2627 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002628}
2629/* END_CASE */
2630
2631/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002632void hash_setup(int alg_arg,
2633 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002634{
2635 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002636 uint8_t *output = NULL;
2637 size_t output_size = 0;
2638 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002639 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002640 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002641 psa_status_t status;
2642
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002644
Neil Armstrongedb20862022-02-07 15:47:44 +01002645 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 output_size = PSA_HASH_LENGTH(alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002647 TEST_CALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002648
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 status = psa_hash_compute(alg, NULL, 0,
2650 output, output_size, &output_length);
2651 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002652
2653 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 status = psa_hash_setup(&operation, alg);
2655 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002656
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002657 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002659
2660 /* If setup failed, reproduce the failure, so as to
2661 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 if (status != PSA_SUCCESS) {
2663 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2664 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002665
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002666 /* Now the operation object should be reusable. */
2667#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2669 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002670#endif
2671
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002672exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 mbedtls_free(output);
2674 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002675}
2676/* END_CASE */
2677
2678/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002679void hash_compute_fail(int alg_arg, data_t *input,
2680 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002681{
2682 psa_algorithm_t alg = alg_arg;
2683 uint8_t *output = NULL;
2684 size_t output_size = output_size_arg;
2685 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002686 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002687 psa_status_t expected_status = expected_status_arg;
2688 psa_status_t status;
2689
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002690 TEST_CALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002691
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002693
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002694 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 status = psa_hash_compute(alg, input->x, input->len,
2696 output, output_size, &output_length);
2697 TEST_EQUAL(status, expected_status);
2698 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002699
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002700 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 status = psa_hash_setup(&operation, alg);
2702 if (status == PSA_SUCCESS) {
2703 status = psa_hash_update(&operation, input->x, input->len);
2704 if (status == PSA_SUCCESS) {
2705 status = psa_hash_finish(&operation, output, output_size,
2706 &output_length);
2707 if (status == PSA_SUCCESS) {
2708 TEST_LE_U(output_length, output_size);
2709 } else {
2710 TEST_EQUAL(status, expected_status);
2711 }
2712 } else {
2713 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002714 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 } else {
2716 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002717 }
2718
Gilles Peskine0a749c82019-11-28 19:33:58 +01002719exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 PSA_ASSERT(psa_hash_abort(&operation));
2721 mbedtls_free(output);
2722 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002723}
2724/* END_CASE */
2725
2726/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002727void hash_compare_fail(int alg_arg, data_t *input,
2728 data_t *reference_hash,
2729 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002730{
2731 psa_algorithm_t alg = alg_arg;
2732 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002733 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002734 psa_status_t status;
2735
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002737
Neil Armstrong55a1be12022-02-07 11:23:20 +01002738 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 status = psa_hash_compare(alg, input->x, input->len,
2740 reference_hash->x, reference_hash->len);
2741 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002742
Neil Armstrong55a1be12022-02-07 11:23:20 +01002743 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 status = psa_hash_setup(&operation, alg);
2745 if (status == PSA_SUCCESS) {
2746 status = psa_hash_update(&operation, input->x, input->len);
2747 if (status == PSA_SUCCESS) {
2748 status = psa_hash_verify(&operation, reference_hash->x,
2749 reference_hash->len);
2750 TEST_EQUAL(status, expected_status);
2751 } else {
2752 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002753 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 } else {
2755 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002756 }
2757
Gilles Peskine88e08462020-01-28 20:43:00 +01002758exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 PSA_ASSERT(psa_hash_abort(&operation));
2760 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002761}
2762/* END_CASE */
2763
2764/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002765void hash_compute_compare(int alg_arg, data_t *input,
2766 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002767{
2768 psa_algorithm_t alg = alg_arg;
2769 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2770 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002771 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002772 size_t i;
2773
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002775
Neil Armstrongca30a002022-02-07 11:40:23 +01002776 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2778 output, PSA_HASH_LENGTH(alg),
2779 &output_length));
2780 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002781 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002782 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002783
Neil Armstrongca30a002022-02-07 11:40:23 +01002784 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 PSA_ASSERT(psa_hash_setup(&operation, alg));
2786 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2787 PSA_ASSERT(psa_hash_finish(&operation, output,
2788 PSA_HASH_LENGTH(alg),
2789 &output_length));
2790 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002791 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002792 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002793
2794 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2796 output, sizeof(output),
2797 &output_length));
2798 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002799 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002800 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002801
Neil Armstrongca30a002022-02-07 11:40:23 +01002802 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 PSA_ASSERT(psa_hash_setup(&operation, alg));
2804 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2805 PSA_ASSERT(psa_hash_finish(&operation, output,
2806 sizeof(output), &output_length));
2807 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002808 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002809 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002810
2811 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2813 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002814
Neil Armstrongca30a002022-02-07 11:40:23 +01002815 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 PSA_ASSERT(psa_hash_setup(&operation, alg));
2817 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2818 PSA_ASSERT(psa_hash_verify(&operation, output,
2819 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002820
2821 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2823 output, output_length + 1),
2824 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002825
Neil Armstrongca30a002022-02-07 11:40:23 +01002826 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 PSA_ASSERT(psa_hash_setup(&operation, alg));
2828 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2829 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2830 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002831
2832 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2834 output, output_length - 1),
2835 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002836
Neil Armstrongca30a002022-02-07 11:40:23 +01002837 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 PSA_ASSERT(psa_hash_setup(&operation, alg));
2839 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2840 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2841 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002842
Gilles Peskine0a749c82019-11-28 19:33:58 +01002843 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 for (i = 0; i < output_length; i++) {
2845 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002846 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002847
2848 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2850 output, output_length),
2851 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002852
2853 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 PSA_ASSERT(psa_hash_setup(&operation, alg));
2855 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2856 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2857 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002858
Gilles Peskine0a749c82019-11-28 19:33:58 +01002859 output[i] ^= 1;
2860 }
2861
2862exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 PSA_ASSERT(psa_hash_abort(&operation));
2864 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002865}
2866/* END_CASE */
2867
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002868/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002869void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002870{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002871 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002872 unsigned char input[] = "";
2873 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002874 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002875 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2876 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2878 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002879 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002880 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002881 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002882
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002884
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002885 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 PSA_ASSERT(psa_hash_setup(&operation, alg));
2887 ASSERT_OPERATION_IS_ACTIVE(operation);
2888 TEST_EQUAL(psa_hash_setup(&operation, alg),
2889 PSA_ERROR_BAD_STATE);
2890 ASSERT_OPERATION_IS_INACTIVE(operation);
2891 PSA_ASSERT(psa_hash_abort(&operation));
2892 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002893
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002894 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2896 PSA_ERROR_BAD_STATE);
2897 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002898
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002899 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002901 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 ASSERT_OPERATION_IS_ACTIVE(operation);
2903 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2904 PSA_ERROR_BAD_STATE);
2905 ASSERT_OPERATION_IS_INACTIVE(operation);
2906 PSA_ASSERT(psa_hash_abort(&operation));
2907 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002908
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002909 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 PSA_ASSERT(psa_hash_setup(&operation, alg));
2911 PSA_ASSERT(psa_hash_finish(&operation,
2912 hash, sizeof(hash), &hash_len));
2913 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2914 PSA_ERROR_BAD_STATE);
2915 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002916
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002917 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 TEST_EQUAL(psa_hash_verify(&operation,
2919 valid_hash, sizeof(valid_hash)),
2920 PSA_ERROR_BAD_STATE);
2921 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002922
2923 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 PSA_ASSERT(psa_hash_setup(&operation, alg));
2925 PSA_ASSERT(psa_hash_finish(&operation,
2926 hash, sizeof(hash), &hash_len));
2927 TEST_EQUAL(psa_hash_verify(&operation,
2928 valid_hash, sizeof(valid_hash)),
2929 PSA_ERROR_BAD_STATE);
2930 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002931
2932 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 PSA_ASSERT(psa_hash_setup(&operation, alg));
2934 ASSERT_OPERATION_IS_ACTIVE(operation);
2935 PSA_ASSERT(psa_hash_verify(&operation,
2936 valid_hash, sizeof(valid_hash)));
2937 ASSERT_OPERATION_IS_INACTIVE(operation);
2938 TEST_EQUAL(psa_hash_verify(&operation,
2939 valid_hash, sizeof(valid_hash)),
2940 PSA_ERROR_BAD_STATE);
2941 ASSERT_OPERATION_IS_INACTIVE(operation);
2942 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002943
2944 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 TEST_EQUAL(psa_hash_finish(&operation,
2946 hash, sizeof(hash), &hash_len),
2947 PSA_ERROR_BAD_STATE);
2948 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002949
2950 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 PSA_ASSERT(psa_hash_setup(&operation, alg));
2952 PSA_ASSERT(psa_hash_finish(&operation,
2953 hash, sizeof(hash), &hash_len));
2954 TEST_EQUAL(psa_hash_finish(&operation,
2955 hash, sizeof(hash), &hash_len),
2956 PSA_ERROR_BAD_STATE);
2957 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002958
2959 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 PSA_ASSERT(psa_hash_setup(&operation, alg));
2961 PSA_ASSERT(psa_hash_verify(&operation,
2962 valid_hash, sizeof(valid_hash)));
2963 TEST_EQUAL(psa_hash_finish(&operation,
2964 hash, sizeof(hash), &hash_len),
2965 PSA_ERROR_BAD_STATE);
2966 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002967
2968exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002970}
2971/* END_CASE */
2972
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002973/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002974void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002975{
2976 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002977 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2978 * appended to it */
2979 unsigned char hash[] = {
2980 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2981 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2983 };
2984 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002985 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002986
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002988
itayzafrir27e69452018-11-01 14:26:34 +02002989 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 PSA_ASSERT(psa_hash_setup(&operation, alg));
2991 ASSERT_OPERATION_IS_ACTIVE(operation);
2992 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2993 PSA_ERROR_INVALID_SIGNATURE);
2994 ASSERT_OPERATION_IS_INACTIVE(operation);
2995 PSA_ASSERT(psa_hash_abort(&operation));
2996 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002997
itayzafrir27e69452018-11-01 14:26:34 +02002998 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 PSA_ASSERT(psa_hash_setup(&operation, alg));
3000 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
3001 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03003002
itayzafrir27e69452018-11-01 14:26:34 +02003003 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 PSA_ASSERT(psa_hash_setup(&operation, alg));
3005 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
3006 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03003007
itayzafrirec93d302018-10-18 18:01:10 +03003008exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03003010}
3011/* END_CASE */
3012
Ronald Cronee414c72021-03-18 18:50:08 +01003013/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003014void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03003015{
3016 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003017 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003019 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003020 size_t hash_len;
3021
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03003023
itayzafrir58028322018-10-25 10:22:01 +03003024 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 PSA_ASSERT(psa_hash_setup(&operation, alg));
3026 TEST_EQUAL(psa_hash_finish(&operation,
3027 hash, expected_size - 1, &hash_len),
3028 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003029
3030exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003032}
3033/* END_CASE */
3034
Ronald Cronee414c72021-03-18 18:50:08 +01003035/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003036void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003037{
3038 psa_algorithm_t alg = PSA_ALG_SHA_256;
3039 unsigned char hash[PSA_HASH_MAX_SIZE];
3040 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3041 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3042 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3043 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3044 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3045 size_t hash_len;
3046
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 PSA_ASSERT(psa_crypto_init());
3048 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003049
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3051 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3052 PSA_ASSERT(psa_hash_finish(&op_finished,
3053 hash, sizeof(hash), &hash_len));
3054 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3055 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003056
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3058 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003059
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3061 PSA_ASSERT(psa_hash_finish(&op_init,
3062 hash, sizeof(hash), &hash_len));
3063 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3064 PSA_ASSERT(psa_hash_finish(&op_finished,
3065 hash, sizeof(hash), &hash_len));
3066 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3067 PSA_ASSERT(psa_hash_finish(&op_aborted,
3068 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003069
3070exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 psa_hash_abort(&op_source);
3072 psa_hash_abort(&op_init);
3073 psa_hash_abort(&op_setup);
3074 psa_hash_abort(&op_finished);
3075 psa_hash_abort(&op_aborted);
3076 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003077}
3078/* END_CASE */
3079
Ronald Cronee414c72021-03-18 18:50:08 +01003080/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003081void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003082{
3083 psa_algorithm_t alg = PSA_ALG_SHA_256;
3084 unsigned char hash[PSA_HASH_MAX_SIZE];
3085 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3086 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3087 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3088 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3089 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3090 size_t hash_len;
3091
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003093
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3095 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3096 PSA_ASSERT(psa_hash_finish(&op_finished,
3097 hash, sizeof(hash), &hash_len));
3098 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3099 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003100
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3102 PSA_ASSERT(psa_hash_finish(&op_target,
3103 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003104
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3106 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3107 PSA_ERROR_BAD_STATE);
3108 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3109 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003110
3111exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 psa_hash_abort(&op_target);
3113 psa_hash_abort(&op_init);
3114 psa_hash_abort(&op_setup);
3115 psa_hash_abort(&op_finished);
3116 psa_hash_abort(&op_aborted);
3117 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003118}
3119/* END_CASE */
3120
itayzafrir58028322018-10-25 10:22:01 +03003121/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003122void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003123{
Jaeden Amero252ef282019-02-15 14:05:35 +00003124 const uint8_t input[1] = { 0 };
3125
Jaeden Amero769ce272019-01-04 11:48:03 +00003126 /* Test each valid way of initializing the object, except for `= {0}`, as
3127 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3128 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003129 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003131 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3132 psa_mac_operation_t zero;
3133
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003135
Jaeden Amero252ef282019-02-15 14:05:35 +00003136 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 TEST_EQUAL(psa_mac_update(&func,
3138 input, sizeof(input)),
3139 PSA_ERROR_BAD_STATE);
3140 TEST_EQUAL(psa_mac_update(&init,
3141 input, sizeof(input)),
3142 PSA_ERROR_BAD_STATE);
3143 TEST_EQUAL(psa_mac_update(&zero,
3144 input, sizeof(input)),
3145 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003146
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003147 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 PSA_ASSERT(psa_mac_abort(&func));
3149 PSA_ASSERT(psa_mac_abort(&init));
3150 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003151}
3152/* END_CASE */
3153
3154/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003155void mac_setup(int key_type_arg,
3156 data_t *key,
3157 int alg_arg,
3158 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003159{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003160 psa_key_type_t key_type = key_type_arg;
3161 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003162 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003163 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003164 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3165#if defined(KNOWN_SUPPORTED_MAC_ALG)
3166 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3167#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003168
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003170
Gilles Peskine449bd832023-01-11 14:50:10 +01003171 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3172 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003173 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003174 }
3175 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003176
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003177 /* The operation object should be reusable. */
3178#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3180 smoke_test_key_data,
3181 sizeof(smoke_test_key_data),
3182 KNOWN_SUPPORTED_MAC_ALG,
3183 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003184 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003185 }
3186 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003187#endif
3188
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003189exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003191}
3192/* END_CASE */
3193
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003194/* 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 +01003195void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003196{
Ronald Cron5425a212020-08-04 14:58:35 +02003197 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003198 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3199 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003200 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003201 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3202 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3204 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003206 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3207 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3208 size_t sign_mac_length = 0;
3209 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3210 const uint8_t verify_mac[] = {
3211 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3212 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003213 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3214 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003215
Gilles Peskine449bd832023-01-11 14:50:10 +01003216 PSA_ASSERT(psa_crypto_init());
3217 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3218 psa_set_key_algorithm(&attributes, alg);
3219 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003220
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3222 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003223
Jaeden Amero252ef282019-02-15 14:05:35 +00003224 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003225 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3226 PSA_ERROR_BAD_STATE);
3227 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003228
3229 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003230 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3231 &sign_mac_length),
3232 PSA_ERROR_BAD_STATE);
3233 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003234
3235 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 TEST_EQUAL(psa_mac_verify_finish(&operation,
3237 verify_mac, sizeof(verify_mac)),
3238 PSA_ERROR_BAD_STATE);
3239 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003240
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003241 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003242 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3243 ASSERT_OPERATION_IS_ACTIVE(operation);
3244 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3245 PSA_ERROR_BAD_STATE);
3246 ASSERT_OPERATION_IS_INACTIVE(operation);
3247 PSA_ASSERT(psa_mac_abort(&operation));
3248 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003249
Jaeden Amero252ef282019-02-15 14:05:35 +00003250 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003251 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3252 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3253 PSA_ASSERT(psa_mac_sign_finish(&operation,
3254 sign_mac, sizeof(sign_mac),
3255 &sign_mac_length));
3256 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3257 PSA_ERROR_BAD_STATE);
3258 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003259
3260 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3262 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3263 PSA_ASSERT(psa_mac_verify_finish(&operation,
3264 verify_mac, sizeof(verify_mac)));
3265 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3266 PSA_ERROR_BAD_STATE);
3267 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003268
3269 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3271 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3272 PSA_ASSERT(psa_mac_sign_finish(&operation,
3273 sign_mac, sizeof(sign_mac),
3274 &sign_mac_length));
3275 TEST_EQUAL(psa_mac_sign_finish(&operation,
3276 sign_mac, sizeof(sign_mac),
3277 &sign_mac_length),
3278 PSA_ERROR_BAD_STATE);
3279 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003280
3281 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003282 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3283 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3284 PSA_ASSERT(psa_mac_verify_finish(&operation,
3285 verify_mac, sizeof(verify_mac)));
3286 TEST_EQUAL(psa_mac_verify_finish(&operation,
3287 verify_mac, sizeof(verify_mac)),
3288 PSA_ERROR_BAD_STATE);
3289 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003290
3291 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3293 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3294 ASSERT_OPERATION_IS_ACTIVE(operation);
3295 TEST_EQUAL(psa_mac_verify_finish(&operation,
3296 verify_mac, sizeof(verify_mac)),
3297 PSA_ERROR_BAD_STATE);
3298 ASSERT_OPERATION_IS_INACTIVE(operation);
3299 PSA_ASSERT(psa_mac_abort(&operation));
3300 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003301
3302 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003303 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3304 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3305 ASSERT_OPERATION_IS_ACTIVE(operation);
3306 TEST_EQUAL(psa_mac_sign_finish(&operation,
3307 sign_mac, sizeof(sign_mac),
3308 &sign_mac_length),
3309 PSA_ERROR_BAD_STATE);
3310 ASSERT_OPERATION_IS_INACTIVE(operation);
3311 PSA_ASSERT(psa_mac_abort(&operation));
3312 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003313
Gilles Peskine449bd832023-01-11 14:50:10 +01003314 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003315
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003316exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003318}
3319/* END_CASE */
3320
3321/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003322void mac_sign_verify_multi(int key_type_arg,
3323 data_t *key_data,
3324 int alg_arg,
3325 data_t *input,
3326 int is_verify,
3327 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003328{
3329 size_t data_part_len = 0;
3330
Gilles Peskine449bd832023-01-11 14:50:10 +01003331 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003332 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003333 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003334
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 if (mac_multipart_internal_func(key_type_arg, key_data,
3336 alg_arg,
3337 input, data_part_len,
3338 expected_mac,
3339 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003340 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003342
3343 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003344 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003345
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 if (mac_multipart_internal_func(key_type_arg, key_data,
3347 alg_arg,
3348 input, data_part_len,
3349 expected_mac,
3350 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003351 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003352 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003353 }
3354
3355 /* Goto is required to silence warnings about unused labels, as we
3356 * don't actually do any test assertions in this function. */
3357 goto exit;
3358}
3359/* END_CASE */
3360
3361/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003362void mac_sign(int key_type_arg,
3363 data_t *key_data,
3364 int alg_arg,
3365 data_t *input,
3366 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003367{
Ronald Cron5425a212020-08-04 14:58:35 +02003368 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003369 psa_key_type_t key_type = key_type_arg;
3370 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003371 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003372 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003373 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003374 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003375 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003376 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003377 const size_t output_sizes_to_test[] = {
3378 0,
3379 1,
3380 expected_mac->len - 1,
3381 expected_mac->len,
3382 expected_mac->len + 1,
3383 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003384
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003386 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003388
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003390
Gilles Peskine449bd832023-01-11 14:50:10 +01003391 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3392 psa_set_key_algorithm(&attributes, alg);
3393 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003394
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3396 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003397
Gilles Peskine449bd832023-01-11 14:50:10 +01003398 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003399 const size_t output_size = output_sizes_to_test[i];
3400 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003401 (output_size >= expected_mac->len ? PSA_SUCCESS :
3402 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003403
Gilles Peskine449bd832023-01-11 14:50:10 +01003404 mbedtls_test_set_step(output_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003405 TEST_CALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003406
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003407 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003408 TEST_EQUAL(psa_mac_compute(key, alg,
3409 input->x, input->len,
3410 actual_mac, output_size, &mac_length),
3411 expected_status);
3412 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003413 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003414 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003415 }
3416
Gilles Peskine449bd832023-01-11 14:50:10 +01003417 if (output_size > 0) {
3418 memset(actual_mac, 0, output_size);
3419 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003420
3421 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003422 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3423 PSA_ASSERT(psa_mac_update(&operation,
3424 input->x, input->len));
3425 TEST_EQUAL(psa_mac_sign_finish(&operation,
3426 actual_mac, output_size,
3427 &mac_length),
3428 expected_status);
3429 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003430
Gilles Peskine449bd832023-01-11 14:50:10 +01003431 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003432 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003433 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003434 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003435 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003436 actual_mac = NULL;
3437 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003438
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003439exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003440 psa_mac_abort(&operation);
3441 psa_destroy_key(key);
3442 PSA_DONE();
3443 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003444}
3445/* END_CASE */
3446
3447/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003448void mac_verify(int key_type_arg,
3449 data_t *key_data,
3450 int alg_arg,
3451 data_t *input,
3452 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003453{
Ronald Cron5425a212020-08-04 14:58:35 +02003454 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003455 psa_key_type_t key_type = key_type_arg;
3456 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003457 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003458 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003459 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003460
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003462
Gilles Peskine449bd832023-01-11 14:50:10 +01003463 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003464
Gilles Peskine449bd832023-01-11 14:50:10 +01003465 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3466 psa_set_key_algorithm(&attributes, alg);
3467 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003468
Gilles Peskine449bd832023-01-11 14:50:10 +01003469 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3470 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003471
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003472 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003473 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3474 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003475
3476 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003477 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3478 PSA_ASSERT(psa_mac_update(&operation,
3479 input->x, input->len));
3480 PSA_ASSERT(psa_mac_verify_finish(&operation,
3481 expected_mac->x,
3482 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003483
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003484 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003485 TEST_EQUAL(psa_mac_verify(key, alg,
3486 input->x, input->len,
3487 expected_mac->x,
3488 expected_mac->len - 1),
3489 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003490
3491 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003492 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3493 PSA_ASSERT(psa_mac_update(&operation,
3494 input->x, input->len));
3495 TEST_EQUAL(psa_mac_verify_finish(&operation,
3496 expected_mac->x,
3497 expected_mac->len - 1),
3498 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003499
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003500 /* Test a MAC that's too long, one-shot case. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003501 TEST_CALLOC(perturbed_mac, expected_mac->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01003502 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3503 TEST_EQUAL(psa_mac_verify(key, alg,
3504 input->x, input->len,
3505 perturbed_mac, expected_mac->len + 1),
3506 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003507
3508 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003509 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3510 PSA_ASSERT(psa_mac_update(&operation,
3511 input->x, input->len));
3512 TEST_EQUAL(psa_mac_verify_finish(&operation,
3513 perturbed_mac,
3514 expected_mac->len + 1),
3515 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003516
3517 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003518 for (size_t i = 0; i < expected_mac->len; i++) {
3519 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003520 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003521
Gilles Peskine449bd832023-01-11 14:50:10 +01003522 TEST_EQUAL(psa_mac_verify(key, alg,
3523 input->x, input->len,
3524 perturbed_mac, expected_mac->len),
3525 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003526
Gilles Peskine449bd832023-01-11 14:50:10 +01003527 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3528 PSA_ASSERT(psa_mac_update(&operation,
3529 input->x, input->len));
3530 TEST_EQUAL(psa_mac_verify_finish(&operation,
3531 perturbed_mac,
3532 expected_mac->len),
3533 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003534 perturbed_mac[i] ^= 1;
3535 }
3536
Gilles Peskine8c9def32018-02-08 10:02:12 +01003537exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003538 psa_mac_abort(&operation);
3539 psa_destroy_key(key);
3540 PSA_DONE();
3541 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003542}
3543/* END_CASE */
3544
3545/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003546void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003547{
Jaeden Ameroab439972019-02-15 14:12:05 +00003548 const uint8_t input[1] = { 0 };
3549 unsigned char output[1] = { 0 };
3550 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003551 /* Test each valid way of initializing the object, except for `= {0}`, as
3552 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3553 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003554 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003555 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003556 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3557 psa_cipher_operation_t zero;
3558
Gilles Peskine449bd832023-01-11 14:50:10 +01003559 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003560
Jaeden Ameroab439972019-02-15 14:12:05 +00003561 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003562 TEST_EQUAL(psa_cipher_update(&func,
3563 input, sizeof(input),
3564 output, sizeof(output),
3565 &output_length),
3566 PSA_ERROR_BAD_STATE);
3567 TEST_EQUAL(psa_cipher_update(&init,
3568 input, sizeof(input),
3569 output, sizeof(output),
3570 &output_length),
3571 PSA_ERROR_BAD_STATE);
3572 TEST_EQUAL(psa_cipher_update(&zero,
3573 input, sizeof(input),
3574 output, sizeof(output),
3575 &output_length),
3576 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003577
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003578 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003579 PSA_ASSERT(psa_cipher_abort(&func));
3580 PSA_ASSERT(psa_cipher_abort(&init));
3581 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003582}
3583/* END_CASE */
3584
3585/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003586void cipher_setup(int key_type_arg,
3587 data_t *key,
3588 int alg_arg,
3589 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003590{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003591 psa_key_type_t key_type = key_type_arg;
3592 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003593 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003594 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003595 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003596#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003597 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3598#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003599
Gilles Peskine449bd832023-01-11 14:50:10 +01003600 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003601
Gilles Peskine449bd832023-01-11 14:50:10 +01003602 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3603 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003604 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003605 }
3606 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003607
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003608 /* The operation object should be reusable. */
3609#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003610 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3611 smoke_test_key_data,
3612 sizeof(smoke_test_key_data),
3613 KNOWN_SUPPORTED_CIPHER_ALG,
3614 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003615 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003616 }
3617 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003618#endif
3619
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003620exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003621 psa_cipher_abort(&operation);
3622 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003623}
3624/* END_CASE */
3625
Ronald Cronee414c72021-03-18 18:50:08 +01003626/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003627void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003628{
Ronald Cron5425a212020-08-04 14:58:35 +02003629 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003630 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3631 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003632 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003633 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003634 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003635 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003636 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003637 0xaa, 0xaa, 0xaa, 0xaa
3638 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003639 const uint8_t text[] = {
3640 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003641 0xbb, 0xbb, 0xbb, 0xbb
3642 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003643 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003644 size_t length = 0;
3645
Gilles Peskine449bd832023-01-11 14:50:10 +01003646 PSA_ASSERT(psa_crypto_init());
3647 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3648 psa_set_key_algorithm(&attributes, alg);
3649 psa_set_key_type(&attributes, key_type);
3650 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3651 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003652
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003653 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003654 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3655 ASSERT_OPERATION_IS_ACTIVE(operation);
3656 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3657 PSA_ERROR_BAD_STATE);
3658 ASSERT_OPERATION_IS_INACTIVE(operation);
3659 PSA_ASSERT(psa_cipher_abort(&operation));
3660 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003661
3662 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003663 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3664 ASSERT_OPERATION_IS_ACTIVE(operation);
3665 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3666 PSA_ERROR_BAD_STATE);
3667 ASSERT_OPERATION_IS_INACTIVE(operation);
3668 PSA_ASSERT(psa_cipher_abort(&operation));
3669 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003670
Jaeden Ameroab439972019-02-15 14:12:05 +00003671 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003672 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3673 buffer, sizeof(buffer),
3674 &length),
3675 PSA_ERROR_BAD_STATE);
3676 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003677
3678 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003679 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3680 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3681 buffer, sizeof(buffer),
3682 &length));
3683 ASSERT_OPERATION_IS_ACTIVE(operation);
3684 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3685 buffer, sizeof(buffer),
3686 &length),
3687 PSA_ERROR_BAD_STATE);
3688 ASSERT_OPERATION_IS_INACTIVE(operation);
3689 PSA_ASSERT(psa_cipher_abort(&operation));
3690 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003691
3692 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003693 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3694 PSA_ASSERT(psa_cipher_set_iv(&operation,
3695 iv, sizeof(iv)));
3696 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3697 buffer, sizeof(buffer),
3698 &length),
3699 PSA_ERROR_BAD_STATE);
3700 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003701
3702 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003703 TEST_EQUAL(psa_cipher_set_iv(&operation,
3704 iv, sizeof(iv)),
3705 PSA_ERROR_BAD_STATE);
3706 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003707
3708 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003709 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3710 PSA_ASSERT(psa_cipher_set_iv(&operation,
3711 iv, sizeof(iv)));
3712 ASSERT_OPERATION_IS_ACTIVE(operation);
3713 TEST_EQUAL(psa_cipher_set_iv(&operation,
3714 iv, sizeof(iv)),
3715 PSA_ERROR_BAD_STATE);
3716 ASSERT_OPERATION_IS_INACTIVE(operation);
3717 PSA_ASSERT(psa_cipher_abort(&operation));
3718 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003719
3720 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003721 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3722 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3723 buffer, sizeof(buffer),
3724 &length));
3725 TEST_EQUAL(psa_cipher_set_iv(&operation,
3726 iv, sizeof(iv)),
3727 PSA_ERROR_BAD_STATE);
3728 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003729
3730 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003731 TEST_EQUAL(psa_cipher_update(&operation,
3732 text, sizeof(text),
3733 buffer, sizeof(buffer),
3734 &length),
3735 PSA_ERROR_BAD_STATE);
3736 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003737
3738 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003739 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3740 ASSERT_OPERATION_IS_ACTIVE(operation);
3741 TEST_EQUAL(psa_cipher_update(&operation,
3742 text, sizeof(text),
3743 buffer, sizeof(buffer),
3744 &length),
3745 PSA_ERROR_BAD_STATE);
3746 ASSERT_OPERATION_IS_INACTIVE(operation);
3747 PSA_ASSERT(psa_cipher_abort(&operation));
3748 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003749
3750 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003751 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3752 PSA_ASSERT(psa_cipher_set_iv(&operation,
3753 iv, sizeof(iv)));
3754 PSA_ASSERT(psa_cipher_finish(&operation,
3755 buffer, sizeof(buffer), &length));
3756 TEST_EQUAL(psa_cipher_update(&operation,
3757 text, sizeof(text),
3758 buffer, sizeof(buffer),
3759 &length),
3760 PSA_ERROR_BAD_STATE);
3761 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003762
3763 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003764 TEST_EQUAL(psa_cipher_finish(&operation,
3765 buffer, sizeof(buffer), &length),
3766 PSA_ERROR_BAD_STATE);
3767 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003768
3769 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003770 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003771 /* Not calling update means we are encrypting an empty buffer, which is OK
3772 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003773 ASSERT_OPERATION_IS_ACTIVE(operation);
3774 TEST_EQUAL(psa_cipher_finish(&operation,
3775 buffer, sizeof(buffer), &length),
3776 PSA_ERROR_BAD_STATE);
3777 ASSERT_OPERATION_IS_INACTIVE(operation);
3778 PSA_ASSERT(psa_cipher_abort(&operation));
3779 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003780
3781 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003782 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3783 PSA_ASSERT(psa_cipher_set_iv(&operation,
3784 iv, sizeof(iv)));
3785 PSA_ASSERT(psa_cipher_finish(&operation,
3786 buffer, sizeof(buffer), &length));
3787 TEST_EQUAL(psa_cipher_finish(&operation,
3788 buffer, sizeof(buffer), &length),
3789 PSA_ERROR_BAD_STATE);
3790 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003791
Gilles Peskine449bd832023-01-11 14:50:10 +01003792 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003793
Jaeden Ameroab439972019-02-15 14:12:05 +00003794exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003795 psa_cipher_abort(&operation);
3796 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003797}
3798/* END_CASE */
3799
3800/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003801void cipher_encrypt_fail(int alg_arg,
3802 int key_type_arg,
3803 data_t *key_data,
3804 data_t *input,
3805 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003806{
Ronald Cron5425a212020-08-04 14:58:35 +02003807 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003808 psa_status_t status;
3809 psa_key_type_t key_type = key_type_arg;
3810 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003811 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003812 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003813 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3814 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003815 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003816 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003817 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003818 size_t function_output_length;
3819 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003820 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3821
Gilles Peskine449bd832023-01-11 14:50:10 +01003822 if (PSA_ERROR_BAD_STATE != expected_status) {
3823 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003824
Gilles Peskine449bd832023-01-11 14:50:10 +01003825 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3826 psa_set_key_algorithm(&attributes, alg);
3827 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003828
Gilles Peskine449bd832023-01-11 14:50:10 +01003829 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3830 input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003831 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003832
Gilles Peskine449bd832023-01-11 14:50:10 +01003833 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3834 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003835 }
3836
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003837 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003838 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3839 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003840
Gilles Peskine449bd832023-01-11 14:50:10 +01003841 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003842
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003843 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003844 status = psa_cipher_encrypt_setup(&operation, key, alg);
3845 if (status == PSA_SUCCESS) {
3846 if (alg != PSA_ALG_ECB_NO_PADDING) {
3847 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3848 iv, iv_size,
3849 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003850 }
3851
Gilles Peskine449bd832023-01-11 14:50:10 +01003852 status = psa_cipher_update(&operation, input->x, input->len,
3853 output, output_buffer_size,
3854 &function_output_length);
3855 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003856 output_length += function_output_length;
3857
Gilles Peskine449bd832023-01-11 14:50:10 +01003858 status = psa_cipher_finish(&operation, output + output_length,
3859 output_buffer_size - output_length,
3860 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003861
Gilles Peskine449bd832023-01-11 14:50:10 +01003862 TEST_EQUAL(status, expected_status);
3863 } else {
3864 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003865 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003866 } else {
3867 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003868 }
3869
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003870exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003871 psa_cipher_abort(&operation);
3872 mbedtls_free(output);
3873 psa_destroy_key(key);
3874 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003875}
3876/* END_CASE */
3877
3878/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003879void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3880 data_t *input, int iv_length,
3881 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003882{
3883 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3884 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3885 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3886 size_t output_buffer_size = 0;
3887 unsigned char *output = NULL;
3888
Gilles Peskine449bd832023-01-11 14:50:10 +01003889 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003890 TEST_CALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003891
Gilles Peskine449bd832023-01-11 14:50:10 +01003892 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003893
Gilles Peskine449bd832023-01-11 14:50:10 +01003894 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3895 psa_set_key_algorithm(&attributes, alg);
3896 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003897
Gilles Peskine449bd832023-01-11 14:50:10 +01003898 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3899 &key));
3900 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3901 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3902 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003903
3904exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003905 psa_cipher_abort(&operation);
3906 mbedtls_free(output);
3907 psa_destroy_key(key);
3908 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003909}
3910/* END_CASE */
3911
3912/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003913void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3914 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003915{
3916 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3917 psa_key_type_t key_type = key_type_arg;
3918 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003919 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3920 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003921 unsigned char *output = NULL;
3922 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003923 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003924 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3925
Gilles Peskine449bd832023-01-11 14:50:10 +01003926 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003927
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003928 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003929 TEST_LE_U(ciphertext->len,
3930 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3931 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3932 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3933 TEST_LE_U(plaintext->len,
3934 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3935 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3936 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003937
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003938
3939 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003940 psa_set_key_usage_flags(&attributes,
3941 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3942 psa_set_key_algorithm(&attributes, alg);
3943 psa_set_key_type(&attributes, key_type);
3944 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3945 &key));
3946 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3947 plaintext->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003948 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003949
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003950 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003951 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3952 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3953 PSA_ERROR_BAD_STATE);
3954 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3955 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3956 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003957
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003958 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003959 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3960 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3961 &length),
3962 PSA_ERROR_BAD_STATE);
3963 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3964 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3965 &length),
3966 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003967
Gilles Peskine286c3142022-04-20 17:09:38 +02003968 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003969 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003970 output_length = 0;
3971 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003972 PSA_ASSERT(psa_cipher_update(&operation,
3973 plaintext->x, plaintext->len,
3974 output, output_buffer_size,
3975 &length));
3976 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003977 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003978 PSA_ASSERT(psa_cipher_finish(&operation,
3979 mbedtls_buffer_offset(output, output_length),
3980 output_buffer_size - output_length,
3981 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003982 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003983 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003984 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003985
Gilles Peskine286c3142022-04-20 17:09:38 +02003986 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003987 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003988 output_length = 0;
3989 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003990 PSA_ASSERT(psa_cipher_update(&operation,
3991 ciphertext->x, ciphertext->len,
3992 output, output_buffer_size,
3993 &length));
3994 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003995 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003996 PSA_ASSERT(psa_cipher_finish(&operation,
3997 mbedtls_buffer_offset(output, output_length),
3998 output_buffer_size - output_length,
3999 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02004000 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004001 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004002 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004003
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004004 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004005 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004006 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
4007 output, output_buffer_size,
4008 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004009 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004010 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004011
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004012 /* One-shot decryption */
4013 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004014 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
4015 output, output_buffer_size,
4016 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004017 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004018 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004019
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004020exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004021 PSA_ASSERT(psa_cipher_abort(&operation));
4022 mbedtls_free(output);
4023 psa_cipher_abort(&operation);
4024 psa_destroy_key(key);
4025 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004026}
4027/* END_CASE */
4028
4029/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004030void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004031{
4032 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4033 psa_algorithm_t alg = alg_arg;
4034 psa_key_type_t key_type = key_type_arg;
4035 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4036 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4037 psa_status_t status;
4038
Gilles Peskine449bd832023-01-11 14:50:10 +01004039 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004040
Gilles Peskine449bd832023-01-11 14:50:10 +01004041 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4042 psa_set_key_algorithm(&attributes, alg);
4043 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004044
4045 /* Usage of either of these two size macros would cause divide by zero
4046 * with incorrect key types previously. Input length should be irrelevant
4047 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004048 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4049 0);
4050 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004051
4052
Gilles Peskine449bd832023-01-11 14:50:10 +01004053 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4054 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004055
4056 /* Should fail due to invalid alg type (to support invalid key type).
4057 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004058 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004059
Gilles Peskine449bd832023-01-11 14:50:10 +01004060 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004061
4062exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004063 psa_cipher_abort(&operation);
4064 psa_destroy_key(key);
4065 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004066}
4067/* END_CASE */
4068
4069/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004070void cipher_encrypt_validation(int alg_arg,
4071 int key_type_arg,
4072 data_t *key_data,
4073 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004074{
4075 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4076 psa_key_type_t key_type = key_type_arg;
4077 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004078 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004079 unsigned char *output1 = NULL;
4080 size_t output1_buffer_size = 0;
4081 size_t output1_length = 0;
4082 unsigned char *output2 = NULL;
4083 size_t output2_buffer_size = 0;
4084 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004085 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004086 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004087 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004088
Gilles Peskine449bd832023-01-11 14:50:10 +01004089 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004090
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4092 psa_set_key_algorithm(&attributes, alg);
4093 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004094
Gilles Peskine449bd832023-01-11 14:50:10 +01004095 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4096 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4097 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004098 TEST_CALLOC(output1, output1_buffer_size);
4099 TEST_CALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004100
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4102 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004103
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004104 /* The one-shot cipher encryption uses generated iv so validating
4105 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004106 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4107 output1_buffer_size, &output1_length));
4108 TEST_LE_U(output1_length,
4109 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4110 TEST_LE_U(output1_length,
4111 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004112
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4114 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004115
Gilles Peskine449bd832023-01-11 14:50:10 +01004116 PSA_ASSERT(psa_cipher_update(&operation,
4117 input->x, input->len,
4118 output2, output2_buffer_size,
4119 &function_output_length));
4120 TEST_LE_U(function_output_length,
4121 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4122 TEST_LE_U(function_output_length,
4123 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004124 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004125
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 PSA_ASSERT(psa_cipher_finish(&operation,
4127 output2 + output2_length,
4128 output2_buffer_size - output2_length,
4129 &function_output_length));
4130 TEST_LE_U(function_output_length,
4131 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4132 TEST_LE_U(function_output_length,
4133 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004134 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004135
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 PSA_ASSERT(psa_cipher_abort(&operation));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004137 TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004138 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004139
Gilles Peskine50e586b2018-06-08 14:28:46 +02004140exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 psa_cipher_abort(&operation);
4142 mbedtls_free(output1);
4143 mbedtls_free(output2);
4144 psa_destroy_key(key);
4145 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004146}
4147/* END_CASE */
4148
4149/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004150void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4151 data_t *key_data, data_t *iv,
4152 data_t *input,
4153 int first_part_size_arg,
4154 int output1_length_arg, int output2_length_arg,
4155 data_t *expected_output,
4156 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004157{
Ronald Cron5425a212020-08-04 14:58:35 +02004158 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004159 psa_key_type_t key_type = key_type_arg;
4160 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004161 psa_status_t status;
4162 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004163 size_t first_part_size = first_part_size_arg;
4164 size_t output1_length = output1_length_arg;
4165 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004166 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004167 size_t output_buffer_size = 0;
4168 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004169 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004170 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004171 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004172
Gilles Peskine449bd832023-01-11 14:50:10 +01004173 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004174
Gilles Peskine449bd832023-01-11 14:50:10 +01004175 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4176 psa_set_key_algorithm(&attributes, alg);
4177 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004178
Gilles Peskine449bd832023-01-11 14:50:10 +01004179 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4180 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004181
Gilles Peskine449bd832023-01-11 14:50:10 +01004182 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004183
Gilles Peskine449bd832023-01-11 14:50:10 +01004184 if (iv->len > 0) {
4185 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004186 }
4187
Gilles Peskine449bd832023-01-11 14:50:10 +01004188 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4189 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004190 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004191
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 TEST_LE_U(first_part_size, input->len);
4193 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4194 output, output_buffer_size,
4195 &function_output_length));
4196 TEST_ASSERT(function_output_length == output1_length);
4197 TEST_LE_U(function_output_length,
4198 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4199 TEST_LE_U(function_output_length,
4200 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004201 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004202
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 if (first_part_size < input->len) {
4204 PSA_ASSERT(psa_cipher_update(&operation,
4205 input->x + first_part_size,
4206 input->len - first_part_size,
4207 (output_buffer_size == 0 ? NULL :
4208 output + total_output_length),
4209 output_buffer_size - total_output_length,
4210 &function_output_length));
4211 TEST_ASSERT(function_output_length == output2_length);
4212 TEST_LE_U(function_output_length,
4213 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4214 alg,
4215 input->len - first_part_size));
4216 TEST_LE_U(function_output_length,
4217 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004218 total_output_length += function_output_length;
4219 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004220
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 status = psa_cipher_finish(&operation,
4222 (output_buffer_size == 0 ? NULL :
4223 output + total_output_length),
4224 output_buffer_size - total_output_length,
4225 &function_output_length);
4226 TEST_LE_U(function_output_length,
4227 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4228 TEST_LE_U(function_output_length,
4229 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004230 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004232
Gilles Peskine449bd832023-01-11 14:50:10 +01004233 if (expected_status == PSA_SUCCESS) {
4234 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004235
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004236 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004237 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004238 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004239
4240exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004241 psa_cipher_abort(&operation);
4242 mbedtls_free(output);
4243 psa_destroy_key(key);
4244 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004245}
4246/* END_CASE */
4247
4248/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004249void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4250 data_t *key_data, data_t *iv,
4251 data_t *input,
4252 int first_part_size_arg,
4253 int output1_length_arg, int output2_length_arg,
4254 data_t *expected_output,
4255 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004256{
Ronald Cron5425a212020-08-04 14:58:35 +02004257 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004258 psa_key_type_t key_type = key_type_arg;
4259 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004260 psa_status_t status;
4261 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004262 size_t first_part_size = first_part_size_arg;
4263 size_t output1_length = output1_length_arg;
4264 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004265 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004266 size_t output_buffer_size = 0;
4267 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004268 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004269 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004270 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004271
Gilles Peskine449bd832023-01-11 14:50:10 +01004272 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004273
Gilles Peskine449bd832023-01-11 14:50:10 +01004274 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4275 psa_set_key_algorithm(&attributes, alg);
4276 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004277
Gilles Peskine449bd832023-01-11 14:50:10 +01004278 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4279 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004280
Gilles Peskine449bd832023-01-11 14:50:10 +01004281 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004282
Gilles Peskine449bd832023-01-11 14:50:10 +01004283 if (iv->len > 0) {
4284 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004285 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004286
Gilles Peskine449bd832023-01-11 14:50:10 +01004287 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4288 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004289 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004290
Gilles Peskine449bd832023-01-11 14:50:10 +01004291 TEST_LE_U(first_part_size, input->len);
4292 PSA_ASSERT(psa_cipher_update(&operation,
4293 input->x, first_part_size,
4294 output, output_buffer_size,
4295 &function_output_length));
4296 TEST_ASSERT(function_output_length == output1_length);
4297 TEST_LE_U(function_output_length,
4298 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4299 TEST_LE_U(function_output_length,
4300 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004301 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004302
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 if (first_part_size < input->len) {
4304 PSA_ASSERT(psa_cipher_update(&operation,
4305 input->x + first_part_size,
4306 input->len - first_part_size,
4307 (output_buffer_size == 0 ? NULL :
4308 output + total_output_length),
4309 output_buffer_size - total_output_length,
4310 &function_output_length));
4311 TEST_ASSERT(function_output_length == output2_length);
4312 TEST_LE_U(function_output_length,
4313 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4314 alg,
4315 input->len - first_part_size));
4316 TEST_LE_U(function_output_length,
4317 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004318 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004319 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004320
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 status = psa_cipher_finish(&operation,
4322 (output_buffer_size == 0 ? NULL :
4323 output + total_output_length),
4324 output_buffer_size - total_output_length,
4325 &function_output_length);
4326 TEST_LE_U(function_output_length,
4327 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4328 TEST_LE_U(function_output_length,
4329 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004330 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004331 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004332
Gilles Peskine449bd832023-01-11 14:50:10 +01004333 if (expected_status == PSA_SUCCESS) {
4334 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004335
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004336 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004337 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004338 }
4339
Gilles Peskine50e586b2018-06-08 14:28:46 +02004340exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004341 psa_cipher_abort(&operation);
4342 mbedtls_free(output);
4343 psa_destroy_key(key);
4344 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004345}
4346/* END_CASE */
4347
Gilles Peskine50e586b2018-06-08 14:28:46 +02004348/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004349void cipher_decrypt_fail(int alg_arg,
4350 int key_type_arg,
4351 data_t *key_data,
4352 data_t *iv,
4353 data_t *input_arg,
4354 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004355{
4356 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4357 psa_status_t status;
4358 psa_key_type_t key_type = key_type_arg;
4359 psa_algorithm_t alg = alg_arg;
4360 psa_status_t expected_status = expected_status_arg;
4361 unsigned char *input = NULL;
4362 size_t input_buffer_size = 0;
4363 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004364 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004365 size_t output_buffer_size = 0;
4366 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004367 size_t function_output_length;
4368 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004369 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4370
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 if (PSA_ERROR_BAD_STATE != expected_status) {
4372 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004373
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4375 psa_set_key_algorithm(&attributes, alg);
4376 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004377
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4379 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004380 }
4381
4382 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004383 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4384 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004385 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 memcpy(input, iv->x, iv->len);
4387 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004388 }
4389
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004391 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004392
Neil Armstrong66a479f2022-02-07 15:41:19 +01004393 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4395 output_buffer_size, &output_length);
4396 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004397
Neil Armstrong66a479f2022-02-07 15:41:19 +01004398 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 status = psa_cipher_decrypt_setup(&operation, key, alg);
4400 if (status == PSA_SUCCESS) {
4401 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4402 input_arg->len) +
4403 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004404 TEST_CALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004405
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 if (iv->len > 0) {
4407 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004408
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 if (status != PSA_SUCCESS) {
4410 TEST_EQUAL(status, expected_status);
4411 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004412 }
4413
Gilles Peskine449bd832023-01-11 14:50:10 +01004414 if (status == PSA_SUCCESS) {
4415 status = psa_cipher_update(&operation,
4416 input_arg->x, input_arg->len,
4417 output_multi, output_buffer_size,
4418 &function_output_length);
4419 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004420 output_length = function_output_length;
4421
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 status = psa_cipher_finish(&operation,
4423 output_multi + output_length,
4424 output_buffer_size - output_length,
4425 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004426
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 TEST_EQUAL(status, expected_status);
4428 } else {
4429 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004430 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004431 } else {
4432 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004433 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004434 } else {
4435 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004436 }
4437
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004438exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 psa_cipher_abort(&operation);
4440 mbedtls_free(input);
4441 mbedtls_free(output);
4442 mbedtls_free(output_multi);
4443 psa_destroy_key(key);
4444 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004445}
4446/* END_CASE */
4447
4448/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004449void cipher_decrypt(int alg_arg,
4450 int key_type_arg,
4451 data_t *key_data,
4452 data_t *iv,
4453 data_t *input_arg,
4454 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004455{
4456 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4457 psa_key_type_t key_type = key_type_arg;
4458 psa_algorithm_t alg = alg_arg;
4459 unsigned char *input = NULL;
4460 size_t input_buffer_size = 0;
4461 unsigned char *output = NULL;
4462 size_t output_buffer_size = 0;
4463 size_t output_length = 0;
4464 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4465
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004467
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4469 psa_set_key_algorithm(&attributes, alg);
4470 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004471
4472 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004473 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4474 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004475 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004476 memcpy(input, iv->x, iv->len);
4477 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004478 }
4479
Gilles Peskine449bd832023-01-11 14:50:10 +01004480 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004481 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004482
Gilles Peskine449bd832023-01-11 14:50:10 +01004483 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4484 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004485
Gilles Peskine449bd832023-01-11 14:50:10 +01004486 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4487 output_buffer_size, &output_length));
4488 TEST_LE_U(output_length,
4489 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4490 TEST_LE_U(output_length,
4491 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004492
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004493 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004494 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004495exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004496 mbedtls_free(input);
4497 mbedtls_free(output);
4498 psa_destroy_key(key);
4499 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004500}
4501/* END_CASE */
4502
4503/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004504void cipher_verify_output(int alg_arg,
4505 int key_type_arg,
4506 data_t *key_data,
4507 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004508{
Ronald Cron5425a212020-08-04 14:58:35 +02004509 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004510 psa_key_type_t key_type = key_type_arg;
4511 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004512 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004513 size_t output1_size = 0;
4514 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004515 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004516 size_t output2_size = 0;
4517 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004518 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004519
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004521
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4523 psa_set_key_algorithm(&attributes, alg);
4524 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004525
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4527 &key));
4528 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004529 TEST_CALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004530
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4532 output1, output1_size,
4533 &output1_length));
4534 TEST_LE_U(output1_length,
4535 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4536 TEST_LE_U(output1_length,
4537 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004538
4539 output2_size = output1_length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004540 TEST_CALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004541
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4543 output2, output2_size,
4544 &output2_length));
4545 TEST_LE_U(output2_length,
4546 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4547 TEST_LE_U(output2_length,
4548 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004549
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004550 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004551
4552exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 mbedtls_free(output1);
4554 mbedtls_free(output2);
4555 psa_destroy_key(key);
4556 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004557}
4558/* END_CASE */
4559
4560/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004561void cipher_verify_output_multipart(int alg_arg,
4562 int key_type_arg,
4563 data_t *key_data,
4564 data_t *input,
4565 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004566{
Ronald Cron5425a212020-08-04 14:58:35 +02004567 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004568 psa_key_type_t key_type = key_type_arg;
4569 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004570 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004572 size_t iv_size = 16;
4573 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004574 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004575 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004576 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004577 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004578 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004579 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004580 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004581 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4582 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004583 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004584
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004586
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4588 psa_set_key_algorithm(&attributes, alg);
4589 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004590
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4592 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004593
Gilles Peskine449bd832023-01-11 14:50:10 +01004594 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4595 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004596
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 if (alg != PSA_ALG_ECB_NO_PADDING) {
4598 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4599 iv, iv_size,
4600 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004601 }
4602
Gilles Peskine449bd832023-01-11 14:50:10 +01004603 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4604 TEST_LE_U(output1_buffer_size,
4605 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004606 TEST_CALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004607
Gilles Peskine449bd832023-01-11 14:50:10 +01004608 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004609
Gilles Peskine449bd832023-01-11 14:50:10 +01004610 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4611 output1, output1_buffer_size,
4612 &function_output_length));
4613 TEST_LE_U(function_output_length,
4614 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4615 TEST_LE_U(function_output_length,
4616 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004617 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004618
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 PSA_ASSERT(psa_cipher_update(&operation1,
4620 input->x + first_part_size,
4621 input->len - first_part_size,
4622 output1, output1_buffer_size,
4623 &function_output_length));
4624 TEST_LE_U(function_output_length,
4625 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4626 alg,
4627 input->len - first_part_size));
4628 TEST_LE_U(function_output_length,
4629 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004630 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004631
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 PSA_ASSERT(psa_cipher_finish(&operation1,
4633 output1 + output1_length,
4634 output1_buffer_size - output1_length,
4635 &function_output_length));
4636 TEST_LE_U(function_output_length,
4637 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4638 TEST_LE_U(function_output_length,
4639 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004640 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004641
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004643
Gilles Peskine048b7f02018-06-08 14:20:49 +02004644 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004645 TEST_LE_U(output2_buffer_size,
4646 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4647 TEST_LE_U(output2_buffer_size,
4648 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004649 TEST_CALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004650
Gilles Peskine449bd832023-01-11 14:50:10 +01004651 if (iv_length > 0) {
4652 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4653 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004654 }
Moran Pekerded84402018-06-06 16:36:50 +03004655
Gilles Peskine449bd832023-01-11 14:50:10 +01004656 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4657 output2, output2_buffer_size,
4658 &function_output_length));
4659 TEST_LE_U(function_output_length,
4660 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4661 TEST_LE_U(function_output_length,
4662 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004663 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004664
Gilles Peskine449bd832023-01-11 14:50:10 +01004665 PSA_ASSERT(psa_cipher_update(&operation2,
4666 output1 + first_part_size,
4667 output1_length - first_part_size,
4668 output2, output2_buffer_size,
4669 &function_output_length));
4670 TEST_LE_U(function_output_length,
4671 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4672 alg,
4673 output1_length - first_part_size));
4674 TEST_LE_U(function_output_length,
4675 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004676 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004677
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 PSA_ASSERT(psa_cipher_finish(&operation2,
4679 output2 + output2_length,
4680 output2_buffer_size - output2_length,
4681 &function_output_length));
4682 TEST_LE_U(function_output_length,
4683 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4684 TEST_LE_U(function_output_length,
4685 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004686 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004687
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004689
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004690 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004691
4692exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 psa_cipher_abort(&operation1);
4694 psa_cipher_abort(&operation2);
4695 mbedtls_free(output1);
4696 mbedtls_free(output2);
4697 psa_destroy_key(key);
4698 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004699}
4700/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004701
Gilles Peskine20035e32018-02-03 22:44:14 +01004702/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004703void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4704 int alg_arg,
4705 data_t *nonce,
4706 data_t *additional_data,
4707 data_t *input_data,
4708 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004709{
Ronald Cron5425a212020-08-04 14:58:35 +02004710 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004711 psa_key_type_t key_type = key_type_arg;
4712 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004713 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004714 unsigned char *output_data = NULL;
4715 size_t output_size = 0;
4716 size_t output_length = 0;
4717 unsigned char *output_data2 = NULL;
4718 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004719 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004720 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004721 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004722
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004724
Gilles Peskine449bd832023-01-11 14:50:10 +01004725 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4726 psa_set_key_algorithm(&attributes, alg);
4727 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004728
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4730 &key));
4731 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4732 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004733
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4735 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004736 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4737 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4739 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4740 TEST_EQUAL(output_size,
4741 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4742 TEST_LE_U(output_size,
4743 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004744 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004745 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004746
Gilles Peskine449bd832023-01-11 14:50:10 +01004747 status = psa_aead_encrypt(key, alg,
4748 nonce->x, nonce->len,
4749 additional_data->x,
4750 additional_data->len,
4751 input_data->x, input_data->len,
4752 output_data, output_size,
4753 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004754
4755 /* If the operation is not supported, just skip and not fail in case the
4756 * encryption involves a common limitation of cryptography hardwares and
4757 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 if (status == PSA_ERROR_NOT_SUPPORTED) {
4759 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4760 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004761 }
4762
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004764
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 if (PSA_SUCCESS == expected_result) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004766 TEST_CALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004767
Gilles Peskine003a4a92019-05-14 16:09:40 +02004768 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4769 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 TEST_EQUAL(input_data->len,
4771 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004772
Gilles Peskine449bd832023-01-11 14:50:10 +01004773 TEST_LE_U(input_data->len,
4774 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004775
Gilles Peskine449bd832023-01-11 14:50:10 +01004776 TEST_EQUAL(psa_aead_decrypt(key, alg,
4777 nonce->x, nonce->len,
4778 additional_data->x,
4779 additional_data->len,
4780 output_data, output_length,
4781 output_data2, output_length,
4782 &output_length2),
4783 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004784
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004785 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004786 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004787 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004788
Gilles Peskinea1cac842018-06-11 19:33:02 +02004789exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004790 psa_destroy_key(key);
4791 mbedtls_free(output_data);
4792 mbedtls_free(output_data2);
4793 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004794}
4795/* END_CASE */
4796
4797/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004798void aead_encrypt(int key_type_arg, data_t *key_data,
4799 int alg_arg,
4800 data_t *nonce,
4801 data_t *additional_data,
4802 data_t *input_data,
4803 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004804{
Ronald Cron5425a212020-08-04 14:58:35 +02004805 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004806 psa_key_type_t key_type = key_type_arg;
4807 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004808 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004809 unsigned char *output_data = NULL;
4810 size_t output_size = 0;
4811 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004812 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004813 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004814
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004816
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4818 psa_set_key_algorithm(&attributes, alg);
4819 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004820
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4822 &key));
4823 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4824 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004825
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4827 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004828 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4829 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004830 TEST_EQUAL(output_size,
4831 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4832 TEST_LE_U(output_size,
4833 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004834 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004835
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 status = psa_aead_encrypt(key, alg,
4837 nonce->x, nonce->len,
4838 additional_data->x, additional_data->len,
4839 input_data->x, input_data->len,
4840 output_data, output_size,
4841 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004842
Ronald Cron28a45ed2021-02-09 20:35:42 +01004843 /* If the operation is not supported, just skip and not fail in case the
4844 * encryption involves a common limitation of cryptography hardwares and
4845 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004846 if (status == PSA_ERROR_NOT_SUPPORTED) {
4847 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4848 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004849 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004850
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 PSA_ASSERT(status);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004852 TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004853 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004854
Gilles Peskinea1cac842018-06-11 19:33:02 +02004855exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004856 psa_destroy_key(key);
4857 mbedtls_free(output_data);
4858 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004859}
4860/* END_CASE */
4861
4862/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004863void aead_decrypt(int key_type_arg, data_t *key_data,
4864 int alg_arg,
4865 data_t *nonce,
4866 data_t *additional_data,
4867 data_t *input_data,
4868 data_t *expected_data,
4869 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004870{
Ronald Cron5425a212020-08-04 14:58:35 +02004871 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004872 psa_key_type_t key_type = key_type_arg;
4873 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004874 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004875 unsigned char *output_data = NULL;
4876 size_t output_size = 0;
4877 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004878 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004879 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004880 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004881
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004883
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4885 psa_set_key_algorithm(&attributes, alg);
4886 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004887
Gilles Peskine449bd832023-01-11 14:50:10 +01004888 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4889 &key));
4890 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4891 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004892
Gilles Peskine449bd832023-01-11 14:50:10 +01004893 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4894 alg);
4895 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4896 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004897 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4898 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 TEST_EQUAL(output_size,
4900 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4901 TEST_LE_U(output_size,
4902 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004903 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004904 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004905
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 status = psa_aead_decrypt(key, alg,
4907 nonce->x, nonce->len,
4908 additional_data->x,
4909 additional_data->len,
4910 input_data->x, input_data->len,
4911 output_data, output_size,
4912 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004913
Ronald Cron28a45ed2021-02-09 20:35:42 +01004914 /* If the operation is not supported, just skip and not fail in case the
4915 * decryption involves a common limitation of cryptography hardwares and
4916 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 if (status == PSA_ERROR_NOT_SUPPORTED) {
4918 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4919 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004920 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004921
Gilles Peskine449bd832023-01-11 14:50:10 +01004922 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004923
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 if (expected_result == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004925 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004926 output_data, output_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004928
Gilles Peskinea1cac842018-06-11 19:33:02 +02004929exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004930 psa_destroy_key(key);
4931 mbedtls_free(output_data);
4932 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004933}
4934/* END_CASE */
4935
4936/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004937void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4938 int alg_arg,
4939 data_t *nonce,
4940 data_t *additional_data,
4941 data_t *input_data,
4942 int do_set_lengths,
4943 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004944{
Paul Elliottd3f82412021-06-16 16:52:21 +01004945 size_t ad_part_len = 0;
4946 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004947 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004948
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4950 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004951
Gilles Peskine449bd832023-01-11 14:50:10 +01004952 if (do_set_lengths) {
4953 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004954 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004955 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004956 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004957 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004958 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004959
4960 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 if (!aead_multipart_internal_func(key_type_arg, key_data,
4962 alg_arg, nonce,
4963 additional_data,
4964 ad_part_len,
4965 input_data, -1,
4966 set_lengths_method,
4967 expected_output,
4968 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004969 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004970 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004971
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 /* length(0) part, length(ad_part_len) part, length(0) part... */
4973 mbedtls_test_set_step(1000 + ad_part_len);
4974
4975 if (!aead_multipart_internal_func(key_type_arg, key_data,
4976 alg_arg, nonce,
4977 additional_data,
4978 ad_part_len,
4979 input_data, -1,
4980 set_lengths_method,
4981 expected_output,
4982 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004983 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004984 }
4985 }
4986
4987 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4988 /* Split data into length(data_part_len) parts. */
4989 mbedtls_test_set_step(2000 + data_part_len);
4990
4991 if (do_set_lengths) {
4992 if (data_part_len & 0x01) {
4993 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4994 } else {
4995 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4996 }
4997 }
4998
4999 if (!aead_multipart_internal_func(key_type_arg, key_data,
5000 alg_arg, nonce,
5001 additional_data, -1,
5002 input_data, data_part_len,
5003 set_lengths_method,
5004 expected_output,
5005 1, 0)) {
5006 break;
5007 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005008
5009 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005010 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005011
Gilles Peskine449bd832023-01-11 14:50:10 +01005012 if (!aead_multipart_internal_func(key_type_arg, key_data,
5013 alg_arg, nonce,
5014 additional_data, -1,
5015 input_data, data_part_len,
5016 set_lengths_method,
5017 expected_output,
5018 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005019 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005020 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005021 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005022
Paul Elliott8fc45162021-06-23 16:06:01 +01005023 /* Goto is required to silence warnings about unused labels, as we
5024 * don't actually do any test assertions in this function. */
5025 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005026}
5027/* END_CASE */
5028
5029/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005030void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5031 int alg_arg,
5032 data_t *nonce,
5033 data_t *additional_data,
5034 data_t *input_data,
5035 int do_set_lengths,
5036 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005037{
Paul Elliottd3f82412021-06-16 16:52:21 +01005038 size_t ad_part_len = 0;
5039 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005040 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005041
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005043 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005045
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 if (do_set_lengths) {
5047 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005048 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005049 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005050 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005052 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005053
Gilles Peskine449bd832023-01-11 14:50:10 +01005054 if (!aead_multipart_internal_func(key_type_arg, key_data,
5055 alg_arg, nonce,
5056 additional_data,
5057 ad_part_len,
5058 input_data, -1,
5059 set_lengths_method,
5060 expected_output,
5061 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005062 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005063 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005064
5065 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005067
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 if (!aead_multipart_internal_func(key_type_arg, key_data,
5069 alg_arg, nonce,
5070 additional_data,
5071 ad_part_len,
5072 input_data, -1,
5073 set_lengths_method,
5074 expected_output,
5075 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005076 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005077 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005078 }
5079
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005081 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005082 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005083
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 if (do_set_lengths) {
5085 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005086 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005087 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005088 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005089 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005090 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005091
Gilles Peskine449bd832023-01-11 14:50:10 +01005092 if (!aead_multipart_internal_func(key_type_arg, key_data,
5093 alg_arg, nonce,
5094 additional_data, -1,
5095 input_data, data_part_len,
5096 set_lengths_method,
5097 expected_output,
5098 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005099 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005101
5102 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005103 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005104
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 if (!aead_multipart_internal_func(key_type_arg, key_data,
5106 alg_arg, nonce,
5107 additional_data, -1,
5108 input_data, data_part_len,
5109 set_lengths_method,
5110 expected_output,
5111 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005112 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005114 }
5115
Paul Elliott8fc45162021-06-23 16:06:01 +01005116 /* Goto is required to silence warnings about unused labels, as we
5117 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005118 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005119}
5120/* END_CASE */
5121
5122/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005123void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5124 int alg_arg,
5125 int nonce_length,
5126 int expected_nonce_length_arg,
5127 data_t *additional_data,
5128 data_t *input_data,
5129 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005130{
5131
5132 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5133 psa_key_type_t key_type = key_type_arg;
5134 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005135 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005136 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5137 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5138 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005139 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005140 size_t actual_nonce_length = 0;
5141 size_t expected_nonce_length = expected_nonce_length_arg;
5142 unsigned char *output = NULL;
5143 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005144 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005145 size_t ciphertext_size = 0;
5146 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005147 size_t tag_length = 0;
5148 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005151
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5153 psa_set_key_algorithm(&attributes, alg);
5154 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005155
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5157 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005158
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005160
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005162
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005163 TEST_CALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005164
Gilles Peskine449bd832023-01-11 14:50:10 +01005165 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005166
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005168
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005169 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005170
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005172
5173 /* If the operation is not supported, just skip and not fail in case the
5174 * encryption involves a common limitation of cryptography hardwares and
5175 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005176 if (status == PSA_ERROR_NOT_SUPPORTED) {
5177 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5178 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005179 }
5180
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005182
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5184 nonce_length,
5185 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005186
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005188
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005190
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 if (expected_status == PSA_SUCCESS) {
5192 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5193 alg));
5194 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005195
Gilles Peskine449bd832023-01-11 14:50:10 +01005196 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005197
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005199 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005200 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5201 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005202
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5204 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005205
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5207 output, output_size,
5208 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005209
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5211 &ciphertext_length, tag_buffer,
5212 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005213 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005214
5215exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 psa_destroy_key(key);
5217 mbedtls_free(output);
5218 mbedtls_free(ciphertext);
5219 psa_aead_abort(&operation);
5220 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005221}
5222/* END_CASE */
5223
5224/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005225void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5226 int alg_arg,
5227 int nonce_length_arg,
5228 int set_lengths_method_arg,
5229 data_t *additional_data,
5230 data_t *input_data,
5231 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005232{
5233
5234 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5235 psa_key_type_t key_type = key_type_arg;
5236 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005237 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005238 uint8_t *nonce_buffer = NULL;
5239 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5240 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5241 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005242 unsigned char *output = NULL;
5243 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005244 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005245 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005246 size_t ciphertext_size = 0;
5247 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005248 size_t tag_length = 0;
5249 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005250 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005251 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005252
Gilles Peskine449bd832023-01-11 14:50:10 +01005253 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005254
Gilles Peskine449bd832023-01-11 14:50:10 +01005255 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5256 psa_set_key_algorithm(&attributes, alg);
5257 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005258
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5260 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005261
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005263
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005265
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005266 TEST_CALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005267
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005269
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005271
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005272 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005273
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005275
5276 /* If the operation is not supported, just skip and not fail in case the
5277 * encryption involves a common limitation of cryptography hardwares and
5278 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 if (status == PSA_ERROR_NOT_SUPPORTED) {
5280 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5281 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005282 }
5283
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005285
Paul Elliott4023ffd2021-09-10 16:21:22 +01005286 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 if (nonce_length_arg == -1) {
5288 /* Arbitrary size buffer, to test zero length valid buffer. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005289 TEST_CALLOC(nonce_buffer, 4);
Gilles Peskine449bd832023-01-11 14:50:10 +01005290 nonce_length = 0;
5291 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005292 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005293 nonce_length = (size_t) nonce_length_arg;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005294 TEST_CALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005295
Gilles Peskine449bd832023-01-11 14:50:10 +01005296 if (nonce_buffer) {
5297 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005298 nonce_buffer[index] = 'a' + index;
5299 }
Paul Elliott66696b52021-08-16 18:42:41 +01005300 }
Paul Elliott863864a2021-07-23 17:28:31 +01005301 }
5302
Gilles Peskine449bd832023-01-11 14:50:10 +01005303 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5304 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5305 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005306 }
5307
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005309
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005311
Gilles Peskine449bd832023-01-11 14:50:10 +01005312 if (expected_status == PSA_SUCCESS) {
5313 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5314 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5315 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005316 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005318 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 }
Paul Elliott863864a2021-07-23 17:28:31 +01005320
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005321 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5323 additional_data->len),
5324 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005325
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5327 output, output_size,
5328 &ciphertext_length),
5329 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005330
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5332 &ciphertext_length, tag_buffer,
5333 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5334 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005335 }
5336
5337exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005338 psa_destroy_key(key);
5339 mbedtls_free(output);
5340 mbedtls_free(ciphertext);
5341 mbedtls_free(nonce_buffer);
5342 psa_aead_abort(&operation);
5343 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005344}
5345/* END_CASE */
5346
5347/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005348void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005349 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005350 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005351 data_t *nonce,
5352 data_t *additional_data,
5353 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005355{
5356
5357 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5358 psa_key_type_t key_type = key_type_arg;
5359 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005360 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005361 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5362 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5363 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005364 unsigned char *output = NULL;
5365 unsigned char *ciphertext = NULL;
5366 size_t output_size = output_size_arg;
5367 size_t ciphertext_size = 0;
5368 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005369 size_t tag_length = 0;
5370 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5371
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005373
Gilles Peskine449bd832023-01-11 14:50:10 +01005374 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5375 psa_set_key_algorithm(&attributes, alg);
5376 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005377
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5379 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005380
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005382
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005383 TEST_CALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005384
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005386
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005387 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005388
Gilles Peskine449bd832023-01-11 14:50:10 +01005389 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005390
5391 /* If the operation is not supported, just skip and not fail in case the
5392 * encryption involves a common limitation of cryptography hardwares and
5393 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 if (status == PSA_ERROR_NOT_SUPPORTED) {
5395 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5396 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005397 }
5398
Gilles Peskine449bd832023-01-11 14:50:10 +01005399 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005400
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5402 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005403
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005405
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5407 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005408
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 status = psa_aead_update(&operation, input_data->x, input_data->len,
5410 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005411
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005413
Gilles Peskine449bd832023-01-11 14:50:10 +01005414 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005415 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005416 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5417 &ciphertext_length, tag_buffer,
5418 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005419 }
5420
5421exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005422 psa_destroy_key(key);
5423 mbedtls_free(output);
5424 mbedtls_free(ciphertext);
5425 psa_aead_abort(&operation);
5426 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005427}
5428/* END_CASE */
5429
Paul Elliott91b021e2021-07-23 18:52:31 +01005430/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005431void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5432 int alg_arg,
5433 int finish_ciphertext_size_arg,
5434 int tag_size_arg,
5435 data_t *nonce,
5436 data_t *additional_data,
5437 data_t *input_data,
5438 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005439{
5440
5441 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5442 psa_key_type_t key_type = key_type_arg;
5443 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005444 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005445 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5446 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5447 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005448 unsigned char *ciphertext = NULL;
5449 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005450 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005451 size_t ciphertext_size = 0;
5452 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5454 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005455 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005456
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005458
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5460 psa_set_key_algorithm(&attributes, alg);
5461 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005462
Gilles Peskine449bd832023-01-11 14:50:10 +01005463 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5464 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005465
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005467
Gilles Peskine449bd832023-01-11 14:50:10 +01005468 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005469
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005470 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005471
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005472 TEST_CALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005473
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005474 TEST_CALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005475
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005477
5478 /* If the operation is not supported, just skip and not fail in case the
5479 * encryption involves a common limitation of cryptography hardwares and
5480 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 if (status == PSA_ERROR_NOT_SUPPORTED) {
5482 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5483 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005484 }
5485
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005487
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005489
Gilles Peskine449bd832023-01-11 14:50:10 +01005490 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5491 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005492
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5494 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005495
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5497 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005498
5499 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 status = psa_aead_finish(&operation, finish_ciphertext,
5501 finish_ciphertext_size,
5502 &ciphertext_length, tag_buffer,
5503 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005504
Gilles Peskine449bd832023-01-11 14:50:10 +01005505 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005506
5507exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 psa_destroy_key(key);
5509 mbedtls_free(ciphertext);
5510 mbedtls_free(finish_ciphertext);
5511 mbedtls_free(tag_buffer);
5512 psa_aead_abort(&operation);
5513 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005514}
5515/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005516
5517/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005518void aead_multipart_verify(int key_type_arg, data_t *key_data,
5519 int alg_arg,
5520 data_t *nonce,
5521 data_t *additional_data,
5522 data_t *input_data,
5523 data_t *tag,
5524 int tag_usage_arg,
5525 int expected_setup_status_arg,
5526 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005527{
5528 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5529 psa_key_type_t key_type = key_type_arg;
5530 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005531 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005532 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5533 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5534 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005535 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005536 unsigned char *plaintext = NULL;
5537 unsigned char *finish_plaintext = NULL;
5538 size_t plaintext_size = 0;
5539 size_t plaintext_length = 0;
5540 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005541 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005542 unsigned char *tag_buffer = NULL;
5543 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005544
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005546
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5548 psa_set_key_algorithm(&attributes, alg);
5549 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005550
Gilles Peskine449bd832023-01-11 14:50:10 +01005551 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5552 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005553
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005555
Gilles Peskine449bd832023-01-11 14:50:10 +01005556 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5557 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005558
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005559 TEST_CALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005560
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005562
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005563 TEST_CALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005564
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005566
5567 /* If the operation is not supported, just skip and not fail in case the
5568 * encryption involves a common limitation of cryptography hardwares and
5569 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005570 if (status == PSA_ERROR_NOT_SUPPORTED) {
5571 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5572 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005573 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005575
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005577 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 }
Paul Elliott9961a662021-09-17 19:19:02 +01005579
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005581
Gilles Peskine449bd832023-01-11 14:50:10 +01005582 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005583
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 status = psa_aead_set_lengths(&operation, additional_data->len,
5585 input_data->len);
5586 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005587
Gilles Peskine449bd832023-01-11 14:50:10 +01005588 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5589 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005590
Gilles Peskine449bd832023-01-11 14:50:10 +01005591 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5592 input_data->len,
5593 plaintext, plaintext_size,
5594 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005595
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005597 tag_buffer = tag->x;
5598 tag_size = tag->len;
5599 }
5600
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 status = psa_aead_verify(&operation, finish_plaintext,
5602 verify_plaintext_size,
5603 &plaintext_length,
5604 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005605
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005607
5608exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005609 psa_destroy_key(key);
5610 mbedtls_free(plaintext);
5611 mbedtls_free(finish_plaintext);
5612 psa_aead_abort(&operation);
5613 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005614}
5615/* END_CASE */
5616
Paul Elliott9961a662021-09-17 19:19:02 +01005617/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005618void aead_multipart_setup(int key_type_arg, data_t *key_data,
5619 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005620{
5621 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5622 psa_key_type_t key_type = key_type_arg;
5623 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005624 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005625 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5626 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5627 psa_status_t expected_status = expected_status_arg;
5628
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005630
Gilles Peskine449bd832023-01-11 14:50:10 +01005631 psa_set_key_usage_flags(&attributes,
5632 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5633 psa_set_key_algorithm(&attributes, alg);
5634 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005635
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5637 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005638
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005640
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005642
Gilles Peskine449bd832023-01-11 14:50:10 +01005643 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005644
Gilles Peskine449bd832023-01-11 14:50:10 +01005645 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005646
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005648
5649exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005650 psa_destroy_key(key);
5651 psa_aead_abort(&operation);
5652 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005653}
5654/* END_CASE */
5655
5656/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005657void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5658 int alg_arg,
5659 data_t *nonce,
5660 data_t *additional_data,
5661 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005662{
5663 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5664 psa_key_type_t key_type = key_type_arg;
5665 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005666 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005667 unsigned char *output_data = NULL;
5668 unsigned char *final_data = NULL;
5669 size_t output_size = 0;
5670 size_t finish_output_size = 0;
5671 size_t output_length = 0;
5672 size_t key_bits = 0;
5673 size_t tag_length = 0;
5674 size_t tag_size = 0;
5675 size_t nonce_length = 0;
5676 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5677 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5678 size_t output_part_length = 0;
5679 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5680
Gilles Peskine449bd832023-01-11 14:50:10 +01005681 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005682
Gilles Peskine449bd832023-01-11 14:50:10 +01005683 psa_set_key_usage_flags(&attributes,
5684 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5685 psa_set_key_algorithm(&attributes, alg);
5686 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005687
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5689 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005690
Gilles Peskine449bd832023-01-11 14:50:10 +01005691 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5692 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005693
Gilles Peskine449bd832023-01-11 14:50:10 +01005694 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005695
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005697
Gilles Peskine449bd832023-01-11 14:50:10 +01005698 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005699
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005700 TEST_CALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005701
Gilles Peskine449bd832023-01-11 14:50:10 +01005702 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005703
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005705
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005706 TEST_CALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005707
5708 /* Test all operations error without calling setup first. */
5709
Gilles Peskine449bd832023-01-11 14:50:10 +01005710 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5711 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005712
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005714
Gilles Peskine449bd832023-01-11 14:50:10 +01005715 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5716 PSA_AEAD_NONCE_MAX_SIZE,
5717 &nonce_length),
5718 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005719
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005721
Paul Elliott481be342021-07-16 17:38:47 +01005722 /* ------------------------------------------------------- */
5723
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5725 input_data->len),
5726 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005727
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005729
Paul Elliott481be342021-07-16 17:38:47 +01005730 /* ------------------------------------------------------- */
5731
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5733 additional_data->len),
5734 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005735
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005737
Paul Elliott481be342021-07-16 17:38:47 +01005738 /* ------------------------------------------------------- */
5739
Gilles Peskine449bd832023-01-11 14:50:10 +01005740 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5741 input_data->len, output_data,
5742 output_size, &output_length),
5743 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005744
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005746
Paul Elliott481be342021-07-16 17:38:47 +01005747 /* ------------------------------------------------------- */
5748
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 Elliottc23a9a02021-06-21 18:32:46 +01005755
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005757
Paul Elliott481be342021-07-16 17:38:47 +01005758 /* ------------------------------------------------------- */
5759
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5761 finish_output_size,
5762 &output_part_length,
5763 tag_buffer,
5764 tag_length),
5765 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005766
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005768
5769 /* Test for double setups. */
5770
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005772
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5774 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005775
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005777
Paul Elliott481be342021-07-16 17:38:47 +01005778 /* ------------------------------------------------------- */
5779
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005781
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5783 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005784
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005786
Paul Elliott374a2be2021-07-16 17:53:40 +01005787 /* ------------------------------------------------------- */
5788
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005790
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5792 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005793
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005795
5796 /* ------------------------------------------------------- */
5797
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005799
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5801 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005802
Gilles Peskine449bd832023-01-11 14:50:10 +01005803 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005804
Paul Elliottc23a9a02021-06-21 18:32:46 +01005805 /* Test for not setting a nonce. */
5806
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005808
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5810 additional_data->len),
5811 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005812
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005814
Paul Elliott7f628422021-09-01 12:08:29 +01005815 /* ------------------------------------------------------- */
5816
Gilles Peskine449bd832023-01-11 14:50:10 +01005817 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005818
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5820 input_data->len, output_data,
5821 output_size, &output_length),
5822 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005823
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005825
Paul Elliottbdc2c682021-09-21 18:37:10 +01005826 /* ------------------------------------------------------- */
5827
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005829
Gilles Peskine449bd832023-01-11 14:50:10 +01005830 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5831 finish_output_size,
5832 &output_part_length,
5833 tag_buffer, tag_length,
5834 &tag_size),
5835 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005836
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005838
5839 /* ------------------------------------------------------- */
5840
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005842
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5844 finish_output_size,
5845 &output_part_length,
5846 tag_buffer,
5847 tag_length),
5848 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005849
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005851
Paul Elliottc23a9a02021-06-21 18:32:46 +01005852 /* Test for double setting nonce. */
5853
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005855
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005857
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5859 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005860
Gilles Peskine449bd832023-01-11 14:50:10 +01005861 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005862
Paul Elliott374a2be2021-07-16 17:53:40 +01005863 /* Test for double generating nonce. */
5864
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005866
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5868 PSA_AEAD_NONCE_MAX_SIZE,
5869 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005870
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5872 PSA_AEAD_NONCE_MAX_SIZE,
5873 &nonce_length),
5874 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005875
5876
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005878
5879 /* Test for generate nonce then set and vice versa */
5880
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005882
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5884 PSA_AEAD_NONCE_MAX_SIZE,
5885 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005886
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5888 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005889
Gilles Peskine449bd832023-01-11 14:50:10 +01005890 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005891
Andrzej Kurekad837522021-12-15 15:28:49 +01005892 /* Test for generating nonce after calling set lengths */
5893
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005895
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5897 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005898
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5900 PSA_AEAD_NONCE_MAX_SIZE,
5901 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005902
Gilles Peskine449bd832023-01-11 14:50:10 +01005903 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005904
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005905 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005906
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005908
Gilles Peskine449bd832023-01-11 14:50:10 +01005909 if (operation.alg == PSA_ALG_CCM) {
5910 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5911 input_data->len),
5912 PSA_ERROR_INVALID_ARGUMENT);
5913 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5914 PSA_AEAD_NONCE_MAX_SIZE,
5915 &nonce_length),
5916 PSA_ERROR_BAD_STATE);
5917 } else {
5918 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5919 input_data->len));
5920 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5921 PSA_AEAD_NONCE_MAX_SIZE,
5922 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005923 }
5924
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005926
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005927 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005928#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005929 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005930
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5932 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5933 input_data->len),
5934 PSA_ERROR_INVALID_ARGUMENT);
5935 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5936 PSA_AEAD_NONCE_MAX_SIZE,
5937 &nonce_length),
5938 PSA_ERROR_BAD_STATE);
5939 } else {
5940 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5941 input_data->len));
5942 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5943 PSA_AEAD_NONCE_MAX_SIZE,
5944 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005945 }
5946
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005948#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005949
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005950 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005951
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005953
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5955 PSA_AEAD_NONCE_MAX_SIZE,
5956 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005957
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 if (operation.alg == PSA_ALG_CCM) {
5959 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5960 input_data->len),
5961 PSA_ERROR_INVALID_ARGUMENT);
5962 } else {
5963 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5964 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005965 }
5966
Gilles Peskine449bd832023-01-11 14:50:10 +01005967 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005968
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005969 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005970 /* Test for setting nonce after calling set lengths */
5971
Gilles Peskine449bd832023-01-11 14:50:10 +01005972 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005973
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5975 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005976
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005978
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005980
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005981 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005982
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005984
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 if (operation.alg == PSA_ALG_CCM) {
5986 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5987 input_data->len),
5988 PSA_ERROR_INVALID_ARGUMENT);
5989 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5990 PSA_ERROR_BAD_STATE);
5991 } else {
5992 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5993 input_data->len));
5994 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005995 }
5996
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005998
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005999 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00006000#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006002
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
6004 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
6005 input_data->len),
6006 PSA_ERROR_INVALID_ARGUMENT);
6007 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6008 PSA_ERROR_BAD_STATE);
6009 } else {
6010 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6011 input_data->len));
6012 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006013 }
6014
Gilles Peskine449bd832023-01-11 14:50:10 +01006015 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00006016#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006017
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006018 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006019
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006021
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006023
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 if (operation.alg == PSA_ALG_CCM) {
6025 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6026 input_data->len),
6027 PSA_ERROR_INVALID_ARGUMENT);
6028 } else {
6029 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6030 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006031 }
6032
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006034
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006035 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006036#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006037 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006038
Gilles Peskine449bd832023-01-11 14:50:10 +01006039 if (operation.alg == PSA_ALG_GCM) {
6040 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6041 SIZE_MAX),
6042 PSA_ERROR_INVALID_ARGUMENT);
6043 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6044 PSA_ERROR_BAD_STATE);
6045 } else if (operation.alg != PSA_ALG_CCM) {
6046 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6047 SIZE_MAX));
6048 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006049 }
6050
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006052#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006053
Tom Cosgrove1797b052022-12-04 17:19:59 +00006054 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006055#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006057
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006059
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 if (operation.alg == PSA_ALG_GCM) {
6061 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6062 SIZE_MAX),
6063 PSA_ERROR_INVALID_ARGUMENT);
6064 } else if (operation.alg != PSA_ALG_CCM) {
6065 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6066 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006067 }
6068
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006070#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006071
6072 /* ------------------------------------------------------- */
6073
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006075
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006077
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6079 PSA_AEAD_NONCE_MAX_SIZE,
6080 &nonce_length),
6081 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006082
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006084
Paul Elliott7220cae2021-06-22 17:25:57 +01006085 /* Test for generating nonce in decrypt setup. */
6086
Gilles Peskine449bd832023-01-11 14:50:10 +01006087 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006088
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6090 PSA_AEAD_NONCE_MAX_SIZE,
6091 &nonce_length),
6092 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006093
Gilles Peskine449bd832023-01-11 14:50:10 +01006094 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006095
Paul Elliottc23a9a02021-06-21 18:32:46 +01006096 /* Test for setting lengths twice. */
6097
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006099
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006101
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6103 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006104
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6106 input_data->len),
6107 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006108
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006110
Andrzej Kurekad837522021-12-15 15:28:49 +01006111 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006112
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006114
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006116
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006118
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6120 additional_data->len),
6121 PSA_ERROR_BAD_STATE);
6122 } else {
6123 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6124 additional_data->len));
6125
6126 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6127 input_data->len),
6128 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006129 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006131
6132 /* ------------------------------------------------------- */
6133
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006135
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006137
Gilles Peskine449bd832023-01-11 14:50:10 +01006138 if (operation.alg == PSA_ALG_CCM) {
6139 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6140 input_data->len, output_data,
6141 output_size, &output_length),
6142 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006143
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 } else {
6145 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6146 input_data->len, output_data,
6147 output_size, &output_length));
6148
6149 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6150 input_data->len),
6151 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006152 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006154
6155 /* ------------------------------------------------------- */
6156
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006158
Gilles Peskine449bd832023-01-11 14:50:10 +01006159 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006160
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 if (operation.alg == PSA_ALG_CCM) {
6162 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6163 finish_output_size,
6164 &output_part_length,
6165 tag_buffer, tag_length,
6166 &tag_size));
6167 } else {
6168 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6169 finish_output_size,
6170 &output_part_length,
6171 tag_buffer, tag_length,
6172 &tag_size));
6173
6174 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6175 input_data->len),
6176 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006177 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006179
6180 /* Test for setting lengths after generating nonce + already starting data. */
6181
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006183
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6185 PSA_AEAD_NONCE_MAX_SIZE,
6186 &nonce_length));
6187 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006188
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6190 additional_data->len),
6191 PSA_ERROR_BAD_STATE);
6192 } else {
6193 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6194 additional_data->len));
6195
6196 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6197 input_data->len),
6198 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006199 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006201
6202 /* ------------------------------------------------------- */
6203
Gilles Peskine449bd832023-01-11 14:50:10 +01006204 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006205
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6207 PSA_AEAD_NONCE_MAX_SIZE,
6208 &nonce_length));
6209 if (operation.alg == PSA_ALG_CCM) {
6210 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6211 input_data->len, output_data,
6212 output_size, &output_length),
6213 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006214
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 } else {
6216 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6217 input_data->len, output_data,
6218 output_size, &output_length));
6219
6220 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6221 input_data->len),
6222 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006223 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006225
6226 /* ------------------------------------------------------- */
6227
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006229
Gilles Peskine449bd832023-01-11 14:50:10 +01006230 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6231 PSA_AEAD_NONCE_MAX_SIZE,
6232 &nonce_length));
6233 if (operation.alg == PSA_ALG_CCM) {
6234 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6235 finish_output_size,
6236 &output_part_length,
6237 tag_buffer, tag_length,
6238 &tag_size));
6239 } else {
6240 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6241 finish_output_size,
6242 &output_part_length,
6243 tag_buffer, tag_length,
6244 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006245
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6247 input_data->len),
6248 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006249 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006251
Paul Elliott243080c2021-07-21 19:01:17 +01006252 /* Test for not sending any additional data or data after setting non zero
6253 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006254
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006256
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006258
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6260 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006261
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6263 finish_output_size,
6264 &output_part_length,
6265 tag_buffer, tag_length,
6266 &tag_size),
6267 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006268
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006270
Paul Elliott243080c2021-07-21 19:01:17 +01006271 /* Test for not sending any additional data or data after setting non-zero
6272 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006273
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006275
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006277
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6279 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006280
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6282 finish_output_size,
6283 &output_part_length,
6284 tag_buffer,
6285 tag_length),
6286 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006287
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006289
Paul Elliott243080c2021-07-21 19:01:17 +01006290 /* Test for not sending any additional data after setting a non-zero length
6291 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006292
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006294
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006296
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6298 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006299
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6301 input_data->len, output_data,
6302 output_size, &output_length),
6303 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006304
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006306
Paul Elliottf94bd992021-09-19 18:15:59 +01006307 /* Test for not sending any data after setting a non-zero length for it.*/
6308
Gilles Peskine449bd832023-01-11 14:50:10 +01006309 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006310
Gilles Peskine449bd832023-01-11 14:50:10 +01006311 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006312
Gilles Peskine449bd832023-01-11 14:50:10 +01006313 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6314 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006315
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6317 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006318
Gilles Peskine449bd832023-01-11 14:50:10 +01006319 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6320 finish_output_size,
6321 &output_part_length,
6322 tag_buffer, tag_length,
6323 &tag_size),
6324 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006325
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006327
Paul Elliottb0450fe2021-09-01 15:06:26 +01006328 /* Test for sending too much additional data after setting lengths. */
6329
Gilles Peskine449bd832023-01-11 14:50:10 +01006330 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006331
Gilles Peskine449bd832023-01-11 14:50:10 +01006332 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006333
Gilles Peskine449bd832023-01-11 14:50:10 +01006334 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006335
6336
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6338 additional_data->len),
6339 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006340
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006342
Paul Elliotta2a09b02021-09-22 14:56:40 +01006343 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006344
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006346
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006348
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6350 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006351
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6353 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006354
Gilles Peskine449bd832023-01-11 14:50:10 +01006355 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6356 1),
6357 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006358
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006360
Paul Elliottb0450fe2021-09-01 15:06:26 +01006361 /* Test for sending too much data after setting lengths. */
6362
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006364
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006366
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006368
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6370 input_data->len, output_data,
6371 output_size, &output_length),
6372 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006373
Gilles Peskine449bd832023-01-11 14:50:10 +01006374 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006375
Paul Elliotta2a09b02021-09-22 14:56:40 +01006376 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006377
Gilles Peskine449bd832023-01-11 14:50:10 +01006378 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006379
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006381
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6383 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006384
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6386 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006387
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6389 input_data->len, output_data,
6390 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006391
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6393 1, output_data,
6394 output_size, &output_length),
6395 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006396
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006398
Paul Elliottc23a9a02021-06-21 18:32:46 +01006399 /* Test sending additional data after data. */
6400
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006402
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006404
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 if (operation.alg != PSA_ALG_CCM) {
6406 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6407 input_data->len, output_data,
6408 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006409
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6411 additional_data->len),
6412 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006413 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006415
Paul Elliott534d0b42021-06-22 19:15:20 +01006416 /* Test calling finish on decryption. */
6417
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006419
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006421
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6423 finish_output_size,
6424 &output_part_length,
6425 tag_buffer, tag_length,
6426 &tag_size),
6427 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006428
Gilles Peskine449bd832023-01-11 14:50:10 +01006429 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006430
6431 /* Test calling verify on encryption. */
6432
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006434
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006436
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6438 finish_output_size,
6439 &output_part_length,
6440 tag_buffer,
6441 tag_length),
6442 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006443
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006445
6446
Paul Elliottc23a9a02021-06-21 18:32:46 +01006447exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 psa_destroy_key(key);
6449 psa_aead_abort(&operation);
6450 mbedtls_free(output_data);
6451 mbedtls_free(final_data);
6452 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006453}
6454/* END_CASE */
6455
6456/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006457void signature_size(int type_arg,
6458 int bits,
6459 int alg_arg,
6460 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006461{
6462 psa_key_type_t type = type_arg;
6463 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006464 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006465
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006467
Gilles Peskinee59236f2018-01-27 23:32:46 +01006468exit:
6469 ;
6470}
6471/* END_CASE */
6472
6473/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006474void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6475 int alg_arg, data_t *input_data,
6476 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006477{
Ronald Cron5425a212020-08-04 14:58:35 +02006478 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006479 psa_key_type_t key_type = key_type_arg;
6480 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006481 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006482 unsigned char *signature = NULL;
6483 size_t signature_size;
6484 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006485 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006486
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006488
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6490 psa_set_key_algorithm(&attributes, alg);
6491 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006492
Gilles Peskine449bd832023-01-11 14:50:10 +01006493 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6494 &key));
6495 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6496 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006497
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006498 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006499 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006500 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6501 key_bits, alg);
6502 TEST_ASSERT(signature_size != 0);
6503 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006504 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006505
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006506 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006507 PSA_ASSERT(psa_sign_hash(key, alg,
6508 input_data->x, input_data->len,
6509 signature, signature_size,
6510 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006511 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006512 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006513 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006514
6515exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006516 /*
6517 * Key attributes may have been returned by psa_get_key_attributes()
6518 * thus reset them as required.
6519 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006520 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006521
Gilles Peskine449bd832023-01-11 14:50:10 +01006522 psa_destroy_key(key);
6523 mbedtls_free(signature);
6524 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006525}
6526/* END_CASE */
6527
Paul Elliott712d5122022-12-07 14:03:10 +00006528/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006529/**
6530 * sign_hash_interruptible() test intentions:
6531 *
6532 * Note: This test can currently only handle ECDSA.
6533 *
6534 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006535 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006536 *
6537 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6538 * expected for different max_ops values.
6539 *
6540 * 3. Test that the number of ops done prior to start and after abort is zero
6541 * and that each successful stage completes some ops (this is not mandated by
6542 * the PSA specification, but is currently the case).
6543 *
6544 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6545 * complete() calls does not alter the number of ops returned.
6546 */
Paul Elliott712d5122022-12-07 14:03:10 +00006547void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6548 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006549 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006550{
6551 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6552 psa_key_type_t key_type = key_type_arg;
6553 psa_algorithm_t alg = alg_arg;
6554 size_t key_bits;
6555 unsigned char *signature = NULL;
6556 size_t signature_size;
6557 size_t signature_length = 0xdeadbeef;
6558 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6559 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006560 uint32_t num_ops = 0;
6561 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006562 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006563 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006564 size_t min_completes = 0;
6565 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006566
Paul Elliott712d5122022-12-07 14:03:10 +00006567 psa_sign_hash_interruptible_operation_t operation =
6568 psa_sign_hash_interruptible_operation_init();
6569
6570 PSA_ASSERT(psa_crypto_init());
6571
6572 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6573 psa_set_key_algorithm(&attributes, alg);
6574 psa_set_key_type(&attributes, key_type);
6575
6576 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6577 &key));
6578 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6579 key_bits = psa_get_key_bits(&attributes);
6580
6581 /* Allocate a buffer which has the size advertised by the
6582 * library. */
6583 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6584 key_bits, alg);
6585 TEST_ASSERT(signature_size != 0);
6586 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006587 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006588
Paul Elliott0c683352022-12-16 19:16:56 +00006589 psa_interruptible_set_max_ops(max_ops);
6590
Paul Elliott6f600372023-02-06 18:41:05 +00006591 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6592 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006593
Paul Elliott712d5122022-12-07 14:03:10 +00006594 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6595 TEST_ASSERT(num_ops_prior == 0);
6596
6597 /* Start performing the signature. */
6598 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6599 input_data->x, input_data->len));
6600
6601 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6602 TEST_ASSERT(num_ops_prior == 0);
6603
6604 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006605 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006606 status = psa_sign_hash_complete(&operation, signature, signature_size,
6607 &signature_length);
6608
Paul Elliott0c683352022-12-16 19:16:56 +00006609 num_completes++;
6610
Paul Elliott712d5122022-12-07 14:03:10 +00006611 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6612 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006613 /* We are asserting here that every complete makes progress
6614 * (completes some ops), which is true of the internal
6615 * implementation and probably any implementation, however this is
6616 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006617 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006618
Paul Elliott712d5122022-12-07 14:03:10 +00006619 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006620
6621 /* Ensure calling get_num_ops() twice still returns the same
6622 * number of ops as previously reported. */
6623 num_ops = psa_sign_hash_get_num_ops(&operation);
6624
6625 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006626 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006627 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006628
6629 TEST_ASSERT(status == PSA_SUCCESS);
6630
Paul Elliott0c683352022-12-16 19:16:56 +00006631 TEST_LE_U(min_completes, num_completes);
6632 TEST_LE_U(num_completes, max_completes);
6633
Paul Elliott712d5122022-12-07 14:03:10 +00006634 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006635 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006636 signature, signature_length);
Paul Elliott712d5122022-12-07 14:03:10 +00006637
6638 PSA_ASSERT(psa_sign_hash_abort(&operation));
6639
Paul Elliott59ad9452022-12-18 15:09:02 +00006640 num_ops = psa_sign_hash_get_num_ops(&operation);
6641 TEST_ASSERT(num_ops == 0);
6642
Paul Elliott712d5122022-12-07 14:03:10 +00006643exit:
6644
6645 /*
6646 * Key attributes may have been returned by psa_get_key_attributes()
6647 * thus reset them as required.
6648 */
6649 psa_reset_key_attributes(&attributes);
6650
6651 psa_destroy_key(key);
6652 mbedtls_free(signature);
6653 PSA_DONE();
6654}
6655/* END_CASE */
6656
Gilles Peskine20035e32018-02-03 22:44:14 +01006657/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006658void sign_hash_fail(int key_type_arg, data_t *key_data,
6659 int alg_arg, data_t *input_data,
6660 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006661{
Ronald Cron5425a212020-08-04 14:58:35 +02006662 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006663 psa_key_type_t key_type = key_type_arg;
6664 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006665 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006666 psa_status_t actual_status;
6667 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006668 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006669 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006670 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006671
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006672 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006673
Gilles Peskine449bd832023-01-11 14:50:10 +01006674 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006675
Gilles Peskine449bd832023-01-11 14:50:10 +01006676 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6677 psa_set_key_algorithm(&attributes, alg);
6678 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006679
Gilles Peskine449bd832023-01-11 14:50:10 +01006680 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6681 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006682
Gilles Peskine449bd832023-01-11 14:50:10 +01006683 actual_status = psa_sign_hash(key, alg,
6684 input_data->x, input_data->len,
6685 signature, signature_size,
6686 &signature_length);
6687 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006688 /* The value of *signature_length is unspecified on error, but
6689 * whatever it is, it should be less than signature_size, so that
6690 * if the caller tries to read *signature_length bytes without
6691 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006692 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006693
6694exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006695 psa_reset_key_attributes(&attributes);
6696 psa_destroy_key(key);
6697 mbedtls_free(signature);
6698 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006699}
6700/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006701
Paul Elliott91007972022-12-16 12:21:24 +00006702/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006703/**
6704 * sign_hash_fail_interruptible() test intentions:
6705 *
6706 * Note: This test can currently only handle ECDSA.
6707 *
6708 * 1. Test that various failure cases for interruptible sign hash fail with the
6709 * correct error codes, and at the correct point (at start or during
6710 * complete).
6711 *
6712 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6713 * expected for different max_ops values.
6714 *
6715 * 3. Test that the number of ops done prior to start and after abort is zero
6716 * and that each successful stage completes some ops (this is not mandated by
6717 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006718 *
6719 * 4. Check that calling complete() when start() fails and complete()
6720 * after completion results in a BAD_STATE error.
6721 *
6722 * 5. Check that calling start() again after start fails results in a BAD_STATE
6723 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006724 */
Paul Elliott91007972022-12-16 12:21:24 +00006725void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6726 int alg_arg, data_t *input_data,
6727 int signature_size_arg,
6728 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006729 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006730 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006731{
6732 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6733 psa_key_type_t key_type = key_type_arg;
6734 psa_algorithm_t alg = alg_arg;
6735 size_t signature_size = signature_size_arg;
6736 psa_status_t actual_status;
6737 psa_status_t expected_start_status = expected_start_status_arg;
6738 psa_status_t expected_complete_status = expected_complete_status_arg;
6739 unsigned char *signature = NULL;
6740 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006741 uint32_t num_ops = 0;
6742 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006743 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006744 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006745 size_t min_completes = 0;
6746 size_t max_completes = 0;
6747
Paul Elliott91007972022-12-16 12:21:24 +00006748 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6749 psa_sign_hash_interruptible_operation_t operation =
6750 psa_sign_hash_interruptible_operation_init();
6751
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006752 TEST_CALLOC(signature, signature_size);
Paul Elliott91007972022-12-16 12:21:24 +00006753
6754 PSA_ASSERT(psa_crypto_init());
6755
6756 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6757 psa_set_key_algorithm(&attributes, alg);
6758 psa_set_key_type(&attributes, key_type);
6759
6760 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6761 &key));
6762
Paul Elliott0c683352022-12-16 19:16:56 +00006763 psa_interruptible_set_max_ops(max_ops);
6764
Paul Elliott6f600372023-02-06 18:41:05 +00006765 interruptible_signverify_get_minmax_completes(max_ops,
6766 expected_complete_status,
6767 &min_completes,
6768 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006769
Paul Elliott91007972022-12-16 12:21:24 +00006770 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6771 TEST_ASSERT(num_ops_prior == 0);
6772
6773 /* Start performing the signature. */
6774 actual_status = psa_sign_hash_start(&operation, key, alg,
6775 input_data->x, input_data->len);
6776
6777 TEST_EQUAL(actual_status, expected_start_status);
6778
Paul Elliottc9774412023-02-06 15:14:07 +00006779 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006780 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006781 * start failed. */
6782 actual_status = psa_sign_hash_complete(&operation, signature,
6783 signature_size,
6784 &signature_length);
6785
6786 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6787
6788 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006789 actual_status = psa_sign_hash_start(&operation, key, alg,
6790 input_data->x, input_data->len);
6791
6792 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6793 }
6794
Paul Elliott91007972022-12-16 12:21:24 +00006795 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6796 TEST_ASSERT(num_ops_prior == 0);
6797
Paul Elliott91007972022-12-16 12:21:24 +00006798 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006799 do {
Paul Elliott91007972022-12-16 12:21:24 +00006800 actual_status = psa_sign_hash_complete(&operation, signature,
6801 signature_size,
6802 &signature_length);
6803
Paul Elliott0c683352022-12-16 19:16:56 +00006804 num_completes++;
6805
Paul Elliott334d7262023-01-20 17:29:41 +00006806 if (actual_status == PSA_SUCCESS ||
6807 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006808 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006809 /* We are asserting here that every complete makes progress
6810 * (completes some ops), which is true of the internal
6811 * implementation and probably any implementation, however this is
6812 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006813 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006814
Paul Elliott91007972022-12-16 12:21:24 +00006815 num_ops_prior = num_ops;
6816 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006817 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006818
Paul Elliottc9774412023-02-06 15:14:07 +00006819 TEST_EQUAL(actual_status, expected_complete_status);
6820
Paul Elliottefebad02023-02-15 16:56:45 +00006821 /* Check that another complete returns BAD_STATE. */
6822 actual_status = psa_sign_hash_complete(&operation, signature,
6823 signature_size,
6824 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006825
Paul Elliottefebad02023-02-15 16:56:45 +00006826 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006827
Paul Elliott91007972022-12-16 12:21:24 +00006828 PSA_ASSERT(psa_sign_hash_abort(&operation));
6829
Paul Elliott59ad9452022-12-18 15:09:02 +00006830 num_ops = psa_sign_hash_get_num_ops(&operation);
6831 TEST_ASSERT(num_ops == 0);
6832
Paul Elliott91007972022-12-16 12:21:24 +00006833 /* The value of *signature_length is unspecified on error, but
6834 * whatever it is, it should be less than signature_size, so that
6835 * if the caller tries to read *signature_length bytes without
6836 * checking the error code then they don't overflow a buffer. */
6837 TEST_LE_U(signature_length, signature_size);
6838
Paul Elliott0c683352022-12-16 19:16:56 +00006839 TEST_LE_U(min_completes, num_completes);
6840 TEST_LE_U(num_completes, max_completes);
6841
Paul Elliott91007972022-12-16 12:21:24 +00006842exit:
6843 psa_reset_key_attributes(&attributes);
6844 psa_destroy_key(key);
6845 mbedtls_free(signature);
6846 PSA_DONE();
6847}
6848/* END_CASE */
6849
mohammad16038cc1cee2018-03-28 01:21:33 +03006850/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006851void sign_verify_hash(int key_type_arg, data_t *key_data,
6852 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006853{
Ronald Cron5425a212020-08-04 14:58:35 +02006854 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006855 psa_key_type_t key_type = key_type_arg;
6856 psa_algorithm_t alg = alg_arg;
6857 size_t key_bits;
6858 unsigned char *signature = NULL;
6859 size_t signature_size;
6860 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006861 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006862
Gilles Peskine449bd832023-01-11 14:50:10 +01006863 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006864
Gilles Peskine449bd832023-01-11 14:50:10 +01006865 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6866 psa_set_key_algorithm(&attributes, alg);
6867 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006868
Gilles Peskine449bd832023-01-11 14:50:10 +01006869 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6870 &key));
6871 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6872 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006873
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006874 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006875 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006876 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6877 key_bits, alg);
6878 TEST_ASSERT(signature_size != 0);
6879 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006880 TEST_CALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006881
6882 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006883 PSA_ASSERT(psa_sign_hash(key, alg,
6884 input_data->x, input_data->len,
6885 signature, signature_size,
6886 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006887 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006888 TEST_LE_U(signature_length, signature_size);
6889 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006890
6891 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 PSA_ASSERT(psa_verify_hash(key, alg,
6893 input_data->x, input_data->len,
6894 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006895
Gilles Peskine449bd832023-01-11 14:50:10 +01006896 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006897 /* Flip a bit in the input and verify that the signature is now
6898 * detected as invalid. Flip a bit at the beginning, not at the end,
6899 * because ECDSA may ignore the last few bits of the input. */
6900 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006901 TEST_EQUAL(psa_verify_hash(key, alg,
6902 input_data->x, input_data->len,
6903 signature, signature_length),
6904 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006905 }
6906
6907exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006908 /*
6909 * Key attributes may have been returned by psa_get_key_attributes()
6910 * thus reset them as required.
6911 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006912 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006913
Gilles Peskine449bd832023-01-11 14:50:10 +01006914 psa_destroy_key(key);
6915 mbedtls_free(signature);
6916 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006917}
6918/* END_CASE */
6919
Paul Elliott712d5122022-12-07 14:03:10 +00006920/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006921/**
6922 * sign_verify_hash_interruptible() test intentions:
6923 *
6924 * Note: This test can currently only handle ECDSA.
6925 *
Paul Elliott8c092052023-03-06 17:49:14 +00006926 * 1. Test that we can sign an input hash with the given keypair and then
6927 * afterwards verify that signature. This is currently the only way to test
6928 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00006929 *
6930 * 2. Test that after corrupting the hash, the verification detects an invalid
6931 * signature.
6932 *
6933 * 3. Test the number of calls to psa_sign_hash_complete() required are as
6934 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00006935 *
6936 * 4. Test that the number of ops done prior to starting signing and after abort
6937 * is zero and that each successful signing stage completes some ops (this is
6938 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00006939 */
Paul Elliott712d5122022-12-07 14:03:10 +00006940void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006941 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006942 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006943{
6944 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6945 psa_key_type_t key_type = key_type_arg;
6946 psa_algorithm_t alg = alg_arg;
6947 size_t key_bits;
6948 unsigned char *signature = NULL;
6949 size_t signature_size;
6950 size_t signature_length = 0xdeadbeef;
6951 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6952 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006953 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00006954 uint32_t num_ops = 0;
6955 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006956 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006957 size_t min_completes = 0;
6958 size_t max_completes = 0;
6959
Paul Elliott712d5122022-12-07 14:03:10 +00006960 psa_sign_hash_interruptible_operation_t sign_operation =
6961 psa_sign_hash_interruptible_operation_init();
6962 psa_verify_hash_interruptible_operation_t verify_operation =
6963 psa_verify_hash_interruptible_operation_init();
6964
6965 PSA_ASSERT(psa_crypto_init());
6966
Paul Elliott0c683352022-12-16 19:16:56 +00006967 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6968 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006969 psa_set_key_algorithm(&attributes, alg);
6970 psa_set_key_type(&attributes, key_type);
6971
6972 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6973 &key));
6974 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6975 key_bits = psa_get_key_bits(&attributes);
6976
6977 /* Allocate a buffer which has the size advertised by the
6978 * library. */
6979 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6980 key_bits, alg);
6981 TEST_ASSERT(signature_size != 0);
6982 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006983 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006984
Paul Elliott0c683352022-12-16 19:16:56 +00006985 psa_interruptible_set_max_ops(max_ops);
6986
Paul Elliott6f600372023-02-06 18:41:05 +00006987 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6988 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006989
Paul Elliott7c173082023-02-26 18:44:45 +00006990 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6991 TEST_ASSERT(num_ops_prior == 0);
6992
Paul Elliott712d5122022-12-07 14:03:10 +00006993 /* Start performing the signature. */
6994 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6995 input_data->x, input_data->len));
6996
Paul Elliott7c173082023-02-26 18:44:45 +00006997 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6998 TEST_ASSERT(num_ops_prior == 0);
6999
Paul Elliott712d5122022-12-07 14:03:10 +00007000 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007001 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007002
Paul Elliott0c683352022-12-16 19:16:56 +00007003 status = psa_sign_hash_complete(&sign_operation, signature,
7004 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00007005 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00007006
7007 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00007008
7009 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7010 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7011 /* We are asserting here that every complete makes progress
7012 * (completes some ops), which is true of the internal
7013 * implementation and probably any implementation, however this is
7014 * not mandated by the PSA specification. */
7015 TEST_ASSERT(num_ops > num_ops_prior);
7016
7017 num_ops_prior = num_ops;
7018 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007019 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007020
7021 TEST_ASSERT(status == PSA_SUCCESS);
7022
Paul Elliott0c683352022-12-16 19:16:56 +00007023 TEST_LE_U(min_completes, num_completes);
7024 TEST_LE_U(num_completes, max_completes);
7025
Paul Elliott712d5122022-12-07 14:03:10 +00007026 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7027
Paul Elliott7c173082023-02-26 18:44:45 +00007028 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7029 TEST_ASSERT(num_ops == 0);
7030
Paul Elliott712d5122022-12-07 14:03:10 +00007031 /* Check that the signature length looks sensible. */
7032 TEST_LE_U(signature_length, signature_size);
7033 TEST_ASSERT(signature_length > 0);
7034
Paul Elliott0c683352022-12-16 19:16:56 +00007035 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007036
7037 /* Start verification. */
7038 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7039 input_data->x, input_data->len,
7040 signature, signature_length));
7041
7042 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007043 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007044 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007045
7046 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007047 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007048
7049 TEST_ASSERT(status == PSA_SUCCESS);
7050
Paul Elliott0c683352022-12-16 19:16:56 +00007051 TEST_LE_U(min_completes, num_completes);
7052 TEST_LE_U(num_completes, max_completes);
7053
Paul Elliott712d5122022-12-07 14:03:10 +00007054 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7055
7056 verify_operation = psa_verify_hash_interruptible_operation_init();
7057
7058 if (input_data->len != 0) {
7059 /* Flip a bit in the input and verify that the signature is now
7060 * detected as invalid. Flip a bit at the beginning, not at the end,
7061 * because ECDSA may ignore the last few bits of the input. */
7062 input_data->x[0] ^= 1;
7063
Paul Elliott712d5122022-12-07 14:03:10 +00007064 /* Start verification. */
7065 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7066 input_data->x, input_data->len,
7067 signature, signature_length));
7068
7069 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007070 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007071 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007072 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007073
7074 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7075 }
7076
7077 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7078
7079exit:
7080 /*
7081 * Key attributes may have been returned by psa_get_key_attributes()
7082 * thus reset them as required.
7083 */
7084 psa_reset_key_attributes(&attributes);
7085
7086 psa_destroy_key(key);
7087 mbedtls_free(signature);
7088 PSA_DONE();
7089}
7090/* END_CASE */
7091
Gilles Peskine9911b022018-06-29 17:30:48 +02007092/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007093void verify_hash(int key_type_arg, data_t *key_data,
7094 int alg_arg, data_t *hash_data,
7095 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007096{
Ronald Cron5425a212020-08-04 14:58:35 +02007097 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007098 psa_key_type_t key_type = key_type_arg;
7099 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007100 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007101
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007103
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007105
Gilles Peskine449bd832023-01-11 14:50:10 +01007106 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7107 psa_set_key_algorithm(&attributes, alg);
7108 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007109
Gilles Peskine449bd832023-01-11 14:50:10 +01007110 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7111 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007112
Gilles Peskine449bd832023-01-11 14:50:10 +01007113 PSA_ASSERT(psa_verify_hash(key, alg,
7114 hash_data->x, hash_data->len,
7115 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007116
itayzafrir5c753392018-05-08 11:18:38 +03007117exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007118 psa_reset_key_attributes(&attributes);
7119 psa_destroy_key(key);
7120 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007121}
7122/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007123
Paul Elliott712d5122022-12-07 14:03:10 +00007124/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007125/**
7126 * verify_hash_interruptible() test intentions:
7127 *
7128 * Note: This test can currently only handle ECDSA.
7129 *
7130 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007131 * only). Given this test only does verification it can accept public keys as
7132 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007133 *
7134 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7135 * expected for different max_ops values.
7136 *
7137 * 3. Test that the number of ops done prior to start and after abort is zero
7138 * and that each successful stage completes some ops (this is not mandated by
7139 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007140 *
7141 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7142 * complete() calls does not alter the number of ops returned.
7143 *
7144 * 5. Test that after corrupting the hash, the verification detects an invalid
7145 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007146 */
Paul Elliott712d5122022-12-07 14:03:10 +00007147void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7148 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007149 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007150{
7151 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7152 psa_key_type_t key_type = key_type_arg;
7153 psa_algorithm_t alg = alg_arg;
7154 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7155 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007156 uint32_t num_ops = 0;
7157 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007158 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007159 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007160 size_t min_completes = 0;
7161 size_t max_completes = 0;
7162
Paul Elliott712d5122022-12-07 14:03:10 +00007163 psa_verify_hash_interruptible_operation_t operation =
7164 psa_verify_hash_interruptible_operation_init();
7165
7166 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7167
7168 PSA_ASSERT(psa_crypto_init());
7169
7170 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7171 psa_set_key_algorithm(&attributes, alg);
7172 psa_set_key_type(&attributes, key_type);
7173
7174 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7175 &key));
7176
Paul Elliott0c683352022-12-16 19:16:56 +00007177 psa_interruptible_set_max_ops(max_ops);
7178
Paul Elliott6f600372023-02-06 18:41:05 +00007179 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7180 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007181
Paul Elliott712d5122022-12-07 14:03:10 +00007182 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7183
7184 TEST_ASSERT(num_ops_prior == 0);
7185
7186 /* Start verification. */
7187 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7188 hash_data->x, hash_data->len,
7189 signature_data->x, signature_data->len)
7190 );
7191
7192 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7193
7194 TEST_ASSERT(num_ops_prior == 0);
7195
7196 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007197 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007198 status = psa_verify_hash_complete(&operation);
7199
Paul Elliott0c683352022-12-16 19:16:56 +00007200 num_completes++;
7201
Paul Elliott712d5122022-12-07 14:03:10 +00007202 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7203 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007204 /* We are asserting here that every complete makes progress
7205 * (completes some ops), which is true of the internal
7206 * implementation and probably any implementation, however this is
7207 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007208 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007209
Paul Elliott712d5122022-12-07 14:03:10 +00007210 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007211
7212 /* Ensure calling get_num_ops() twice still returns the same
7213 * number of ops as previously reported. */
7214 num_ops = psa_verify_hash_get_num_ops(&operation);
7215
7216 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007217 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007218 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007219
7220 TEST_ASSERT(status == PSA_SUCCESS);
7221
Paul Elliott0c683352022-12-16 19:16:56 +00007222 TEST_LE_U(min_completes, num_completes);
7223 TEST_LE_U(num_completes, max_completes);
7224
Paul Elliott712d5122022-12-07 14:03:10 +00007225 PSA_ASSERT(psa_verify_hash_abort(&operation));
7226
Paul Elliott59ad9452022-12-18 15:09:02 +00007227 num_ops = psa_verify_hash_get_num_ops(&operation);
7228 TEST_ASSERT(num_ops == 0);
7229
Paul Elliott8359c142023-02-24 18:40:10 +00007230 if (hash_data->len != 0) {
7231 /* Flip a bit in the hash and verify that the signature is now detected
7232 * as invalid. Flip a bit at the beginning, not at the end, because
7233 * ECDSA may ignore the last few bits of the input. */
7234 hash_data->x[0] ^= 1;
7235
7236 /* Start verification. */
7237 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7238 hash_data->x, hash_data->len,
7239 signature_data->x, signature_data->len));
7240
7241 /* Continue performing the signature until complete. */
7242 do {
7243 status = psa_verify_hash_complete(&operation);
7244 } while (status == PSA_OPERATION_INCOMPLETE);
7245
7246 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7247 }
7248
Paul Elliott712d5122022-12-07 14:03:10 +00007249exit:
7250 psa_reset_key_attributes(&attributes);
7251 psa_destroy_key(key);
7252 PSA_DONE();
7253}
7254/* END_CASE */
7255
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007256/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007257void verify_hash_fail(int key_type_arg, data_t *key_data,
7258 int alg_arg, data_t *hash_data,
7259 data_t *signature_data,
7260 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007261{
Ronald Cron5425a212020-08-04 14:58:35 +02007262 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007263 psa_key_type_t key_type = key_type_arg;
7264 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007265 psa_status_t actual_status;
7266 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007267 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007268
Gilles Peskine449bd832023-01-11 14:50:10 +01007269 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007270
Gilles Peskine449bd832023-01-11 14:50:10 +01007271 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7272 psa_set_key_algorithm(&attributes, alg);
7273 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007274
Gilles Peskine449bd832023-01-11 14:50:10 +01007275 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7276 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007277
Gilles Peskine449bd832023-01-11 14:50:10 +01007278 actual_status = psa_verify_hash(key, alg,
7279 hash_data->x, hash_data->len,
7280 signature_data->x, signature_data->len);
7281 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007282
7283exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007284 psa_reset_key_attributes(&attributes);
7285 psa_destroy_key(key);
7286 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007287}
7288/* END_CASE */
7289
Paul Elliott91007972022-12-16 12:21:24 +00007290/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007291/**
7292 * verify_hash_fail_interruptible() test intentions:
7293 *
7294 * Note: This test can currently only handle ECDSA.
7295 *
7296 * 1. Test that various failure cases for interruptible verify hash fail with
7297 * the correct error codes, and at the correct point (at start or during
7298 * complete).
7299 *
7300 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7301 * expected for different max_ops values.
7302 *
7303 * 3. Test that the number of ops done prior to start and after abort is zero
7304 * and that each successful stage completes some ops (this is not mandated by
7305 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007306 *
7307 * 4. Check that calling complete() when start() fails and complete()
7308 * after completion results in a BAD_STATE error.
7309 *
7310 * 5. Check that calling start() again after start fails results in a BAD_STATE
7311 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007312 */
Paul Elliott91007972022-12-16 12:21:24 +00007313void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7314 int alg_arg, data_t *hash_data,
7315 data_t *signature_data,
7316 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007317 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007318 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007319{
7320 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7321 psa_key_type_t key_type = key_type_arg;
7322 psa_algorithm_t alg = alg_arg;
7323 psa_status_t actual_status;
7324 psa_status_t expected_start_status = expected_start_status_arg;
7325 psa_status_t expected_complete_status = expected_complete_status_arg;
7326 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007327 uint32_t num_ops = 0;
7328 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007329 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007330 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007331 size_t min_completes = 0;
7332 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007333 psa_verify_hash_interruptible_operation_t operation =
7334 psa_verify_hash_interruptible_operation_init();
7335
7336 PSA_ASSERT(psa_crypto_init());
7337
7338 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7339 psa_set_key_algorithm(&attributes, alg);
7340 psa_set_key_type(&attributes, key_type);
7341
7342 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7343 &key));
7344
Paul Elliott0c683352022-12-16 19:16:56 +00007345 psa_interruptible_set_max_ops(max_ops);
7346
Paul Elliott6f600372023-02-06 18:41:05 +00007347 interruptible_signverify_get_minmax_completes(max_ops,
7348 expected_complete_status,
7349 &min_completes,
7350 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007351
Paul Elliott91007972022-12-16 12:21:24 +00007352 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7353 TEST_ASSERT(num_ops_prior == 0);
7354
7355 /* Start verification. */
7356 actual_status = psa_verify_hash_start(&operation, key, alg,
7357 hash_data->x, hash_data->len,
7358 signature_data->x,
7359 signature_data->len);
7360
7361 TEST_EQUAL(actual_status, expected_start_status);
7362
Paul Elliottc9774412023-02-06 15:14:07 +00007363 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007364 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007365 * start failed. */
7366 actual_status = psa_verify_hash_complete(&operation);
7367
7368 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7369
7370 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007371 actual_status = psa_verify_hash_start(&operation, key, alg,
7372 hash_data->x, hash_data->len,
7373 signature_data->x,
7374 signature_data->len);
7375
7376 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7377 }
7378
Paul Elliott91007972022-12-16 12:21:24 +00007379 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7380 TEST_ASSERT(num_ops_prior == 0);
7381
Paul Elliott91007972022-12-16 12:21:24 +00007382 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007383 do {
Paul Elliott91007972022-12-16 12:21:24 +00007384 actual_status = psa_verify_hash_complete(&operation);
7385
Paul Elliott0c683352022-12-16 19:16:56 +00007386 num_completes++;
7387
Paul Elliott334d7262023-01-20 17:29:41 +00007388 if (actual_status == PSA_SUCCESS ||
7389 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007390 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007391 /* We are asserting here that every complete makes progress
7392 * (completes some ops), which is true of the internal
7393 * implementation and probably any implementation, however this is
7394 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007395 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007396
Paul Elliott91007972022-12-16 12:21:24 +00007397 num_ops_prior = num_ops;
7398 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007399 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007400
Paul Elliottc9774412023-02-06 15:14:07 +00007401 TEST_EQUAL(actual_status, expected_complete_status);
7402
Paul Elliottefebad02023-02-15 16:56:45 +00007403 /* Check that another complete returns BAD_STATE. */
7404 actual_status = psa_verify_hash_complete(&operation);
7405 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007406
Paul Elliott0c683352022-12-16 19:16:56 +00007407 TEST_LE_U(min_completes, num_completes);
7408 TEST_LE_U(num_completes, max_completes);
7409
Paul Elliott91007972022-12-16 12:21:24 +00007410 PSA_ASSERT(psa_verify_hash_abort(&operation));
7411
Paul Elliott59ad9452022-12-18 15:09:02 +00007412 num_ops = psa_verify_hash_get_num_ops(&operation);
7413 TEST_ASSERT(num_ops == 0);
7414
Paul Elliott91007972022-12-16 12:21:24 +00007415exit:
7416 psa_reset_key_attributes(&attributes);
7417 psa_destroy_key(key);
7418 PSA_DONE();
7419}
7420/* END_CASE */
7421
Paul Elliott20a36062022-12-18 13:21:25 +00007422/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007423/**
7424 * interruptible_signverify_hash_state_test() test intentions:
7425 *
7426 * Note: This test can currently only handle ECDSA.
7427 *
7428 * 1. Test that calling the various interruptible sign and verify hash functions
7429 * in incorrect orders returns BAD_STATE errors.
7430 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007431void interruptible_signverify_hash_state_test(int key_type_arg,
7432 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007433{
7434 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7435 psa_key_type_t key_type = key_type_arg;
7436 psa_algorithm_t alg = alg_arg;
7437 size_t key_bits;
7438 unsigned char *signature = NULL;
7439 size_t signature_size;
7440 size_t signature_length = 0xdeadbeef;
7441 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7442 psa_sign_hash_interruptible_operation_t sign_operation =
7443 psa_sign_hash_interruptible_operation_init();
7444 psa_verify_hash_interruptible_operation_t verify_operation =
7445 psa_verify_hash_interruptible_operation_init();
7446
7447 PSA_ASSERT(psa_crypto_init());
7448
7449 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7450 PSA_KEY_USAGE_VERIFY_HASH);
7451 psa_set_key_algorithm(&attributes, alg);
7452 psa_set_key_type(&attributes, key_type);
7453
7454 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7455 &key));
7456 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7457 key_bits = psa_get_key_bits(&attributes);
7458
7459 /* Allocate a buffer which has the size advertised by the
7460 * library. */
7461 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7462 key_bits, alg);
7463 TEST_ASSERT(signature_size != 0);
7464 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007465 TEST_CALLOC(signature, signature_size);
Paul Elliott20a36062022-12-18 13:21:25 +00007466
7467 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7468
7469 /* --- Attempt completes prior to starts --- */
7470 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7471 signature_size,
7472 &signature_length),
7473 PSA_ERROR_BAD_STATE);
7474
7475 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7476
7477 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7478 PSA_ERROR_BAD_STATE);
7479
7480 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7481
7482 /* --- Aborts in all other places. --- */
7483 psa_sign_hash_abort(&sign_operation);
7484
7485 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7486 input_data->x, input_data->len));
7487
7488 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7489
7490 psa_interruptible_set_max_ops(1);
7491
7492 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7493 input_data->x, input_data->len));
7494
7495 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7496 signature_size,
7497 &signature_length),
7498 PSA_OPERATION_INCOMPLETE);
7499
7500 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7501
7502 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7503
7504 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7505 input_data->x, input_data->len));
7506
7507 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7508 signature_size,
7509 &signature_length));
7510
7511 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7512
7513 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7514
7515 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7516 input_data->x, input_data->len,
7517 signature, signature_length));
7518
7519 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7520
7521 psa_interruptible_set_max_ops(1);
7522
7523 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7524 input_data->x, input_data->len,
7525 signature, signature_length));
7526
7527 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7528 PSA_OPERATION_INCOMPLETE);
7529
7530 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7531
7532 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7533
7534 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7535 input_data->x, input_data->len,
7536 signature, signature_length));
7537
7538 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7539
7540 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7541
7542 /* --- Attempt double starts. --- */
7543
7544 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7545 input_data->x, input_data->len));
7546
7547 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7548 input_data->x, input_data->len),
7549 PSA_ERROR_BAD_STATE);
7550
7551 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7552
7553 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7554 input_data->x, input_data->len,
7555 signature, signature_length));
7556
7557 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7558 input_data->x, input_data->len,
7559 signature, signature_length),
7560 PSA_ERROR_BAD_STATE);
7561
7562 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7563
Paul Elliott76d671a2023-02-07 17:45:18 +00007564exit:
7565 /*
7566 * Key attributes may have been returned by psa_get_key_attributes()
7567 * thus reset them as required.
7568 */
7569 psa_reset_key_attributes(&attributes);
7570
7571 psa_destroy_key(key);
7572 mbedtls_free(signature);
7573 PSA_DONE();
7574}
7575/* END_CASE */
7576
7577/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007578/**
Paul Elliottc2033502023-02-26 17:09:14 +00007579 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007580 *
7581 * Note: This test can currently only handle ECDSA.
7582 *
7583 * 1. Test various edge cases in the interruptible sign and verify hash
7584 * interfaces.
7585 */
Paul Elliottc2033502023-02-26 17:09:14 +00007586void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007587 data_t *key_data, int alg_arg, data_t *input_data)
7588{
7589 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7590 psa_key_type_t key_type = key_type_arg;
7591 psa_algorithm_t alg = alg_arg;
7592 size_t key_bits;
7593 unsigned char *signature = NULL;
7594 size_t signature_size;
7595 size_t signature_length = 0xdeadbeef;
7596 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7597 uint8_t *input_buffer = NULL;
7598 psa_sign_hash_interruptible_operation_t sign_operation =
7599 psa_sign_hash_interruptible_operation_init();
7600 psa_verify_hash_interruptible_operation_t verify_operation =
7601 psa_verify_hash_interruptible_operation_init();
7602
7603 PSA_ASSERT(psa_crypto_init());
7604
7605 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7606 PSA_KEY_USAGE_VERIFY_HASH);
7607 psa_set_key_algorithm(&attributes, alg);
7608 psa_set_key_type(&attributes, key_type);
7609
7610 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7611 &key));
7612 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7613 key_bits = psa_get_key_bits(&attributes);
7614
7615 /* Allocate a buffer which has the size advertised by the
7616 * library. */
7617 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7618 key_bits, alg);
7619 TEST_ASSERT(signature_size != 0);
7620 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007621 TEST_CALLOC(signature, signature_size);
Paul Elliott76d671a2023-02-07 17:45:18 +00007622
Paul Elliott20a36062022-12-18 13:21:25 +00007623 /* --- Change function inputs mid run, to cause an error (sign only,
7624 * verify passes all inputs to start. --- */
7625
7626 psa_interruptible_set_max_ops(1);
7627
7628 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7629 input_data->x, input_data->len));
7630
7631 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7632 signature_size,
7633 &signature_length),
7634 PSA_OPERATION_INCOMPLETE);
7635
7636 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7637 0,
7638 &signature_length),
7639 PSA_ERROR_BUFFER_TOO_SMALL);
7640
Paul Elliottc9774412023-02-06 15:14:07 +00007641 /* And test that this invalidates the operation. */
7642 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7643 0,
7644 &signature_length),
7645 PSA_ERROR_BAD_STATE);
7646
Paul Elliott20a36062022-12-18 13:21:25 +00007647 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7648
Paul Elliottf9c91a72023-02-05 18:06:38 +00007649 /* Trash the hash buffer in between start and complete, to ensure
7650 * no reliance on external buffers. */
7651 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7652
7653 input_buffer = mbedtls_calloc(1, input_data->len);
7654 TEST_ASSERT(input_buffer != NULL);
7655
7656 memcpy(input_buffer, input_data->x, input_data->len);
7657
7658 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7659 input_buffer, input_data->len));
7660
7661 memset(input_buffer, '!', input_data->len);
7662 mbedtls_free(input_buffer);
7663 input_buffer = NULL;
7664
7665 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7666 signature_size,
7667 &signature_length));
7668
7669 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7670
7671 input_buffer = mbedtls_calloc(1, input_data->len);
7672 TEST_ASSERT(input_buffer != NULL);
7673
7674 memcpy(input_buffer, input_data->x, input_data->len);
7675
7676 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7677 input_buffer, input_data->len,
7678 signature, signature_length));
7679
7680 memset(input_buffer, '!', input_data->len);
7681 mbedtls_free(input_buffer);
7682 input_buffer = NULL;
7683
7684 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7685
7686 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7687
Paul Elliott20a36062022-12-18 13:21:25 +00007688exit:
7689 /*
7690 * Key attributes may have been returned by psa_get_key_attributes()
7691 * thus reset them as required.
7692 */
7693 psa_reset_key_attributes(&attributes);
7694
7695 psa_destroy_key(key);
7696 mbedtls_free(signature);
7697 PSA_DONE();
7698}
7699/* END_CASE */
7700
Paul Elliotta4cb9092023-02-07 18:01:55 +00007701/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007702/**
Paul Elliott57702242023-02-26 20:36:10 +00007703 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007704 *
7705 * Note: This test can currently only handle ECDSA.
7706 *
7707 * 1. Test that setting max ops is reflected in both interruptible sign and
7708 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007709 * 2. Test that changing the value of max_ops to unlimited during an operation
7710 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007711 *
7712 * 3. Test that calling get_num_ops() between complete calls gives the same
7713 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007714 */
Paul Elliott57702242023-02-26 20:36:10 +00007715void interruptible_signverify_hash_ops_tests(int key_type_arg,
7716 data_t *key_data, int alg_arg,
7717 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007718{
7719 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7720 psa_key_type_t key_type = key_type_arg;
7721 psa_algorithm_t alg = alg_arg;
7722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007723 size_t key_bits;
7724 unsigned char *signature = NULL;
7725 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007726 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007727 uint32_t num_ops = 0;
7728 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7729
Paul Elliotta4cb9092023-02-07 18:01:55 +00007730 psa_sign_hash_interruptible_operation_t sign_operation =
7731 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007732 psa_verify_hash_interruptible_operation_t verify_operation =
7733 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007734
7735 PSA_ASSERT(psa_crypto_init());
7736
7737 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7738 PSA_KEY_USAGE_VERIFY_HASH);
7739 psa_set_key_algorithm(&attributes, alg);
7740 psa_set_key_type(&attributes, key_type);
7741
Paul Elliottf1743e22023-02-15 18:44:16 +00007742 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7743 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7744 key_bits = psa_get_key_bits(&attributes);
7745
7746 /* Allocate a buffer which has the size advertised by the
7747 * library. */
7748 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7749
7750 TEST_ASSERT(signature_size != 0);
7751 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007752 TEST_CALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007753
7754 /* Check that default max ops gets set if we don't set it. */
7755 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7756 input_data->x, input_data->len));
7757
7758 TEST_EQUAL(psa_interruptible_get_max_ops(),
7759 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7760
7761 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7762
Paul Elliottf1743e22023-02-15 18:44:16 +00007763 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7764 input_data->x, input_data->len,
7765 signature, signature_size));
7766
7767 TEST_EQUAL(psa_interruptible_get_max_ops(),
7768 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7769
7770 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7771
Paul Elliotta4cb9092023-02-07 18:01:55 +00007772 /* Check that max ops gets set properly. */
7773
7774 psa_interruptible_set_max_ops(0xbeef);
7775
Paul Elliottf1743e22023-02-15 18:44:16 +00007776 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007777
Paul Elliott9e8819f2023-02-26 19:01:35 +00007778 /* --- Ensure changing the max ops mid operation works (operation should
7779 * complete successfully after setting max ops to unlimited --- */
7780 psa_interruptible_set_max_ops(1);
7781
7782 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7783 input_data->x, input_data->len));
7784
7785 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7786 signature_size,
7787 &signature_length),
7788 PSA_OPERATION_INCOMPLETE);
7789
7790 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7791
7792 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7793 signature_size,
7794 &signature_length));
7795
7796 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7797
7798 psa_interruptible_set_max_ops(1);
7799
7800 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7801 input_data->x, input_data->len,
7802 signature, signature_length));
7803
7804 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7805 PSA_OPERATION_INCOMPLETE);
7806
7807 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7808
7809 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7810
7811 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7812
Paul Elliottc1e04002023-02-26 20:27:23 +00007813 /* --- Test that not calling get_num_ops inbetween complete calls does not
7814 * result in lost ops. ---*/
7815
7816 psa_interruptible_set_max_ops(1);
7817
7818 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7819 input_data->x, input_data->len));
7820
7821 /* Continue performing the signature until complete. */
7822 do {
7823 status = psa_sign_hash_complete(&sign_operation, signature,
7824 signature_size,
7825 &signature_length);
7826
7827 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7828
7829 } while (status == PSA_OPERATION_INCOMPLETE);
7830
7831 PSA_ASSERT(status);
7832
7833 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7834
7835 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7836 input_data->x, input_data->len));
7837
7838 /* Continue performing the signature until complete. */
7839 do {
7840 status = psa_sign_hash_complete(&sign_operation, signature,
7841 signature_size,
7842 &signature_length);
7843 } while (status == PSA_OPERATION_INCOMPLETE);
7844
7845 PSA_ASSERT(status);
7846
7847 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7848
7849 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7850
7851 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7852 input_data->x, input_data->len,
7853 signature, signature_length));
7854
7855 /* Continue performing the verification until complete. */
7856 do {
7857 status = psa_verify_hash_complete(&verify_operation);
7858
7859 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7860
7861 } while (status == PSA_OPERATION_INCOMPLETE);
7862
7863 PSA_ASSERT(status);
7864
7865 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7866
7867 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7868 input_data->x, input_data->len,
7869 signature, signature_length));
7870
7871 /* Continue performing the verification until complete. */
7872 do {
7873 status = psa_verify_hash_complete(&verify_operation);
7874
7875 } while (status == PSA_OPERATION_INCOMPLETE);
7876
7877 PSA_ASSERT(status);
7878
7879 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7880
7881 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7882
Paul Elliotta4cb9092023-02-07 18:01:55 +00007883exit:
7884 /*
7885 * Key attributes may have been returned by psa_get_key_attributes()
7886 * thus reset them as required.
7887 */
7888 psa_reset_key_attributes(&attributes);
7889
7890 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007891 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007892 PSA_DONE();
7893}
7894/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007895
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007896/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007897void sign_message_deterministic(int key_type_arg,
7898 data_t *key_data,
7899 int alg_arg,
7900 data_t *input_data,
7901 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007902{
7903 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7904 psa_key_type_t key_type = key_type_arg;
7905 psa_algorithm_t alg = alg_arg;
7906 size_t key_bits;
7907 unsigned char *signature = NULL;
7908 size_t signature_size;
7909 size_t signature_length = 0xdeadbeef;
7910 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7911
Gilles Peskine449bd832023-01-11 14:50:10 +01007912 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007913
Gilles Peskine449bd832023-01-11 14:50:10 +01007914 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7915 psa_set_key_algorithm(&attributes, alg);
7916 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007917
Gilles Peskine449bd832023-01-11 14:50:10 +01007918 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7919 &key));
7920 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7921 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007922
Gilles Peskine449bd832023-01-11 14:50:10 +01007923 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7924 TEST_ASSERT(signature_size != 0);
7925 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007926 TEST_CALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007927
Gilles Peskine449bd832023-01-11 14:50:10 +01007928 PSA_ASSERT(psa_sign_message(key, alg,
7929 input_data->x, input_data->len,
7930 signature, signature_size,
7931 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007932
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01007933 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01007934 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007935
7936exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007937 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007938
Gilles Peskine449bd832023-01-11 14:50:10 +01007939 psa_destroy_key(key);
7940 mbedtls_free(signature);
7941 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007942
7943}
7944/* END_CASE */
7945
7946/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007947void sign_message_fail(int key_type_arg,
7948 data_t *key_data,
7949 int alg_arg,
7950 data_t *input_data,
7951 int signature_size_arg,
7952 int expected_status_arg)
7953{
7954 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7955 psa_key_type_t key_type = key_type_arg;
7956 psa_algorithm_t alg = alg_arg;
7957 size_t signature_size = signature_size_arg;
7958 psa_status_t actual_status;
7959 psa_status_t expected_status = expected_status_arg;
7960 unsigned char *signature = NULL;
7961 size_t signature_length = 0xdeadbeef;
7962 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7963
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007964 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01007965
7966 PSA_ASSERT(psa_crypto_init());
7967
7968 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7969 psa_set_key_algorithm(&attributes, alg);
7970 psa_set_key_type(&attributes, key_type);
7971
7972 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7973 &key));
7974
7975 actual_status = psa_sign_message(key, alg,
7976 input_data->x, input_data->len,
7977 signature, signature_size,
7978 &signature_length);
7979 TEST_EQUAL(actual_status, expected_status);
7980 /* The value of *signature_length is unspecified on error, but
7981 * whatever it is, it should be less than signature_size, so that
7982 * if the caller tries to read *signature_length bytes without
7983 * checking the error code then they don't overflow a buffer. */
7984 TEST_LE_U(signature_length, signature_size);
7985
7986exit:
7987 psa_reset_key_attributes(&attributes);
7988 psa_destroy_key(key);
7989 mbedtls_free(signature);
7990 PSA_DONE();
7991}
7992/* END_CASE */
7993
7994/* BEGIN_CASE */
7995void sign_verify_message(int key_type_arg,
7996 data_t *key_data,
7997 int alg_arg,
7998 data_t *input_data)
7999{
8000 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8001 psa_key_type_t key_type = key_type_arg;
8002 psa_algorithm_t alg = alg_arg;
8003 size_t key_bits;
8004 unsigned char *signature = NULL;
8005 size_t signature_size;
8006 size_t signature_length = 0xdeadbeef;
8007 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8008
8009 PSA_ASSERT(psa_crypto_init());
8010
8011 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
8012 PSA_KEY_USAGE_VERIFY_MESSAGE);
8013 psa_set_key_algorithm(&attributes, alg);
8014 psa_set_key_type(&attributes, key_type);
8015
8016 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8017 &key));
8018 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8019 key_bits = psa_get_key_bits(&attributes);
8020
8021 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8022 TEST_ASSERT(signature_size != 0);
8023 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008024 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008025
8026 PSA_ASSERT(psa_sign_message(key, alg,
8027 input_data->x, input_data->len,
8028 signature, signature_size,
8029 &signature_length));
8030 TEST_LE_U(signature_length, signature_size);
8031 TEST_ASSERT(signature_length > 0);
8032
8033 PSA_ASSERT(psa_verify_message(key, alg,
8034 input_data->x, input_data->len,
8035 signature, signature_length));
8036
8037 if (input_data->len != 0) {
8038 /* Flip a bit in the input and verify that the signature is now
8039 * detected as invalid. Flip a bit at the beginning, not at the end,
8040 * because ECDSA may ignore the last few bits of the input. */
8041 input_data->x[0] ^= 1;
8042 TEST_EQUAL(psa_verify_message(key, alg,
8043 input_data->x, input_data->len,
8044 signature, signature_length),
8045 PSA_ERROR_INVALID_SIGNATURE);
8046 }
8047
8048exit:
8049 psa_reset_key_attributes(&attributes);
8050
8051 psa_destroy_key(key);
8052 mbedtls_free(signature);
8053 PSA_DONE();
8054}
8055/* END_CASE */
8056
8057/* BEGIN_CASE */
8058void verify_message(int key_type_arg,
8059 data_t *key_data,
8060 int alg_arg,
8061 data_t *input_data,
8062 data_t *signature_data)
8063{
8064 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8065 psa_key_type_t key_type = key_type_arg;
8066 psa_algorithm_t alg = alg_arg;
8067 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8068
8069 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8070
8071 PSA_ASSERT(psa_crypto_init());
8072
8073 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8074 psa_set_key_algorithm(&attributes, alg);
8075 psa_set_key_type(&attributes, key_type);
8076
8077 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8078 &key));
8079
8080 PSA_ASSERT(psa_verify_message(key, alg,
8081 input_data->x, input_data->len,
8082 signature_data->x, signature_data->len));
8083
8084exit:
8085 psa_reset_key_attributes(&attributes);
8086 psa_destroy_key(key);
8087 PSA_DONE();
8088}
8089/* END_CASE */
8090
8091/* BEGIN_CASE */
8092void verify_message_fail(int key_type_arg,
8093 data_t *key_data,
8094 int alg_arg,
8095 data_t *hash_data,
8096 data_t *signature_data,
8097 int expected_status_arg)
8098{
8099 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8100 psa_key_type_t key_type = key_type_arg;
8101 psa_algorithm_t alg = alg_arg;
8102 psa_status_t actual_status;
8103 psa_status_t expected_status = expected_status_arg;
8104 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8105
8106 PSA_ASSERT(psa_crypto_init());
8107
8108 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8109 psa_set_key_algorithm(&attributes, alg);
8110 psa_set_key_type(&attributes, key_type);
8111
8112 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8113 &key));
8114
8115 actual_status = psa_verify_message(key, alg,
8116 hash_data->x, hash_data->len,
8117 signature_data->x,
8118 signature_data->len);
8119 TEST_EQUAL(actual_status, expected_status);
8120
8121exit:
8122 psa_reset_key_attributes(&attributes);
8123 psa_destroy_key(key);
8124 PSA_DONE();
8125}
8126/* END_CASE */
8127
8128/* BEGIN_CASE */
8129void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008130 data_t *key_data,
8131 int alg_arg,
8132 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008133 data_t *label,
8134 int expected_output_length_arg,
8135 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008136{
Ronald Cron5425a212020-08-04 14:58:35 +02008137 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008138 psa_key_type_t key_type = key_type_arg;
8139 psa_algorithm_t alg = alg_arg;
8140 size_t expected_output_length = expected_output_length_arg;
8141 size_t key_bits;
8142 unsigned char *output = NULL;
8143 size_t output_size;
8144 size_t output_length = ~0;
8145 psa_status_t actual_status;
8146 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008147 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008148
Gilles Peskine449bd832023-01-11 14:50:10 +01008149 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008150
Gilles Peskine656896e2018-06-29 19:12:28 +02008151 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008152 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8153 psa_set_key_algorithm(&attributes, alg);
8154 psa_set_key_type(&attributes, key_type);
8155 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8156 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008157
8158 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008159 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8160 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008161
Gilles Peskine449bd832023-01-11 14:50:10 +01008162 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8163 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008164 TEST_CALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008165
8166 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008167 actual_status = psa_asymmetric_encrypt(key, alg,
8168 input_data->x, input_data->len,
8169 label->x, label->len,
8170 output, output_size,
8171 &output_length);
8172 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008173 if (actual_status == PSA_SUCCESS) {
8174 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008175 } else {
8176 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008177 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008178
Gilles Peskine68428122018-06-30 18:42:41 +02008179 /* If the label is empty, the test framework puts a non-null pointer
8180 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008181 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008182 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008183 if (output_size != 0) {
8184 memset(output, 0, output_size);
8185 }
8186 actual_status = psa_asymmetric_encrypt(key, alg,
8187 input_data->x, input_data->len,
8188 NULL, label->len,
8189 output, output_size,
8190 &output_length);
8191 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008192 if (actual_status == PSA_SUCCESS) {
8193 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008194 } else {
8195 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008196 }
Gilles Peskine68428122018-06-30 18:42:41 +02008197 }
8198
Gilles Peskine656896e2018-06-29 19:12:28 +02008199exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008200 /*
8201 * Key attributes may have been returned by psa_get_key_attributes()
8202 * thus reset them as required.
8203 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008204 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008205
Gilles Peskine449bd832023-01-11 14:50:10 +01008206 psa_destroy_key(key);
8207 mbedtls_free(output);
8208 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008209}
8210/* END_CASE */
8211
8212/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008213void asymmetric_encrypt_decrypt(int key_type_arg,
8214 data_t *key_data,
8215 int alg_arg,
8216 data_t *input_data,
8217 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008218{
Ronald Cron5425a212020-08-04 14:58:35 +02008219 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008220 psa_key_type_t key_type = key_type_arg;
8221 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008222 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008223 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008224 size_t output_size;
8225 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008226 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008227 size_t output2_size;
8228 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008229 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008230
Gilles Peskine449bd832023-01-11 14:50:10 +01008231 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008232
Gilles Peskine449bd832023-01-11 14:50:10 +01008233 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8234 psa_set_key_algorithm(&attributes, alg);
8235 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008236
Gilles Peskine449bd832023-01-11 14:50:10 +01008237 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8238 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008239
8240 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008241 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8242 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008243
Gilles Peskine449bd832023-01-11 14:50:10 +01008244 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8245 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008246 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008247
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008248 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008249 TEST_LE_U(output2_size,
8250 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8251 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008252 TEST_CALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008253
Gilles Peskineeebd7382018-06-08 18:11:54 +02008254 /* We test encryption by checking that encrypt-then-decrypt gives back
8255 * the original plaintext because of the non-optional random
8256 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008257 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8258 input_data->x, input_data->len,
8259 label->x, label->len,
8260 output, output_size,
8261 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008262 /* We don't know what ciphertext length to expect, but check that
8263 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008264 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008265
Gilles Peskine449bd832023-01-11 14:50:10 +01008266 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8267 output, output_length,
8268 label->x, label->len,
8269 output2, output2_size,
8270 &output2_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008271 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008272 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008273
8274exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008275 /*
8276 * Key attributes may have been returned by psa_get_key_attributes()
8277 * thus reset them as required.
8278 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008279 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008280
Gilles Peskine449bd832023-01-11 14:50:10 +01008281 psa_destroy_key(key);
8282 mbedtls_free(output);
8283 mbedtls_free(output2);
8284 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008285}
8286/* END_CASE */
8287
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008288/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008289void asymmetric_decrypt(int key_type_arg,
8290 data_t *key_data,
8291 int alg_arg,
8292 data_t *input_data,
8293 data_t *label,
8294 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008295{
Ronald Cron5425a212020-08-04 14:58:35 +02008296 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008297 psa_key_type_t key_type = key_type_arg;
8298 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008299 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008300 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008301 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008302 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008303 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008304
Gilles Peskine449bd832023-01-11 14:50:10 +01008305 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008306
Gilles Peskine449bd832023-01-11 14:50:10 +01008307 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8308 psa_set_key_algorithm(&attributes, alg);
8309 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008310
Gilles Peskine449bd832023-01-11 14:50:10 +01008311 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8312 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008313
Gilles Peskine449bd832023-01-11 14:50:10 +01008314 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8315 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008316
8317 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008318 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8319 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008320 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008321
Gilles Peskine449bd832023-01-11 14:50:10 +01008322 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8323 input_data->x, input_data->len,
8324 label->x, label->len,
8325 output,
8326 output_size,
8327 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008328 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008329 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008330
Gilles Peskine68428122018-06-30 18:42:41 +02008331 /* If the label is empty, the test framework puts a non-null pointer
8332 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008333 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008334 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008335 if (output_size != 0) {
8336 memset(output, 0, output_size);
8337 }
8338 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8339 input_data->x, input_data->len,
8340 NULL, label->len,
8341 output,
8342 output_size,
8343 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008344 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008345 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008346 }
8347
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008348exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008349 psa_reset_key_attributes(&attributes);
8350 psa_destroy_key(key);
8351 mbedtls_free(output);
8352 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008353}
8354/* END_CASE */
8355
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008356/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008357void asymmetric_decrypt_fail(int key_type_arg,
8358 data_t *key_data,
8359 int alg_arg,
8360 data_t *input_data,
8361 data_t *label,
8362 int output_size_arg,
8363 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008364{
Ronald Cron5425a212020-08-04 14:58:35 +02008365 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008366 psa_key_type_t key_type = key_type_arg;
8367 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008368 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008369 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008370 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008371 psa_status_t actual_status;
8372 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008373 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008374
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008375 TEST_CALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008376
Gilles Peskine449bd832023-01-11 14:50:10 +01008377 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008378
Gilles Peskine449bd832023-01-11 14:50:10 +01008379 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8380 psa_set_key_algorithm(&attributes, alg);
8381 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008382
Gilles Peskine449bd832023-01-11 14:50:10 +01008383 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8384 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008385
Gilles Peskine449bd832023-01-11 14:50:10 +01008386 actual_status = psa_asymmetric_decrypt(key, alg,
8387 input_data->x, input_data->len,
8388 label->x, label->len,
8389 output, output_size,
8390 &output_length);
8391 TEST_EQUAL(actual_status, expected_status);
8392 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008393
Gilles Peskine68428122018-06-30 18:42:41 +02008394 /* If the label is empty, the test framework puts a non-null pointer
8395 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008396 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008397 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008398 if (output_size != 0) {
8399 memset(output, 0, output_size);
8400 }
8401 actual_status = psa_asymmetric_decrypt(key, alg,
8402 input_data->x, input_data->len,
8403 NULL, label->len,
8404 output, output_size,
8405 &output_length);
8406 TEST_EQUAL(actual_status, expected_status);
8407 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008408 }
8409
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008410exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008411 psa_reset_key_attributes(&attributes);
8412 psa_destroy_key(key);
8413 mbedtls_free(output);
8414 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008415}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008416/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008417
8418/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008419void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008420{
8421 /* Test each valid way of initializing the object, except for `= {0}`, as
8422 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8423 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008424 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008425 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008426 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008427 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8428 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008429
Gilles Peskine449bd832023-01-11 14:50:10 +01008430 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008431
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008432 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008433 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8434 PSA_ERROR_BAD_STATE);
8435 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8436 PSA_ERROR_BAD_STATE);
8437 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8438 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008439
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008440 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008441 PSA_ASSERT(psa_key_derivation_abort(&func));
8442 PSA_ASSERT(psa_key_derivation_abort(&init));
8443 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008444}
8445/* END_CASE */
8446
Janos Follath16de4a42019-06-13 16:32:24 +01008447/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008448void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008449{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008450 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008451 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008452 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008453
Gilles Peskine449bd832023-01-11 14:50:10 +01008454 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008455
Gilles Peskine449bd832023-01-11 14:50:10 +01008456 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8457 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008458
8459exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008460 psa_key_derivation_abort(&operation);
8461 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008462}
8463/* END_CASE */
8464
Janos Follathaf3c2a02019-06-12 12:34:34 +01008465/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008466void derive_set_capacity(int alg_arg, int capacity_arg,
8467 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008468{
8469 psa_algorithm_t alg = alg_arg;
8470 size_t capacity = capacity_arg;
8471 psa_status_t expected_status = expected_status_arg;
8472 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8473
Gilles Peskine449bd832023-01-11 14:50:10 +01008474 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008475
Gilles Peskine449bd832023-01-11 14:50:10 +01008476 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008477
Gilles Peskine449bd832023-01-11 14:50:10 +01008478 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8479 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008480
8481exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008482 psa_key_derivation_abort(&operation);
8483 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008484}
8485/* END_CASE */
8486
8487/* BEGIN_CASE */
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308488void parse_binary_string_test(data_t *input, int output)
8489{
8490 uint64_t value;
8491 value = parse_binary_string(input);
8492 TEST_EQUAL(value, output);
8493}
8494/* END_CASE */
8495
8496/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008497void derive_input(int alg_arg,
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308498 int step_arg1, int key_type_arg1, data_t *input1,
8499 int expected_status_arg1,
8500 int step_arg2, int key_type_arg2, data_t *input2,
8501 int expected_status_arg2,
8502 int step_arg3, int key_type_arg3, data_t *input3,
8503 int expected_status_arg3,
Gilles Peskine449bd832023-01-11 14:50:10 +01008504 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008505{
8506 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008507 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308508 uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
Gilles Peskine449bd832023-01-11 14:50:10 +01008509 psa_status_t expected_statuses[] = { expected_status_arg1,
8510 expected_status_arg2,
8511 expected_status_arg3 };
8512 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008513 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8514 MBEDTLS_SVC_KEY_ID_INIT,
8515 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008516 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8517 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8518 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008519 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008520 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008521 psa_status_t expected_output_status = expected_output_status_arg;
8522 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008523
Gilles Peskine449bd832023-01-11 14:50:10 +01008524 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008525
Gilles Peskine449bd832023-01-11 14:50:10 +01008526 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8527 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008528
Gilles Peskine449bd832023-01-11 14:50:10 +01008529 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008530
Gilles Peskine449bd832023-01-11 14:50:10 +01008531 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8532 mbedtls_test_set_step(i);
8533 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008534 /* Skip this step */
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308535 } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE &&
8536 key_types[i] != INPUT_INTEGER) {
8537 psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i]));
Gilles Peskine449bd832023-01-11 14:50:10 +01008538 PSA_ASSERT(psa_import_key(&attributes,
8539 inputs[i]->x, inputs[i]->len,
8540 &keys[i]));
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308541 if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008542 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008543 // When taking a private key as secret input, use key agreement
8544 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008545 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8546 &operation, keys[i]),
8547 expected_statuses[i]);
8548 } else {
8549 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8550 keys[i]),
8551 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008552 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008553 } else {
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308554 if (key_types[i] == INPUT_INTEGER) {
8555 TEST_EQUAL(psa_key_derivation_input_integer(
8556 &operation, steps[i],
8557 parse_binary_string(inputs[i])),
8558 expected_statuses[i]);
8559 } else {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308560 TEST_EQUAL(psa_key_derivation_input_bytes(
8561 &operation, steps[i],
8562 inputs[i]->x, inputs[i]->len),
8563 expected_statuses[i]);
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308564 }
Janos Follathaf3c2a02019-06-12 12:34:34 +01008565 }
8566 }
8567
Gilles Peskine449bd832023-01-11 14:50:10 +01008568 if (output_key_type != PSA_KEY_TYPE_NONE) {
8569 psa_reset_key_attributes(&attributes);
8570 psa_set_key_type(&attributes, output_key_type);
8571 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008572 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008573 psa_key_derivation_output_key(&attributes, &operation,
8574 &output_key);
8575 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008576 uint8_t buffer[1];
8577 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008578 psa_key_derivation_output_bytes(&operation,
8579 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008580 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008581 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008582
Janos Follathaf3c2a02019-06-12 12:34:34 +01008583exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008584 psa_key_derivation_abort(&operation);
8585 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8586 psa_destroy_key(keys[i]);
8587 }
8588 psa_destroy_key(output_key);
8589 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008590}
8591/* END_CASE */
8592
Kusumit Ghoderao42b02b92023-06-06 16:48:46 +05308593/* BEGIN_CASE*/
8594void derive_input_invalid_cost(int alg_arg, int64_t cost)
8595{
8596 psa_algorithm_t alg = alg_arg;
8597 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8598
8599 PSA_ASSERT(psa_crypto_init());
8600 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8601
8602 TEST_EQUAL(psa_key_derivation_input_integer(&operation,
8603 PSA_KEY_DERIVATION_INPUT_COST,
8604 cost),
8605 PSA_ERROR_NOT_SUPPORTED);
8606
8607exit:
8608 psa_key_derivation_abort(&operation);
8609 PSA_DONE();
8610}
8611/* END_CASE*/
8612
Janos Follathd958bb72019-07-03 15:02:16 +01008613/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008614void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008615{
Janos Follathd958bb72019-07-03 15:02:16 +01008616 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008617 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008618 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008619 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008620 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008621 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008622 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008623 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008624 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008625 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008626 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8627 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008628 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008629 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008630
Gilles Peskine449bd832023-01-11 14:50:10 +01008631 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008632
Gilles Peskine449bd832023-01-11 14:50:10 +01008633 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8634 psa_set_key_algorithm(&attributes, alg);
8635 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008636
Gilles Peskine449bd832023-01-11 14:50:10 +01008637 PSA_ASSERT(psa_import_key(&attributes,
8638 key_data, sizeof(key_data),
8639 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008640
8641 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008642 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8643 input1, input1_length,
8644 input2, input2_length,
8645 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008646 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008647 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008648
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008649 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008650 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8651 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008652
Gilles Peskine449bd832023-01-11 14:50:10 +01008653 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008654
Gilles Peskine449bd832023-01-11 14:50:10 +01008655 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8656 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008657
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008658exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008659 psa_key_derivation_abort(&operation);
8660 psa_destroy_key(key);
8661 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008662}
8663/* END_CASE */
8664
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008665/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008666void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008667{
8668 uint8_t output_buffer[16];
8669 size_t buffer_size = 16;
8670 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008671 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008672
Gilles Peskine449bd832023-01-11 14:50:10 +01008673 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8674 output_buffer, buffer_size)
8675 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008676
Gilles Peskine449bd832023-01-11 14:50:10 +01008677 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8678 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008679
Gilles Peskine449bd832023-01-11 14:50:10 +01008680 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008681
Gilles Peskine449bd832023-01-11 14:50:10 +01008682 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8683 output_buffer, buffer_size)
8684 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008685
Gilles Peskine449bd832023-01-11 14:50:10 +01008686 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8687 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008688
8689exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008690 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008691}
8692/* END_CASE */
8693
8694/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008695void derive_output(int alg_arg,
8696 int step1_arg, data_t *input1, int expected_status_arg1,
8697 int step2_arg, data_t *input2, int expected_status_arg2,
8698 int step3_arg, data_t *input3, int expected_status_arg3,
8699 int step4_arg, data_t *input4, int expected_status_arg4,
8700 data_t *key_agreement_peer_key,
8701 int requested_capacity_arg,
8702 data_t *expected_output1,
8703 data_t *expected_output2,
8704 int other_key_input_type,
8705 int key_input_type,
8706 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008707{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008708 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008709 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8710 data_t *inputs[] = { input1, input2, input3, input4 };
8711 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8712 MBEDTLS_SVC_KEY_ID_INIT,
8713 MBEDTLS_SVC_KEY_ID_INIT,
8714 MBEDTLS_SVC_KEY_ID_INIT };
8715 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8716 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008717 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008718 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008719 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008720 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008721 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008722 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008723 size_t output_buffer_size = 0;
8724 uint8_t *output_buffer = NULL;
8725 size_t expected_capacity;
8726 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008727 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8728 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8729 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8730 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008731 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008732 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008733 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008734
Gilles Peskine449bd832023-01-11 14:50:10 +01008735 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8736 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008737 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008738 }
8739 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008740 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008741 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008742 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008743 TEST_CALLOC(output_buffer, output_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008744 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008745
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008746 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008747 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8748 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8749 requested_capacity));
8750 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8751 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008752 case 0:
8753 break;
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308754 case PSA_KEY_DERIVATION_INPUT_COST:
8755 TEST_EQUAL(psa_key_derivation_input_integer(
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308756 &operation, steps[i],
8757 parse_binary_string(inputs[i])),
8758 statuses[i]);
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308759 if (statuses[i] != PSA_SUCCESS) {
8760 goto exit;
8761 }
8762 break;
8763 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Gilles Peskine1468da72019-05-29 17:35:49 +02008764 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008765 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008766 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008767 TEST_EQUAL(psa_key_derivation_input_bytes(
8768 &operation, steps[i],
8769 inputs[i]->x, inputs[i]->len),
8770 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008771
Gilles Peskine449bd832023-01-11 14:50:10 +01008772 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008773 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008774 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008775 break;
8776 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008777 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8778 psa_set_key_algorithm(&attributes1, alg);
8779 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008780
Gilles Peskine449bd832023-01-11 14:50:10 +01008781 PSA_ASSERT(psa_import_key(&attributes1,
8782 inputs[i]->x, inputs[i]->len,
8783 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008784
Gilles Peskine449bd832023-01-11 14:50:10 +01008785 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8786 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8787 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8788 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008789 }
8790
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308791 TEST_EQUAL(psa_key_derivation_input_key(&operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01008792 steps[i],
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308793 keys[i]),
8794 statuses[i]);
8795
8796 if (statuses[i] != PSA_SUCCESS) {
8797 goto exit;
8798 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008799 break;
8800 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008801 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008802 break;
8803 }
8804 break;
8805 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008806 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008807 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008808 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8809 steps[i],
8810 inputs[i]->x,
8811 inputs[i]->len),
8812 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008813 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008814 case 1: // input key, type DERIVE
8815 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8817 psa_set_key_algorithm(&attributes2, alg);
8818 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008819
8820 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 if (other_key_input_type == 11) {
8822 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8823 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008824
Gilles Peskine449bd832023-01-11 14:50:10 +01008825 PSA_ASSERT(psa_import_key(&attributes2,
8826 inputs[i]->x, inputs[i]->len,
8827 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008828
Gilles Peskine449bd832023-01-11 14:50:10 +01008829 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8830 steps[i],
8831 keys[i]),
8832 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008833 break;
8834 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008835 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8836 psa_set_key_algorithm(&attributes3, alg);
8837 psa_set_key_type(&attributes3,
8838 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008839
Gilles Peskine449bd832023-01-11 14:50:10 +01008840 PSA_ASSERT(psa_import_key(&attributes3,
8841 inputs[i]->x, inputs[i]->len,
8842 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008843
Gilles Peskine449bd832023-01-11 14:50:10 +01008844 TEST_EQUAL(psa_key_derivation_key_agreement(
8845 &operation,
8846 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8847 keys[i], key_agreement_peer_key->x,
8848 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008849 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008850 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008851 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008852 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008853 }
8854
Gilles Peskine449bd832023-01-11 14:50:10 +01008855 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008856 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008857 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008858 break;
8859 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008860 TEST_EQUAL(psa_key_derivation_input_bytes(
8861 &operation, steps[i],
8862 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008863
Gilles Peskine449bd832023-01-11 14:50:10 +01008864 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008865 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008866 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008867 break;
8868 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008869 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008870
Gilles Peskine449bd832023-01-11 14:50:10 +01008871 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8872 &current_capacity));
8873 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008874 expected_capacity = requested_capacity;
8875
Gilles Peskine449bd832023-01-11 14:50:10 +01008876 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008877 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8878
8879 /* For output key derivation secret must be provided using
8880 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008881 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008882 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008883 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008884
Gilles Peskine449bd832023-01-11 14:50:10 +01008885 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8886 psa_set_key_algorithm(&attributes4, alg);
8887 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8888 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008889
Gilles Peskine449bd832023-01-11 14:50:10 +01008890 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8891 &derived_key), expected_status);
8892 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008893 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008894 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008895 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008896 status = psa_key_derivation_output_bytes(&operation,
8897 output_buffer, output_sizes[i]);
8898 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008899 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008900 TEST_ASSERT(status == PSA_SUCCESS ||
8901 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008902 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008903 } else if (expected_capacity == 0 ||
8904 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008905 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008906 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008907 expected_capacity = 0;
8908 continue;
8909 }
8910 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008911 PSA_ASSERT(status);
8912 if (output_sizes[i] != 0) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008913 TEST_MEMORY_COMPARE(output_buffer, output_sizes[i],
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008914 expected_outputs[i], output_sizes[i]);
Gilles Peskine449bd832023-01-11 14:50:10 +01008915 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008916 /* Check the operation status. */
8917 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008918 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8919 &current_capacity));
8920 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008921 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008922 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008923 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008924
8925exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008926 mbedtls_free(output_buffer);
8927 psa_key_derivation_abort(&operation);
8928 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8929 psa_destroy_key(keys[i]);
8930 }
8931 psa_destroy_key(derived_key);
8932 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008933}
8934/* END_CASE */
8935
8936/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008937void derive_full(int alg_arg,
8938 data_t *key_data,
8939 data_t *input1,
8940 data_t *input2,
8941 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008942{
Ronald Cron5425a212020-08-04 14:58:35 +02008943 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008944 psa_algorithm_t alg = alg_arg;
8945 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008946 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008947 unsigned char output_buffer[16];
8948 size_t expected_capacity = requested_capacity;
8949 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008950 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008951
Gilles Peskine449bd832023-01-11 14:50:10 +01008952 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008953
Gilles Peskine449bd832023-01-11 14:50:10 +01008954 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8955 psa_set_key_algorithm(&attributes, alg);
8956 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008957
Gilles Peskine449bd832023-01-11 14:50:10 +01008958 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8959 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008960
Gilles Peskine449bd832023-01-11 14:50:10 +01008961 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8962 input1->x, input1->len,
8963 input2->x, input2->len,
8964 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008965 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008966 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008967
Gilles Peskine449bd832023-01-11 14:50:10 +01008968 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8969 &current_capacity));
8970 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008971
8972 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008973 while (current_capacity > 0) {
8974 size_t read_size = sizeof(output_buffer);
8975 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008976 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008977 }
8978 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8979 output_buffer,
8980 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008981 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008982 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8983 &current_capacity));
8984 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008985 }
8986
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008987 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008988 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8989 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008990
Gilles Peskine449bd832023-01-11 14:50:10 +01008991 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008992
8993exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008994 psa_key_derivation_abort(&operation);
8995 psa_destroy_key(key);
8996 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008997}
8998/* END_CASE */
8999
Stephan Koch78109f52023-04-12 14:19:36 +02009000/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01009001void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
9002 int derivation_step,
9003 int capacity, int expected_capacity_status_arg,
9004 data_t *expected_output,
9005 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009006{
9007 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
9008 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04009009 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009010 uint8_t *output_buffer = NULL;
9011 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04009012 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
9013 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
9014 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009015
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009016 TEST_CALLOC(output_buffer, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009017 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009018
Gilles Peskine449bd832023-01-11 14:50:10 +01009019 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9020 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
9021 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009022
Gilles Peskine449bd832023-01-11 14:50:10 +01009023 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
9024 step, input->x, input->len),
9025 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009026
Gilles Peskine449bd832023-01-11 14:50:10 +01009027 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009028 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009029 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009030
Gilles Peskine449bd832023-01-11 14:50:10 +01009031 status = psa_key_derivation_output_bytes(&operation, output_buffer,
9032 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009033
Gilles Peskine449bd832023-01-11 14:50:10 +01009034 TEST_EQUAL(status, expected_output_status);
9035 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009036 TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009037 expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009038 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009039
9040exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009041 mbedtls_free(output_buffer);
9042 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009043 PSA_DONE();
9044}
9045/* END_CASE */
9046
Janos Follathe60c9052019-07-03 13:51:30 +01009047/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009048void derive_key_exercise(int alg_arg,
9049 data_t *key_data,
9050 data_t *input1,
9051 data_t *input2,
9052 int derived_type_arg,
9053 int derived_bits_arg,
9054 int derived_usage_arg,
9055 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009056{
Ronald Cron5425a212020-08-04 14:58:35 +02009057 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9058 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009059 psa_algorithm_t alg = alg_arg;
9060 psa_key_type_t derived_type = derived_type_arg;
9061 size_t derived_bits = derived_bits_arg;
9062 psa_key_usage_t derived_usage = derived_usage_arg;
9063 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009064 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009065 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009066 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009067 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009068
Gilles Peskine449bd832023-01-11 14:50:10 +01009069 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009070
Gilles Peskine449bd832023-01-11 14:50:10 +01009071 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9072 psa_set_key_algorithm(&attributes, alg);
9073 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9074 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9075 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009076
9077 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009078 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9079 input1->x, input1->len,
9080 input2->x, input2->len,
9081 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009082 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009083 }
Janos Follathe60c9052019-07-03 13:51:30 +01009084
Gilles Peskine449bd832023-01-11 14:50:10 +01009085 psa_set_key_usage_flags(&attributes, derived_usage);
9086 psa_set_key_algorithm(&attributes, derived_alg);
9087 psa_set_key_type(&attributes, derived_type);
9088 psa_set_key_bits(&attributes, derived_bits);
9089 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9090 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009091
9092 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009093 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9094 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9095 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009096
9097 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009098 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009099 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009100 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009101
9102exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009103 /*
9104 * Key attributes may have been returned by psa_get_key_attributes()
9105 * thus reset them as required.
9106 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009107 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009108
Gilles Peskine449bd832023-01-11 14:50:10 +01009109 psa_key_derivation_abort(&operation);
9110 psa_destroy_key(base_key);
9111 psa_destroy_key(derived_key);
9112 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009113}
9114/* END_CASE */
9115
Janos Follath42fd8882019-07-03 14:17:09 +01009116/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009117void derive_key_export(int alg_arg,
9118 data_t *key_data,
9119 data_t *input1,
9120 data_t *input2,
9121 int bytes1_arg,
9122 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009123{
Ronald Cron5425a212020-08-04 14:58:35 +02009124 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9125 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009126 psa_algorithm_t alg = alg_arg;
9127 size_t bytes1 = bytes1_arg;
9128 size_t bytes2 = bytes2_arg;
9129 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009130 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009131 uint8_t *output_buffer = NULL;
9132 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009133 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9134 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009135 size_t length;
9136
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009137 TEST_CALLOC(output_buffer, capacity);
9138 TEST_CALLOC(export_buffer, capacity);
Gilles Peskine449bd832023-01-11 14:50:10 +01009139 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009140
Gilles Peskine449bd832023-01-11 14:50:10 +01009141 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9142 psa_set_key_algorithm(&base_attributes, alg);
9143 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9144 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9145 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009146
9147 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009148 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9149 input1->x, input1->len,
9150 input2->x, input2->len,
9151 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009152 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009153 }
Janos Follath42fd8882019-07-03 14:17:09 +01009154
Gilles Peskine449bd832023-01-11 14:50:10 +01009155 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9156 output_buffer,
9157 capacity));
9158 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009159
9160 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009161 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9162 input1->x, input1->len,
9163 input2->x, input2->len,
9164 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009165 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009166 }
Janos Follath42fd8882019-07-03 14:17:09 +01009167
Gilles Peskine449bd832023-01-11 14:50:10 +01009168 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9169 psa_set_key_algorithm(&derived_attributes, 0);
9170 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9171 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9172 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9173 &derived_key));
9174 PSA_ASSERT(psa_export_key(derived_key,
9175 export_buffer, bytes1,
9176 &length));
9177 TEST_EQUAL(length, bytes1);
9178 PSA_ASSERT(psa_destroy_key(derived_key));
9179 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9180 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9181 &derived_key));
9182 PSA_ASSERT(psa_export_key(derived_key,
9183 export_buffer + bytes1, bytes2,
9184 &length));
9185 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009186
9187 /* Compare the outputs from the two runs. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009188 TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009189 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009190
9191exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009192 mbedtls_free(output_buffer);
9193 mbedtls_free(export_buffer);
9194 psa_key_derivation_abort(&operation);
9195 psa_destroy_key(base_key);
9196 psa_destroy_key(derived_key);
9197 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009198}
9199/* END_CASE */
9200
9201/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009202void derive_key_type(int alg_arg,
9203 data_t *key_data,
9204 data_t *input1,
9205 data_t *input2,
9206 int key_type_arg, int bits_arg,
9207 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009208{
9209 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9210 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9211 const psa_algorithm_t alg = alg_arg;
9212 const psa_key_type_t key_type = key_type_arg;
9213 const size_t bits = bits_arg;
9214 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9215 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009216 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009217 uint8_t *export_buffer = NULL;
9218 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9219 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9220 size_t export_length;
9221
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009222 TEST_CALLOC(export_buffer, export_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01009223 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009224
Gilles Peskine449bd832023-01-11 14:50:10 +01009225 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9226 psa_set_key_algorithm(&base_attributes, alg);
9227 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9228 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9229 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009230
Gilles Peskine449bd832023-01-11 14:50:10 +01009231 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009232 &operation, base_key, alg,
9233 input1->x, input1->len,
9234 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009235 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009236 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009237 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009238
Gilles Peskine449bd832023-01-11 14:50:10 +01009239 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9240 psa_set_key_algorithm(&derived_attributes, 0);
9241 psa_set_key_type(&derived_attributes, key_type);
9242 psa_set_key_bits(&derived_attributes, bits);
9243 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9244 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009245
Gilles Peskine449bd832023-01-11 14:50:10 +01009246 PSA_ASSERT(psa_export_key(derived_key,
9247 export_buffer, export_buffer_size,
9248 &export_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009249 TEST_MEMORY_COMPARE(export_buffer, export_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009250 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009251
9252exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009253 mbedtls_free(export_buffer);
9254 psa_key_derivation_abort(&operation);
9255 psa_destroy_key(base_key);
9256 psa_destroy_key(derived_key);
9257 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009258}
9259/* END_CASE */
9260
9261/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009262void derive_key(int alg_arg,
9263 data_t *key_data, data_t *input1, data_t *input2,
9264 int type_arg, int bits_arg,
9265 int expected_status_arg,
9266 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009267{
Ronald Cron5425a212020-08-04 14:58:35 +02009268 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9269 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009270 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009271 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009272 size_t bits = bits_arg;
9273 psa_status_t expected_status = expected_status_arg;
9274 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9275 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9276 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9277
Gilles Peskine449bd832023-01-11 14:50:10 +01009278 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009279
Gilles Peskine449bd832023-01-11 14:50:10 +01009280 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9281 psa_set_key_algorithm(&base_attributes, alg);
9282 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9283 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9284 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009285
Gilles Peskine449bd832023-01-11 14:50:10 +01009286 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9287 input1->x, input1->len,
9288 input2->x, input2->len,
9289 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009290 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009291 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009292
Gilles Peskine449bd832023-01-11 14:50:10 +01009293 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9294 psa_set_key_algorithm(&derived_attributes, 0);
9295 psa_set_key_type(&derived_attributes, type);
9296 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009297
9298 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009299 psa_key_derivation_output_key(&derived_attributes,
9300 &operation,
9301 &derived_key);
9302 if (is_large_output > 0) {
9303 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9304 }
9305 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009306
9307exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009308 psa_key_derivation_abort(&operation);
9309 psa_destroy_key(base_key);
9310 psa_destroy_key(derived_key);
9311 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009312}
9313/* END_CASE */
9314
9315/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009316void key_agreement_setup(int alg_arg,
9317 int our_key_type_arg, int our_key_alg_arg,
9318 data_t *our_key_data, data_t *peer_key_data,
9319 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009320{
Ronald Cron5425a212020-08-04 14:58:35 +02009321 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009322 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009323 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009324 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009325 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009326 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009327 psa_status_t expected_status = expected_status_arg;
9328 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009329
Gilles Peskine449bd832023-01-11 14:50:10 +01009330 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009331
Gilles Peskine449bd832023-01-11 14:50:10 +01009332 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9333 psa_set_key_algorithm(&attributes, our_key_alg);
9334 psa_set_key_type(&attributes, our_key_type);
9335 PSA_ASSERT(psa_import_key(&attributes,
9336 our_key_data->x, our_key_data->len,
9337 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009338
Gilles Peskine77f40d82019-04-11 21:27:06 +02009339 /* The tests currently include inputs that should fail at either step.
9340 * Test cases that fail at the setup step should be changed to call
9341 * key_derivation_setup instead, and this function should be renamed
9342 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009343 status = psa_key_derivation_setup(&operation, alg);
9344 if (status == PSA_SUCCESS) {
9345 TEST_EQUAL(psa_key_derivation_key_agreement(
9346 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9347 our_key,
9348 peer_key_data->x, peer_key_data->len),
9349 expected_status);
9350 } else {
9351 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009352 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009353
9354exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009355 psa_key_derivation_abort(&operation);
9356 psa_destroy_key(our_key);
9357 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009358}
9359/* END_CASE */
9360
9361/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009362void raw_key_agreement(int alg_arg,
9363 int our_key_type_arg, data_t *our_key_data,
9364 data_t *peer_key_data,
9365 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009366{
Ronald Cron5425a212020-08-04 14:58:35 +02009367 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009368 psa_algorithm_t alg = alg_arg;
9369 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009370 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009371 unsigned char *output = NULL;
9372 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009373 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009374
Gilles Peskine449bd832023-01-11 14:50:10 +01009375 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009376
Gilles Peskine449bd832023-01-11 14:50:10 +01009377 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9378 psa_set_key_algorithm(&attributes, alg);
9379 psa_set_key_type(&attributes, our_key_type);
9380 PSA_ASSERT(psa_import_key(&attributes,
9381 our_key_data->x, our_key_data->len,
9382 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009383
Gilles Peskine449bd832023-01-11 14:50:10 +01009384 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9385 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009386
Gilles Peskine992bee82022-04-13 23:25:52 +02009387 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009388 TEST_LE_U(expected_output->len,
9389 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9390 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9391 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009392
9393 /* Good case with exact output size */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009394 TEST_CALLOC(output, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009395 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9396 peer_key_data->x, peer_key_data->len,
9397 output, expected_output->len,
9398 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009399 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009400 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009401 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009402 output = NULL;
9403 output_length = ~0;
9404
9405 /* Larger buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009406 TEST_CALLOC(output, expected_output->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009407 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9408 peer_key_data->x, peer_key_data->len,
9409 output, expected_output->len + 1,
9410 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009411 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009412 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009413 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009414 output = NULL;
9415 output_length = ~0;
9416
9417 /* Buffer too small */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009418 TEST_CALLOC(output, expected_output->len - 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009419 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9420 peer_key_data->x, peer_key_data->len,
9421 output, expected_output->len - 1,
9422 &output_length),
9423 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009424 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009425 TEST_LE_U(output_length, expected_output->len - 1);
9426 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009427 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009428
9429exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009430 mbedtls_free(output);
9431 psa_destroy_key(our_key);
9432 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009433}
9434/* END_CASE */
9435
9436/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009437void key_agreement_capacity(int alg_arg,
9438 int our_key_type_arg, data_t *our_key_data,
9439 data_t *peer_key_data,
9440 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009441{
Ronald Cron5425a212020-08-04 14:58:35 +02009442 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009443 psa_algorithm_t alg = alg_arg;
9444 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009445 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009446 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009447 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009448 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009449
Gilles Peskine449bd832023-01-11 14:50:10 +01009450 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009451
Gilles Peskine449bd832023-01-11 14:50:10 +01009452 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9453 psa_set_key_algorithm(&attributes, alg);
9454 psa_set_key_type(&attributes, our_key_type);
9455 PSA_ASSERT(psa_import_key(&attributes,
9456 our_key_data->x, our_key_data->len,
9457 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009458
Gilles Peskine449bd832023-01-11 14:50:10 +01009459 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9460 PSA_ASSERT(psa_key_derivation_key_agreement(
9461 &operation,
9462 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9463 peer_key_data->x, peer_key_data->len));
9464 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009465 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009466 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9467 PSA_KEY_DERIVATION_INPUT_INFO,
9468 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009469 }
Gilles Peskine59685592018-09-18 12:11:34 +02009470
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009471 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009472 PSA_ASSERT(psa_key_derivation_get_capacity(
9473 &operation, &actual_capacity));
9474 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009475
Gilles Peskinebf491972018-10-25 22:36:12 +02009476 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009477 while (actual_capacity > sizeof(output)) {
9478 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9479 output, sizeof(output)));
9480 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009481 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009482 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9483 output, actual_capacity));
9484 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9485 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009486
Gilles Peskine59685592018-09-18 12:11:34 +02009487exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009488 psa_key_derivation_abort(&operation);
9489 psa_destroy_key(our_key);
9490 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009491}
9492/* END_CASE */
9493
9494/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009495void key_agreement_output(int alg_arg,
9496 int our_key_type_arg, data_t *our_key_data,
9497 data_t *peer_key_data,
9498 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009499{
Ronald Cron5425a212020-08-04 14:58:35 +02009500 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009501 psa_algorithm_t alg = alg_arg;
9502 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009503 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009504 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009505 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009506
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009507 TEST_CALLOC(actual_output, MAX(expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009508 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009509
Gilles Peskine449bd832023-01-11 14:50:10 +01009510 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009511
Gilles Peskine449bd832023-01-11 14:50:10 +01009512 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9513 psa_set_key_algorithm(&attributes, alg);
9514 psa_set_key_type(&attributes, our_key_type);
9515 PSA_ASSERT(psa_import_key(&attributes,
9516 our_key_data->x, our_key_data->len,
9517 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009518
Gilles Peskine449bd832023-01-11 14:50:10 +01009519 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9520 PSA_ASSERT(psa_key_derivation_key_agreement(
9521 &operation,
9522 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9523 peer_key_data->x, peer_key_data->len));
9524 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009525 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009526 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9527 PSA_KEY_DERIVATION_INPUT_INFO,
9528 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009529 }
Gilles Peskine59685592018-09-18 12:11:34 +02009530
Gilles Peskine449bd832023-01-11 14:50:10 +01009531 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9532 actual_output,
9533 expected_output1->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009534 TEST_MEMORY_COMPARE(actual_output, expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009535 expected_output1->x, expected_output1->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009536 if (expected_output2->len != 0) {
9537 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9538 actual_output,
9539 expected_output2->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009540 TEST_MEMORY_COMPARE(actual_output, expected_output2->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009541 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009542 }
Gilles Peskine59685592018-09-18 12:11:34 +02009543
9544exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009545 psa_key_derivation_abort(&operation);
9546 psa_destroy_key(our_key);
9547 PSA_DONE();
9548 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009549}
9550/* END_CASE */
9551
9552/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009553void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009554{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009555 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009556 unsigned char *output = NULL;
9557 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009558 size_t i;
9559 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009560
Gilles Peskine449bd832023-01-11 14:50:10 +01009561 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009562
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009563 TEST_CALLOC(output, bytes);
9564 TEST_CALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009565
Gilles Peskine449bd832023-01-11 14:50:10 +01009566 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009567
Gilles Peskinea50d7392018-06-21 10:22:13 +02009568 /* Run several times, to ensure that every output byte will be
9569 * nonzero at least once with overwhelming probability
9570 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009571 for (run = 0; run < 10; run++) {
9572 if (bytes != 0) {
9573 memset(output, 0, bytes);
9574 }
9575 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009576
Gilles Peskine449bd832023-01-11 14:50:10 +01009577 for (i = 0; i < bytes; i++) {
9578 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009579 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009580 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009581 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009582 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009583
9584 /* Check that every byte was changed to nonzero at least once. This
9585 * validates that psa_generate_random is overwriting every byte of
9586 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009587 for (i = 0; i < bytes; i++) {
9588 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009589 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009590
9591exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009592 PSA_DONE();
9593 mbedtls_free(output);
9594 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009595}
9596/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009597
9598/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009599void generate_key(int type_arg,
9600 int bits_arg,
9601 int usage_arg,
9602 int alg_arg,
9603 int expected_status_arg,
9604 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009605{
Ronald Cron5425a212020-08-04 14:58:35 +02009606 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009607 psa_key_type_t type = type_arg;
9608 psa_key_usage_t usage = usage_arg;
9609 size_t bits = bits_arg;
9610 psa_algorithm_t alg = alg_arg;
9611 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009612 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009613 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009614
Gilles Peskine449bd832023-01-11 14:50:10 +01009615 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009616
Gilles Peskine449bd832023-01-11 14:50:10 +01009617 psa_set_key_usage_flags(&attributes, usage);
9618 psa_set_key_algorithm(&attributes, alg);
9619 psa_set_key_type(&attributes, type);
9620 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009621
9622 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009623 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009624
Gilles Peskine449bd832023-01-11 14:50:10 +01009625 if (is_large_key > 0) {
9626 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9627 }
9628 TEST_EQUAL(status, expected_status);
9629 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009630 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009631 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009632
9633 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009634 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9635 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9636 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009637
Gilles Peskine818ca122018-06-20 18:16:48 +02009638 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009639 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009640 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009641 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009642
9643exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009644 /*
9645 * Key attributes may have been returned by psa_get_key_attributes()
9646 * thus reset them as required.
9647 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009648 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009649
Gilles Peskine449bd832023-01-11 14:50:10 +01009650 psa_destroy_key(key);
9651 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009652}
9653/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009654
Valerio Setti19fec542023-07-25 12:31:50 +02009655/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine449bd832023-01-11 14:50:10 +01009656void generate_key_rsa(int bits_arg,
9657 data_t *e_arg,
9658 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009659{
Ronald Cron5425a212020-08-04 14:58:35 +02009660 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009661 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009662 size_t bits = bits_arg;
9663 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9664 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9665 psa_status_t expected_status = expected_status_arg;
9666 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9667 uint8_t *exported = NULL;
9668 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009669 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009670 size_t exported_length = SIZE_MAX;
9671 uint8_t *e_read_buffer = NULL;
9672 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009673 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009674 size_t e_read_length = SIZE_MAX;
9675
Gilles Peskine449bd832023-01-11 14:50:10 +01009676 if (e_arg->len == 0 ||
9677 (e_arg->len == 3 &&
9678 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009679 is_default_public_exponent = 1;
9680 e_read_size = 0;
9681 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009682 TEST_CALLOC(e_read_buffer, e_read_size);
9683 TEST_CALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009684
Gilles Peskine449bd832023-01-11 14:50:10 +01009685 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009686
Gilles Peskine449bd832023-01-11 14:50:10 +01009687 psa_set_key_usage_flags(&attributes, usage);
9688 psa_set_key_algorithm(&attributes, alg);
9689 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9690 e_arg->x, e_arg->len));
9691 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009692
9693 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009694 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9695 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009696 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009697 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009698
9699 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009700 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9701 TEST_EQUAL(psa_get_key_type(&attributes), type);
9702 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9703 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
9704 e_read_buffer, e_read_size,
9705 &e_read_length));
9706 if (is_default_public_exponent) {
9707 TEST_EQUAL(e_read_length, 0);
9708 } else {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009709 TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009710 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009711
9712 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009713 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009714 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009715 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009716
9717 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009718 PSA_ASSERT(psa_export_public_key(key,
9719 exported, exported_size,
9720 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009721 {
9722 uint8_t *p = exported;
9723 uint8_t *end = exported + exported_length;
9724 size_t len;
9725 /* RSAPublicKey ::= SEQUENCE {
9726 * modulus INTEGER, -- n
9727 * publicExponent INTEGER } -- e
9728 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009729 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9730 MBEDTLS_ASN1_SEQUENCE |
9731 MBEDTLS_ASN1_CONSTRUCTED));
9732 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9733 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9734 MBEDTLS_ASN1_INTEGER));
9735 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009736 ++p;
9737 --len;
9738 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009739 if (e_arg->len == 0) {
9740 TEST_EQUAL(len, 3);
9741 TEST_EQUAL(p[0], 1);
9742 TEST_EQUAL(p[1], 0);
9743 TEST_EQUAL(p[2], 1);
9744 } else {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009745 TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009746 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009747 }
9748
9749exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009750 /*
9751 * Key attributes may have been returned by psa_get_key_attributes() or
9752 * set by psa_set_key_domain_parameters() thus reset them as required.
9753 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009754 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009755
Gilles Peskine449bd832023-01-11 14:50:10 +01009756 psa_destroy_key(key);
9757 PSA_DONE();
9758 mbedtls_free(e_read_buffer);
9759 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009760}
9761/* END_CASE */
9762
Darryl Greend49a4992018-06-18 17:27:26 +01009763/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009764void persistent_key_load_key_from_storage(data_t *data,
9765 int type_arg, int bits_arg,
9766 int usage_flags_arg, int alg_arg,
9767 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009768{
Gilles Peskine449bd832023-01-11 14:50:10 +01009769 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009770 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009771 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9772 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009773 psa_key_type_t type = type_arg;
9774 size_t bits = bits_arg;
9775 psa_key_usage_t usage_flags = usage_flags_arg;
9776 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009777 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009778 unsigned char *first_export = NULL;
9779 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009780 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009781 size_t first_exported_length;
9782 size_t second_exported_length;
9783
Gilles Peskine449bd832023-01-11 14:50:10 +01009784 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009785 TEST_CALLOC(first_export, export_size);
9786 TEST_CALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009787 }
Darryl Greend49a4992018-06-18 17:27:26 +01009788
Gilles Peskine449bd832023-01-11 14:50:10 +01009789 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009790
Gilles Peskine449bd832023-01-11 14:50:10 +01009791 psa_set_key_id(&attributes, key_id);
9792 psa_set_key_usage_flags(&attributes, usage_flags);
9793 psa_set_key_algorithm(&attributes, alg);
9794 psa_set_key_type(&attributes, type);
9795 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009796
Gilles Peskine449bd832023-01-11 14:50:10 +01009797 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009798 case IMPORT_KEY:
9799 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009800 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9801 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009802 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009803
Darryl Green0c6575a2018-11-07 16:05:30 +00009804 case GENERATE_KEY:
9805 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009806 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009807 break;
9808
9809 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009810#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009811 {
9812 /* Create base key */
9813 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9814 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9815 psa_set_key_usage_flags(&base_attributes,
9816 PSA_KEY_USAGE_DERIVE);
9817 psa_set_key_algorithm(&base_attributes, derive_alg);
9818 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9819 PSA_ASSERT(psa_import_key(&base_attributes,
9820 data->x, data->len,
9821 &base_key));
9822 /* Derive a key. */
9823 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9824 PSA_ASSERT(psa_key_derivation_input_key(
9825 &operation,
9826 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9827 PSA_ASSERT(psa_key_derivation_input_bytes(
9828 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9829 NULL, 0));
9830 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9831 &operation,
9832 &key));
9833 PSA_ASSERT(psa_key_derivation_abort(&operation));
9834 PSA_ASSERT(psa_destroy_key(base_key));
9835 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9836 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009837#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009838 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009839#endif
9840 break;
9841
9842 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01009843 TEST_FAIL("generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009844 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009845 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009846 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009847
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009848 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009849 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9850 PSA_ASSERT(psa_export_key(key,
9851 first_export, export_size,
9852 &first_exported_length));
9853 if (generation_method == IMPORT_KEY) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009854 TEST_MEMORY_COMPARE(data->x, data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009855 first_export, first_exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01009856 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009857 }
Darryl Greend49a4992018-06-18 17:27:26 +01009858
9859 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009860 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009861 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009862 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009863
Darryl Greend49a4992018-06-18 17:27:26 +01009864 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009865 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9866 TEST_ASSERT(mbedtls_svc_key_id_equal(
9867 psa_get_key_id(&attributes), key_id));
9868 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9869 PSA_KEY_LIFETIME_PERSISTENT);
9870 TEST_EQUAL(psa_get_key_type(&attributes), type);
9871 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9872 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9873 mbedtls_test_update_key_usage_flags(usage_flags));
9874 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009875
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009876 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009877 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9878 PSA_ASSERT(psa_export_key(key,
9879 second_export, export_size,
9880 &second_exported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009881 TEST_MEMORY_COMPARE(first_export, first_exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009882 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009883 }
9884
9885 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009886 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009887 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009888 }
Darryl Greend49a4992018-06-18 17:27:26 +01009889
9890exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009891 /*
9892 * Key attributes may have been returned by psa_get_key_attributes()
9893 * thus reset them as required.
9894 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009895 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009896
Gilles Peskine449bd832023-01-11 14:50:10 +01009897 mbedtls_free(first_export);
9898 mbedtls_free(second_export);
9899 psa_key_derivation_abort(&operation);
9900 psa_destroy_key(base_key);
9901 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009902 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009903}
9904/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009905
Neil Armstronga557cb82022-06-10 08:58:32 +02009906/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009907void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9908 int primitive_arg, int hash_arg, int role_arg,
9909 int test_input, data_t *pw_data,
9910 int inj_err_type_arg,
9911 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009912{
9913 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9914 psa_pake_operation_t operation = psa_pake_operation_init();
9915 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009916 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009917 psa_key_type_t key_type_pw = key_type_pw_arg;
9918 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009919 psa_algorithm_t hash_alg = hash_arg;
9920 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009921 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9922 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009923 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9924 psa_status_t expected_error = expected_error_arg;
9925 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009926 unsigned char *output_buffer = NULL;
9927 size_t output_len = 0;
9928
Gilles Peskine449bd832023-01-11 14:50:10 +01009929 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009930
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009931 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009932 PSA_PAKE_STEP_KEY_SHARE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009933 TEST_CALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009934
Gilles Peskine449bd832023-01-11 14:50:10 +01009935 if (pw_data->len > 0) {
9936 psa_set_key_usage_flags(&attributes, key_usage_pw);
9937 psa_set_key_algorithm(&attributes, alg);
9938 psa_set_key_type(&attributes, key_type_pw);
9939 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9940 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009941 }
9942
Gilles Peskine449bd832023-01-11 14:50:10 +01009943 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9944 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9945 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009946
Gilles Peskine449bd832023-01-11 14:50:10 +01009947 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009948
Gilles Peskine449bd832023-01-11 14:50:10 +01009949 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9950 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9951 expected_error);
9952 PSA_ASSERT(psa_pake_abort(&operation));
9953 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9954 expected_error);
9955 PSA_ASSERT(psa_pake_abort(&operation));
9956 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9957 expected_error);
9958 PSA_ASSERT(psa_pake_abort(&operation));
9959 TEST_EQUAL(psa_pake_set_role(&operation, role),
9960 expected_error);
9961 PSA_ASSERT(psa_pake_abort(&operation));
9962 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9963 NULL, 0, NULL),
9964 expected_error);
9965 PSA_ASSERT(psa_pake_abort(&operation));
9966 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9967 expected_error);
9968 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009969 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009970 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009971
Gilles Peskine449bd832023-01-11 14:50:10 +01009972 status = psa_pake_setup(&operation, &cipher_suite);
9973 if (status != PSA_SUCCESS) {
9974 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009975 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009976 }
9977
Gilles Peskine449bd832023-01-11 14:50:10 +01009978 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9979 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9980 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009981 goto exit;
9982 }
9983
Gilles Peskine449bd832023-01-11 14:50:10 +01009984 status = psa_pake_set_role(&operation, role);
9985 if (status != PSA_SUCCESS) {
9986 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009987 goto exit;
9988 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009989
Gilles Peskine449bd832023-01-11 14:50:10 +01009990 if (pw_data->len > 0) {
9991 status = psa_pake_set_password_key(&operation, key);
9992 if (status != PSA_SUCCESS) {
9993 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009994 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009995 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009996 }
9997
Gilles Peskine449bd832023-01-11 14:50:10 +01009998 if (inj_err_type == INJECT_ERR_INVALID_USER) {
9999 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10000 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010001 goto exit;
10002 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010003
Gilles Peskine449bd832023-01-11 14:50:10 +010010004 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
10005 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10006 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010007 goto exit;
10008 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010009
Gilles Peskine449bd832023-01-11 14:50:10 +010010010 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010011 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010012 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
10013 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010014 goto exit;
10015 }
10016
Gilles Peskine449bd832023-01-11 14:50:10 +010010017 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010018 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010019 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
10020 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010021 goto exit;
10022 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010023
Gilles Peskine449bd832023-01-11 14:50:10 +010010024 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
10025 PSA_PAKE_STEP_KEY_SHARE);
10026 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
10027 PSA_PAKE_STEP_ZK_PUBLIC);
10028 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
10029 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010030
Gilles Peskine449bd832023-01-11 14:50:10 +010010031 if (test_input) {
10032 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10033 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
10034 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010035 goto exit;
10036 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010037
Gilles Peskine449bd832023-01-11 14:50:10 +010010038 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10039 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10040 output_buffer, size_zk_proof),
10041 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010042 goto exit;
10043 }
10044
Gilles Peskine449bd832023-01-11 14:50:10 +010010045 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10046 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
10047 output_buffer, size_zk_proof),
10048 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010049 goto exit;
10050 }
10051
Gilles Peskine449bd832023-01-11 14:50:10 +010010052 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
10053 output_buffer, size_key_share);
10054 if (status != PSA_SUCCESS) {
10055 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010056 goto exit;
10057 }
10058
Gilles Peskine449bd832023-01-11 14:50:10 +010010059 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10060 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10061 output_buffer, size_zk_public + 1),
10062 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010063 goto exit;
10064 }
10065
Gilles Peskine449bd832023-01-11 14:50:10 +010010066 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010067 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010068 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10069 output_buffer, size_zk_public + 1);
10070 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10071 output_buffer, size_zk_public),
10072 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010073 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010074 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010075 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010076 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10077 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10078 NULL, 0, NULL),
10079 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010080 goto exit;
10081 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010082
Gilles Peskine449bd832023-01-11 14:50:10 +010010083 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10084 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10085 output_buffer, buf_size, &output_len),
10086 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010087 goto exit;
10088 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010089
Gilles Peskine449bd832023-01-11 14:50:10 +010010090 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10091 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10092 output_buffer, buf_size, &output_len),
10093 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010094 goto exit;
10095 }
10096
Gilles Peskine449bd832023-01-11 14:50:10 +010010097 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10098 output_buffer, buf_size, &output_len);
10099 if (status != PSA_SUCCESS) {
10100 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010101 goto exit;
10102 }
10103
Gilles Peskine449bd832023-01-11 14:50:10 +010010104 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010105
Gilles Peskine449bd832023-01-11 14:50:10 +010010106 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10107 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10108 output_buffer, size_zk_public - 1, &output_len),
10109 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010110 goto exit;
10111 }
10112
Gilles Peskine449bd832023-01-11 14:50:10 +010010113 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010114 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010115 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10116 output_buffer, size_zk_public - 1, &output_len);
10117 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10118 output_buffer, buf_size, &output_len),
10119 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010120 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010121 }
10122 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010123
10124exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010125 PSA_ASSERT(psa_destroy_key(key));
10126 PSA_ASSERT(psa_pake_abort(&operation));
10127 mbedtls_free(output_buffer);
10128 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010129}
10130/* END_CASE */
10131
Neil Armstronga557cb82022-06-10 08:58:32 +020010132/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010133void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10134 int client_input_first, int inject_error,
10135 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010136{
10137 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10138 psa_pake_operation_t server = psa_pake_operation_init();
10139 psa_pake_operation_t client = psa_pake_operation_init();
10140 psa_algorithm_t alg = alg_arg;
10141 psa_algorithm_t hash_alg = hash_arg;
10142 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10143 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10144
Gilles Peskine449bd832023-01-11 14:50:10 +010010145 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010146
Gilles Peskine449bd832023-01-11 14:50:10 +010010147 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10148 psa_set_key_algorithm(&attributes, alg);
10149 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10150 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10151 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010152
Gilles Peskine449bd832023-01-11 14:50:10 +010010153 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10154 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10155 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010156
10157
Gilles Peskine449bd832023-01-11 14:50:10 +010010158 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10159 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010160
Gilles Peskine449bd832023-01-11 14:50:10 +010010161 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10162 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010163
Gilles Peskine449bd832023-01-11 14:50:10 +010010164 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10165 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010166
Gilles Peskine449bd832023-01-11 14:50:10 +010010167 ecjpake_do_round(alg, primitive_arg, &server, &client,
10168 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010169
Gilles Peskine449bd832023-01-11 14:50:10 +010010170 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010171 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010172 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010173
Gilles Peskine449bd832023-01-11 14:50:10 +010010174 ecjpake_do_round(alg, primitive_arg, &server, &client,
10175 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010176
10177exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010178 psa_destroy_key(key);
10179 psa_pake_abort(&server);
10180 psa_pake_abort(&client);
10181 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010182}
10183/* END_CASE */
10184
10185/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010186void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10187 int derive_alg_arg, data_t *pw_data,
10188 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010189{
10190 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10191 psa_pake_operation_t server = psa_pake_operation_init();
10192 psa_pake_operation_t client = psa_pake_operation_init();
10193 psa_algorithm_t alg = alg_arg;
10194 psa_algorithm_t hash_alg = hash_arg;
10195 psa_algorithm_t derive_alg = derive_alg_arg;
10196 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10198 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010199 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010200 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010201 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010202 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010203
Gilles Peskine449bd832023-01-11 14:50:10 +010010204 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010205
Gilles Peskine449bd832023-01-11 14:50:10 +010010206 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10207 psa_set_key_algorithm(&attributes, alg);
10208 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10209 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10210 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010211
Gilles Peskine449bd832023-01-11 14:50:10 +010010212 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10213 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10214 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010215
Neil Armstrong1e855602022-06-15 11:32:11 +020010216 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010217 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10218 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010219
Gilles Peskine449bd832023-01-11 14:50:10 +010010220 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10221 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10222 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10223 PSA_KEY_DERIVATION_INPUT_SEED,
10224 (const uint8_t *) "", 0));
10225 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10226 PSA_KEY_DERIVATION_INPUT_SEED,
10227 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010228 }
10229
Gilles Peskine449bd832023-01-11 14:50:10 +010010230 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10231 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010232
Gilles Peskine449bd832023-01-11 14:50:10 +010010233 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10234 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010235
Gilles Peskine449bd832023-01-11 14:50:10 +010010236 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10237 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010238
Gilles Peskine449bd832023-01-11 14:50:10 +010010239 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10240 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10241 PSA_ERROR_BAD_STATE);
10242 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10243 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010244 goto exit;
10245 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010246
Neil Armstrongf983caf2022-06-15 15:27:48 +020010247 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010248 ecjpake_do_round(alg, primitive_arg, &server, &client,
10249 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010250
Gilles Peskine449bd832023-01-11 14:50:10 +010010251 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10252 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10253 PSA_ERROR_BAD_STATE);
10254 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10255 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010256 goto exit;
10257 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010258
Neil Armstrongf983caf2022-06-15 15:27:48 +020010259 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010260 ecjpake_do_round(alg, primitive_arg, &server, &client,
10261 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010262
Gilles Peskine449bd832023-01-11 14:50:10 +010010263 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10264 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010265
10266exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010267 psa_key_derivation_abort(&server_derive);
10268 psa_key_derivation_abort(&client_derive);
10269 psa_destroy_key(key);
10270 psa_pake_abort(&server);
10271 psa_pake_abort(&client);
10272 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010273}
10274/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010275
10276/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010277void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010278{
10279 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10280 const size_t bits = 256;
10281 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010282 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010283 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010284 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010285
10286 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10287 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010288 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10289 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10290 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10291 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010292 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010293 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10294 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010295
10296 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010297 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10298 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10299 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10300 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10301 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10302 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010303
10304 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010305 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10306 PSA_PAKE_OUTPUT_MAX_SIZE);
10307 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10308 PSA_PAKE_OUTPUT_MAX_SIZE);
10309 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10310 PSA_PAKE_OUTPUT_MAX_SIZE);
10311 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10312 PSA_PAKE_INPUT_MAX_SIZE);
10313 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10314 PSA_PAKE_INPUT_MAX_SIZE);
10315 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10316 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010317}
10318/* END_CASE */