blob: b58077ba21331c15492ea589ad59e26af6557fcd [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
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 ASSERT_ALLOC(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
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 ASSERT_ALLOC(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
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 ASSERT_ALLOC(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
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 ASSERT_COMPARE(expected_output->x, expected_output->len,
587 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 ASSERT_COMPARE(expected_output->x, expected_output->len,
696 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 ASSERT_ALLOC(buffer0, buffer_length);
748 ASSERT_ALLOC(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);
1369 TEST_EQUAL(status, expected_status);
1370 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001371 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001373
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1375 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1376 if (attr_bits != 0) {
1377 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1378 }
1379 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 PSA_ASSERT(psa_destroy_key(key));
1382 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001383
1384exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001385 /*
1386 * Key attributes may have been returned by psa_get_key_attributes()
1387 * thus reset them as required.
1388 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 psa_destroy_key(key);
1392 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001393}
1394/* END_CASE */
1395
1396/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001397/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001398void import_large_key(int type_arg, int byte_size_arg,
1399 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001400{
1401 psa_key_type_t type = type_arg;
1402 size_t byte_size = byte_size_arg;
1403 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1404 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001405 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001406 psa_status_t status;
1407 uint8_t *buffer = NULL;
1408 size_t buffer_size = byte_size + 1;
1409 size_t n;
1410
Steven Cooreman69967ce2021-01-18 18:01:08 +01001411 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001412 * accommodate large keys due to heap size constraints */
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 ASSERT_ALLOC_WEAK(buffer, buffer_size);
1414 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001415
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001417
1418 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1420 psa_set_key_type(&attributes, type);
1421 status = psa_import_key(&attributes, buffer, byte_size, &key);
1422 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1423 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 if (status == PSA_SUCCESS) {
1426 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1427 TEST_EQUAL(psa_get_key_type(&attributes), type);
1428 TEST_EQUAL(psa_get_key_bits(&attributes),
1429 PSA_BYTES_TO_BITS(byte_size));
1430 ASSERT_NO_SLOT_NUMBER(&attributes);
1431 memset(buffer, 0, byte_size + 1);
1432 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1433 for (n = 0; n < byte_size; n++) {
1434 TEST_EQUAL(buffer[n], 'K');
1435 }
1436 for (n = byte_size; n < buffer_size; n++) {
1437 TEST_EQUAL(buffer[n], 0);
1438 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001439 }
1440
1441exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001442 /*
1443 * Key attributes may have been returned by psa_get_key_attributes()
1444 * thus reset them as required.
1445 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001447
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 psa_destroy_key(key);
1449 PSA_DONE();
1450 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001451}
1452/* END_CASE */
1453
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001454/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001455/* Import an RSA key with a valid structure (but not valid numbers
1456 * inside, beyond having sensible size and parity). This is expected to
1457 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001458void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001459{
Ronald Cron5425a212020-08-04 14:58:35 +02001460 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001461 size_t bits = bits_arg;
1462 psa_status_t expected_status = expected_status_arg;
1463 psa_status_t status;
1464 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001465 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001466 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001468 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001469 unsigned char *p;
1470 int ret;
1471 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001472 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001473
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 PSA_ASSERT(psa_crypto_init());
1475 ASSERT_ALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1478 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001479 length = ret;
1480
1481 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 psa_set_key_type(&attributes, type);
1483 status = psa_import_key(&attributes, p, length, &key);
1484 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 if (status == PSA_SUCCESS) {
1487 PSA_ASSERT(psa_destroy_key(key));
1488 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001489
1490exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 mbedtls_free(buffer);
1492 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001493}
1494/* END_CASE */
1495
1496/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001497void import_export(data_t *data,
1498 int type_arg,
1499 int usage_arg, int alg_arg,
1500 int lifetime_arg,
1501 int expected_bits,
1502 int export_size_delta,
1503 int expected_export_status_arg,
1504 /*whether reexport must give the original input exactly*/
1505 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001506{
Ronald Cron5425a212020-08-04 14:58:35 +02001507 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001508 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001509 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001510 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001511 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301512 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001513 unsigned char *exported = NULL;
1514 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001515 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001516 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001517 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001518 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001519 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001520
Moran Pekercb088e72018-07-17 17:36:59 +03001521 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 ASSERT_ALLOC(exported, export_size);
1523 if (!canonical_input) {
1524 ASSERT_ALLOC(reexported, export_size);
1525 }
1526 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 psa_set_key_lifetime(&attributes, lifetime);
1529 psa_set_key_usage_flags(&attributes, usage_arg);
1530 psa_set_key_algorithm(&attributes, alg);
1531 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001532
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001533 if (PSA_KEY_TYPE_IS_DH(type) &&
1534 expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) {
Przemek Stekiel654bef02022-12-15 13:28:02 +01001535 /* Simulate that buffer is too small, by decreasing its size by 1 byte. */
1536 export_size -= 1;
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001537 }
1538
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001539 /* Import the key */
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001540 TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key),
Przemek Stekiel2e7c33d2023-04-27 12:29:45 +02001541 PSA_SUCCESS);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001542
1543 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1545 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1546 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1547 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001548
1549 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 status = psa_export_key(key, exported, export_size, &exported_length);
1551 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001552
1553 /* The exported length must be set by psa_export_key() to a value between 0
1554 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1556 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1557 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1560 export_size - exported_length));
1561 if (status != PSA_SUCCESS) {
1562 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001563 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001564 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001565
Gilles Peskineea38a922021-02-13 00:05:16 +01001566 /* Run sanity checks on the exported key. For non-canonical inputs,
1567 * this validates the canonical representations. For canonical inputs,
1568 * this doesn't directly validate the implementation, but it still helps
1569 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 if (!psa_key_lifetime_is_external(lifetime)) {
1571 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301572 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 }
Archana4d7ae1d2021-07-07 02:50:22 +05301574 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001575
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 if (canonical_input) {
1577 ASSERT_COMPARE(data->x, data->len, exported, exported_length);
1578 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001579 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1581 &key2));
1582 PSA_ASSERT(psa_export_key(key2,
1583 reexported,
1584 export_size,
1585 &reexported_length));
1586 ASSERT_COMPARE(exported, exported_length,
1587 reexported, reexported_length);
1588 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001589 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 TEST_LE_U(exported_length,
1591 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1592 psa_get_key_bits(&got_attributes)));
Valerio Setti1eacae82023-07-28 16:07:03 +02001593 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
1594 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
1595 } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
1596 TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1597 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001598
1599destroy:
1600 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 PSA_ASSERT(psa_destroy_key(key));
1602 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001603
1604exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001605 /*
1606 * Key attributes may have been returned by psa_get_key_attributes()
1607 * thus reset them as required.
1608 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 psa_reset_key_attributes(&got_attributes);
1610 psa_destroy_key(key);
1611 mbedtls_free(exported);
1612 mbedtls_free(reexported);
1613 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001614}
1615/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001616
Moran Pekerf709f4a2018-06-06 17:26:04 +03001617/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001618void import_export_public_key(data_t *data,
1619 int type_arg, // key pair or public key
1620 int alg_arg,
1621 int lifetime_arg,
1622 int export_size_delta,
1623 int expected_export_status_arg,
1624 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001625{
Ronald Cron5425a212020-08-04 14:58:35 +02001626 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001627 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001628 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001629 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001630 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301631 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001632 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001633 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001634 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001635 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001636
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001638
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 psa_set_key_lifetime(&attributes, lifetime);
1640 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1641 psa_set_key_algorithm(&attributes, alg);
1642 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001643
1644 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001646
Gilles Peskine49c25912018-10-29 15:15:31 +01001647 /* Export the public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 ASSERT_ALLOC(exported, export_size);
1649 status = psa_export_public_key(key,
1650 exported, export_size,
1651 &exported_length);
1652 TEST_EQUAL(status, expected_export_status);
1653 if (status == PSA_SUCCESS) {
1654 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001655 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1657 bits = psa_get_key_bits(&attributes);
1658 TEST_LE_U(expected_public_key->len,
1659 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1660 TEST_LE_U(expected_public_key->len,
1661 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1662 TEST_LE_U(expected_public_key->len,
1663 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1664 ASSERT_COMPARE(expected_public_key->x, expected_public_key->len,
1665 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001666 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001667exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001668 /*
1669 * Key attributes may have been returned by psa_get_key_attributes()
1670 * thus reset them as required.
1671 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 mbedtls_free(exported);
1675 psa_destroy_key(key);
1676 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001677}
1678/* END_CASE */
1679
Gilles Peskine20035e32018-02-03 22:44:14 +01001680/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001681void import_and_exercise_key(data_t *data,
1682 int type_arg,
1683 int bits_arg,
1684 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001685{
Ronald Cron5425a212020-08-04 14:58:35 +02001686 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001687 psa_key_type_t type = type_arg;
1688 size_t bits = bits_arg;
1689 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001691 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001692 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 psa_set_key_usage_flags(&attributes, usage);
1697 psa_set_key_algorithm(&attributes, alg);
1698 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001699
1700 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001702
1703 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1705 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1706 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001707
1708 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001710 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001712
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 PSA_ASSERT(psa_destroy_key(key));
1714 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001715
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001716exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001717 /*
1718 * Key attributes may have been returned by psa_get_key_attributes()
1719 * thus reset them as required.
1720 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001722
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 psa_reset_key_attributes(&attributes);
1724 psa_destroy_key(key);
1725 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001726}
1727/* END_CASE */
1728
1729/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001730void effective_key_attributes(int type_arg, int expected_type_arg,
1731 int bits_arg, int expected_bits_arg,
1732 int usage_arg, int expected_usage_arg,
1733 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001734{
Ronald Cron5425a212020-08-04 14:58:35 +02001735 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001736 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001737 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001738 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001739 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001740 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001741 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001742 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001743 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001744 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001745
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 psa_set_key_usage_flags(&attributes, usage);
1749 psa_set_key_algorithm(&attributes, alg);
1750 psa_set_key_type(&attributes, key_type);
1751 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001752
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 PSA_ASSERT(psa_generate_key(&attributes, &key));
1754 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001755
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1757 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1758 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1759 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1760 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001761
1762exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001763 /*
1764 * Key attributes may have been returned by psa_get_key_attributes()
1765 * thus reset them as required.
1766 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001768
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 psa_destroy_key(key);
1770 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001771}
1772/* END_CASE */
1773
1774/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001775void check_key_policy(int type_arg, int bits_arg,
1776 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001777{
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1779 usage_arg,
1780 mbedtls_test_update_key_usage_flags(usage_arg),
1781 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001782 goto exit;
1783}
1784/* END_CASE */
1785
1786/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001787void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001788{
1789 /* Test each valid way of initializing the object, except for `= {0}`, as
1790 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1791 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001792 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001794 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1795 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001796
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001798
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1800 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1801 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 TEST_EQUAL(psa_get_key_type(&func), 0);
1804 TEST_EQUAL(psa_get_key_type(&init), 0);
1805 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001806
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 TEST_EQUAL(psa_get_key_bits(&func), 0);
1808 TEST_EQUAL(psa_get_key_bits(&init), 0);
1809 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001810
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1812 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1813 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001814
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1816 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1817 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001818}
1819/* END_CASE */
1820
1821/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001822void mac_key_policy(int policy_usage_arg,
1823 int policy_alg_arg,
1824 int key_type_arg,
1825 data_t *key_data,
1826 int exercise_alg_arg,
1827 int expected_status_sign_arg,
1828 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001829{
Ronald Cron5425a212020-08-04 14:58:35 +02001830 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001831 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001832 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001833 psa_key_type_t key_type = key_type_arg;
1834 psa_algorithm_t policy_alg = policy_alg_arg;
1835 psa_algorithm_t exercise_alg = exercise_alg_arg;
1836 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001837 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001838 psa_status_t expected_status_sign = expected_status_sign_arg;
1839 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001840 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001841
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 psa_set_key_usage_flags(&attributes, policy_usage);
1845 psa_set_key_algorithm(&attributes, policy_alg);
1846 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001847
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1849 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001850
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1852 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001853
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1855 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001856
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001857 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001859 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1861 input, 128,
1862 mac, PSA_MAC_MAX_SIZE, &mac_len),
1863 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001864
Neil Armstrong3af9b972022-02-07 12:20:21 +01001865 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 PSA_ASSERT(psa_mac_abort(&operation));
1867 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1868 if (status == PSA_SUCCESS) {
1869 status = psa_mac_update(&operation, input, 128);
1870 if (status == PSA_SUCCESS) {
1871 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1872 &mac_len),
1873 expected_status_sign);
1874 } else {
1875 TEST_EQUAL(status, expected_status_sign);
1876 }
1877 } else {
1878 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001879 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001881
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001882 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 status = psa_mac_verify(key, exercise_alg, input, 128,
1884 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001885
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1887 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1888 } else {
1889 TEST_EQUAL(status, expected_status_verify);
1890 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001891
Neil Armstrong3af9b972022-02-07 12:20:21 +01001892 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1894 if (status == PSA_SUCCESS) {
1895 status = psa_mac_update(&operation, input, 128);
1896 if (status == PSA_SUCCESS) {
1897 status = psa_mac_verify_finish(&operation, mac, mac_len);
1898 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1899 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1900 } else {
1901 TEST_EQUAL(status, expected_status_verify);
1902 }
1903 } else {
1904 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001905 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 } else {
1907 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001908 }
1909
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 memset(mac, 0, sizeof(mac));
1913 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1914 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001915
1916exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 psa_mac_abort(&operation);
1918 psa_destroy_key(key);
1919 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001920}
1921/* END_CASE */
1922
1923/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001924void cipher_key_policy(int policy_usage_arg,
1925 int policy_alg,
1926 int key_type,
1927 data_t *key_data,
1928 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001929{
Ronald Cron5425a212020-08-04 14:58:35 +02001930 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001931 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001932 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001933 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001934 size_t output_buffer_size = 0;
1935 size_t input_buffer_size = 0;
1936 size_t output_length = 0;
1937 uint8_t *output = NULL;
1938 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001939 psa_status_t status;
1940
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1942 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1943 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001944
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 ASSERT_ALLOC(input, input_buffer_size);
1946 ASSERT_ALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001947
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001949
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 psa_set_key_usage_flags(&attributes, policy_usage);
1951 psa_set_key_algorithm(&attributes, policy_alg);
1952 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001953
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1955 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001956
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001957 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 TEST_EQUAL(policy_usage,
1959 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001960
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001961 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1963 output, output_buffer_size,
1964 &output_length);
1965 if (policy_alg == exercise_alg &&
1966 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1967 PSA_ASSERT(status);
1968 } else {
1969 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1970 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001971
1972 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1974 if (policy_alg == exercise_alg &&
1975 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1976 PSA_ASSERT(status);
1977 } else {
1978 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1979 }
1980 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001981
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001982 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1984 input, input_buffer_size,
1985 &output_length);
1986 if (policy_alg == exercise_alg &&
1987 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1988 PSA_ASSERT(status);
1989 } else {
1990 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1991 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001992
1993 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
1995 if (policy_alg == exercise_alg &&
1996 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1997 PSA_ASSERT(status);
1998 } else {
1999 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2000 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002001
2002exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 psa_cipher_abort(&operation);
2004 mbedtls_free(input);
2005 mbedtls_free(output);
2006 psa_destroy_key(key);
2007 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002008}
2009/* END_CASE */
2010
2011/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002012void aead_key_policy(int policy_usage_arg,
2013 int policy_alg,
2014 int key_type,
2015 data_t *key_data,
2016 int nonce_length_arg,
2017 int tag_length_arg,
2018 int exercise_alg,
2019 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002020{
Ronald Cron5425a212020-08-04 14:58:35 +02002021 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002022 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002023 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002024 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002025 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002026 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002028 size_t nonce_length = nonce_length_arg;
2029 unsigned char tag[16];
2030 size_t tag_length = tag_length_arg;
2031 size_t output_length;
2032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 TEST_LE_U(nonce_length, sizeof(nonce));
2034 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 psa_set_key_usage_flags(&attributes, policy_usage);
2039 psa_set_key_algorithm(&attributes, policy_alg);
2040 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2043 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002044
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002045 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 TEST_EQUAL(policy_usage,
2047 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002048
Neil Armstrong752d8112022-02-07 14:51:11 +01002049 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 status = psa_aead_encrypt(key, exercise_alg,
2051 nonce, nonce_length,
2052 NULL, 0,
2053 NULL, 0,
2054 tag, tag_length,
2055 &output_length);
2056 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2057 TEST_EQUAL(status, expected_status);
2058 } else {
2059 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2060 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002061
Neil Armstrong752d8112022-02-07 14:51:11 +01002062 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2064 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2065 TEST_EQUAL(status, expected_status);
2066 } else {
2067 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2068 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002069
2070 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 memset(tag, 0, sizeof(tag));
2072 status = psa_aead_decrypt(key, exercise_alg,
2073 nonce, nonce_length,
2074 NULL, 0,
2075 tag, tag_length,
2076 NULL, 0,
2077 &output_length);
2078 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2079 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2080 } else if (expected_status == PSA_SUCCESS) {
2081 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2082 } else {
2083 TEST_EQUAL(status, expected_status);
2084 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002085
Neil Armstrong752d8112022-02-07 14:51:11 +01002086 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 PSA_ASSERT(psa_aead_abort(&operation));
2088 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2089 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2090 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2091 } else {
2092 TEST_EQUAL(status, expected_status);
2093 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002094
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002095exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 PSA_ASSERT(psa_aead_abort(&operation));
2097 psa_destroy_key(key);
2098 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002099}
2100/* END_CASE */
2101
2102/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002103void asymmetric_encryption_key_policy(int policy_usage_arg,
2104 int policy_alg,
2105 int key_type,
2106 data_t *key_data,
2107 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002108{
Ronald Cron5425a212020-08-04 14:58:35 +02002109 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002110 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002111 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002112 psa_status_t status;
2113 size_t key_bits;
2114 size_t buffer_length;
2115 unsigned char *buffer = NULL;
2116 size_t output_length;
2117
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 psa_set_key_usage_flags(&attributes, policy_usage);
2121 psa_set_key_algorithm(&attributes, policy_alg);
2122 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2125 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002126
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002127 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 TEST_EQUAL(policy_usage,
2129 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2132 key_bits = psa_get_key_bits(&attributes);
2133 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2134 exercise_alg);
2135 ASSERT_ALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 status = psa_asymmetric_encrypt(key, exercise_alg,
2138 NULL, 0,
2139 NULL, 0,
2140 buffer, buffer_length,
2141 &output_length);
2142 if (policy_alg == exercise_alg &&
2143 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2144 PSA_ASSERT(status);
2145 } else {
2146 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2147 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 if (buffer_length != 0) {
2150 memset(buffer, 0, buffer_length);
2151 }
2152 status = psa_asymmetric_decrypt(key, exercise_alg,
2153 buffer, buffer_length,
2154 NULL, 0,
2155 buffer, buffer_length,
2156 &output_length);
2157 if (policy_alg == exercise_alg &&
2158 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2159 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2160 } else {
2161 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2162 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002163
2164exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002165 /*
2166 * Key attributes may have been returned by psa_get_key_attributes()
2167 * thus reset them as required.
2168 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 psa_destroy_key(key);
2172 PSA_DONE();
2173 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002174}
2175/* END_CASE */
2176
2177/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002178void asymmetric_signature_key_policy(int policy_usage_arg,
2179 int policy_alg,
2180 int key_type,
2181 data_t *key_data,
2182 int exercise_alg,
2183 int payload_length_arg,
2184 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002185{
Ronald Cron5425a212020-08-04 14:58:35 +02002186 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002187 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002188 psa_key_usage_t policy_usage = policy_usage_arg;
2189 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002190 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002192 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2193 * compatible with the policy and `payload_length_arg` is supposed to be
2194 * a valid input length to sign. If `payload_length_arg <= 0`,
2195 * `exercise_alg` is supposed to be forbidden by the policy. */
2196 int compatible_alg = payload_length_arg > 0;
2197 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002199 size_t signature_length;
2200
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002201 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002202 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 TEST_EQUAL(expected_usage,
2204 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002205
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 psa_set_key_usage_flags(&attributes, policy_usage);
2209 psa_set_key_algorithm(&attributes, policy_alg);
2210 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002211
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2213 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002214
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 status = psa_sign_hash(key, exercise_alg,
2218 payload, payload_length,
2219 signature, sizeof(signature),
2220 &signature_length);
2221 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2222 PSA_ASSERT(status);
2223 } else {
2224 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2225 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002226
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 memset(signature, 0, sizeof(signature));
2228 status = psa_verify_hash(key, exercise_alg,
2229 payload, payload_length,
2230 signature, sizeof(signature));
2231 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2232 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2233 } else {
2234 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2235 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002236
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2238 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2239 status = psa_sign_message(key, exercise_alg,
2240 payload, payload_length,
2241 signature, sizeof(signature),
2242 &signature_length);
2243 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2244 PSA_ASSERT(status);
2245 } else {
2246 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2247 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002248
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 memset(signature, 0, sizeof(signature));
2250 status = psa_verify_message(key, exercise_alg,
2251 payload, payload_length,
2252 signature, sizeof(signature));
2253 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2254 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2255 } else {
2256 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2257 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002258 }
2259
Gilles Peskined5b33222018-06-18 22:20:03 +02002260exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 psa_destroy_key(key);
2262 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002263}
2264/* END_CASE */
2265
Janos Follathba3fab92019-06-11 14:50:16 +01002266/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002267void derive_key_policy(int policy_usage,
2268 int policy_alg,
2269 int key_type,
2270 data_t *key_data,
2271 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002272{
Ronald Cron5425a212020-08-04 14:58:35 +02002273 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002274 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002275 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002276 psa_status_t status;
2277
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 psa_set_key_usage_flags(&attributes, policy_usage);
2281 psa_set_key_algorithm(&attributes, policy_alg);
2282 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002283
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2285 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002286
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2290 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2291 PSA_ASSERT(psa_key_derivation_input_bytes(
2292 &operation,
2293 PSA_KEY_DERIVATION_INPUT_SEED,
2294 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002295 }
Janos Follathba3fab92019-06-11 14:50:16 +01002296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 status = psa_key_derivation_input_key(&operation,
2298 PSA_KEY_DERIVATION_INPUT_SECRET,
2299 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 if (policy_alg == exercise_alg &&
2302 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2303 PSA_ASSERT(status);
2304 } else {
2305 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2306 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002307
2308exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 psa_key_derivation_abort(&operation);
2310 psa_destroy_key(key);
2311 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002312}
2313/* END_CASE */
2314
2315/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002316void agreement_key_policy(int policy_usage,
2317 int policy_alg,
2318 int key_type_arg,
2319 data_t *key_data,
2320 int exercise_alg,
2321 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002322{
Ronald Cron5425a212020-08-04 14:58:35 +02002323 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002324 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002325 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002326 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002327 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002328 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 psa_set_key_usage_flags(&attributes, policy_usage);
2333 psa_set_key_algorithm(&attributes, policy_alg);
2334 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002335
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2337 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2340 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002341
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002343
2344exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 psa_key_derivation_abort(&operation);
2346 psa_destroy_key(key);
2347 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002348}
2349/* END_CASE */
2350
2351/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002352void key_policy_alg2(int key_type_arg, data_t *key_data,
2353 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002354{
Ronald Cron5425a212020-08-04 14:58:35 +02002355 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002356 psa_key_type_t key_type = key_type_arg;
2357 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2358 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2359 psa_key_usage_t usage = usage_arg;
2360 psa_algorithm_t alg = alg_arg;
2361 psa_algorithm_t alg2 = alg2_arg;
2362
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 psa_set_key_usage_flags(&attributes, usage);
2366 psa_set_key_algorithm(&attributes, alg);
2367 psa_set_key_enrollment_algorithm(&attributes, alg2);
2368 psa_set_key_type(&attributes, key_type);
2369 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2370 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002371
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002372 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 usage = mbedtls_test_update_key_usage_flags(usage);
2374 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2375 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2376 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2377 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002378
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002380 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 }
2382 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002383 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002385
2386exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002387 /*
2388 * Key attributes may have been returned by psa_get_key_attributes()
2389 * thus reset them as required.
2390 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002392
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 psa_destroy_key(key);
2394 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002395}
2396/* END_CASE */
2397
2398/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002399void raw_agreement_key_policy(int policy_usage,
2400 int policy_alg,
2401 int key_type_arg,
2402 data_t *key_data,
2403 int exercise_alg,
2404 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002405{
Ronald Cron5425a212020-08-04 14:58:35 +02002406 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002407 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002408 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002409 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002410 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002411 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002412
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 psa_set_key_usage_flags(&attributes, policy_usage);
2416 psa_set_key_algorithm(&attributes, policy_alg);
2417 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2420 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002421
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002425
2426exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 psa_key_derivation_abort(&operation);
2428 psa_destroy_key(key);
2429 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002430}
2431/* END_CASE */
2432
2433/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002434void copy_success(int source_usage_arg,
2435 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002436 int source_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 int type_arg, data_t *material,
2438 int copy_attributes,
2439 int target_usage_arg,
2440 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002441 int target_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 int expected_usage_arg,
2443 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002444{
Gilles Peskineca25db92019-04-19 11:43:08 +02002445 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2446 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002447 psa_key_usage_t expected_usage = expected_usage_arg;
2448 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002449 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302450 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2451 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002452 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2453 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002454 uint8_t *export_buffer = NULL;
2455
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002457
Gilles Peskineca25db92019-04-19 11:43:08 +02002458 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2460 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2461 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2462 psa_set_key_type(&source_attributes, type_arg);
2463 psa_set_key_lifetime(&source_attributes, source_lifetime);
2464 PSA_ASSERT(psa_import_key(&source_attributes,
2465 material->x, material->len,
2466 &source_key));
2467 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002468
Gilles Peskineca25db92019-04-19 11:43:08 +02002469 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002471 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002472 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 if (target_usage_arg != -1) {
2476 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2477 }
2478 if (target_alg_arg != -1) {
2479 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2480 }
2481 if (target_alg2_arg != -1) {
2482 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2483 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002484
Archana8a180362021-07-05 02:18:48 +05302485
Gilles Peskine57ab7212019-01-28 13:03:09 +01002486 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 PSA_ASSERT(psa_copy_key(source_key,
2488 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002489
2490 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002492
2493 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2495 TEST_EQUAL(psa_get_key_type(&source_attributes),
2496 psa_get_key_type(&target_attributes));
2497 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2498 psa_get_key_bits(&target_attributes));
2499 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2500 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2501 TEST_EQUAL(expected_alg2,
2502 psa_get_key_enrollment_algorithm(&target_attributes));
2503 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002504 size_t length;
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 ASSERT_ALLOC(export_buffer, material->len);
2506 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2507 material->len, &length));
2508 ASSERT_COMPARE(material->x, material->len,
2509 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002510 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 if (!psa_key_lifetime_is_external(target_lifetime)) {
2513 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302514 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 }
2516 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302517 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 }
Archana8a180362021-07-05 02:18:48 +05302519 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002520
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002522
2523exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002524 /*
2525 * Source and target key attributes may have been returned by
2526 * psa_get_key_attributes() thus reset them as required.
2527 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 psa_reset_key_attributes(&source_attributes);
2529 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002530
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 PSA_DONE();
2532 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002533}
2534/* END_CASE */
2535
2536/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002537void copy_fail(int source_usage_arg,
2538 int source_alg_arg, int source_alg2_arg,
2539 int source_lifetime_arg,
2540 int type_arg, data_t *material,
2541 int target_type_arg, int target_bits_arg,
2542 int target_usage_arg,
2543 int target_alg_arg, int target_alg2_arg,
2544 int target_id_arg, int target_lifetime_arg,
2545 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002546{
2547 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2548 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002549 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2550 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002552
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002554
2555 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2557 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2558 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2559 psa_set_key_type(&source_attributes, type_arg);
2560 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2561 PSA_ASSERT(psa_import_key(&source_attributes,
2562 material->x, material->len,
2563 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002564
2565 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 psa_set_key_id(&target_attributes, key_id);
2567 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2568 psa_set_key_type(&target_attributes, target_type_arg);
2569 psa_set_key_bits(&target_attributes, target_bits_arg);
2570 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2571 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2572 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002573
2574 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 TEST_EQUAL(psa_copy_key(source_key,
2576 &target_attributes, &target_key),
2577 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002578
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002580
Gilles Peskine4a644642019-05-03 17:14:08 +02002581exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 psa_reset_key_attributes(&source_attributes);
2583 psa_reset_key_attributes(&target_attributes);
2584 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002585}
2586/* END_CASE */
2587
2588/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002589void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002590{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002591 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002592 /* Test each valid way of initializing the object, except for `= {0}`, as
2593 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2594 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002595 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002597 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2598 psa_hash_operation_t zero;
2599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002601
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002602 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2604 PSA_ERROR_BAD_STATE);
2605 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2606 PSA_ERROR_BAD_STATE);
2607 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2608 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002609
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002610 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 PSA_ASSERT(psa_hash_abort(&func));
2612 PSA_ASSERT(psa_hash_abort(&init));
2613 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002614}
2615/* END_CASE */
2616
2617/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002618void hash_setup(int alg_arg,
2619 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002620{
2621 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002622 uint8_t *output = NULL;
2623 size_t output_size = 0;
2624 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002625 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002626 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002627 psa_status_t status;
2628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002630
Neil Armstrongedb20862022-02-07 15:47:44 +01002631 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 output_size = PSA_HASH_LENGTH(alg);
2633 ASSERT_ALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 status = psa_hash_compute(alg, NULL, 0,
2636 output, output_size, &output_length);
2637 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002638
2639 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 status = psa_hash_setup(&operation, alg);
2641 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002642
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002643 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002645
2646 /* If setup failed, reproduce the failure, so as to
2647 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 if (status != PSA_SUCCESS) {
2649 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2650 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002651
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002652 /* Now the operation object should be reusable. */
2653#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2655 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002656#endif
2657
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002658exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 mbedtls_free(output);
2660 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002661}
2662/* END_CASE */
2663
2664/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002665void hash_compute_fail(int alg_arg, data_t *input,
2666 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002667{
2668 psa_algorithm_t alg = alg_arg;
2669 uint8_t *output = NULL;
2670 size_t output_size = output_size_arg;
2671 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002672 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002673 psa_status_t expected_status = expected_status_arg;
2674 psa_status_t status;
2675
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 ASSERT_ALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002679
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002680 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 status = psa_hash_compute(alg, input->x, input->len,
2682 output, output_size, &output_length);
2683 TEST_EQUAL(status, expected_status);
2684 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002685
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002686 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 status = psa_hash_setup(&operation, alg);
2688 if (status == PSA_SUCCESS) {
2689 status = psa_hash_update(&operation, input->x, input->len);
2690 if (status == PSA_SUCCESS) {
2691 status = psa_hash_finish(&operation, output, output_size,
2692 &output_length);
2693 if (status == PSA_SUCCESS) {
2694 TEST_LE_U(output_length, output_size);
2695 } else {
2696 TEST_EQUAL(status, expected_status);
2697 }
2698 } else {
2699 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002700 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 } else {
2702 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002703 }
2704
Gilles Peskine0a749c82019-11-28 19:33:58 +01002705exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 PSA_ASSERT(psa_hash_abort(&operation));
2707 mbedtls_free(output);
2708 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002709}
2710/* END_CASE */
2711
2712/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002713void hash_compare_fail(int alg_arg, data_t *input,
2714 data_t *reference_hash,
2715 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002716{
2717 psa_algorithm_t alg = alg_arg;
2718 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002719 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002720 psa_status_t status;
2721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002723
Neil Armstrong55a1be12022-02-07 11:23:20 +01002724 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 status = psa_hash_compare(alg, input->x, input->len,
2726 reference_hash->x, reference_hash->len);
2727 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002728
Neil Armstrong55a1be12022-02-07 11:23:20 +01002729 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 status = psa_hash_setup(&operation, alg);
2731 if (status == PSA_SUCCESS) {
2732 status = psa_hash_update(&operation, input->x, input->len);
2733 if (status == PSA_SUCCESS) {
2734 status = psa_hash_verify(&operation, reference_hash->x,
2735 reference_hash->len);
2736 TEST_EQUAL(status, expected_status);
2737 } else {
2738 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002739 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 } else {
2741 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002742 }
2743
Gilles Peskine88e08462020-01-28 20:43:00 +01002744exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 PSA_ASSERT(psa_hash_abort(&operation));
2746 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002747}
2748/* END_CASE */
2749
2750/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002751void hash_compute_compare(int alg_arg, data_t *input,
2752 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002753{
2754 psa_algorithm_t alg = alg_arg;
2755 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2756 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002757 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002758 size_t i;
2759
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002761
Neil Armstrongca30a002022-02-07 11:40:23 +01002762 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2764 output, PSA_HASH_LENGTH(alg),
2765 &output_length));
2766 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2767 ASSERT_COMPARE(output, output_length,
2768 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002769
Neil Armstrongca30a002022-02-07 11:40:23 +01002770 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 PSA_ASSERT(psa_hash_setup(&operation, alg));
2772 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2773 PSA_ASSERT(psa_hash_finish(&operation, output,
2774 PSA_HASH_LENGTH(alg),
2775 &output_length));
2776 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2777 ASSERT_COMPARE(output, output_length,
2778 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002779
2780 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2782 output, sizeof(output),
2783 &output_length));
2784 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2785 ASSERT_COMPARE(output, output_length,
2786 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002787
Neil Armstrongca30a002022-02-07 11:40:23 +01002788 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 PSA_ASSERT(psa_hash_setup(&operation, alg));
2790 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2791 PSA_ASSERT(psa_hash_finish(&operation, output,
2792 sizeof(output), &output_length));
2793 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2794 ASSERT_COMPARE(output, output_length,
2795 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002796
2797 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2799 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002800
Neil Armstrongca30a002022-02-07 11:40:23 +01002801 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 PSA_ASSERT(psa_hash_setup(&operation, alg));
2803 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2804 PSA_ASSERT(psa_hash_verify(&operation, output,
2805 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002806
2807 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2809 output, output_length + 1),
2810 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002811
Neil Armstrongca30a002022-02-07 11:40:23 +01002812 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 PSA_ASSERT(psa_hash_setup(&operation, alg));
2814 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2815 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2816 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002817
2818 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2820 output, output_length - 1),
2821 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002822
Neil Armstrongca30a002022-02-07 11:40:23 +01002823 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 PSA_ASSERT(psa_hash_setup(&operation, alg));
2825 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2826 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2827 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002828
Gilles Peskine0a749c82019-11-28 19:33:58 +01002829 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 for (i = 0; i < output_length; i++) {
2831 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002832 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002833
2834 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2836 output, output_length),
2837 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002838
2839 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 PSA_ASSERT(psa_hash_setup(&operation, alg));
2841 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2842 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2843 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002844
Gilles Peskine0a749c82019-11-28 19:33:58 +01002845 output[i] ^= 1;
2846 }
2847
2848exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 PSA_ASSERT(psa_hash_abort(&operation));
2850 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002851}
2852/* END_CASE */
2853
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002854/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002855void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002856{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002857 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002858 unsigned char input[] = "";
2859 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002860 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002861 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2862 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2864 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002865 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002866 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002867 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002868
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002870
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002871 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 PSA_ASSERT(psa_hash_setup(&operation, alg));
2873 ASSERT_OPERATION_IS_ACTIVE(operation);
2874 TEST_EQUAL(psa_hash_setup(&operation, alg),
2875 PSA_ERROR_BAD_STATE);
2876 ASSERT_OPERATION_IS_INACTIVE(operation);
2877 PSA_ASSERT(psa_hash_abort(&operation));
2878 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002879
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002880 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002881 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2882 PSA_ERROR_BAD_STATE);
2883 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002884
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002885 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002887 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 ASSERT_OPERATION_IS_ACTIVE(operation);
2889 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2890 PSA_ERROR_BAD_STATE);
2891 ASSERT_OPERATION_IS_INACTIVE(operation);
2892 PSA_ASSERT(psa_hash_abort(&operation));
2893 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002894
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002895 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 PSA_ASSERT(psa_hash_setup(&operation, alg));
2897 PSA_ASSERT(psa_hash_finish(&operation,
2898 hash, sizeof(hash), &hash_len));
2899 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2900 PSA_ERROR_BAD_STATE);
2901 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002902
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002903 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 TEST_EQUAL(psa_hash_verify(&operation,
2905 valid_hash, sizeof(valid_hash)),
2906 PSA_ERROR_BAD_STATE);
2907 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002908
2909 /* Call verify 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_verify(&operation,
2914 valid_hash, sizeof(valid_hash)),
2915 PSA_ERROR_BAD_STATE);
2916 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002917
2918 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 PSA_ASSERT(psa_hash_setup(&operation, alg));
2920 ASSERT_OPERATION_IS_ACTIVE(operation);
2921 PSA_ASSERT(psa_hash_verify(&operation,
2922 valid_hash, sizeof(valid_hash)));
2923 ASSERT_OPERATION_IS_INACTIVE(operation);
2924 TEST_EQUAL(psa_hash_verify(&operation,
2925 valid_hash, sizeof(valid_hash)),
2926 PSA_ERROR_BAD_STATE);
2927 ASSERT_OPERATION_IS_INACTIVE(operation);
2928 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002929
2930 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 TEST_EQUAL(psa_hash_finish(&operation,
2932 hash, sizeof(hash), &hash_len),
2933 PSA_ERROR_BAD_STATE);
2934 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002935
2936 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 PSA_ASSERT(psa_hash_setup(&operation, alg));
2938 PSA_ASSERT(psa_hash_finish(&operation,
2939 hash, sizeof(hash), &hash_len));
2940 TEST_EQUAL(psa_hash_finish(&operation,
2941 hash, sizeof(hash), &hash_len),
2942 PSA_ERROR_BAD_STATE);
2943 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002944
2945 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 PSA_ASSERT(psa_hash_setup(&operation, alg));
2947 PSA_ASSERT(psa_hash_verify(&operation,
2948 valid_hash, sizeof(valid_hash)));
2949 TEST_EQUAL(psa_hash_finish(&operation,
2950 hash, sizeof(hash), &hash_len),
2951 PSA_ERROR_BAD_STATE);
2952 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002953
2954exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002955 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002956}
2957/* END_CASE */
2958
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002959/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002960void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002961{
2962 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002963 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2964 * appended to it */
2965 unsigned char hash[] = {
2966 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2967 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2969 };
2970 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002971 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002972
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002974
itayzafrir27e69452018-11-01 14:26:34 +02002975 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 PSA_ASSERT(psa_hash_setup(&operation, alg));
2977 ASSERT_OPERATION_IS_ACTIVE(operation);
2978 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2979 PSA_ERROR_INVALID_SIGNATURE);
2980 ASSERT_OPERATION_IS_INACTIVE(operation);
2981 PSA_ASSERT(psa_hash_abort(&operation));
2982 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002983
itayzafrir27e69452018-11-01 14:26:34 +02002984 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 PSA_ASSERT(psa_hash_setup(&operation, alg));
2986 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
2987 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03002988
itayzafrir27e69452018-11-01 14:26:34 +02002989 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 PSA_ASSERT(psa_hash_setup(&operation, alg));
2991 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
2992 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03002993
itayzafrirec93d302018-10-18 18:01:10 +03002994exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03002996}
2997/* END_CASE */
2998
Ronald Cronee414c72021-03-18 18:50:08 +01002999/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003000void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03003001{
3002 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003003 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003005 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003006 size_t hash_len;
3007
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03003009
itayzafrir58028322018-10-25 10:22:01 +03003010 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 PSA_ASSERT(psa_hash_setup(&operation, alg));
3012 TEST_EQUAL(psa_hash_finish(&operation,
3013 hash, expected_size - 1, &hash_len),
3014 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003015
3016exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003018}
3019/* END_CASE */
3020
Ronald Cronee414c72021-03-18 18:50:08 +01003021/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003022void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003023{
3024 psa_algorithm_t alg = PSA_ALG_SHA_256;
3025 unsigned char hash[PSA_HASH_MAX_SIZE];
3026 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3027 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3028 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3029 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3030 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3031 size_t hash_len;
3032
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 PSA_ASSERT(psa_crypto_init());
3034 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003035
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3037 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3038 PSA_ASSERT(psa_hash_finish(&op_finished,
3039 hash, sizeof(hash), &hash_len));
3040 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3041 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003042
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3044 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003045
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3047 PSA_ASSERT(psa_hash_finish(&op_init,
3048 hash, sizeof(hash), &hash_len));
3049 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3050 PSA_ASSERT(psa_hash_finish(&op_finished,
3051 hash, sizeof(hash), &hash_len));
3052 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3053 PSA_ASSERT(psa_hash_finish(&op_aborted,
3054 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003055
3056exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 psa_hash_abort(&op_source);
3058 psa_hash_abort(&op_init);
3059 psa_hash_abort(&op_setup);
3060 psa_hash_abort(&op_finished);
3061 psa_hash_abort(&op_aborted);
3062 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003063}
3064/* END_CASE */
3065
Ronald Cronee414c72021-03-18 18:50:08 +01003066/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003067void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003068{
3069 psa_algorithm_t alg = PSA_ALG_SHA_256;
3070 unsigned char hash[PSA_HASH_MAX_SIZE];
3071 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3072 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3073 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3074 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3075 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3076 size_t hash_len;
3077
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3081 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3082 PSA_ASSERT(psa_hash_finish(&op_finished,
3083 hash, sizeof(hash), &hash_len));
3084 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3085 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003086
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3088 PSA_ASSERT(psa_hash_finish(&op_target,
3089 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003090
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3092 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3093 PSA_ERROR_BAD_STATE);
3094 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3095 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003096
3097exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 psa_hash_abort(&op_target);
3099 psa_hash_abort(&op_init);
3100 psa_hash_abort(&op_setup);
3101 psa_hash_abort(&op_finished);
3102 psa_hash_abort(&op_aborted);
3103 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003104}
3105/* END_CASE */
3106
itayzafrir58028322018-10-25 10:22:01 +03003107/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003108void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003109{
Jaeden Amero252ef282019-02-15 14:05:35 +00003110 const uint8_t input[1] = { 0 };
3111
Jaeden Amero769ce272019-01-04 11:48:03 +00003112 /* Test each valid way of initializing the object, except for `= {0}`, as
3113 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3114 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003115 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003117 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3118 psa_mac_operation_t zero;
3119
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003121
Jaeden Amero252ef282019-02-15 14:05:35 +00003122 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 TEST_EQUAL(psa_mac_update(&func,
3124 input, sizeof(input)),
3125 PSA_ERROR_BAD_STATE);
3126 TEST_EQUAL(psa_mac_update(&init,
3127 input, sizeof(input)),
3128 PSA_ERROR_BAD_STATE);
3129 TEST_EQUAL(psa_mac_update(&zero,
3130 input, sizeof(input)),
3131 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003132
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003133 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 PSA_ASSERT(psa_mac_abort(&func));
3135 PSA_ASSERT(psa_mac_abort(&init));
3136 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003137}
3138/* END_CASE */
3139
3140/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003141void mac_setup(int key_type_arg,
3142 data_t *key,
3143 int alg_arg,
3144 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003145{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003146 psa_key_type_t key_type = key_type_arg;
3147 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003148 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003149 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003150 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3151#if defined(KNOWN_SUPPORTED_MAC_ALG)
3152 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3153#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003154
Gilles Peskine449bd832023-01-11 14:50:10 +01003155 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003156
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3158 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003159 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 }
3161 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003162
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003163 /* The operation object should be reusable. */
3164#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3166 smoke_test_key_data,
3167 sizeof(smoke_test_key_data),
3168 KNOWN_SUPPORTED_MAC_ALG,
3169 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003170 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003171 }
3172 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003173#endif
3174
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003175exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003176 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003177}
3178/* END_CASE */
3179
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003180/* 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 +01003181void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003182{
Ronald Cron5425a212020-08-04 14:58:35 +02003183 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003184 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3185 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003186 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003187 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3188 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3190 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003191 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003192 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3193 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3194 size_t sign_mac_length = 0;
3195 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3196 const uint8_t verify_mac[] = {
3197 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3198 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3200 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003201
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 PSA_ASSERT(psa_crypto_init());
3203 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3204 psa_set_key_algorithm(&attributes, alg);
3205 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003206
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3208 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003209
Jaeden Amero252ef282019-02-15 14:05:35 +00003210 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3212 PSA_ERROR_BAD_STATE);
3213 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003214
3215 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003216 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3217 &sign_mac_length),
3218 PSA_ERROR_BAD_STATE);
3219 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003220
3221 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003222 TEST_EQUAL(psa_mac_verify_finish(&operation,
3223 verify_mac, sizeof(verify_mac)),
3224 PSA_ERROR_BAD_STATE);
3225 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003226
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003227 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003228 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3229 ASSERT_OPERATION_IS_ACTIVE(operation);
3230 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3231 PSA_ERROR_BAD_STATE);
3232 ASSERT_OPERATION_IS_INACTIVE(operation);
3233 PSA_ASSERT(psa_mac_abort(&operation));
3234 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003235
Jaeden Amero252ef282019-02-15 14:05:35 +00003236 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003237 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3238 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3239 PSA_ASSERT(psa_mac_sign_finish(&operation,
3240 sign_mac, sizeof(sign_mac),
3241 &sign_mac_length));
3242 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3243 PSA_ERROR_BAD_STATE);
3244 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003245
3246 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003247 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3248 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3249 PSA_ASSERT(psa_mac_verify_finish(&operation,
3250 verify_mac, sizeof(verify_mac)));
3251 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3252 PSA_ERROR_BAD_STATE);
3253 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003254
3255 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3257 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3258 PSA_ASSERT(psa_mac_sign_finish(&operation,
3259 sign_mac, sizeof(sign_mac),
3260 &sign_mac_length));
3261 TEST_EQUAL(psa_mac_sign_finish(&operation,
3262 sign_mac, sizeof(sign_mac),
3263 &sign_mac_length),
3264 PSA_ERROR_BAD_STATE);
3265 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003266
3267 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3269 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3270 PSA_ASSERT(psa_mac_verify_finish(&operation,
3271 verify_mac, sizeof(verify_mac)));
3272 TEST_EQUAL(psa_mac_verify_finish(&operation,
3273 verify_mac, sizeof(verify_mac)),
3274 PSA_ERROR_BAD_STATE);
3275 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003276
3277 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3279 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3280 ASSERT_OPERATION_IS_ACTIVE(operation);
3281 TEST_EQUAL(psa_mac_verify_finish(&operation,
3282 verify_mac, sizeof(verify_mac)),
3283 PSA_ERROR_BAD_STATE);
3284 ASSERT_OPERATION_IS_INACTIVE(operation);
3285 PSA_ASSERT(psa_mac_abort(&operation));
3286 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003287
3288 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003289 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3290 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3291 ASSERT_OPERATION_IS_ACTIVE(operation);
3292 TEST_EQUAL(psa_mac_sign_finish(&operation,
3293 sign_mac, sizeof(sign_mac),
3294 &sign_mac_length),
3295 PSA_ERROR_BAD_STATE);
3296 ASSERT_OPERATION_IS_INACTIVE(operation);
3297 PSA_ASSERT(psa_mac_abort(&operation));
3298 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003299
Gilles Peskine449bd832023-01-11 14:50:10 +01003300 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003301
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003302exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003303 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003304}
3305/* END_CASE */
3306
3307/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003308void mac_sign_verify_multi(int key_type_arg,
3309 data_t *key_data,
3310 int alg_arg,
3311 data_t *input,
3312 int is_verify,
3313 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003314{
3315 size_t data_part_len = 0;
3316
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003318 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003320
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 if (mac_multipart_internal_func(key_type_arg, key_data,
3322 alg_arg,
3323 input, data_part_len,
3324 expected_mac,
3325 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003326 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003328
3329 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003331
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 if (mac_multipart_internal_func(key_type_arg, key_data,
3333 alg_arg,
3334 input, data_part_len,
3335 expected_mac,
3336 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003337 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003338 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003339 }
3340
3341 /* Goto is required to silence warnings about unused labels, as we
3342 * don't actually do any test assertions in this function. */
3343 goto exit;
3344}
3345/* END_CASE */
3346
3347/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003348void mac_sign(int key_type_arg,
3349 data_t *key_data,
3350 int alg_arg,
3351 data_t *input,
3352 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003353{
Ronald Cron5425a212020-08-04 14:58:35 +02003354 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003355 psa_key_type_t key_type = key_type_arg;
3356 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003357 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003358 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003359 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003360 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003361 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003362 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003363 const size_t output_sizes_to_test[] = {
3364 0,
3365 1,
3366 expected_mac->len - 1,
3367 expected_mac->len,
3368 expected_mac->len + 1,
3369 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003370
Gilles Peskine449bd832023-01-11 14:50:10 +01003371 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003372 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003374
Gilles Peskine449bd832023-01-11 14:50:10 +01003375 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003376
Gilles Peskine449bd832023-01-11 14:50:10 +01003377 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3378 psa_set_key_algorithm(&attributes, alg);
3379 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003380
Gilles Peskine449bd832023-01-11 14:50:10 +01003381 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3382 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003383
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003385 const size_t output_size = output_sizes_to_test[i];
3386 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 (output_size >= expected_mac->len ? PSA_SUCCESS :
3388 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003389
Gilles Peskine449bd832023-01-11 14:50:10 +01003390 mbedtls_test_set_step(output_size);
3391 ASSERT_ALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003392
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003393 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003394 TEST_EQUAL(psa_mac_compute(key, alg,
3395 input->x, input->len,
3396 actual_mac, output_size, &mac_length),
3397 expected_status);
3398 if (expected_status == PSA_SUCCESS) {
3399 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3400 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003401 }
3402
Gilles Peskine449bd832023-01-11 14:50:10 +01003403 if (output_size > 0) {
3404 memset(actual_mac, 0, output_size);
3405 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003406
3407 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003408 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3409 PSA_ASSERT(psa_mac_update(&operation,
3410 input->x, input->len));
3411 TEST_EQUAL(psa_mac_sign_finish(&operation,
3412 actual_mac, output_size,
3413 &mac_length),
3414 expected_status);
3415 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003416
Gilles Peskine449bd832023-01-11 14:50:10 +01003417 if (expected_status == PSA_SUCCESS) {
3418 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3419 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003420 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003421 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003422 actual_mac = NULL;
3423 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003424
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003425exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003426 psa_mac_abort(&operation);
3427 psa_destroy_key(key);
3428 PSA_DONE();
3429 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003430}
3431/* END_CASE */
3432
3433/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003434void mac_verify(int key_type_arg,
3435 data_t *key_data,
3436 int alg_arg,
3437 data_t *input,
3438 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003439{
Ronald Cron5425a212020-08-04 14:58:35 +02003440 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003441 psa_key_type_t key_type = key_type_arg;
3442 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003443 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003444 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003445 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003446
Gilles Peskine449bd832023-01-11 14:50:10 +01003447 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003448
Gilles Peskine449bd832023-01-11 14:50:10 +01003449 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003450
Gilles Peskine449bd832023-01-11 14:50:10 +01003451 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3452 psa_set_key_algorithm(&attributes, alg);
3453 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003454
Gilles Peskine449bd832023-01-11 14:50:10 +01003455 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3456 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003457
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003458 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003459 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3460 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003461
3462 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003463 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3464 PSA_ASSERT(psa_mac_update(&operation,
3465 input->x, input->len));
3466 PSA_ASSERT(psa_mac_verify_finish(&operation,
3467 expected_mac->x,
3468 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003469
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003470 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003471 TEST_EQUAL(psa_mac_verify(key, alg,
3472 input->x, input->len,
3473 expected_mac->x,
3474 expected_mac->len - 1),
3475 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003476
3477 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003478 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3479 PSA_ASSERT(psa_mac_update(&operation,
3480 input->x, input->len));
3481 TEST_EQUAL(psa_mac_verify_finish(&operation,
3482 expected_mac->x,
3483 expected_mac->len - 1),
3484 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003485
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003486 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003487 ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1);
3488 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3489 TEST_EQUAL(psa_mac_verify(key, alg,
3490 input->x, input->len,
3491 perturbed_mac, expected_mac->len + 1),
3492 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003493
3494 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003495 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3496 PSA_ASSERT(psa_mac_update(&operation,
3497 input->x, input->len));
3498 TEST_EQUAL(psa_mac_verify_finish(&operation,
3499 perturbed_mac,
3500 expected_mac->len + 1),
3501 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003502
3503 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003504 for (size_t i = 0; i < expected_mac->len; i++) {
3505 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003506 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003507
Gilles Peskine449bd832023-01-11 14:50:10 +01003508 TEST_EQUAL(psa_mac_verify(key, alg,
3509 input->x, input->len,
3510 perturbed_mac, expected_mac->len),
3511 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003512
Gilles Peskine449bd832023-01-11 14:50:10 +01003513 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3514 PSA_ASSERT(psa_mac_update(&operation,
3515 input->x, input->len));
3516 TEST_EQUAL(psa_mac_verify_finish(&operation,
3517 perturbed_mac,
3518 expected_mac->len),
3519 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003520 perturbed_mac[i] ^= 1;
3521 }
3522
Gilles Peskine8c9def32018-02-08 10:02:12 +01003523exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003524 psa_mac_abort(&operation);
3525 psa_destroy_key(key);
3526 PSA_DONE();
3527 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003528}
3529/* END_CASE */
3530
3531/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003532void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003533{
Jaeden Ameroab439972019-02-15 14:12:05 +00003534 const uint8_t input[1] = { 0 };
3535 unsigned char output[1] = { 0 };
3536 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003537 /* Test each valid way of initializing the object, except for `= {0}`, as
3538 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3539 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003540 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003541 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003542 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3543 psa_cipher_operation_t zero;
3544
Gilles Peskine449bd832023-01-11 14:50:10 +01003545 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003546
Jaeden Ameroab439972019-02-15 14:12:05 +00003547 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003548 TEST_EQUAL(psa_cipher_update(&func,
3549 input, sizeof(input),
3550 output, sizeof(output),
3551 &output_length),
3552 PSA_ERROR_BAD_STATE);
3553 TEST_EQUAL(psa_cipher_update(&init,
3554 input, sizeof(input),
3555 output, sizeof(output),
3556 &output_length),
3557 PSA_ERROR_BAD_STATE);
3558 TEST_EQUAL(psa_cipher_update(&zero,
3559 input, sizeof(input),
3560 output, sizeof(output),
3561 &output_length),
3562 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003563
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003564 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003565 PSA_ASSERT(psa_cipher_abort(&func));
3566 PSA_ASSERT(psa_cipher_abort(&init));
3567 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003568}
3569/* END_CASE */
3570
3571/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003572void cipher_setup(int key_type_arg,
3573 data_t *key,
3574 int alg_arg,
3575 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003576{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003577 psa_key_type_t key_type = key_type_arg;
3578 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003579 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003580 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003581 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003582#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003583 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3584#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003585
Gilles Peskine449bd832023-01-11 14:50:10 +01003586 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003587
Gilles Peskine449bd832023-01-11 14:50:10 +01003588 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3589 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003590 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003591 }
3592 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003593
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003594 /* The operation object should be reusable. */
3595#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003596 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3597 smoke_test_key_data,
3598 sizeof(smoke_test_key_data),
3599 KNOWN_SUPPORTED_CIPHER_ALG,
3600 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003601 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003602 }
3603 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003604#endif
3605
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003606exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003607 psa_cipher_abort(&operation);
3608 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003609}
3610/* END_CASE */
3611
Ronald Cronee414c72021-03-18 18:50:08 +01003612/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003613void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003614{
Ronald Cron5425a212020-08-04 14:58:35 +02003615 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003616 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3617 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003618 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003619 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003620 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003621 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003622 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003623 0xaa, 0xaa, 0xaa, 0xaa
3624 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003625 const uint8_t text[] = {
3626 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003627 0xbb, 0xbb, 0xbb, 0xbb
3628 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003629 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003630 size_t length = 0;
3631
Gilles Peskine449bd832023-01-11 14:50:10 +01003632 PSA_ASSERT(psa_crypto_init());
3633 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3634 psa_set_key_algorithm(&attributes, alg);
3635 psa_set_key_type(&attributes, key_type);
3636 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3637 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003638
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003639 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003640 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3641 ASSERT_OPERATION_IS_ACTIVE(operation);
3642 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3643 PSA_ERROR_BAD_STATE);
3644 ASSERT_OPERATION_IS_INACTIVE(operation);
3645 PSA_ASSERT(psa_cipher_abort(&operation));
3646 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003647
3648 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003649 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3650 ASSERT_OPERATION_IS_ACTIVE(operation);
3651 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3652 PSA_ERROR_BAD_STATE);
3653 ASSERT_OPERATION_IS_INACTIVE(operation);
3654 PSA_ASSERT(psa_cipher_abort(&operation));
3655 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003656
Jaeden Ameroab439972019-02-15 14:12:05 +00003657 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003658 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3659 buffer, sizeof(buffer),
3660 &length),
3661 PSA_ERROR_BAD_STATE);
3662 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003663
3664 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003665 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3666 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3667 buffer, sizeof(buffer),
3668 &length));
3669 ASSERT_OPERATION_IS_ACTIVE(operation);
3670 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3671 buffer, sizeof(buffer),
3672 &length),
3673 PSA_ERROR_BAD_STATE);
3674 ASSERT_OPERATION_IS_INACTIVE(operation);
3675 PSA_ASSERT(psa_cipher_abort(&operation));
3676 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003677
3678 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003679 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3680 PSA_ASSERT(psa_cipher_set_iv(&operation,
3681 iv, sizeof(iv)));
3682 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3683 buffer, sizeof(buffer),
3684 &length),
3685 PSA_ERROR_BAD_STATE);
3686 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003687
3688 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003689 TEST_EQUAL(psa_cipher_set_iv(&operation,
3690 iv, sizeof(iv)),
3691 PSA_ERROR_BAD_STATE);
3692 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003693
3694 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003695 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3696 PSA_ASSERT(psa_cipher_set_iv(&operation,
3697 iv, sizeof(iv)));
3698 ASSERT_OPERATION_IS_ACTIVE(operation);
3699 TEST_EQUAL(psa_cipher_set_iv(&operation,
3700 iv, sizeof(iv)),
3701 PSA_ERROR_BAD_STATE);
3702 ASSERT_OPERATION_IS_INACTIVE(operation);
3703 PSA_ASSERT(psa_cipher_abort(&operation));
3704 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003705
3706 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003707 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3708 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3709 buffer, sizeof(buffer),
3710 &length));
3711 TEST_EQUAL(psa_cipher_set_iv(&operation,
3712 iv, sizeof(iv)),
3713 PSA_ERROR_BAD_STATE);
3714 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003715
3716 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003717 TEST_EQUAL(psa_cipher_update(&operation,
3718 text, sizeof(text),
3719 buffer, sizeof(buffer),
3720 &length),
3721 PSA_ERROR_BAD_STATE);
3722 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003723
3724 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003725 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3726 ASSERT_OPERATION_IS_ACTIVE(operation);
3727 TEST_EQUAL(psa_cipher_update(&operation,
3728 text, sizeof(text),
3729 buffer, sizeof(buffer),
3730 &length),
3731 PSA_ERROR_BAD_STATE);
3732 ASSERT_OPERATION_IS_INACTIVE(operation);
3733 PSA_ASSERT(psa_cipher_abort(&operation));
3734 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003735
3736 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003737 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3738 PSA_ASSERT(psa_cipher_set_iv(&operation,
3739 iv, sizeof(iv)));
3740 PSA_ASSERT(psa_cipher_finish(&operation,
3741 buffer, sizeof(buffer), &length));
3742 TEST_EQUAL(psa_cipher_update(&operation,
3743 text, sizeof(text),
3744 buffer, sizeof(buffer),
3745 &length),
3746 PSA_ERROR_BAD_STATE);
3747 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003748
3749 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003750 TEST_EQUAL(psa_cipher_finish(&operation,
3751 buffer, sizeof(buffer), &length),
3752 PSA_ERROR_BAD_STATE);
3753 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003754
3755 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003756 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003757 /* Not calling update means we are encrypting an empty buffer, which is OK
3758 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003759 ASSERT_OPERATION_IS_ACTIVE(operation);
3760 TEST_EQUAL(psa_cipher_finish(&operation,
3761 buffer, sizeof(buffer), &length),
3762 PSA_ERROR_BAD_STATE);
3763 ASSERT_OPERATION_IS_INACTIVE(operation);
3764 PSA_ASSERT(psa_cipher_abort(&operation));
3765 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003766
3767 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003768 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3769 PSA_ASSERT(psa_cipher_set_iv(&operation,
3770 iv, sizeof(iv)));
3771 PSA_ASSERT(psa_cipher_finish(&operation,
3772 buffer, sizeof(buffer), &length));
3773 TEST_EQUAL(psa_cipher_finish(&operation,
3774 buffer, sizeof(buffer), &length),
3775 PSA_ERROR_BAD_STATE);
3776 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003777
Gilles Peskine449bd832023-01-11 14:50:10 +01003778 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003779
Jaeden Ameroab439972019-02-15 14:12:05 +00003780exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003781 psa_cipher_abort(&operation);
3782 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003783}
3784/* END_CASE */
3785
3786/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003787void cipher_encrypt_fail(int alg_arg,
3788 int key_type_arg,
3789 data_t *key_data,
3790 data_t *input,
3791 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003792{
Ronald Cron5425a212020-08-04 14:58:35 +02003793 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003794 psa_status_t status;
3795 psa_key_type_t key_type = key_type_arg;
3796 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003797 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003798 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003799 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3800 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003801 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003802 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003803 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003804 size_t function_output_length;
3805 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003806 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3807
Gilles Peskine449bd832023-01-11 14:50:10 +01003808 if (PSA_ERROR_BAD_STATE != expected_status) {
3809 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003810
Gilles Peskine449bd832023-01-11 14:50:10 +01003811 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3812 psa_set_key_algorithm(&attributes, alg);
3813 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003814
Gilles Peskine449bd832023-01-11 14:50:10 +01003815 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3816 input->len);
3817 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003818
Gilles Peskine449bd832023-01-11 14:50:10 +01003819 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3820 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003821 }
3822
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003823 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003824 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3825 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003826
Gilles Peskine449bd832023-01-11 14:50:10 +01003827 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003828
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003829 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003830 status = psa_cipher_encrypt_setup(&operation, key, alg);
3831 if (status == PSA_SUCCESS) {
3832 if (alg != PSA_ALG_ECB_NO_PADDING) {
3833 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3834 iv, iv_size,
3835 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003836 }
3837
Gilles Peskine449bd832023-01-11 14:50:10 +01003838 status = psa_cipher_update(&operation, input->x, input->len,
3839 output, output_buffer_size,
3840 &function_output_length);
3841 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003842 output_length += function_output_length;
3843
Gilles Peskine449bd832023-01-11 14:50:10 +01003844 status = psa_cipher_finish(&operation, output + output_length,
3845 output_buffer_size - output_length,
3846 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003847
Gilles Peskine449bd832023-01-11 14:50:10 +01003848 TEST_EQUAL(status, expected_status);
3849 } else {
3850 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003851 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003852 } else {
3853 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003854 }
3855
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003856exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003857 psa_cipher_abort(&operation);
3858 mbedtls_free(output);
3859 psa_destroy_key(key);
3860 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003861}
3862/* END_CASE */
3863
3864/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003865void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3866 data_t *input, int iv_length,
3867 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003868{
3869 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3870 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3871 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3872 size_t output_buffer_size = 0;
3873 unsigned char *output = NULL;
3874
Gilles Peskine449bd832023-01-11 14:50:10 +01003875 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
3876 ASSERT_ALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003877
Gilles Peskine449bd832023-01-11 14:50:10 +01003878 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003879
Gilles Peskine449bd832023-01-11 14:50:10 +01003880 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3881 psa_set_key_algorithm(&attributes, alg);
3882 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003883
Gilles Peskine449bd832023-01-11 14:50:10 +01003884 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3885 &key));
3886 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3887 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3888 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003889
3890exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003891 psa_cipher_abort(&operation);
3892 mbedtls_free(output);
3893 psa_destroy_key(key);
3894 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003895}
3896/* END_CASE */
3897
3898/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003899void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3900 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003901{
3902 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3903 psa_key_type_t key_type = key_type_arg;
3904 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003905 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3906 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003907 unsigned char *output = NULL;
3908 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003909 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003910 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3911
Gilles Peskine449bd832023-01-11 14:50:10 +01003912 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003913
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003914 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003915 TEST_LE_U(ciphertext->len,
3916 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3917 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3918 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3919 TEST_LE_U(plaintext->len,
3920 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3921 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3922 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003923
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003924
3925 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003926 psa_set_key_usage_flags(&attributes,
3927 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3928 psa_set_key_algorithm(&attributes, alg);
3929 psa_set_key_type(&attributes, key_type);
3930 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3931 &key));
3932 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3933 plaintext->len);
3934 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003935
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003936 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003937 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3938 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3939 PSA_ERROR_BAD_STATE);
3940 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3941 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3942 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003943
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003944 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003945 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3946 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3947 &length),
3948 PSA_ERROR_BAD_STATE);
3949 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3950 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3951 &length),
3952 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003953
Gilles Peskine286c3142022-04-20 17:09:38 +02003954 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003955 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003956 output_length = 0;
3957 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 PSA_ASSERT(psa_cipher_update(&operation,
3959 plaintext->x, plaintext->len,
3960 output, output_buffer_size,
3961 &length));
3962 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003963 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003964 PSA_ASSERT(psa_cipher_finish(&operation,
3965 mbedtls_buffer_offset(output, output_length),
3966 output_buffer_size - output_length,
3967 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003968 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003969 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3970 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003971
Gilles Peskine286c3142022-04-20 17:09:38 +02003972 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003973 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003974 output_length = 0;
3975 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003976 PSA_ASSERT(psa_cipher_update(&operation,
3977 ciphertext->x, ciphertext->len,
3978 output, output_buffer_size,
3979 &length));
3980 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003981 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003982 PSA_ASSERT(psa_cipher_finish(&operation,
3983 mbedtls_buffer_offset(output, output_length),
3984 output_buffer_size - output_length,
3985 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003986 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003987 ASSERT_COMPARE(plaintext->x, plaintext->len,
3988 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003989
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003990 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003991 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
3993 output, output_buffer_size,
3994 &output_length));
3995 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3996 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003997
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003998 /* One-shot decryption */
3999 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004000 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
4001 output, output_buffer_size,
4002 &output_length));
4003 ASSERT_COMPARE(plaintext->x, plaintext->len,
4004 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004005
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004006exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004007 PSA_ASSERT(psa_cipher_abort(&operation));
4008 mbedtls_free(output);
4009 psa_cipher_abort(&operation);
4010 psa_destroy_key(key);
4011 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004012}
4013/* END_CASE */
4014
4015/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004016void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004017{
4018 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4019 psa_algorithm_t alg = alg_arg;
4020 psa_key_type_t key_type = key_type_arg;
4021 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4022 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4023 psa_status_t status;
4024
Gilles Peskine449bd832023-01-11 14:50:10 +01004025 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004026
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4028 psa_set_key_algorithm(&attributes, alg);
4029 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004030
4031 /* Usage of either of these two size macros would cause divide by zero
4032 * with incorrect key types previously. Input length should be irrelevant
4033 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004034 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4035 0);
4036 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004037
4038
Gilles Peskine449bd832023-01-11 14:50:10 +01004039 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4040 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004041
4042 /* Should fail due to invalid alg type (to support invalid key type).
4043 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004044 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004045
Gilles Peskine449bd832023-01-11 14:50:10 +01004046 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004047
4048exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004049 psa_cipher_abort(&operation);
4050 psa_destroy_key(key);
4051 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004052}
4053/* END_CASE */
4054
4055/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004056void cipher_encrypt_validation(int alg_arg,
4057 int key_type_arg,
4058 data_t *key_data,
4059 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004060{
4061 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4062 psa_key_type_t key_type = key_type_arg;
4063 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004064 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004065 unsigned char *output1 = NULL;
4066 size_t output1_buffer_size = 0;
4067 size_t output1_length = 0;
4068 unsigned char *output2 = NULL;
4069 size_t output2_buffer_size = 0;
4070 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004071 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004072 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004073 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004074
Gilles Peskine449bd832023-01-11 14:50:10 +01004075 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004076
Gilles Peskine449bd832023-01-11 14:50:10 +01004077 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4078 psa_set_key_algorithm(&attributes, alg);
4079 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004080
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4082 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4083 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4084 ASSERT_ALLOC(output1, output1_buffer_size);
4085 ASSERT_ALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004086
Gilles Peskine449bd832023-01-11 14:50:10 +01004087 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4088 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004089
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004090 /* The one-shot cipher encryption uses generated iv so validating
4091 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004092 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4093 output1_buffer_size, &output1_length));
4094 TEST_LE_U(output1_length,
4095 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4096 TEST_LE_U(output1_length,
4097 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004098
Gilles Peskine449bd832023-01-11 14:50:10 +01004099 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4100 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004101
Gilles Peskine449bd832023-01-11 14:50:10 +01004102 PSA_ASSERT(psa_cipher_update(&operation,
4103 input->x, input->len,
4104 output2, output2_buffer_size,
4105 &function_output_length));
4106 TEST_LE_U(function_output_length,
4107 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4108 TEST_LE_U(function_output_length,
4109 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004110 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004111
Gilles Peskine449bd832023-01-11 14:50:10 +01004112 PSA_ASSERT(psa_cipher_finish(&operation,
4113 output2 + output2_length,
4114 output2_buffer_size - output2_length,
4115 &function_output_length));
4116 TEST_LE_U(function_output_length,
4117 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4118 TEST_LE_U(function_output_length,
4119 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004120 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004121
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 PSA_ASSERT(psa_cipher_abort(&operation));
4123 ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size,
4124 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004125
Gilles Peskine50e586b2018-06-08 14:28:46 +02004126exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004127 psa_cipher_abort(&operation);
4128 mbedtls_free(output1);
4129 mbedtls_free(output2);
4130 psa_destroy_key(key);
4131 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004132}
4133/* END_CASE */
4134
4135/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004136void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4137 data_t *key_data, data_t *iv,
4138 data_t *input,
4139 int first_part_size_arg,
4140 int output1_length_arg, int output2_length_arg,
4141 data_t *expected_output,
4142 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004143{
Ronald Cron5425a212020-08-04 14:58:35 +02004144 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004145 psa_key_type_t key_type = key_type_arg;
4146 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004147 psa_status_t status;
4148 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004149 size_t first_part_size = first_part_size_arg;
4150 size_t output1_length = output1_length_arg;
4151 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004152 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004153 size_t output_buffer_size = 0;
4154 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004155 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004156 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004157 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004158
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004160
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4162 psa_set_key_algorithm(&attributes, alg);
4163 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004164
Gilles Peskine449bd832023-01-11 14:50:10 +01004165 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4166 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004167
Gilles Peskine449bd832023-01-11 14:50:10 +01004168 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004169
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 if (iv->len > 0) {
4171 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004172 }
4173
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4175 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4176 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004177
Gilles Peskine449bd832023-01-11 14:50:10 +01004178 TEST_LE_U(first_part_size, input->len);
4179 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4180 output, output_buffer_size,
4181 &function_output_length));
4182 TEST_ASSERT(function_output_length == output1_length);
4183 TEST_LE_U(function_output_length,
4184 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4185 TEST_LE_U(function_output_length,
4186 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004187 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004188
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 if (first_part_size < input->len) {
4190 PSA_ASSERT(psa_cipher_update(&operation,
4191 input->x + first_part_size,
4192 input->len - first_part_size,
4193 (output_buffer_size == 0 ? NULL :
4194 output + total_output_length),
4195 output_buffer_size - total_output_length,
4196 &function_output_length));
4197 TEST_ASSERT(function_output_length == output2_length);
4198 TEST_LE_U(function_output_length,
4199 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4200 alg,
4201 input->len - first_part_size));
4202 TEST_LE_U(function_output_length,
4203 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004204 total_output_length += function_output_length;
4205 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004206
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 status = psa_cipher_finish(&operation,
4208 (output_buffer_size == 0 ? NULL :
4209 output + total_output_length),
4210 output_buffer_size - total_output_length,
4211 &function_output_length);
4212 TEST_LE_U(function_output_length,
4213 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4214 TEST_LE_U(function_output_length,
4215 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004216 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004218
Gilles Peskine449bd832023-01-11 14:50:10 +01004219 if (expected_status == PSA_SUCCESS) {
4220 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004221
Gilles Peskine449bd832023-01-11 14:50:10 +01004222 ASSERT_COMPARE(expected_output->x, expected_output->len,
4223 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004224 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004225
4226exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004227 psa_cipher_abort(&operation);
4228 mbedtls_free(output);
4229 psa_destroy_key(key);
4230 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004231}
4232/* END_CASE */
4233
4234/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004235void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4236 data_t *key_data, data_t *iv,
4237 data_t *input,
4238 int first_part_size_arg,
4239 int output1_length_arg, int output2_length_arg,
4240 data_t *expected_output,
4241 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004242{
Ronald Cron5425a212020-08-04 14:58:35 +02004243 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004244 psa_key_type_t key_type = key_type_arg;
4245 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004246 psa_status_t status;
4247 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004248 size_t first_part_size = first_part_size_arg;
4249 size_t output1_length = output1_length_arg;
4250 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004251 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004252 size_t output_buffer_size = 0;
4253 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004254 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004255 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004256 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004257
Gilles Peskine449bd832023-01-11 14:50:10 +01004258 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004259
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4261 psa_set_key_algorithm(&attributes, alg);
4262 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004263
Gilles Peskine449bd832023-01-11 14:50:10 +01004264 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4265 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004266
Gilles Peskine449bd832023-01-11 14:50:10 +01004267 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004268
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 if (iv->len > 0) {
4270 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004271 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004272
Gilles Peskine449bd832023-01-11 14:50:10 +01004273 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4274 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4275 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004276
Gilles Peskine449bd832023-01-11 14:50:10 +01004277 TEST_LE_U(first_part_size, input->len);
4278 PSA_ASSERT(psa_cipher_update(&operation,
4279 input->x, first_part_size,
4280 output, output_buffer_size,
4281 &function_output_length));
4282 TEST_ASSERT(function_output_length == output1_length);
4283 TEST_LE_U(function_output_length,
4284 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4285 TEST_LE_U(function_output_length,
4286 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004287 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004288
Gilles Peskine449bd832023-01-11 14:50:10 +01004289 if (first_part_size < input->len) {
4290 PSA_ASSERT(psa_cipher_update(&operation,
4291 input->x + first_part_size,
4292 input->len - first_part_size,
4293 (output_buffer_size == 0 ? NULL :
4294 output + total_output_length),
4295 output_buffer_size - total_output_length,
4296 &function_output_length));
4297 TEST_ASSERT(function_output_length == output2_length);
4298 TEST_LE_U(function_output_length,
4299 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4300 alg,
4301 input->len - first_part_size));
4302 TEST_LE_U(function_output_length,
4303 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004304 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004305 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004306
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 status = psa_cipher_finish(&operation,
4308 (output_buffer_size == 0 ? NULL :
4309 output + total_output_length),
4310 output_buffer_size - total_output_length,
4311 &function_output_length);
4312 TEST_LE_U(function_output_length,
4313 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4314 TEST_LE_U(function_output_length,
4315 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004316 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004318
Gilles Peskine449bd832023-01-11 14:50:10 +01004319 if (expected_status == PSA_SUCCESS) {
4320 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004321
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 ASSERT_COMPARE(expected_output->x, expected_output->len,
4323 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004324 }
4325
Gilles Peskine50e586b2018-06-08 14:28:46 +02004326exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 psa_cipher_abort(&operation);
4328 mbedtls_free(output);
4329 psa_destroy_key(key);
4330 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004331}
4332/* END_CASE */
4333
Gilles Peskine50e586b2018-06-08 14:28:46 +02004334/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004335void cipher_decrypt_fail(int alg_arg,
4336 int key_type_arg,
4337 data_t *key_data,
4338 data_t *iv,
4339 data_t *input_arg,
4340 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004341{
4342 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4343 psa_status_t status;
4344 psa_key_type_t key_type = key_type_arg;
4345 psa_algorithm_t alg = alg_arg;
4346 psa_status_t expected_status = expected_status_arg;
4347 unsigned char *input = NULL;
4348 size_t input_buffer_size = 0;
4349 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004350 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004351 size_t output_buffer_size = 0;
4352 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004353 size_t function_output_length;
4354 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004355 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4356
Gilles Peskine449bd832023-01-11 14:50:10 +01004357 if (PSA_ERROR_BAD_STATE != expected_status) {
4358 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004359
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4361 psa_set_key_algorithm(&attributes, alg);
4362 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004363
Gilles Peskine449bd832023-01-11 14:50:10 +01004364 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4365 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004366 }
4367
4368 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4370 if (input_buffer_size > 0) {
4371 ASSERT_ALLOC(input, input_buffer_size);
4372 memcpy(input, iv->x, iv->len);
4373 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004374 }
4375
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4377 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004378
Neil Armstrong66a479f2022-02-07 15:41:19 +01004379 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004380 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4381 output_buffer_size, &output_length);
4382 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004383
Neil Armstrong66a479f2022-02-07 15:41:19 +01004384 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 status = psa_cipher_decrypt_setup(&operation, key, alg);
4386 if (status == PSA_SUCCESS) {
4387 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4388 input_arg->len) +
4389 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4390 ASSERT_ALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004391
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 if (iv->len > 0) {
4393 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004394
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 if (status != PSA_SUCCESS) {
4396 TEST_EQUAL(status, expected_status);
4397 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004398 }
4399
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 if (status == PSA_SUCCESS) {
4401 status = psa_cipher_update(&operation,
4402 input_arg->x, input_arg->len,
4403 output_multi, output_buffer_size,
4404 &function_output_length);
4405 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004406 output_length = function_output_length;
4407
Gilles Peskine449bd832023-01-11 14:50:10 +01004408 status = psa_cipher_finish(&operation,
4409 output_multi + output_length,
4410 output_buffer_size - output_length,
4411 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004412
Gilles Peskine449bd832023-01-11 14:50:10 +01004413 TEST_EQUAL(status, expected_status);
4414 } else {
4415 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004416 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004417 } else {
4418 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004419 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 } else {
4421 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004422 }
4423
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004424exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004425 psa_cipher_abort(&operation);
4426 mbedtls_free(input);
4427 mbedtls_free(output);
4428 mbedtls_free(output_multi);
4429 psa_destroy_key(key);
4430 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004431}
4432/* END_CASE */
4433
4434/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004435void cipher_decrypt(int alg_arg,
4436 int key_type_arg,
4437 data_t *key_data,
4438 data_t *iv,
4439 data_t *input_arg,
4440 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004441{
4442 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4443 psa_key_type_t key_type = key_type_arg;
4444 psa_algorithm_t alg = alg_arg;
4445 unsigned char *input = NULL;
4446 size_t input_buffer_size = 0;
4447 unsigned char *output = NULL;
4448 size_t output_buffer_size = 0;
4449 size_t output_length = 0;
4450 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4451
Gilles Peskine449bd832023-01-11 14:50:10 +01004452 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004453
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4455 psa_set_key_algorithm(&attributes, alg);
4456 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004457
4458 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4460 if (input_buffer_size > 0) {
4461 ASSERT_ALLOC(input, input_buffer_size);
4462 memcpy(input, iv->x, iv->len);
4463 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004464 }
4465
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4467 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004468
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4470 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004471
Gilles Peskine449bd832023-01-11 14:50:10 +01004472 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4473 output_buffer_size, &output_length));
4474 TEST_LE_U(output_length,
4475 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4476 TEST_LE_U(output_length,
4477 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004478
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 ASSERT_COMPARE(expected_output->x, expected_output->len,
4480 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004481exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 mbedtls_free(input);
4483 mbedtls_free(output);
4484 psa_destroy_key(key);
4485 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004486}
4487/* END_CASE */
4488
4489/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004490void cipher_verify_output(int alg_arg,
4491 int key_type_arg,
4492 data_t *key_data,
4493 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004494{
Ronald Cron5425a212020-08-04 14:58:35 +02004495 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004496 psa_key_type_t key_type = key_type_arg;
4497 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004498 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004499 size_t output1_size = 0;
4500 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004501 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004502 size_t output2_size = 0;
4503 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004504 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004505
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004507
Gilles Peskine449bd832023-01-11 14:50:10 +01004508 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4509 psa_set_key_algorithm(&attributes, alg);
4510 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004511
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4513 &key));
4514 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4515 ASSERT_ALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004516
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4518 output1, output1_size,
4519 &output1_length));
4520 TEST_LE_U(output1_length,
4521 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4522 TEST_LE_U(output1_length,
4523 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004524
4525 output2_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 ASSERT_ALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004527
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4529 output2, output2_size,
4530 &output2_length));
4531 TEST_LE_U(output2_length,
4532 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4533 TEST_LE_U(output2_length,
4534 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004535
Gilles Peskine449bd832023-01-11 14:50:10 +01004536 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004537
4538exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 mbedtls_free(output1);
4540 mbedtls_free(output2);
4541 psa_destroy_key(key);
4542 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004543}
4544/* END_CASE */
4545
4546/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004547void cipher_verify_output_multipart(int alg_arg,
4548 int key_type_arg,
4549 data_t *key_data,
4550 data_t *input,
4551 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004552{
Ronald Cron5425a212020-08-04 14:58:35 +02004553 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004554 psa_key_type_t key_type = key_type_arg;
4555 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004556 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004557 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004558 size_t iv_size = 16;
4559 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004560 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004561 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004562 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004563 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004564 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004565 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004566 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004567 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4568 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004569 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004570
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004572
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4574 psa_set_key_algorithm(&attributes, alg);
4575 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004576
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4578 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004579
Gilles Peskine449bd832023-01-11 14:50:10 +01004580 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4581 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004582
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 if (alg != PSA_ALG_ECB_NO_PADDING) {
4584 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4585 iv, iv_size,
4586 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004587 }
4588
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4590 TEST_LE_U(output1_buffer_size,
4591 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
4592 ASSERT_ALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004593
Gilles Peskine449bd832023-01-11 14:50:10 +01004594 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004595
Gilles Peskine449bd832023-01-11 14:50:10 +01004596 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4597 output1, output1_buffer_size,
4598 &function_output_length));
4599 TEST_LE_U(function_output_length,
4600 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4601 TEST_LE_U(function_output_length,
4602 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004603 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004604
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 PSA_ASSERT(psa_cipher_update(&operation1,
4606 input->x + first_part_size,
4607 input->len - first_part_size,
4608 output1, output1_buffer_size,
4609 &function_output_length));
4610 TEST_LE_U(function_output_length,
4611 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4612 alg,
4613 input->len - first_part_size));
4614 TEST_LE_U(function_output_length,
4615 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004616 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004617
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 PSA_ASSERT(psa_cipher_finish(&operation1,
4619 output1 + output1_length,
4620 output1_buffer_size - output1_length,
4621 &function_output_length));
4622 TEST_LE_U(function_output_length,
4623 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4624 TEST_LE_U(function_output_length,
4625 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004626 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004627
Gilles Peskine449bd832023-01-11 14:50:10 +01004628 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004629
Gilles Peskine048b7f02018-06-08 14:20:49 +02004630 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 TEST_LE_U(output2_buffer_size,
4632 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4633 TEST_LE_U(output2_buffer_size,
4634 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
4635 ASSERT_ALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004636
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 if (iv_length > 0) {
4638 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4639 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004640 }
Moran Pekerded84402018-06-06 16:36:50 +03004641
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4643 output2, output2_buffer_size,
4644 &function_output_length));
4645 TEST_LE_U(function_output_length,
4646 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4647 TEST_LE_U(function_output_length,
4648 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004649 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004650
Gilles Peskine449bd832023-01-11 14:50:10 +01004651 PSA_ASSERT(psa_cipher_update(&operation2,
4652 output1 + first_part_size,
4653 output1_length - first_part_size,
4654 output2, output2_buffer_size,
4655 &function_output_length));
4656 TEST_LE_U(function_output_length,
4657 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4658 alg,
4659 output1_length - first_part_size));
4660 TEST_LE_U(function_output_length,
4661 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004662 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004663
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 PSA_ASSERT(psa_cipher_finish(&operation2,
4665 output2 + output2_length,
4666 output2_buffer_size - output2_length,
4667 &function_output_length));
4668 TEST_LE_U(function_output_length,
4669 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4670 TEST_LE_U(function_output_length,
4671 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004672 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004673
Gilles Peskine449bd832023-01-11 14:50:10 +01004674 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004675
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004677
4678exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 psa_cipher_abort(&operation1);
4680 psa_cipher_abort(&operation2);
4681 mbedtls_free(output1);
4682 mbedtls_free(output2);
4683 psa_destroy_key(key);
4684 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004685}
4686/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004687
Gilles Peskine20035e32018-02-03 22:44:14 +01004688/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004689void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4690 int alg_arg,
4691 data_t *nonce,
4692 data_t *additional_data,
4693 data_t *input_data,
4694 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004695{
Ronald Cron5425a212020-08-04 14:58:35 +02004696 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004697 psa_key_type_t key_type = key_type_arg;
4698 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004699 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004700 unsigned char *output_data = NULL;
4701 size_t output_size = 0;
4702 size_t output_length = 0;
4703 unsigned char *output_data2 = NULL;
4704 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004705 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004706 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004707 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004708
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004710
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4712 psa_set_key_algorithm(&attributes, alg);
4713 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004714
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4716 &key));
4717 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4718 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004719
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4721 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004722 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4723 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4725 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4726 TEST_EQUAL(output_size,
4727 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4728 TEST_LE_U(output_size,
4729 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004730 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004732
Gilles Peskine449bd832023-01-11 14:50:10 +01004733 status = psa_aead_encrypt(key, alg,
4734 nonce->x, nonce->len,
4735 additional_data->x,
4736 additional_data->len,
4737 input_data->x, input_data->len,
4738 output_data, output_size,
4739 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004740
4741 /* If the operation is not supported, just skip and not fail in case the
4742 * encryption involves a common limitation of cryptography hardwares and
4743 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 if (status == PSA_ERROR_NOT_SUPPORTED) {
4745 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4746 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004747 }
4748
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004750
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 if (PSA_SUCCESS == expected_result) {
4752 ASSERT_ALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004753
Gilles Peskine003a4a92019-05-14 16:09:40 +02004754 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4755 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 TEST_EQUAL(input_data->len,
4757 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004758
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 TEST_LE_U(input_data->len,
4760 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004761
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 TEST_EQUAL(psa_aead_decrypt(key, alg,
4763 nonce->x, nonce->len,
4764 additional_data->x,
4765 additional_data->len,
4766 output_data, output_length,
4767 output_data2, output_length,
4768 &output_length2),
4769 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004770
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 ASSERT_COMPARE(input_data->x, input_data->len,
4772 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004773 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004774
Gilles Peskinea1cac842018-06-11 19:33:02 +02004775exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004776 psa_destroy_key(key);
4777 mbedtls_free(output_data);
4778 mbedtls_free(output_data2);
4779 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004780}
4781/* END_CASE */
4782
4783/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004784void aead_encrypt(int key_type_arg, data_t *key_data,
4785 int alg_arg,
4786 data_t *nonce,
4787 data_t *additional_data,
4788 data_t *input_data,
4789 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004790{
Ronald Cron5425a212020-08-04 14:58:35 +02004791 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004792 psa_key_type_t key_type = key_type_arg;
4793 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004794 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004795 unsigned char *output_data = NULL;
4796 size_t output_size = 0;
4797 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004798 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004799 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004800
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004802
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4804 psa_set_key_algorithm(&attributes, alg);
4805 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004806
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4808 &key));
4809 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4810 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004811
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4813 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004814 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4815 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 TEST_EQUAL(output_size,
4817 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4818 TEST_LE_U(output_size,
4819 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
4820 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004821
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 status = psa_aead_encrypt(key, alg,
4823 nonce->x, nonce->len,
4824 additional_data->x, additional_data->len,
4825 input_data->x, input_data->len,
4826 output_data, output_size,
4827 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004828
Ronald Cron28a45ed2021-02-09 20:35:42 +01004829 /* If the operation is not supported, just skip and not fail in case the
4830 * encryption involves a common limitation of cryptography hardwares and
4831 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 if (status == PSA_ERROR_NOT_SUPPORTED) {
4833 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4834 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004835 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004836
Gilles Peskine449bd832023-01-11 14:50:10 +01004837 PSA_ASSERT(status);
4838 ASSERT_COMPARE(expected_result->x, expected_result->len,
4839 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004840
Gilles Peskinea1cac842018-06-11 19:33:02 +02004841exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 psa_destroy_key(key);
4843 mbedtls_free(output_data);
4844 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004845}
4846/* END_CASE */
4847
4848/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004849void aead_decrypt(int key_type_arg, data_t *key_data,
4850 int alg_arg,
4851 data_t *nonce,
4852 data_t *additional_data,
4853 data_t *input_data,
4854 data_t *expected_data,
4855 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004856{
Ronald Cron5425a212020-08-04 14:58:35 +02004857 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004858 psa_key_type_t key_type = key_type_arg;
4859 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004860 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004861 unsigned char *output_data = NULL;
4862 size_t output_size = 0;
4863 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004864 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004865 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004866 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004869
Gilles Peskine449bd832023-01-11 14:50:10 +01004870 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4871 psa_set_key_algorithm(&attributes, alg);
4872 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004873
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4875 &key));
4876 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4877 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004878
Gilles Peskine449bd832023-01-11 14:50:10 +01004879 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4880 alg);
4881 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4882 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004883 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4884 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 TEST_EQUAL(output_size,
4886 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4887 TEST_LE_U(output_size,
4888 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004889 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004890 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004891
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 status = psa_aead_decrypt(key, alg,
4893 nonce->x, nonce->len,
4894 additional_data->x,
4895 additional_data->len,
4896 input_data->x, input_data->len,
4897 output_data, output_size,
4898 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004899
Ronald Cron28a45ed2021-02-09 20:35:42 +01004900 /* If the operation is not supported, just skip and not fail in case the
4901 * decryption involves a common limitation of cryptography hardwares and
4902 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 if (status == PSA_ERROR_NOT_SUPPORTED) {
4904 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4905 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004906 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004907
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004909
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 if (expected_result == PSA_SUCCESS) {
4911 ASSERT_COMPARE(expected_data->x, expected_data->len,
4912 output_data, output_length);
4913 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004914
Gilles Peskinea1cac842018-06-11 19:33:02 +02004915exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 psa_destroy_key(key);
4917 mbedtls_free(output_data);
4918 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004919}
4920/* END_CASE */
4921
4922/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004923void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4924 int alg_arg,
4925 data_t *nonce,
4926 data_t *additional_data,
4927 data_t *input_data,
4928 int do_set_lengths,
4929 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004930{
Paul Elliottd3f82412021-06-16 16:52:21 +01004931 size_t ad_part_len = 0;
4932 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004933 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004934
Gilles Peskine449bd832023-01-11 14:50:10 +01004935 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4936 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004937
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 if (do_set_lengths) {
4939 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004940 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004941 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004942 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004944 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004945
4946 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004947 if (!aead_multipart_internal_func(key_type_arg, key_data,
4948 alg_arg, nonce,
4949 additional_data,
4950 ad_part_len,
4951 input_data, -1,
4952 set_lengths_method,
4953 expected_output,
4954 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004955 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004956 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004957
Gilles Peskine449bd832023-01-11 14:50:10 +01004958 /* length(0) part, length(ad_part_len) part, length(0) part... */
4959 mbedtls_test_set_step(1000 + ad_part_len);
4960
4961 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, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004969 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 }
4971 }
4972
4973 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4974 /* Split data into length(data_part_len) parts. */
4975 mbedtls_test_set_step(2000 + data_part_len);
4976
4977 if (do_set_lengths) {
4978 if (data_part_len & 0x01) {
4979 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4980 } else {
4981 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4982 }
4983 }
4984
4985 if (!aead_multipart_internal_func(key_type_arg, key_data,
4986 alg_arg, nonce,
4987 additional_data, -1,
4988 input_data, data_part_len,
4989 set_lengths_method,
4990 expected_output,
4991 1, 0)) {
4992 break;
4993 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004994
4995 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004997
Gilles Peskine449bd832023-01-11 14:50:10 +01004998 if (!aead_multipart_internal_func(key_type_arg, key_data,
4999 alg_arg, nonce,
5000 additional_data, -1,
5001 input_data, data_part_len,
5002 set_lengths_method,
5003 expected_output,
5004 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005005 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005007 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005008
Paul Elliott8fc45162021-06-23 16:06:01 +01005009 /* Goto is required to silence warnings about unused labels, as we
5010 * don't actually do any test assertions in this function. */
5011 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005012}
5013/* END_CASE */
5014
5015/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005016void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5017 int alg_arg,
5018 data_t *nonce,
5019 data_t *additional_data,
5020 data_t *input_data,
5021 int do_set_lengths,
5022 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005023{
Paul Elliottd3f82412021-06-16 16:52:21 +01005024 size_t ad_part_len = 0;
5025 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005026 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005027
Gilles Peskine449bd832023-01-11 14:50:10 +01005028 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005029 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005031
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 if (do_set_lengths) {
5033 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005034 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005036 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005038 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005039
Gilles Peskine449bd832023-01-11 14:50:10 +01005040 if (!aead_multipart_internal_func(key_type_arg, key_data,
5041 alg_arg, nonce,
5042 additional_data,
5043 ad_part_len,
5044 input_data, -1,
5045 set_lengths_method,
5046 expected_output,
5047 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005048 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005049 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005050
5051 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005052 mbedtls_test_set_step(1000 + ad_part_len);
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, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005062 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005063 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005064 }
5065
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005067 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005069
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 if (do_set_lengths) {
5071 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005072 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005073 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005074 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005076 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005077
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 if (!aead_multipart_internal_func(key_type_arg, key_data,
5079 alg_arg, nonce,
5080 additional_data, -1,
5081 input_data, data_part_len,
5082 set_lengths_method,
5083 expected_output,
5084 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005085 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005087
5088 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005089 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005090
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 if (!aead_multipart_internal_func(key_type_arg, key_data,
5092 alg_arg, nonce,
5093 additional_data, -1,
5094 input_data, data_part_len,
5095 set_lengths_method,
5096 expected_output,
5097 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005098 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005099 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005100 }
5101
Paul Elliott8fc45162021-06-23 16:06:01 +01005102 /* Goto is required to silence warnings about unused labels, as we
5103 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005104 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005105}
5106/* END_CASE */
5107
5108/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005109void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5110 int alg_arg,
5111 int nonce_length,
5112 int expected_nonce_length_arg,
5113 data_t *additional_data,
5114 data_t *input_data,
5115 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005116{
5117
5118 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5119 psa_key_type_t key_type = key_type_arg;
5120 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005121 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005122 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5123 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5124 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005125 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005126 size_t actual_nonce_length = 0;
5127 size_t expected_nonce_length = expected_nonce_length_arg;
5128 unsigned char *output = NULL;
5129 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005130 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005131 size_t ciphertext_size = 0;
5132 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005133 size_t tag_length = 0;
5134 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005135
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005137
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5139 psa_set_key_algorithm(&attributes, alg);
5140 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005141
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5143 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005144
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005146
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005148
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 ASSERT_ALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005150
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005152
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005154
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005156
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005158
5159 /* If the operation is not supported, just skip and not fail in case the
5160 * encryption involves a common limitation of cryptography hardwares and
5161 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 if (status == PSA_ERROR_NOT_SUPPORTED) {
5163 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5164 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005165 }
5166
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005168
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5170 nonce_length,
5171 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005172
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005174
Gilles Peskine449bd832023-01-11 14:50:10 +01005175 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005176
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 if (expected_status == PSA_SUCCESS) {
5178 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5179 alg));
5180 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005181
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005183
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005185 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005186 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5187 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005188
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5190 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005191
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5193 output, output_size,
5194 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005195
Gilles Peskine449bd832023-01-11 14:50:10 +01005196 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5197 &ciphertext_length, tag_buffer,
5198 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005199 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005200
5201exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 psa_destroy_key(key);
5203 mbedtls_free(output);
5204 mbedtls_free(ciphertext);
5205 psa_aead_abort(&operation);
5206 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005207}
5208/* END_CASE */
5209
5210/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005211void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5212 int alg_arg,
5213 int nonce_length_arg,
5214 int set_lengths_method_arg,
5215 data_t *additional_data,
5216 data_t *input_data,
5217 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005218{
5219
5220 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5221 psa_key_type_t key_type = key_type_arg;
5222 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005223 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005224 uint8_t *nonce_buffer = NULL;
5225 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5226 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5227 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005228 unsigned char *output = NULL;
5229 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005230 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005231 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005232 size_t ciphertext_size = 0;
5233 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005234 size_t tag_length = 0;
5235 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005236 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005237 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005238
Gilles Peskine449bd832023-01-11 14:50:10 +01005239 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005240
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5242 psa_set_key_algorithm(&attributes, alg);
5243 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005244
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5246 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005247
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005249
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005251
Gilles Peskine449bd832023-01-11 14:50:10 +01005252 ASSERT_ALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005253
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005255
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005257
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005259
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005261
5262 /* If the operation is not supported, just skip and not fail in case the
5263 * encryption involves a common limitation of cryptography hardwares and
5264 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 if (status == PSA_ERROR_NOT_SUPPORTED) {
5266 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5267 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005268 }
5269
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005271
Paul Elliott4023ffd2021-09-10 16:21:22 +01005272 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 if (nonce_length_arg == -1) {
5274 /* Arbitrary size buffer, to test zero length valid buffer. */
5275 ASSERT_ALLOC(nonce_buffer, 4);
5276 nonce_length = 0;
5277 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005278 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 nonce_length = (size_t) nonce_length_arg;
5280 ASSERT_ALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005281
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 if (nonce_buffer) {
5283 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005284 nonce_buffer[index] = 'a' + index;
5285 }
Paul Elliott66696b52021-08-16 18:42:41 +01005286 }
Paul Elliott863864a2021-07-23 17:28:31 +01005287 }
5288
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5290 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5291 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005292 }
5293
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005295
Gilles Peskine449bd832023-01-11 14:50:10 +01005296 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005297
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 if (expected_status == PSA_SUCCESS) {
5299 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5300 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5301 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005302 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005303 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005304 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005305 }
Paul Elliott863864a2021-07-23 17:28:31 +01005306
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005307 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5309 additional_data->len),
5310 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005311
Gilles Peskine449bd832023-01-11 14:50:10 +01005312 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5313 output, output_size,
5314 &ciphertext_length),
5315 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005316
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5318 &ciphertext_length, tag_buffer,
5319 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5320 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005321 }
5322
5323exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 psa_destroy_key(key);
5325 mbedtls_free(output);
5326 mbedtls_free(ciphertext);
5327 mbedtls_free(nonce_buffer);
5328 psa_aead_abort(&operation);
5329 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005330}
5331/* END_CASE */
5332
5333/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005334void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005335 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005336 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005337 data_t *nonce,
5338 data_t *additional_data,
5339 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005340 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005341{
5342
5343 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5344 psa_key_type_t key_type = key_type_arg;
5345 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005346 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005347 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5348 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5349 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005350 unsigned char *output = NULL;
5351 unsigned char *ciphertext = NULL;
5352 size_t output_size = output_size_arg;
5353 size_t ciphertext_size = 0;
5354 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005355 size_t tag_length = 0;
5356 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5357
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005359
Gilles Peskine449bd832023-01-11 14:50:10 +01005360 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5361 psa_set_key_algorithm(&attributes, alg);
5362 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005363
Gilles Peskine449bd832023-01-11 14:50:10 +01005364 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5365 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005366
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005368
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 ASSERT_ALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005370
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005372
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005374
Gilles Peskine449bd832023-01-11 14:50:10 +01005375 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005376
5377 /* If the operation is not supported, just skip and not fail in case the
5378 * encryption involves a common limitation of cryptography hardwares and
5379 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005380 if (status == PSA_ERROR_NOT_SUPPORTED) {
5381 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5382 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005383 }
5384
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005386
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5388 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005389
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005391
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5393 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005394
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 status = psa_aead_update(&operation, input_data->x, input_data->len,
5396 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005397
Gilles Peskine449bd832023-01-11 14:50:10 +01005398 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005399
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005401 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5403 &ciphertext_length, tag_buffer,
5404 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005405 }
5406
5407exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 psa_destroy_key(key);
5409 mbedtls_free(output);
5410 mbedtls_free(ciphertext);
5411 psa_aead_abort(&operation);
5412 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005413}
5414/* END_CASE */
5415
Paul Elliott91b021e2021-07-23 18:52:31 +01005416/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005417void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5418 int alg_arg,
5419 int finish_ciphertext_size_arg,
5420 int tag_size_arg,
5421 data_t *nonce,
5422 data_t *additional_data,
5423 data_t *input_data,
5424 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005425{
5426
5427 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5428 psa_key_type_t key_type = key_type_arg;
5429 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005430 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005431 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5432 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5433 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005434 unsigned char *ciphertext = NULL;
5435 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005436 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005437 size_t ciphertext_size = 0;
5438 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5440 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005441 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005442
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005444
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5446 psa_set_key_algorithm(&attributes, alg);
5447 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005448
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5450 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005451
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005453
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005455
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005457
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005459
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 ASSERT_ALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005461
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005463
5464 /* If the operation is not supported, just skip and not fail in case the
5465 * encryption involves a common limitation of cryptography hardwares and
5466 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 if (status == PSA_ERROR_NOT_SUPPORTED) {
5468 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5469 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005470 }
5471
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005473
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005475
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5477 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005478
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5480 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005481
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5483 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005484
5485 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 status = psa_aead_finish(&operation, finish_ciphertext,
5487 finish_ciphertext_size,
5488 &ciphertext_length, tag_buffer,
5489 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005490
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005492
5493exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005494 psa_destroy_key(key);
5495 mbedtls_free(ciphertext);
5496 mbedtls_free(finish_ciphertext);
5497 mbedtls_free(tag_buffer);
5498 psa_aead_abort(&operation);
5499 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005500}
5501/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005502
5503/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005504void aead_multipart_verify(int key_type_arg, data_t *key_data,
5505 int alg_arg,
5506 data_t *nonce,
5507 data_t *additional_data,
5508 data_t *input_data,
5509 data_t *tag,
5510 int tag_usage_arg,
5511 int expected_setup_status_arg,
5512 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005513{
5514 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5515 psa_key_type_t key_type = key_type_arg;
5516 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005517 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005518 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5519 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5520 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005521 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005522 unsigned char *plaintext = NULL;
5523 unsigned char *finish_plaintext = NULL;
5524 size_t plaintext_size = 0;
5525 size_t plaintext_length = 0;
5526 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005527 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005528 unsigned char *tag_buffer = NULL;
5529 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005530
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005532
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5534 psa_set_key_algorithm(&attributes, alg);
5535 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005536
Gilles Peskine449bd832023-01-11 14:50:10 +01005537 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5538 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005539
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005541
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5543 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005544
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 ASSERT_ALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005546
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005548
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 ASSERT_ALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005550
Gilles Peskine449bd832023-01-11 14:50:10 +01005551 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005552
5553 /* If the operation is not supported, just skip and not fail in case the
5554 * encryption involves a common limitation of cryptography hardwares and
5555 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005556 if (status == PSA_ERROR_NOT_SUPPORTED) {
5557 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5558 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005559 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005561
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005563 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 }
Paul Elliott9961a662021-09-17 19:19:02 +01005565
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005567
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005569
Gilles Peskine449bd832023-01-11 14:50:10 +01005570 status = psa_aead_set_lengths(&operation, additional_data->len,
5571 input_data->len);
5572 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005573
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5575 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005576
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5578 input_data->len,
5579 plaintext, plaintext_size,
5580 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005581
Gilles Peskine449bd832023-01-11 14:50:10 +01005582 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005583 tag_buffer = tag->x;
5584 tag_size = tag->len;
5585 }
5586
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 status = psa_aead_verify(&operation, finish_plaintext,
5588 verify_plaintext_size,
5589 &plaintext_length,
5590 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005591
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005593
5594exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 psa_destroy_key(key);
5596 mbedtls_free(plaintext);
5597 mbedtls_free(finish_plaintext);
5598 psa_aead_abort(&operation);
5599 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005600}
5601/* END_CASE */
5602
Paul Elliott9961a662021-09-17 19:19:02 +01005603/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005604void aead_multipart_setup(int key_type_arg, data_t *key_data,
5605 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005606{
5607 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5608 psa_key_type_t key_type = key_type_arg;
5609 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005610 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005611 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5612 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5613 psa_status_t expected_status = expected_status_arg;
5614
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005616
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 psa_set_key_usage_flags(&attributes,
5618 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5619 psa_set_key_algorithm(&attributes, alg);
5620 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005621
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5623 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005624
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005626
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005628
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005630
Gilles Peskine449bd832023-01-11 14:50:10 +01005631 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005632
Gilles Peskine449bd832023-01-11 14:50:10 +01005633 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005634
5635exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 psa_destroy_key(key);
5637 psa_aead_abort(&operation);
5638 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005639}
5640/* END_CASE */
5641
5642/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005643void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5644 int alg_arg,
5645 data_t *nonce,
5646 data_t *additional_data,
5647 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005648{
5649 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5650 psa_key_type_t key_type = key_type_arg;
5651 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005652 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005653 unsigned char *output_data = NULL;
5654 unsigned char *final_data = NULL;
5655 size_t output_size = 0;
5656 size_t finish_output_size = 0;
5657 size_t output_length = 0;
5658 size_t key_bits = 0;
5659 size_t tag_length = 0;
5660 size_t tag_size = 0;
5661 size_t nonce_length = 0;
5662 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5663 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5664 size_t output_part_length = 0;
5665 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5666
Gilles Peskine449bd832023-01-11 14:50:10 +01005667 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005668
Gilles Peskine449bd832023-01-11 14:50:10 +01005669 psa_set_key_usage_flags(&attributes,
5670 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5671 psa_set_key_algorithm(&attributes, alg);
5672 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005673
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5675 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005676
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5678 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005679
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005681
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005683
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005685
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 ASSERT_ALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005687
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005689
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005691
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 ASSERT_ALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005693
5694 /* Test all operations error without calling setup first. */
5695
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5697 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005698
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005700
Gilles Peskine449bd832023-01-11 14:50:10 +01005701 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5702 PSA_AEAD_NONCE_MAX_SIZE,
5703 &nonce_length),
5704 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005705
Gilles Peskine449bd832023-01-11 14:50:10 +01005706 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005707
Paul Elliott481be342021-07-16 17:38:47 +01005708 /* ------------------------------------------------------- */
5709
Gilles Peskine449bd832023-01-11 14:50:10 +01005710 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5711 input_data->len),
5712 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005713
Gilles Peskine449bd832023-01-11 14:50:10 +01005714 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005715
Paul Elliott481be342021-07-16 17:38:47 +01005716 /* ------------------------------------------------------- */
5717
Gilles Peskine449bd832023-01-11 14:50:10 +01005718 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5719 additional_data->len),
5720 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005721
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005723
Paul Elliott481be342021-07-16 17:38:47 +01005724 /* ------------------------------------------------------- */
5725
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5727 input_data->len, output_data,
5728 output_size, &output_length),
5729 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005730
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005732
Paul Elliott481be342021-07-16 17:38:47 +01005733 /* ------------------------------------------------------- */
5734
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5736 finish_output_size,
5737 &output_part_length,
5738 tag_buffer, tag_length,
5739 &tag_size),
5740 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005741
Gilles Peskine449bd832023-01-11 14:50:10 +01005742 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005743
Paul Elliott481be342021-07-16 17:38:47 +01005744 /* ------------------------------------------------------- */
5745
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5747 finish_output_size,
5748 &output_part_length,
5749 tag_buffer,
5750 tag_length),
5751 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005752
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005754
5755 /* Test for double setups. */
5756
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005758
Gilles Peskine449bd832023-01-11 14:50:10 +01005759 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5760 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005761
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005763
Paul Elliott481be342021-07-16 17:38:47 +01005764 /* ------------------------------------------------------- */
5765
Gilles Peskine449bd832023-01-11 14:50:10 +01005766 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005767
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5769 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005770
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005772
Paul Elliott374a2be2021-07-16 17:53:40 +01005773 /* ------------------------------------------------------- */
5774
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005776
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5778 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005779
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005781
5782 /* ------------------------------------------------------- */
5783
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005785
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5787 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005788
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005790
Paul Elliottc23a9a02021-06-21 18:32:46 +01005791 /* Test for not setting a nonce. */
5792
Gilles Peskine449bd832023-01-11 14:50:10 +01005793 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005794
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5796 additional_data->len),
5797 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005798
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005800
Paul Elliott7f628422021-09-01 12:08:29 +01005801 /* ------------------------------------------------------- */
5802
Gilles Peskine449bd832023-01-11 14:50:10 +01005803 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005804
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5806 input_data->len, output_data,
5807 output_size, &output_length),
5808 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005809
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005811
Paul Elliottbdc2c682021-09-21 18:37:10 +01005812 /* ------------------------------------------------------- */
5813
Gilles Peskine449bd832023-01-11 14:50:10 +01005814 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005815
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5817 finish_output_size,
5818 &output_part_length,
5819 tag_buffer, tag_length,
5820 &tag_size),
5821 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005822
Gilles Peskine449bd832023-01-11 14:50:10 +01005823 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005824
5825 /* ------------------------------------------------------- */
5826
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005828
Gilles Peskine449bd832023-01-11 14:50:10 +01005829 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5830 finish_output_size,
5831 &output_part_length,
5832 tag_buffer,
5833 tag_length),
5834 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005835
Gilles Peskine449bd832023-01-11 14:50:10 +01005836 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005837
Paul Elliottc23a9a02021-06-21 18:32:46 +01005838 /* Test for double setting nonce. */
5839
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005841
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005843
Gilles Peskine449bd832023-01-11 14:50:10 +01005844 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5845 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005846
Gilles Peskine449bd832023-01-11 14:50:10 +01005847 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005848
Paul Elliott374a2be2021-07-16 17:53:40 +01005849 /* Test for double generating nonce. */
5850
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005852
Gilles Peskine449bd832023-01-11 14:50:10 +01005853 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5854 PSA_AEAD_NONCE_MAX_SIZE,
5855 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005856
Gilles Peskine449bd832023-01-11 14:50:10 +01005857 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5858 PSA_AEAD_NONCE_MAX_SIZE,
5859 &nonce_length),
5860 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005861
5862
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005864
5865 /* Test for generate nonce then set and vice versa */
5866
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005868
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5870 PSA_AEAD_NONCE_MAX_SIZE,
5871 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005872
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5874 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005875
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005877
Andrzej Kurekad837522021-12-15 15:28:49 +01005878 /* Test for generating nonce after calling set lengths */
5879
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005881
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5883 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005884
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5886 PSA_AEAD_NONCE_MAX_SIZE,
5887 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005888
Gilles Peskine449bd832023-01-11 14:50:10 +01005889 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005890
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005891 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005892
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005894
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 if (operation.alg == PSA_ALG_CCM) {
5896 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5897 input_data->len),
5898 PSA_ERROR_INVALID_ARGUMENT);
5899 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5900 PSA_AEAD_NONCE_MAX_SIZE,
5901 &nonce_length),
5902 PSA_ERROR_BAD_STATE);
5903 } else {
5904 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5905 input_data->len));
5906 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5907 PSA_AEAD_NONCE_MAX_SIZE,
5908 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005909 }
5910
Gilles Peskine449bd832023-01-11 14:50:10 +01005911 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005912
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005913 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005914#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005916
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5918 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5919 input_data->len),
5920 PSA_ERROR_INVALID_ARGUMENT);
5921 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5922 PSA_AEAD_NONCE_MAX_SIZE,
5923 &nonce_length),
5924 PSA_ERROR_BAD_STATE);
5925 } else {
5926 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5927 input_data->len));
5928 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5929 PSA_AEAD_NONCE_MAX_SIZE,
5930 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005931 }
5932
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005934#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005935
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005936 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005937
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005939
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5941 PSA_AEAD_NONCE_MAX_SIZE,
5942 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005943
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 if (operation.alg == PSA_ALG_CCM) {
5945 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5946 input_data->len),
5947 PSA_ERROR_INVALID_ARGUMENT);
5948 } else {
5949 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5950 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005951 }
5952
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005954
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005955 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005956 /* Test for setting nonce after calling set lengths */
5957
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005959
Gilles Peskine449bd832023-01-11 14:50:10 +01005960 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5961 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005962
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005964
Gilles Peskine449bd832023-01-11 14:50:10 +01005965 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005966
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005967 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005968
Gilles Peskine449bd832023-01-11 14:50:10 +01005969 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005970
Gilles Peskine449bd832023-01-11 14:50:10 +01005971 if (operation.alg == PSA_ALG_CCM) {
5972 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5973 input_data->len),
5974 PSA_ERROR_INVALID_ARGUMENT);
5975 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5976 PSA_ERROR_BAD_STATE);
5977 } else {
5978 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5979 input_data->len));
5980 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005981 }
5982
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005984
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005985 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005986#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005987 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005988
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5990 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5991 input_data->len),
5992 PSA_ERROR_INVALID_ARGUMENT);
5993 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5994 PSA_ERROR_BAD_STATE);
5995 } else {
5996 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5997 input_data->len));
5998 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005999 }
6000
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00006002#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006003
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006004 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006005
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006007
Gilles Peskine449bd832023-01-11 14:50:10 +01006008 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006009
Gilles Peskine449bd832023-01-11 14:50:10 +01006010 if (operation.alg == PSA_ALG_CCM) {
6011 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6012 input_data->len),
6013 PSA_ERROR_INVALID_ARGUMENT);
6014 } else {
6015 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6016 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006017 }
6018
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006020
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006021 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006022#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006024
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 if (operation.alg == PSA_ALG_GCM) {
6026 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6027 SIZE_MAX),
6028 PSA_ERROR_INVALID_ARGUMENT);
6029 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6030 PSA_ERROR_BAD_STATE);
6031 } else if (operation.alg != PSA_ALG_CCM) {
6032 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6033 SIZE_MAX));
6034 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006035 }
6036
Gilles Peskine449bd832023-01-11 14:50:10 +01006037 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006038#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006039
Tom Cosgrove1797b052022-12-04 17:19:59 +00006040 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006041#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006042 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006043
Gilles Peskine449bd832023-01-11 14:50:10 +01006044 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006045
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 if (operation.alg == PSA_ALG_GCM) {
6047 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6048 SIZE_MAX),
6049 PSA_ERROR_INVALID_ARGUMENT);
6050 } else if (operation.alg != PSA_ALG_CCM) {
6051 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6052 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006053 }
6054
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006056#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006057
6058 /* ------------------------------------------------------- */
6059
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006061
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006063
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6065 PSA_AEAD_NONCE_MAX_SIZE,
6066 &nonce_length),
6067 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006068
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006070
Paul Elliott7220cae2021-06-22 17:25:57 +01006071 /* Test for generating nonce in decrypt setup. */
6072
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006074
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6076 PSA_AEAD_NONCE_MAX_SIZE,
6077 &nonce_length),
6078 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006079
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006081
Paul Elliottc23a9a02021-06-21 18:32:46 +01006082 /* Test for setting lengths twice. */
6083
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006085
Gilles Peskine449bd832023-01-11 14:50:10 +01006086 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006087
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6089 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006090
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6092 input_data->len),
6093 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006094
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006096
Andrzej Kurekad837522021-12-15 15:28:49 +01006097 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006098
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006100
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006102
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006104
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6106 additional_data->len),
6107 PSA_ERROR_BAD_STATE);
6108 } else {
6109 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6110 additional_data->len));
6111
6112 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6113 input_data->len),
6114 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006115 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006117
6118 /* ------------------------------------------------------- */
6119
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006121
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006123
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 if (operation.alg == PSA_ALG_CCM) {
6125 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6126 input_data->len, output_data,
6127 output_size, &output_length),
6128 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006129
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 } else {
6131 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6132 input_data->len, output_data,
6133 output_size, &output_length));
6134
6135 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6136 input_data->len),
6137 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006138 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006140
6141 /* ------------------------------------------------------- */
6142
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006144
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006146
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 if (operation.alg == PSA_ALG_CCM) {
6148 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6149 finish_output_size,
6150 &output_part_length,
6151 tag_buffer, tag_length,
6152 &tag_size));
6153 } else {
6154 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6155 finish_output_size,
6156 &output_part_length,
6157 tag_buffer, tag_length,
6158 &tag_size));
6159
6160 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6161 input_data->len),
6162 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006163 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006165
6166 /* Test for setting lengths after generating nonce + already starting data. */
6167
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006169
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6171 PSA_AEAD_NONCE_MAX_SIZE,
6172 &nonce_length));
6173 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006174
Gilles Peskine449bd832023-01-11 14:50:10 +01006175 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6176 additional_data->len),
6177 PSA_ERROR_BAD_STATE);
6178 } else {
6179 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6180 additional_data->len));
6181
6182 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6183 input_data->len),
6184 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006185 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006187
6188 /* ------------------------------------------------------- */
6189
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006191
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6193 PSA_AEAD_NONCE_MAX_SIZE,
6194 &nonce_length));
6195 if (operation.alg == PSA_ALG_CCM) {
6196 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6197 input_data->len, output_data,
6198 output_size, &output_length),
6199 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006200
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 } else {
6202 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6203 input_data->len, output_data,
6204 output_size, &output_length));
6205
6206 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6207 input_data->len),
6208 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006209 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006211
6212 /* ------------------------------------------------------- */
6213
Gilles Peskine449bd832023-01-11 14:50:10 +01006214 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006215
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6217 PSA_AEAD_NONCE_MAX_SIZE,
6218 &nonce_length));
6219 if (operation.alg == PSA_ALG_CCM) {
6220 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6221 finish_output_size,
6222 &output_part_length,
6223 tag_buffer, tag_length,
6224 &tag_size));
6225 } else {
6226 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6227 finish_output_size,
6228 &output_part_length,
6229 tag_buffer, tag_length,
6230 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006231
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6233 input_data->len),
6234 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006235 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006236 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006237
Paul Elliott243080c2021-07-21 19:01:17 +01006238 /* Test for not sending any additional data or data after setting non zero
6239 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006240
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006242
Gilles Peskine449bd832023-01-11 14:50:10 +01006243 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006244
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6246 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006247
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6249 finish_output_size,
6250 &output_part_length,
6251 tag_buffer, tag_length,
6252 &tag_size),
6253 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006254
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006256
Paul Elliott243080c2021-07-21 19:01:17 +01006257 /* Test for not sending any additional data or data after setting non-zero
6258 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006261
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006263
Gilles Peskine449bd832023-01-11 14:50:10 +01006264 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6265 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006266
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6268 finish_output_size,
6269 &output_part_length,
6270 tag_buffer,
6271 tag_length),
6272 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006273
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006275
Paul Elliott243080c2021-07-21 19:01:17 +01006276 /* Test for not sending any additional data after setting a non-zero length
6277 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006278
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006280
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006282
Gilles Peskine449bd832023-01-11 14:50:10 +01006283 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6284 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006285
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6287 input_data->len, output_data,
6288 output_size, &output_length),
6289 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006290
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006292
Paul Elliottf94bd992021-09-19 18:15:59 +01006293 /* Test for not sending any data after setting a non-zero length for it.*/
6294
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006296
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006298
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6300 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006301
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6303 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006304
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6306 finish_output_size,
6307 &output_part_length,
6308 tag_buffer, tag_length,
6309 &tag_size),
6310 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006311
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006313
Paul Elliottb0450fe2021-09-01 15:06:26 +01006314 /* Test for sending too much additional data after setting lengths. */
6315
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006317
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006319
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006321
6322
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6324 additional_data->len),
6325 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006326
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006328
Paul Elliotta2a09b02021-09-22 14:56:40 +01006329 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006330
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006332
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006334
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6336 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006337
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6339 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006340
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6342 1),
6343 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006344
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006346
Paul Elliottb0450fe2021-09-01 15:06:26 +01006347 /* Test for sending too much data after setting lengths. */
6348
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006350
Gilles Peskine449bd832023-01-11 14:50:10 +01006351 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006352
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006354
Gilles Peskine449bd832023-01-11 14:50:10 +01006355 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6356 input_data->len, output_data,
6357 output_size, &output_length),
6358 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006359
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006361
Paul Elliotta2a09b02021-09-22 14:56:40 +01006362 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006363
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006365
Gilles Peskine449bd832023-01-11 14:50:10 +01006366 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006367
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6369 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006370
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6372 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006373
Gilles Peskine449bd832023-01-11 14:50:10 +01006374 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6375 input_data->len, output_data,
6376 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006377
Gilles Peskine449bd832023-01-11 14:50:10 +01006378 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6379 1, output_data,
6380 output_size, &output_length),
6381 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006382
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006384
Paul Elliottc23a9a02021-06-21 18:32:46 +01006385 /* Test sending additional data after data. */
6386
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006388
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006390
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 if (operation.alg != PSA_ALG_CCM) {
6392 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6393 input_data->len, output_data,
6394 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006395
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6397 additional_data->len),
6398 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006399 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006400 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006401
Paul Elliott534d0b42021-06-22 19:15:20 +01006402 /* Test calling finish on decryption. */
6403
Gilles Peskine449bd832023-01-11 14:50:10 +01006404 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006405
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006407
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6409 finish_output_size,
6410 &output_part_length,
6411 tag_buffer, tag_length,
6412 &tag_size),
6413 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006414
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006416
6417 /* Test calling verify on encryption. */
6418
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006420
Gilles Peskine449bd832023-01-11 14:50:10 +01006421 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006422
Gilles Peskine449bd832023-01-11 14:50:10 +01006423 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6424 finish_output_size,
6425 &output_part_length,
6426 tag_buffer,
6427 tag_length),
6428 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006429
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006431
6432
Paul Elliottc23a9a02021-06-21 18:32:46 +01006433exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 psa_destroy_key(key);
6435 psa_aead_abort(&operation);
6436 mbedtls_free(output_data);
6437 mbedtls_free(final_data);
6438 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006439}
6440/* END_CASE */
6441
6442/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006443void signature_size(int type_arg,
6444 int bits,
6445 int alg_arg,
6446 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006447{
6448 psa_key_type_t type = type_arg;
6449 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006451
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006453
Gilles Peskinee59236f2018-01-27 23:32:46 +01006454exit:
6455 ;
6456}
6457/* END_CASE */
6458
6459/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006460void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6461 int alg_arg, data_t *input_data,
6462 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006463{
Ronald Cron5425a212020-08-04 14:58:35 +02006464 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006465 psa_key_type_t key_type = key_type_arg;
6466 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006467 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006468 unsigned char *signature = NULL;
6469 size_t signature_size;
6470 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006471 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006472
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006474
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6476 psa_set_key_algorithm(&attributes, alg);
6477 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006478
Gilles Peskine449bd832023-01-11 14:50:10 +01006479 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6480 &key));
6481 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6482 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006483
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006484 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006485 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6487 key_bits, alg);
6488 TEST_ASSERT(signature_size != 0);
6489 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6490 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006491
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006492 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006493 PSA_ASSERT(psa_sign_hash(key, alg,
6494 input_data->x, input_data->len,
6495 signature, signature_size,
6496 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006497 /* Verify that the signature is what is expected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 ASSERT_COMPARE(output_data->x, output_data->len,
6499 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006500
6501exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006502 /*
6503 * Key attributes may have been returned by psa_get_key_attributes()
6504 * thus reset them as required.
6505 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006507
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 psa_destroy_key(key);
6509 mbedtls_free(signature);
6510 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006511}
6512/* END_CASE */
6513
Paul Elliott712d5122022-12-07 14:03:10 +00006514/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006515/**
6516 * sign_hash_interruptible() test intentions:
6517 *
6518 * Note: This test can currently only handle ECDSA.
6519 *
6520 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006521 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006522 *
6523 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6524 * expected for different max_ops values.
6525 *
6526 * 3. Test that the number of ops done prior to start and after abort is zero
6527 * and that each successful stage completes some ops (this is not mandated by
6528 * the PSA specification, but is currently the case).
6529 *
6530 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6531 * complete() calls does not alter the number of ops returned.
6532 */
Paul Elliott712d5122022-12-07 14:03:10 +00006533void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6534 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006535 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006536{
6537 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6538 psa_key_type_t key_type = key_type_arg;
6539 psa_algorithm_t alg = alg_arg;
6540 size_t key_bits;
6541 unsigned char *signature = NULL;
6542 size_t signature_size;
6543 size_t signature_length = 0xdeadbeef;
6544 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6545 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006546 uint32_t num_ops = 0;
6547 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006548 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006549 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006550 size_t min_completes = 0;
6551 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006552
Paul Elliott712d5122022-12-07 14:03:10 +00006553 psa_sign_hash_interruptible_operation_t operation =
6554 psa_sign_hash_interruptible_operation_init();
6555
6556 PSA_ASSERT(psa_crypto_init());
6557
6558 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6559 psa_set_key_algorithm(&attributes, alg);
6560 psa_set_key_type(&attributes, key_type);
6561
6562 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6563 &key));
6564 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6565 key_bits = psa_get_key_bits(&attributes);
6566
6567 /* Allocate a buffer which has the size advertised by the
6568 * library. */
6569 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6570 key_bits, alg);
6571 TEST_ASSERT(signature_size != 0);
6572 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6573 ASSERT_ALLOC(signature, signature_size);
6574
Paul Elliott0c683352022-12-16 19:16:56 +00006575 psa_interruptible_set_max_ops(max_ops);
6576
Paul Elliott6f600372023-02-06 18:41:05 +00006577 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6578 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006579
Paul Elliott712d5122022-12-07 14:03:10 +00006580 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6581 TEST_ASSERT(num_ops_prior == 0);
6582
6583 /* Start performing the signature. */
6584 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6585 input_data->x, input_data->len));
6586
6587 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6588 TEST_ASSERT(num_ops_prior == 0);
6589
6590 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006591 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006592 status = psa_sign_hash_complete(&operation, signature, signature_size,
6593 &signature_length);
6594
Paul Elliott0c683352022-12-16 19:16:56 +00006595 num_completes++;
6596
Paul Elliott712d5122022-12-07 14:03:10 +00006597 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6598 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006599 /* We are asserting here that every complete makes progress
6600 * (completes some ops), which is true of the internal
6601 * implementation and probably any implementation, however this is
6602 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006603 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006604
Paul Elliott712d5122022-12-07 14:03:10 +00006605 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006606
6607 /* Ensure calling get_num_ops() twice still returns the same
6608 * number of ops as previously reported. */
6609 num_ops = psa_sign_hash_get_num_ops(&operation);
6610
6611 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006612 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006613 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006614
6615 TEST_ASSERT(status == PSA_SUCCESS);
6616
Paul Elliott0c683352022-12-16 19:16:56 +00006617 TEST_LE_U(min_completes, num_completes);
6618 TEST_LE_U(num_completes, max_completes);
6619
Paul Elliott712d5122022-12-07 14:03:10 +00006620 /* Verify that the signature is what is expected. */
6621 ASSERT_COMPARE(output_data->x, output_data->len,
6622 signature, signature_length);
6623
6624 PSA_ASSERT(psa_sign_hash_abort(&operation));
6625
Paul Elliott59ad9452022-12-18 15:09:02 +00006626 num_ops = psa_sign_hash_get_num_ops(&operation);
6627 TEST_ASSERT(num_ops == 0);
6628
Paul Elliott712d5122022-12-07 14:03:10 +00006629exit:
6630
6631 /*
6632 * Key attributes may have been returned by psa_get_key_attributes()
6633 * thus reset them as required.
6634 */
6635 psa_reset_key_attributes(&attributes);
6636
6637 psa_destroy_key(key);
6638 mbedtls_free(signature);
6639 PSA_DONE();
6640}
6641/* END_CASE */
6642
Gilles Peskine20035e32018-02-03 22:44:14 +01006643/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006644void sign_hash_fail(int key_type_arg, data_t *key_data,
6645 int alg_arg, data_t *input_data,
6646 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006647{
Ronald Cron5425a212020-08-04 14:58:35 +02006648 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006649 psa_key_type_t key_type = key_type_arg;
6650 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006651 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006652 psa_status_t actual_status;
6653 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006654 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006655 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006656 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006657
Gilles Peskine449bd832023-01-11 14:50:10 +01006658 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006659
Gilles Peskine449bd832023-01-11 14:50:10 +01006660 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006661
Gilles Peskine449bd832023-01-11 14:50:10 +01006662 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6663 psa_set_key_algorithm(&attributes, alg);
6664 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006665
Gilles Peskine449bd832023-01-11 14:50:10 +01006666 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6667 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006668
Gilles Peskine449bd832023-01-11 14:50:10 +01006669 actual_status = psa_sign_hash(key, alg,
6670 input_data->x, input_data->len,
6671 signature, signature_size,
6672 &signature_length);
6673 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006674 /* The value of *signature_length is unspecified on error, but
6675 * whatever it is, it should be less than signature_size, so that
6676 * if the caller tries to read *signature_length bytes without
6677 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006678 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006679
6680exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006681 psa_reset_key_attributes(&attributes);
6682 psa_destroy_key(key);
6683 mbedtls_free(signature);
6684 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006685}
6686/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006687
Paul Elliott91007972022-12-16 12:21:24 +00006688/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006689/**
6690 * sign_hash_fail_interruptible() test intentions:
6691 *
6692 * Note: This test can currently only handle ECDSA.
6693 *
6694 * 1. Test that various failure cases for interruptible sign hash fail with the
6695 * correct error codes, and at the correct point (at start or during
6696 * complete).
6697 *
6698 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6699 * expected for different max_ops values.
6700 *
6701 * 3. Test that the number of ops done prior to start and after abort is zero
6702 * and that each successful stage completes some ops (this is not mandated by
6703 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006704 *
6705 * 4. Check that calling complete() when start() fails and complete()
6706 * after completion results in a BAD_STATE error.
6707 *
6708 * 5. Check that calling start() again after start fails results in a BAD_STATE
6709 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006710 */
Paul Elliott91007972022-12-16 12:21:24 +00006711void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6712 int alg_arg, data_t *input_data,
6713 int signature_size_arg,
6714 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006715 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006716 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006717{
6718 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6719 psa_key_type_t key_type = key_type_arg;
6720 psa_algorithm_t alg = alg_arg;
6721 size_t signature_size = signature_size_arg;
6722 psa_status_t actual_status;
6723 psa_status_t expected_start_status = expected_start_status_arg;
6724 psa_status_t expected_complete_status = expected_complete_status_arg;
6725 unsigned char *signature = NULL;
6726 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006727 uint32_t num_ops = 0;
6728 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006729 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006730 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006731 size_t min_completes = 0;
6732 size_t max_completes = 0;
6733
Paul Elliott91007972022-12-16 12:21:24 +00006734 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6735 psa_sign_hash_interruptible_operation_t operation =
6736 psa_sign_hash_interruptible_operation_init();
6737
6738 ASSERT_ALLOC(signature, signature_size);
6739
6740 PSA_ASSERT(psa_crypto_init());
6741
6742 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6743 psa_set_key_algorithm(&attributes, alg);
6744 psa_set_key_type(&attributes, key_type);
6745
6746 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6747 &key));
6748
Paul Elliott0c683352022-12-16 19:16:56 +00006749 psa_interruptible_set_max_ops(max_ops);
6750
Paul Elliott6f600372023-02-06 18:41:05 +00006751 interruptible_signverify_get_minmax_completes(max_ops,
6752 expected_complete_status,
6753 &min_completes,
6754 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006755
Paul Elliott91007972022-12-16 12:21:24 +00006756 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6757 TEST_ASSERT(num_ops_prior == 0);
6758
6759 /* Start performing the signature. */
6760 actual_status = psa_sign_hash_start(&operation, key, alg,
6761 input_data->x, input_data->len);
6762
6763 TEST_EQUAL(actual_status, expected_start_status);
6764
Paul Elliottc9774412023-02-06 15:14:07 +00006765 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006766 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006767 * start failed. */
6768 actual_status = psa_sign_hash_complete(&operation, signature,
6769 signature_size,
6770 &signature_length);
6771
6772 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6773
6774 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006775 actual_status = psa_sign_hash_start(&operation, key, alg,
6776 input_data->x, input_data->len);
6777
6778 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6779 }
6780
Paul Elliott91007972022-12-16 12:21:24 +00006781 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6782 TEST_ASSERT(num_ops_prior == 0);
6783
Paul Elliott91007972022-12-16 12:21:24 +00006784 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006785 do {
Paul Elliott91007972022-12-16 12:21:24 +00006786 actual_status = psa_sign_hash_complete(&operation, signature,
6787 signature_size,
6788 &signature_length);
6789
Paul Elliott0c683352022-12-16 19:16:56 +00006790 num_completes++;
6791
Paul Elliott334d7262023-01-20 17:29:41 +00006792 if (actual_status == PSA_SUCCESS ||
6793 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006794 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006795 /* We are asserting here that every complete makes progress
6796 * (completes some ops), which is true of the internal
6797 * implementation and probably any implementation, however this is
6798 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006799 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006800
Paul Elliott91007972022-12-16 12:21:24 +00006801 num_ops_prior = num_ops;
6802 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006803 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006804
Paul Elliottc9774412023-02-06 15:14:07 +00006805 TEST_EQUAL(actual_status, expected_complete_status);
6806
Paul Elliottefebad02023-02-15 16:56:45 +00006807 /* Check that another complete returns BAD_STATE. */
6808 actual_status = psa_sign_hash_complete(&operation, signature,
6809 signature_size,
6810 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006811
Paul Elliottefebad02023-02-15 16:56:45 +00006812 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006813
Paul Elliott91007972022-12-16 12:21:24 +00006814 PSA_ASSERT(psa_sign_hash_abort(&operation));
6815
Paul Elliott59ad9452022-12-18 15:09:02 +00006816 num_ops = psa_sign_hash_get_num_ops(&operation);
6817 TEST_ASSERT(num_ops == 0);
6818
Paul Elliott91007972022-12-16 12:21:24 +00006819 /* The value of *signature_length is unspecified on error, but
6820 * whatever it is, it should be less than signature_size, so that
6821 * if the caller tries to read *signature_length bytes without
6822 * checking the error code then they don't overflow a buffer. */
6823 TEST_LE_U(signature_length, signature_size);
6824
Paul Elliott0c683352022-12-16 19:16:56 +00006825 TEST_LE_U(min_completes, num_completes);
6826 TEST_LE_U(num_completes, max_completes);
6827
Paul Elliott91007972022-12-16 12:21:24 +00006828exit:
6829 psa_reset_key_attributes(&attributes);
6830 psa_destroy_key(key);
6831 mbedtls_free(signature);
6832 PSA_DONE();
6833}
6834/* END_CASE */
6835
mohammad16038cc1cee2018-03-28 01:21:33 +03006836/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006837void sign_verify_hash(int key_type_arg, data_t *key_data,
6838 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006839{
Ronald Cron5425a212020-08-04 14:58:35 +02006840 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006841 psa_key_type_t key_type = key_type_arg;
6842 psa_algorithm_t alg = alg_arg;
6843 size_t key_bits;
6844 unsigned char *signature = NULL;
6845 size_t signature_size;
6846 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006847 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006848
Gilles Peskine449bd832023-01-11 14:50:10 +01006849 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006850
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6852 psa_set_key_algorithm(&attributes, alg);
6853 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006854
Gilles Peskine449bd832023-01-11 14:50:10 +01006855 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6856 &key));
6857 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6858 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006859
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006860 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006861 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006862 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6863 key_bits, alg);
6864 TEST_ASSERT(signature_size != 0);
6865 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6866 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006867
6868 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006869 PSA_ASSERT(psa_sign_hash(key, alg,
6870 input_data->x, input_data->len,
6871 signature, signature_size,
6872 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006873 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006874 TEST_LE_U(signature_length, signature_size);
6875 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006876
6877 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006878 PSA_ASSERT(psa_verify_hash(key, alg,
6879 input_data->x, input_data->len,
6880 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006881
Gilles Peskine449bd832023-01-11 14:50:10 +01006882 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006883 /* Flip a bit in the input and verify that the signature is now
6884 * detected as invalid. Flip a bit at the beginning, not at the end,
6885 * because ECDSA may ignore the last few bits of the input. */
6886 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006887 TEST_EQUAL(psa_verify_hash(key, alg,
6888 input_data->x, input_data->len,
6889 signature, signature_length),
6890 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006891 }
6892
6893exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006894 /*
6895 * Key attributes may have been returned by psa_get_key_attributes()
6896 * thus reset them as required.
6897 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006898 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006899
Gilles Peskine449bd832023-01-11 14:50:10 +01006900 psa_destroy_key(key);
6901 mbedtls_free(signature);
6902 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006903}
6904/* END_CASE */
6905
Paul Elliott712d5122022-12-07 14:03:10 +00006906/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006907/**
6908 * sign_verify_hash_interruptible() test intentions:
6909 *
6910 * Note: This test can currently only handle ECDSA.
6911 *
Paul Elliott8c092052023-03-06 17:49:14 +00006912 * 1. Test that we can sign an input hash with the given keypair and then
6913 * afterwards verify that signature. This is currently the only way to test
6914 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00006915 *
6916 * 2. Test that after corrupting the hash, the verification detects an invalid
6917 * signature.
6918 *
6919 * 3. Test the number of calls to psa_sign_hash_complete() required are as
6920 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00006921 *
6922 * 4. Test that the number of ops done prior to starting signing and after abort
6923 * is zero and that each successful signing stage completes some ops (this is
6924 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00006925 */
Paul Elliott712d5122022-12-07 14:03:10 +00006926void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006927 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006928 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006929{
6930 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6931 psa_key_type_t key_type = key_type_arg;
6932 psa_algorithm_t alg = alg_arg;
6933 size_t key_bits;
6934 unsigned char *signature = NULL;
6935 size_t signature_size;
6936 size_t signature_length = 0xdeadbeef;
6937 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6938 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006939 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00006940 uint32_t num_ops = 0;
6941 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006942 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006943 size_t min_completes = 0;
6944 size_t max_completes = 0;
6945
Paul Elliott712d5122022-12-07 14:03:10 +00006946 psa_sign_hash_interruptible_operation_t sign_operation =
6947 psa_sign_hash_interruptible_operation_init();
6948 psa_verify_hash_interruptible_operation_t verify_operation =
6949 psa_verify_hash_interruptible_operation_init();
6950
6951 PSA_ASSERT(psa_crypto_init());
6952
Paul Elliott0c683352022-12-16 19:16:56 +00006953 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6954 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006955 psa_set_key_algorithm(&attributes, alg);
6956 psa_set_key_type(&attributes, key_type);
6957
6958 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6959 &key));
6960 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6961 key_bits = psa_get_key_bits(&attributes);
6962
6963 /* Allocate a buffer which has the size advertised by the
6964 * library. */
6965 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6966 key_bits, alg);
6967 TEST_ASSERT(signature_size != 0);
6968 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6969 ASSERT_ALLOC(signature, signature_size);
6970
Paul Elliott0c683352022-12-16 19:16:56 +00006971 psa_interruptible_set_max_ops(max_ops);
6972
Paul Elliott6f600372023-02-06 18:41:05 +00006973 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6974 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006975
Paul Elliott7c173082023-02-26 18:44:45 +00006976 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6977 TEST_ASSERT(num_ops_prior == 0);
6978
Paul Elliott712d5122022-12-07 14:03:10 +00006979 /* Start performing the signature. */
6980 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6981 input_data->x, input_data->len));
6982
Paul Elliott7c173082023-02-26 18:44:45 +00006983 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6984 TEST_ASSERT(num_ops_prior == 0);
6985
Paul Elliott712d5122022-12-07 14:03:10 +00006986 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006987 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006988
Paul Elliott0c683352022-12-16 19:16:56 +00006989 status = psa_sign_hash_complete(&sign_operation, signature,
6990 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00006991 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00006992
6993 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00006994
6995 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6996 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
6997 /* We are asserting here that every complete makes progress
6998 * (completes some ops), which is true of the internal
6999 * implementation and probably any implementation, however this is
7000 * not mandated by the PSA specification. */
7001 TEST_ASSERT(num_ops > num_ops_prior);
7002
7003 num_ops_prior = num_ops;
7004 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007005 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007006
7007 TEST_ASSERT(status == PSA_SUCCESS);
7008
Paul Elliott0c683352022-12-16 19:16:56 +00007009 TEST_LE_U(min_completes, num_completes);
7010 TEST_LE_U(num_completes, max_completes);
7011
Paul Elliott712d5122022-12-07 14:03:10 +00007012 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7013
Paul Elliott7c173082023-02-26 18:44:45 +00007014 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7015 TEST_ASSERT(num_ops == 0);
7016
Paul Elliott712d5122022-12-07 14:03:10 +00007017 /* Check that the signature length looks sensible. */
7018 TEST_LE_U(signature_length, signature_size);
7019 TEST_ASSERT(signature_length > 0);
7020
Paul Elliott0c683352022-12-16 19:16:56 +00007021 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007022
7023 /* Start verification. */
7024 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7025 input_data->x, input_data->len,
7026 signature, signature_length));
7027
7028 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007029 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007030 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007031
7032 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007033 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007034
7035 TEST_ASSERT(status == PSA_SUCCESS);
7036
Paul Elliott0c683352022-12-16 19:16:56 +00007037 TEST_LE_U(min_completes, num_completes);
7038 TEST_LE_U(num_completes, max_completes);
7039
Paul Elliott712d5122022-12-07 14:03:10 +00007040 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7041
7042 verify_operation = psa_verify_hash_interruptible_operation_init();
7043
7044 if (input_data->len != 0) {
7045 /* Flip a bit in the input and verify that the signature is now
7046 * detected as invalid. Flip a bit at the beginning, not at the end,
7047 * because ECDSA may ignore the last few bits of the input. */
7048 input_data->x[0] ^= 1;
7049
Paul Elliott712d5122022-12-07 14:03:10 +00007050 /* Start verification. */
7051 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7052 input_data->x, input_data->len,
7053 signature, signature_length));
7054
7055 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007056 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007057 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007058 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007059
7060 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7061 }
7062
7063 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7064
7065exit:
7066 /*
7067 * Key attributes may have been returned by psa_get_key_attributes()
7068 * thus reset them as required.
7069 */
7070 psa_reset_key_attributes(&attributes);
7071
7072 psa_destroy_key(key);
7073 mbedtls_free(signature);
7074 PSA_DONE();
7075}
7076/* END_CASE */
7077
Gilles Peskine9911b022018-06-29 17:30:48 +02007078/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007079void verify_hash(int key_type_arg, data_t *key_data,
7080 int alg_arg, data_t *hash_data,
7081 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007082{
Ronald Cron5425a212020-08-04 14:58:35 +02007083 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007084 psa_key_type_t key_type = key_type_arg;
7085 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007086 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007087
Gilles Peskine449bd832023-01-11 14:50:10 +01007088 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007089
Gilles Peskine449bd832023-01-11 14:50:10 +01007090 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007091
Gilles Peskine449bd832023-01-11 14:50:10 +01007092 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7093 psa_set_key_algorithm(&attributes, alg);
7094 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007095
Gilles Peskine449bd832023-01-11 14:50:10 +01007096 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7097 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007098
Gilles Peskine449bd832023-01-11 14:50:10 +01007099 PSA_ASSERT(psa_verify_hash(key, alg,
7100 hash_data->x, hash_data->len,
7101 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007102
itayzafrir5c753392018-05-08 11:18:38 +03007103exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 psa_reset_key_attributes(&attributes);
7105 psa_destroy_key(key);
7106 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007107}
7108/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007109
Paul Elliott712d5122022-12-07 14:03:10 +00007110/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007111/**
7112 * verify_hash_interruptible() test intentions:
7113 *
7114 * Note: This test can currently only handle ECDSA.
7115 *
7116 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007117 * only). Given this test only does verification it can accept public keys as
7118 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007119 *
7120 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7121 * expected for different max_ops values.
7122 *
7123 * 3. Test that the number of ops done prior to start and after abort is zero
7124 * and that each successful stage completes some ops (this is not mandated by
7125 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007126 *
7127 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7128 * complete() calls does not alter the number of ops returned.
7129 *
7130 * 5. Test that after corrupting the hash, the verification detects an invalid
7131 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007132 */
Paul Elliott712d5122022-12-07 14:03:10 +00007133void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7134 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007135 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007136{
7137 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7138 psa_key_type_t key_type = key_type_arg;
7139 psa_algorithm_t alg = alg_arg;
7140 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7141 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007142 uint32_t num_ops = 0;
7143 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007144 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007145 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007146 size_t min_completes = 0;
7147 size_t max_completes = 0;
7148
Paul Elliott712d5122022-12-07 14:03:10 +00007149 psa_verify_hash_interruptible_operation_t operation =
7150 psa_verify_hash_interruptible_operation_init();
7151
7152 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7153
7154 PSA_ASSERT(psa_crypto_init());
7155
7156 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7157 psa_set_key_algorithm(&attributes, alg);
7158 psa_set_key_type(&attributes, key_type);
7159
7160 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7161 &key));
7162
Paul Elliott0c683352022-12-16 19:16:56 +00007163 psa_interruptible_set_max_ops(max_ops);
7164
Paul Elliott6f600372023-02-06 18:41:05 +00007165 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7166 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007167
Paul Elliott712d5122022-12-07 14:03:10 +00007168 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7169
7170 TEST_ASSERT(num_ops_prior == 0);
7171
7172 /* Start verification. */
7173 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7174 hash_data->x, hash_data->len,
7175 signature_data->x, signature_data->len)
7176 );
7177
7178 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7179
7180 TEST_ASSERT(num_ops_prior == 0);
7181
7182 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007183 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007184 status = psa_verify_hash_complete(&operation);
7185
Paul Elliott0c683352022-12-16 19:16:56 +00007186 num_completes++;
7187
Paul Elliott712d5122022-12-07 14:03:10 +00007188 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7189 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007190 /* We are asserting here that every complete makes progress
7191 * (completes some ops), which is true of the internal
7192 * implementation and probably any implementation, however this is
7193 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007194 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007195
Paul Elliott712d5122022-12-07 14:03:10 +00007196 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007197
7198 /* Ensure calling get_num_ops() twice still returns the same
7199 * number of ops as previously reported. */
7200 num_ops = psa_verify_hash_get_num_ops(&operation);
7201
7202 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007203 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007204 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007205
7206 TEST_ASSERT(status == PSA_SUCCESS);
7207
Paul Elliott0c683352022-12-16 19:16:56 +00007208 TEST_LE_U(min_completes, num_completes);
7209 TEST_LE_U(num_completes, max_completes);
7210
Paul Elliott712d5122022-12-07 14:03:10 +00007211 PSA_ASSERT(psa_verify_hash_abort(&operation));
7212
Paul Elliott59ad9452022-12-18 15:09:02 +00007213 num_ops = psa_verify_hash_get_num_ops(&operation);
7214 TEST_ASSERT(num_ops == 0);
7215
Paul Elliott8359c142023-02-24 18:40:10 +00007216 if (hash_data->len != 0) {
7217 /* Flip a bit in the hash and verify that the signature is now detected
7218 * as invalid. Flip a bit at the beginning, not at the end, because
7219 * ECDSA may ignore the last few bits of the input. */
7220 hash_data->x[0] ^= 1;
7221
7222 /* Start verification. */
7223 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7224 hash_data->x, hash_data->len,
7225 signature_data->x, signature_data->len));
7226
7227 /* Continue performing the signature until complete. */
7228 do {
7229 status = psa_verify_hash_complete(&operation);
7230 } while (status == PSA_OPERATION_INCOMPLETE);
7231
7232 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7233 }
7234
Paul Elliott712d5122022-12-07 14:03:10 +00007235exit:
7236 psa_reset_key_attributes(&attributes);
7237 psa_destroy_key(key);
7238 PSA_DONE();
7239}
7240/* END_CASE */
7241
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007242/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007243void verify_hash_fail(int key_type_arg, data_t *key_data,
7244 int alg_arg, data_t *hash_data,
7245 data_t *signature_data,
7246 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007247{
Ronald Cron5425a212020-08-04 14:58:35 +02007248 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007249 psa_key_type_t key_type = key_type_arg;
7250 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007251 psa_status_t actual_status;
7252 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007253 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007254
Gilles Peskine449bd832023-01-11 14:50:10 +01007255 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007256
Gilles Peskine449bd832023-01-11 14:50:10 +01007257 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7258 psa_set_key_algorithm(&attributes, alg);
7259 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007260
Gilles Peskine449bd832023-01-11 14:50:10 +01007261 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7262 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007263
Gilles Peskine449bd832023-01-11 14:50:10 +01007264 actual_status = psa_verify_hash(key, alg,
7265 hash_data->x, hash_data->len,
7266 signature_data->x, signature_data->len);
7267 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007268
7269exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007270 psa_reset_key_attributes(&attributes);
7271 psa_destroy_key(key);
7272 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007273}
7274/* END_CASE */
7275
Paul Elliott91007972022-12-16 12:21:24 +00007276/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007277/**
7278 * verify_hash_fail_interruptible() test intentions:
7279 *
7280 * Note: This test can currently only handle ECDSA.
7281 *
7282 * 1. Test that various failure cases for interruptible verify hash fail with
7283 * the correct error codes, and at the correct point (at start or during
7284 * complete).
7285 *
7286 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7287 * expected for different max_ops values.
7288 *
7289 * 3. Test that the number of ops done prior to start and after abort is zero
7290 * and that each successful stage completes some ops (this is not mandated by
7291 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007292 *
7293 * 4. Check that calling complete() when start() fails and complete()
7294 * after completion results in a BAD_STATE error.
7295 *
7296 * 5. Check that calling start() again after start fails results in a BAD_STATE
7297 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007298 */
Paul Elliott91007972022-12-16 12:21:24 +00007299void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7300 int alg_arg, data_t *hash_data,
7301 data_t *signature_data,
7302 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007303 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007304 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007305{
7306 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7307 psa_key_type_t key_type = key_type_arg;
7308 psa_algorithm_t alg = alg_arg;
7309 psa_status_t actual_status;
7310 psa_status_t expected_start_status = expected_start_status_arg;
7311 psa_status_t expected_complete_status = expected_complete_status_arg;
7312 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007313 uint32_t num_ops = 0;
7314 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007315 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007316 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007317 size_t min_completes = 0;
7318 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007319 psa_verify_hash_interruptible_operation_t operation =
7320 psa_verify_hash_interruptible_operation_init();
7321
7322 PSA_ASSERT(psa_crypto_init());
7323
7324 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7325 psa_set_key_algorithm(&attributes, alg);
7326 psa_set_key_type(&attributes, key_type);
7327
7328 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7329 &key));
7330
Paul Elliott0c683352022-12-16 19:16:56 +00007331 psa_interruptible_set_max_ops(max_ops);
7332
Paul Elliott6f600372023-02-06 18:41:05 +00007333 interruptible_signverify_get_minmax_completes(max_ops,
7334 expected_complete_status,
7335 &min_completes,
7336 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007337
Paul Elliott91007972022-12-16 12:21:24 +00007338 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7339 TEST_ASSERT(num_ops_prior == 0);
7340
7341 /* Start verification. */
7342 actual_status = psa_verify_hash_start(&operation, key, alg,
7343 hash_data->x, hash_data->len,
7344 signature_data->x,
7345 signature_data->len);
7346
7347 TEST_EQUAL(actual_status, expected_start_status);
7348
Paul Elliottc9774412023-02-06 15:14:07 +00007349 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007350 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007351 * start failed. */
7352 actual_status = psa_verify_hash_complete(&operation);
7353
7354 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7355
7356 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007357 actual_status = psa_verify_hash_start(&operation, key, alg,
7358 hash_data->x, hash_data->len,
7359 signature_data->x,
7360 signature_data->len);
7361
7362 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7363 }
7364
Paul Elliott91007972022-12-16 12:21:24 +00007365 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7366 TEST_ASSERT(num_ops_prior == 0);
7367
Paul Elliott91007972022-12-16 12:21:24 +00007368 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007369 do {
Paul Elliott91007972022-12-16 12:21:24 +00007370 actual_status = psa_verify_hash_complete(&operation);
7371
Paul Elliott0c683352022-12-16 19:16:56 +00007372 num_completes++;
7373
Paul Elliott334d7262023-01-20 17:29:41 +00007374 if (actual_status == PSA_SUCCESS ||
7375 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007376 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007377 /* We are asserting here that every complete makes progress
7378 * (completes some ops), which is true of the internal
7379 * implementation and probably any implementation, however this is
7380 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007381 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007382
Paul Elliott91007972022-12-16 12:21:24 +00007383 num_ops_prior = num_ops;
7384 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007385 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007386
Paul Elliottc9774412023-02-06 15:14:07 +00007387 TEST_EQUAL(actual_status, expected_complete_status);
7388
Paul Elliottefebad02023-02-15 16:56:45 +00007389 /* Check that another complete returns BAD_STATE. */
7390 actual_status = psa_verify_hash_complete(&operation);
7391 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007392
Paul Elliott0c683352022-12-16 19:16:56 +00007393 TEST_LE_U(min_completes, num_completes);
7394 TEST_LE_U(num_completes, max_completes);
7395
Paul Elliott91007972022-12-16 12:21:24 +00007396 PSA_ASSERT(psa_verify_hash_abort(&operation));
7397
Paul Elliott59ad9452022-12-18 15:09:02 +00007398 num_ops = psa_verify_hash_get_num_ops(&operation);
7399 TEST_ASSERT(num_ops == 0);
7400
Paul Elliott91007972022-12-16 12:21:24 +00007401exit:
7402 psa_reset_key_attributes(&attributes);
7403 psa_destroy_key(key);
7404 PSA_DONE();
7405}
7406/* END_CASE */
7407
Paul Elliott20a36062022-12-18 13:21:25 +00007408/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007409/**
7410 * interruptible_signverify_hash_state_test() test intentions:
7411 *
7412 * Note: This test can currently only handle ECDSA.
7413 *
7414 * 1. Test that calling the various interruptible sign and verify hash functions
7415 * in incorrect orders returns BAD_STATE errors.
7416 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007417void interruptible_signverify_hash_state_test(int key_type_arg,
7418 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007419{
7420 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7421 psa_key_type_t key_type = key_type_arg;
7422 psa_algorithm_t alg = alg_arg;
7423 size_t key_bits;
7424 unsigned char *signature = NULL;
7425 size_t signature_size;
7426 size_t signature_length = 0xdeadbeef;
7427 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7428 psa_sign_hash_interruptible_operation_t sign_operation =
7429 psa_sign_hash_interruptible_operation_init();
7430 psa_verify_hash_interruptible_operation_t verify_operation =
7431 psa_verify_hash_interruptible_operation_init();
7432
7433 PSA_ASSERT(psa_crypto_init());
7434
7435 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7436 PSA_KEY_USAGE_VERIFY_HASH);
7437 psa_set_key_algorithm(&attributes, alg);
7438 psa_set_key_type(&attributes, key_type);
7439
7440 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7441 &key));
7442 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7443 key_bits = psa_get_key_bits(&attributes);
7444
7445 /* Allocate a buffer which has the size advertised by the
7446 * library. */
7447 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7448 key_bits, alg);
7449 TEST_ASSERT(signature_size != 0);
7450 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7451 ASSERT_ALLOC(signature, signature_size);
7452
7453 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7454
7455 /* --- Attempt completes prior to starts --- */
7456 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7457 signature_size,
7458 &signature_length),
7459 PSA_ERROR_BAD_STATE);
7460
7461 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7462
7463 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7464 PSA_ERROR_BAD_STATE);
7465
7466 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7467
7468 /* --- Aborts in all other places. --- */
7469 psa_sign_hash_abort(&sign_operation);
7470
7471 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7472 input_data->x, input_data->len));
7473
7474 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7475
7476 psa_interruptible_set_max_ops(1);
7477
7478 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7479 input_data->x, input_data->len));
7480
7481 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7482 signature_size,
7483 &signature_length),
7484 PSA_OPERATION_INCOMPLETE);
7485
7486 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7487
7488 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7489
7490 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7491 input_data->x, input_data->len));
7492
7493 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7494 signature_size,
7495 &signature_length));
7496
7497 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7498
7499 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7500
7501 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7502 input_data->x, input_data->len,
7503 signature, signature_length));
7504
7505 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7506
7507 psa_interruptible_set_max_ops(1);
7508
7509 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7510 input_data->x, input_data->len,
7511 signature, signature_length));
7512
7513 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7514 PSA_OPERATION_INCOMPLETE);
7515
7516 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7517
7518 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7519
7520 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7521 input_data->x, input_data->len,
7522 signature, signature_length));
7523
7524 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7525
7526 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7527
7528 /* --- Attempt double starts. --- */
7529
7530 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7531 input_data->x, input_data->len));
7532
7533 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7534 input_data->x, input_data->len),
7535 PSA_ERROR_BAD_STATE);
7536
7537 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7538
7539 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7540 input_data->x, input_data->len,
7541 signature, signature_length));
7542
7543 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7544 input_data->x, input_data->len,
7545 signature, signature_length),
7546 PSA_ERROR_BAD_STATE);
7547
7548 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7549
Paul Elliott76d671a2023-02-07 17:45:18 +00007550exit:
7551 /*
7552 * Key attributes may have been returned by psa_get_key_attributes()
7553 * thus reset them as required.
7554 */
7555 psa_reset_key_attributes(&attributes);
7556
7557 psa_destroy_key(key);
7558 mbedtls_free(signature);
7559 PSA_DONE();
7560}
7561/* END_CASE */
7562
7563/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007564/**
Paul Elliottc2033502023-02-26 17:09:14 +00007565 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007566 *
7567 * Note: This test can currently only handle ECDSA.
7568 *
7569 * 1. Test various edge cases in the interruptible sign and verify hash
7570 * interfaces.
7571 */
Paul Elliottc2033502023-02-26 17:09:14 +00007572void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007573 data_t *key_data, int alg_arg, data_t *input_data)
7574{
7575 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7576 psa_key_type_t key_type = key_type_arg;
7577 psa_algorithm_t alg = alg_arg;
7578 size_t key_bits;
7579 unsigned char *signature = NULL;
7580 size_t signature_size;
7581 size_t signature_length = 0xdeadbeef;
7582 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7583 uint8_t *input_buffer = NULL;
7584 psa_sign_hash_interruptible_operation_t sign_operation =
7585 psa_sign_hash_interruptible_operation_init();
7586 psa_verify_hash_interruptible_operation_t verify_operation =
7587 psa_verify_hash_interruptible_operation_init();
7588
7589 PSA_ASSERT(psa_crypto_init());
7590
7591 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7592 PSA_KEY_USAGE_VERIFY_HASH);
7593 psa_set_key_algorithm(&attributes, alg);
7594 psa_set_key_type(&attributes, key_type);
7595
7596 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7597 &key));
7598 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7599 key_bits = psa_get_key_bits(&attributes);
7600
7601 /* Allocate a buffer which has the size advertised by the
7602 * library. */
7603 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7604 key_bits, alg);
7605 TEST_ASSERT(signature_size != 0);
7606 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7607 ASSERT_ALLOC(signature, signature_size);
7608
Paul Elliott20a36062022-12-18 13:21:25 +00007609 /* --- Change function inputs mid run, to cause an error (sign only,
7610 * verify passes all inputs to start. --- */
7611
7612 psa_interruptible_set_max_ops(1);
7613
7614 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7615 input_data->x, input_data->len));
7616
7617 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7618 signature_size,
7619 &signature_length),
7620 PSA_OPERATION_INCOMPLETE);
7621
7622 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7623 0,
7624 &signature_length),
7625 PSA_ERROR_BUFFER_TOO_SMALL);
7626
Paul Elliottc9774412023-02-06 15:14:07 +00007627 /* And test that this invalidates the operation. */
7628 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7629 0,
7630 &signature_length),
7631 PSA_ERROR_BAD_STATE);
7632
Paul Elliott20a36062022-12-18 13:21:25 +00007633 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7634
Paul Elliottf9c91a72023-02-05 18:06:38 +00007635 /* Trash the hash buffer in between start and complete, to ensure
7636 * no reliance on external buffers. */
7637 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7638
7639 input_buffer = mbedtls_calloc(1, input_data->len);
7640 TEST_ASSERT(input_buffer != NULL);
7641
7642 memcpy(input_buffer, input_data->x, input_data->len);
7643
7644 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7645 input_buffer, input_data->len));
7646
7647 memset(input_buffer, '!', input_data->len);
7648 mbedtls_free(input_buffer);
7649 input_buffer = NULL;
7650
7651 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7652 signature_size,
7653 &signature_length));
7654
7655 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7656
7657 input_buffer = mbedtls_calloc(1, input_data->len);
7658 TEST_ASSERT(input_buffer != NULL);
7659
7660 memcpy(input_buffer, input_data->x, input_data->len);
7661
7662 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7663 input_buffer, input_data->len,
7664 signature, signature_length));
7665
7666 memset(input_buffer, '!', input_data->len);
7667 mbedtls_free(input_buffer);
7668 input_buffer = NULL;
7669
7670 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7671
7672 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7673
Paul Elliott20a36062022-12-18 13:21:25 +00007674exit:
7675 /*
7676 * Key attributes may have been returned by psa_get_key_attributes()
7677 * thus reset them as required.
7678 */
7679 psa_reset_key_attributes(&attributes);
7680
7681 psa_destroy_key(key);
7682 mbedtls_free(signature);
7683 PSA_DONE();
7684}
7685/* END_CASE */
7686
Paul Elliotta4cb9092023-02-07 18:01:55 +00007687/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007688/**
Paul Elliott57702242023-02-26 20:36:10 +00007689 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007690 *
7691 * Note: This test can currently only handle ECDSA.
7692 *
7693 * 1. Test that setting max ops is reflected in both interruptible sign and
7694 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007695 * 2. Test that changing the value of max_ops to unlimited during an operation
7696 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007697 *
7698 * 3. Test that calling get_num_ops() between complete calls gives the same
7699 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007700 */
Paul Elliott57702242023-02-26 20:36:10 +00007701void interruptible_signverify_hash_ops_tests(int key_type_arg,
7702 data_t *key_data, int alg_arg,
7703 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007704{
7705 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7706 psa_key_type_t key_type = key_type_arg;
7707 psa_algorithm_t alg = alg_arg;
7708 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007709 size_t key_bits;
7710 unsigned char *signature = NULL;
7711 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007712 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007713 uint32_t num_ops = 0;
7714 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7715
Paul Elliotta4cb9092023-02-07 18:01:55 +00007716 psa_sign_hash_interruptible_operation_t sign_operation =
7717 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007718 psa_verify_hash_interruptible_operation_t verify_operation =
7719 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007720
7721 PSA_ASSERT(psa_crypto_init());
7722
7723 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7724 PSA_KEY_USAGE_VERIFY_HASH);
7725 psa_set_key_algorithm(&attributes, alg);
7726 psa_set_key_type(&attributes, key_type);
7727
Paul Elliottf1743e22023-02-15 18:44:16 +00007728 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7729 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7730 key_bits = psa_get_key_bits(&attributes);
7731
7732 /* Allocate a buffer which has the size advertised by the
7733 * library. */
7734 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7735
7736 TEST_ASSERT(signature_size != 0);
7737 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7738 ASSERT_ALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007739
7740 /* Check that default max ops gets set if we don't set it. */
7741 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7742 input_data->x, input_data->len));
7743
7744 TEST_EQUAL(psa_interruptible_get_max_ops(),
7745 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7746
7747 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7748
Paul Elliottf1743e22023-02-15 18:44:16 +00007749 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7750 input_data->x, input_data->len,
7751 signature, signature_size));
7752
7753 TEST_EQUAL(psa_interruptible_get_max_ops(),
7754 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7755
7756 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7757
Paul Elliotta4cb9092023-02-07 18:01:55 +00007758 /* Check that max ops gets set properly. */
7759
7760 psa_interruptible_set_max_ops(0xbeef);
7761
Paul Elliottf1743e22023-02-15 18:44:16 +00007762 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007763
Paul Elliott9e8819f2023-02-26 19:01:35 +00007764 /* --- Ensure changing the max ops mid operation works (operation should
7765 * complete successfully after setting max ops to unlimited --- */
7766 psa_interruptible_set_max_ops(1);
7767
7768 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7769 input_data->x, input_data->len));
7770
7771 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7772 signature_size,
7773 &signature_length),
7774 PSA_OPERATION_INCOMPLETE);
7775
7776 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7777
7778 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7779 signature_size,
7780 &signature_length));
7781
7782 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7783
7784 psa_interruptible_set_max_ops(1);
7785
7786 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7787 input_data->x, input_data->len,
7788 signature, signature_length));
7789
7790 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7791 PSA_OPERATION_INCOMPLETE);
7792
7793 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7794
7795 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7796
7797 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7798
Paul Elliottc1e04002023-02-26 20:27:23 +00007799 /* --- Test that not calling get_num_ops inbetween complete calls does not
7800 * result in lost ops. ---*/
7801
7802 psa_interruptible_set_max_ops(1);
7803
7804 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7805 input_data->x, input_data->len));
7806
7807 /* Continue performing the signature until complete. */
7808 do {
7809 status = psa_sign_hash_complete(&sign_operation, signature,
7810 signature_size,
7811 &signature_length);
7812
7813 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7814
7815 } while (status == PSA_OPERATION_INCOMPLETE);
7816
7817 PSA_ASSERT(status);
7818
7819 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7820
7821 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7822 input_data->x, input_data->len));
7823
7824 /* Continue performing the signature until complete. */
7825 do {
7826 status = psa_sign_hash_complete(&sign_operation, signature,
7827 signature_size,
7828 &signature_length);
7829 } while (status == PSA_OPERATION_INCOMPLETE);
7830
7831 PSA_ASSERT(status);
7832
7833 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7834
7835 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7836
7837 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7838 input_data->x, input_data->len,
7839 signature, signature_length));
7840
7841 /* Continue performing the verification until complete. */
7842 do {
7843 status = psa_verify_hash_complete(&verify_operation);
7844
7845 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7846
7847 } while (status == PSA_OPERATION_INCOMPLETE);
7848
7849 PSA_ASSERT(status);
7850
7851 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7852
7853 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7854 input_data->x, input_data->len,
7855 signature, signature_length));
7856
7857 /* Continue performing the verification until complete. */
7858 do {
7859 status = psa_verify_hash_complete(&verify_operation);
7860
7861 } while (status == PSA_OPERATION_INCOMPLETE);
7862
7863 PSA_ASSERT(status);
7864
7865 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7866
7867 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7868
Paul Elliotta4cb9092023-02-07 18:01:55 +00007869exit:
7870 /*
7871 * Key attributes may have been returned by psa_get_key_attributes()
7872 * thus reset them as required.
7873 */
7874 psa_reset_key_attributes(&attributes);
7875
7876 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007877 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007878 PSA_DONE();
7879}
7880/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007881
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007882/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007883void sign_message_deterministic(int key_type_arg,
7884 data_t *key_data,
7885 int alg_arg,
7886 data_t *input_data,
7887 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007888{
7889 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7890 psa_key_type_t key_type = key_type_arg;
7891 psa_algorithm_t alg = alg_arg;
7892 size_t key_bits;
7893 unsigned char *signature = NULL;
7894 size_t signature_size;
7895 size_t signature_length = 0xdeadbeef;
7896 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7897
Gilles Peskine449bd832023-01-11 14:50:10 +01007898 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007899
Gilles Peskine449bd832023-01-11 14:50:10 +01007900 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7901 psa_set_key_algorithm(&attributes, alg);
7902 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007903
Gilles Peskine449bd832023-01-11 14:50:10 +01007904 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7905 &key));
7906 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7907 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007908
Gilles Peskine449bd832023-01-11 14:50:10 +01007909 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7910 TEST_ASSERT(signature_size != 0);
7911 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7912 ASSERT_ALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007913
Gilles Peskine449bd832023-01-11 14:50:10 +01007914 PSA_ASSERT(psa_sign_message(key, alg,
7915 input_data->x, input_data->len,
7916 signature, signature_size,
7917 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007918
Gilles Peskine449bd832023-01-11 14:50:10 +01007919 ASSERT_COMPARE(output_data->x, output_data->len,
7920 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007921
7922exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007923 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007924
Gilles Peskine449bd832023-01-11 14:50:10 +01007925 psa_destroy_key(key);
7926 mbedtls_free(signature);
7927 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007928
7929}
7930/* END_CASE */
7931
7932/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007933void sign_message_fail(int key_type_arg,
7934 data_t *key_data,
7935 int alg_arg,
7936 data_t *input_data,
7937 int signature_size_arg,
7938 int expected_status_arg)
7939{
7940 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7941 psa_key_type_t key_type = key_type_arg;
7942 psa_algorithm_t alg = alg_arg;
7943 size_t signature_size = signature_size_arg;
7944 psa_status_t actual_status;
7945 psa_status_t expected_status = expected_status_arg;
7946 unsigned char *signature = NULL;
7947 size_t signature_length = 0xdeadbeef;
7948 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7949
7950 ASSERT_ALLOC(signature, signature_size);
7951
7952 PSA_ASSERT(psa_crypto_init());
7953
7954 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7955 psa_set_key_algorithm(&attributes, alg);
7956 psa_set_key_type(&attributes, key_type);
7957
7958 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7959 &key));
7960
7961 actual_status = psa_sign_message(key, alg,
7962 input_data->x, input_data->len,
7963 signature, signature_size,
7964 &signature_length);
7965 TEST_EQUAL(actual_status, expected_status);
7966 /* The value of *signature_length is unspecified on error, but
7967 * whatever it is, it should be less than signature_size, so that
7968 * if the caller tries to read *signature_length bytes without
7969 * checking the error code then they don't overflow a buffer. */
7970 TEST_LE_U(signature_length, signature_size);
7971
7972exit:
7973 psa_reset_key_attributes(&attributes);
7974 psa_destroy_key(key);
7975 mbedtls_free(signature);
7976 PSA_DONE();
7977}
7978/* END_CASE */
7979
7980/* BEGIN_CASE */
7981void sign_verify_message(int key_type_arg,
7982 data_t *key_data,
7983 int alg_arg,
7984 data_t *input_data)
7985{
7986 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7987 psa_key_type_t key_type = key_type_arg;
7988 psa_algorithm_t alg = alg_arg;
7989 size_t key_bits;
7990 unsigned char *signature = NULL;
7991 size_t signature_size;
7992 size_t signature_length = 0xdeadbeef;
7993 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7994
7995 PSA_ASSERT(psa_crypto_init());
7996
7997 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
7998 PSA_KEY_USAGE_VERIFY_MESSAGE);
7999 psa_set_key_algorithm(&attributes, alg);
8000 psa_set_key_type(&attributes, key_type);
8001
8002 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8003 &key));
8004 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8005 key_bits = psa_get_key_bits(&attributes);
8006
8007 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8008 TEST_ASSERT(signature_size != 0);
8009 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
8010 ASSERT_ALLOC(signature, signature_size);
8011
8012 PSA_ASSERT(psa_sign_message(key, alg,
8013 input_data->x, input_data->len,
8014 signature, signature_size,
8015 &signature_length));
8016 TEST_LE_U(signature_length, signature_size);
8017 TEST_ASSERT(signature_length > 0);
8018
8019 PSA_ASSERT(psa_verify_message(key, alg,
8020 input_data->x, input_data->len,
8021 signature, signature_length));
8022
8023 if (input_data->len != 0) {
8024 /* Flip a bit in the input and verify that the signature is now
8025 * detected as invalid. Flip a bit at the beginning, not at the end,
8026 * because ECDSA may ignore the last few bits of the input. */
8027 input_data->x[0] ^= 1;
8028 TEST_EQUAL(psa_verify_message(key, alg,
8029 input_data->x, input_data->len,
8030 signature, signature_length),
8031 PSA_ERROR_INVALID_SIGNATURE);
8032 }
8033
8034exit:
8035 psa_reset_key_attributes(&attributes);
8036
8037 psa_destroy_key(key);
8038 mbedtls_free(signature);
8039 PSA_DONE();
8040}
8041/* END_CASE */
8042
8043/* BEGIN_CASE */
8044void verify_message(int key_type_arg,
8045 data_t *key_data,
8046 int alg_arg,
8047 data_t *input_data,
8048 data_t *signature_data)
8049{
8050 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8051 psa_key_type_t key_type = key_type_arg;
8052 psa_algorithm_t alg = alg_arg;
8053 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8054
8055 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8056
8057 PSA_ASSERT(psa_crypto_init());
8058
8059 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8060 psa_set_key_algorithm(&attributes, alg);
8061 psa_set_key_type(&attributes, key_type);
8062
8063 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8064 &key));
8065
8066 PSA_ASSERT(psa_verify_message(key, alg,
8067 input_data->x, input_data->len,
8068 signature_data->x, signature_data->len));
8069
8070exit:
8071 psa_reset_key_attributes(&attributes);
8072 psa_destroy_key(key);
8073 PSA_DONE();
8074}
8075/* END_CASE */
8076
8077/* BEGIN_CASE */
8078void verify_message_fail(int key_type_arg,
8079 data_t *key_data,
8080 int alg_arg,
8081 data_t *hash_data,
8082 data_t *signature_data,
8083 int expected_status_arg)
8084{
8085 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8086 psa_key_type_t key_type = key_type_arg;
8087 psa_algorithm_t alg = alg_arg;
8088 psa_status_t actual_status;
8089 psa_status_t expected_status = expected_status_arg;
8090 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8091
8092 PSA_ASSERT(psa_crypto_init());
8093
8094 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8095 psa_set_key_algorithm(&attributes, alg);
8096 psa_set_key_type(&attributes, key_type);
8097
8098 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8099 &key));
8100
8101 actual_status = psa_verify_message(key, alg,
8102 hash_data->x, hash_data->len,
8103 signature_data->x,
8104 signature_data->len);
8105 TEST_EQUAL(actual_status, expected_status);
8106
8107exit:
8108 psa_reset_key_attributes(&attributes);
8109 psa_destroy_key(key);
8110 PSA_DONE();
8111}
8112/* END_CASE */
8113
8114/* BEGIN_CASE */
8115void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008116 data_t *key_data,
8117 int alg_arg,
8118 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008119 data_t *label,
8120 int expected_output_length_arg,
8121 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008122{
Ronald Cron5425a212020-08-04 14:58:35 +02008123 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008124 psa_key_type_t key_type = key_type_arg;
8125 psa_algorithm_t alg = alg_arg;
8126 size_t expected_output_length = expected_output_length_arg;
8127 size_t key_bits;
8128 unsigned char *output = NULL;
8129 size_t output_size;
8130 size_t output_length = ~0;
8131 psa_status_t actual_status;
8132 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008133 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008134
Gilles Peskine449bd832023-01-11 14:50:10 +01008135 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008136
Gilles Peskine656896e2018-06-29 19:12:28 +02008137 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008138 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8139 psa_set_key_algorithm(&attributes, alg);
8140 psa_set_key_type(&attributes, key_type);
8141 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8142 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008143
8144 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008145 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8146 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008147
Gilles Peskine449bd832023-01-11 14:50:10 +01008148 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8149 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
8150 ASSERT_ALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008151
8152 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008153 actual_status = psa_asymmetric_encrypt(key, alg,
8154 input_data->x, input_data->len,
8155 label->x, label->len,
8156 output, output_size,
8157 &output_length);
8158 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008159 if (actual_status == PSA_SUCCESS) {
8160 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008161 } else {
8162 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008163 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008164
Gilles Peskine68428122018-06-30 18:42:41 +02008165 /* If the label is empty, the test framework puts a non-null pointer
8166 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008167 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008168 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008169 if (output_size != 0) {
8170 memset(output, 0, output_size);
8171 }
8172 actual_status = psa_asymmetric_encrypt(key, alg,
8173 input_data->x, input_data->len,
8174 NULL, label->len,
8175 output, output_size,
8176 &output_length);
8177 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008178 if (actual_status == PSA_SUCCESS) {
8179 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008180 } else {
8181 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008182 }
Gilles Peskine68428122018-06-30 18:42:41 +02008183 }
8184
Gilles Peskine656896e2018-06-29 19:12:28 +02008185exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008186 /*
8187 * Key attributes may have been returned by psa_get_key_attributes()
8188 * thus reset them as required.
8189 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008190 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008191
Gilles Peskine449bd832023-01-11 14:50:10 +01008192 psa_destroy_key(key);
8193 mbedtls_free(output);
8194 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008195}
8196/* END_CASE */
8197
8198/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008199void asymmetric_encrypt_decrypt(int key_type_arg,
8200 data_t *key_data,
8201 int alg_arg,
8202 data_t *input_data,
8203 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008204{
Ronald Cron5425a212020-08-04 14:58:35 +02008205 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008206 psa_key_type_t key_type = key_type_arg;
8207 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008208 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008209 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008210 size_t output_size;
8211 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008212 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008213 size_t output2_size;
8214 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008215 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008216
Gilles Peskine449bd832023-01-11 14:50:10 +01008217 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008218
Gilles Peskine449bd832023-01-11 14:50:10 +01008219 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8220 psa_set_key_algorithm(&attributes, alg);
8221 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008222
Gilles Peskine449bd832023-01-11 14:50:10 +01008223 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8224 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008225
8226 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008227 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8228 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008229
Gilles Peskine449bd832023-01-11 14:50:10 +01008230 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8231 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
8232 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008233
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008234 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008235 TEST_LE_U(output2_size,
8236 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8237 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
8238 ASSERT_ALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008239
Gilles Peskineeebd7382018-06-08 18:11:54 +02008240 /* We test encryption by checking that encrypt-then-decrypt gives back
8241 * the original plaintext because of the non-optional random
8242 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008243 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8244 input_data->x, input_data->len,
8245 label->x, label->len,
8246 output, output_size,
8247 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008248 /* We don't know what ciphertext length to expect, but check that
8249 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008250 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008251
Gilles Peskine449bd832023-01-11 14:50:10 +01008252 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8253 output, output_length,
8254 label->x, label->len,
8255 output2, output2_size,
8256 &output2_length));
8257 ASSERT_COMPARE(input_data->x, input_data->len,
8258 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008259
8260exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008261 /*
8262 * Key attributes may have been returned by psa_get_key_attributes()
8263 * thus reset them as required.
8264 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008265 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008266
Gilles Peskine449bd832023-01-11 14:50:10 +01008267 psa_destroy_key(key);
8268 mbedtls_free(output);
8269 mbedtls_free(output2);
8270 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008271}
8272/* END_CASE */
8273
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008274/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008275void asymmetric_decrypt(int key_type_arg,
8276 data_t *key_data,
8277 int alg_arg,
8278 data_t *input_data,
8279 data_t *label,
8280 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008281{
Ronald Cron5425a212020-08-04 14:58:35 +02008282 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008283 psa_key_type_t key_type = key_type_arg;
8284 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008285 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008286 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008287 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008288 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008289 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008290
Gilles Peskine449bd832023-01-11 14:50:10 +01008291 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008292
Gilles Peskine449bd832023-01-11 14:50:10 +01008293 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8294 psa_set_key_algorithm(&attributes, alg);
8295 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008296
Gilles Peskine449bd832023-01-11 14:50:10 +01008297 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8298 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008299
Gilles Peskine449bd832023-01-11 14:50:10 +01008300 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8301 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008302
8303 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008304 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8305 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
8306 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008307
Gilles Peskine449bd832023-01-11 14:50:10 +01008308 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8309 input_data->x, input_data->len,
8310 label->x, label->len,
8311 output,
8312 output_size,
8313 &output_length));
8314 ASSERT_COMPARE(expected_data->x, expected_data->len,
8315 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008316
Gilles Peskine68428122018-06-30 18:42:41 +02008317 /* If the label is empty, the test framework puts a non-null pointer
8318 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008319 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008320 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008321 if (output_size != 0) {
8322 memset(output, 0, output_size);
8323 }
8324 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8325 input_data->x, input_data->len,
8326 NULL, label->len,
8327 output,
8328 output_size,
8329 &output_length));
8330 ASSERT_COMPARE(expected_data->x, expected_data->len,
8331 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008332 }
8333
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008334exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008335 psa_reset_key_attributes(&attributes);
8336 psa_destroy_key(key);
8337 mbedtls_free(output);
8338 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008339}
8340/* END_CASE */
8341
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008342/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008343void asymmetric_decrypt_fail(int key_type_arg,
8344 data_t *key_data,
8345 int alg_arg,
8346 data_t *input_data,
8347 data_t *label,
8348 int output_size_arg,
8349 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008350{
Ronald Cron5425a212020-08-04 14:58:35 +02008351 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008352 psa_key_type_t key_type = key_type_arg;
8353 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008354 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008355 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008356 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008357 psa_status_t actual_status;
8358 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008360
Gilles Peskine449bd832023-01-11 14:50:10 +01008361 ASSERT_ALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008362
Gilles Peskine449bd832023-01-11 14:50:10 +01008363 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008364
Gilles Peskine449bd832023-01-11 14:50:10 +01008365 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8366 psa_set_key_algorithm(&attributes, alg);
8367 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008368
Gilles Peskine449bd832023-01-11 14:50:10 +01008369 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8370 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008371
Gilles Peskine449bd832023-01-11 14:50:10 +01008372 actual_status = psa_asymmetric_decrypt(key, alg,
8373 input_data->x, input_data->len,
8374 label->x, label->len,
8375 output, output_size,
8376 &output_length);
8377 TEST_EQUAL(actual_status, expected_status);
8378 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008379
Gilles Peskine68428122018-06-30 18:42:41 +02008380 /* If the label is empty, the test framework puts a non-null pointer
8381 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008382 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008383 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008384 if (output_size != 0) {
8385 memset(output, 0, output_size);
8386 }
8387 actual_status = psa_asymmetric_decrypt(key, alg,
8388 input_data->x, input_data->len,
8389 NULL, label->len,
8390 output, output_size,
8391 &output_length);
8392 TEST_EQUAL(actual_status, expected_status);
8393 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008394 }
8395
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008396exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008397 psa_reset_key_attributes(&attributes);
8398 psa_destroy_key(key);
8399 mbedtls_free(output);
8400 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008401}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008402/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008403
8404/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008405void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008406{
8407 /* Test each valid way of initializing the object, except for `= {0}`, as
8408 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8409 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008410 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008411 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008412 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008413 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8414 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008415
Gilles Peskine449bd832023-01-11 14:50:10 +01008416 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008417
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008418 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008419 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8420 PSA_ERROR_BAD_STATE);
8421 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8422 PSA_ERROR_BAD_STATE);
8423 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8424 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008425
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008426 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008427 PSA_ASSERT(psa_key_derivation_abort(&func));
8428 PSA_ASSERT(psa_key_derivation_abort(&init));
8429 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008430}
8431/* END_CASE */
8432
Janos Follath16de4a42019-06-13 16:32:24 +01008433/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008434void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008435{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008436 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008437 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008438 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008439
Gilles Peskine449bd832023-01-11 14:50:10 +01008440 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008441
Gilles Peskine449bd832023-01-11 14:50:10 +01008442 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8443 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008444
8445exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008446 psa_key_derivation_abort(&operation);
8447 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008448}
8449/* END_CASE */
8450
Janos Follathaf3c2a02019-06-12 12:34:34 +01008451/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008452void derive_set_capacity(int alg_arg, int capacity_arg,
8453 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008454{
8455 psa_algorithm_t alg = alg_arg;
8456 size_t capacity = capacity_arg;
8457 psa_status_t expected_status = expected_status_arg;
8458 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8459
Gilles Peskine449bd832023-01-11 14:50:10 +01008460 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008461
Gilles Peskine449bd832023-01-11 14:50:10 +01008462 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008463
Gilles Peskine449bd832023-01-11 14:50:10 +01008464 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8465 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008466
8467exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008468 psa_key_derivation_abort(&operation);
8469 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008470}
8471/* END_CASE */
8472
8473/* BEGIN_CASE */
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308474void parse_binary_string_test(data_t *input, int output)
8475{
8476 uint64_t value;
8477 value = parse_binary_string(input);
8478 TEST_EQUAL(value, output);
8479}
8480/* END_CASE */
8481
8482/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008483void derive_input(int alg_arg,
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308484 int step_arg1, int key_type_arg1, data_t *input1,
8485 int expected_status_arg1,
8486 int step_arg2, int key_type_arg2, data_t *input2,
8487 int expected_status_arg2,
8488 int step_arg3, int key_type_arg3, data_t *input3,
8489 int expected_status_arg3,
Gilles Peskine449bd832023-01-11 14:50:10 +01008490 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008491{
8492 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008493 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308494 uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
Gilles Peskine449bd832023-01-11 14:50:10 +01008495 psa_status_t expected_statuses[] = { expected_status_arg1,
8496 expected_status_arg2,
8497 expected_status_arg3 };
8498 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008499 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8500 MBEDTLS_SVC_KEY_ID_INIT,
8501 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008502 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8503 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8504 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008505 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008506 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008507 psa_status_t expected_output_status = expected_output_status_arg;
8508 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008509
Gilles Peskine449bd832023-01-11 14:50:10 +01008510 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008511
Gilles Peskine449bd832023-01-11 14:50:10 +01008512 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8513 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008514
Gilles Peskine449bd832023-01-11 14:50:10 +01008515 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008516
Gilles Peskine449bd832023-01-11 14:50:10 +01008517 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8518 mbedtls_test_set_step(i);
8519 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008520 /* Skip this step */
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308521 } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE &&
8522 key_types[i] != INPUT_INTEGER) {
8523 psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i]));
Gilles Peskine449bd832023-01-11 14:50:10 +01008524 PSA_ASSERT(psa_import_key(&attributes,
8525 inputs[i]->x, inputs[i]->len,
8526 &keys[i]));
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308527 if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008528 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008529 // When taking a private key as secret input, use key agreement
8530 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008531 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8532 &operation, keys[i]),
8533 expected_statuses[i]);
8534 } else {
8535 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8536 keys[i]),
8537 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008538 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008539 } else {
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308540 if (key_types[i] == INPUT_INTEGER) {
8541 TEST_EQUAL(psa_key_derivation_input_integer(
8542 &operation, steps[i],
8543 parse_binary_string(inputs[i])),
8544 expected_statuses[i]);
8545 } else {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308546 TEST_EQUAL(psa_key_derivation_input_bytes(
8547 &operation, steps[i],
8548 inputs[i]->x, inputs[i]->len),
8549 expected_statuses[i]);
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308550 }
Janos Follathaf3c2a02019-06-12 12:34:34 +01008551 }
8552 }
8553
Gilles Peskine449bd832023-01-11 14:50:10 +01008554 if (output_key_type != PSA_KEY_TYPE_NONE) {
8555 psa_reset_key_attributes(&attributes);
8556 psa_set_key_type(&attributes, output_key_type);
8557 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008558 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008559 psa_key_derivation_output_key(&attributes, &operation,
8560 &output_key);
8561 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008562 uint8_t buffer[1];
8563 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008564 psa_key_derivation_output_bytes(&operation,
8565 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008566 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008567 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008568
Janos Follathaf3c2a02019-06-12 12:34:34 +01008569exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008570 psa_key_derivation_abort(&operation);
8571 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8572 psa_destroy_key(keys[i]);
8573 }
8574 psa_destroy_key(output_key);
8575 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008576}
8577/* END_CASE */
8578
Kusumit Ghoderao42b02b92023-06-06 16:48:46 +05308579/* BEGIN_CASE*/
8580void derive_input_invalid_cost(int alg_arg, int64_t cost)
8581{
8582 psa_algorithm_t alg = alg_arg;
8583 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8584
8585 PSA_ASSERT(psa_crypto_init());
8586 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8587
8588 TEST_EQUAL(psa_key_derivation_input_integer(&operation,
8589 PSA_KEY_DERIVATION_INPUT_COST,
8590 cost),
8591 PSA_ERROR_NOT_SUPPORTED);
8592
8593exit:
8594 psa_key_derivation_abort(&operation);
8595 PSA_DONE();
8596}
8597/* END_CASE*/
8598
Janos Follathd958bb72019-07-03 15:02:16 +01008599/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008600void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008601{
Janos Follathd958bb72019-07-03 15:02:16 +01008602 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008603 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008604 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008605 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008606 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008607 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008608 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008609 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008610 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008611 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008612 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8613 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008614 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008615 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008616
Gilles Peskine449bd832023-01-11 14:50:10 +01008617 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008618
Gilles Peskine449bd832023-01-11 14:50:10 +01008619 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8620 psa_set_key_algorithm(&attributes, alg);
8621 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008622
Gilles Peskine449bd832023-01-11 14:50:10 +01008623 PSA_ASSERT(psa_import_key(&attributes,
8624 key_data, sizeof(key_data),
8625 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008626
8627 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008628 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8629 input1, input1_length,
8630 input2, input2_length,
8631 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008632 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008633 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008634
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008635 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008636 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8637 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008638
Gilles Peskine449bd832023-01-11 14:50:10 +01008639 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008640
Gilles Peskine449bd832023-01-11 14:50:10 +01008641 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8642 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008643
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008644exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008645 psa_key_derivation_abort(&operation);
8646 psa_destroy_key(key);
8647 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008648}
8649/* END_CASE */
8650
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008651/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008652void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008653{
8654 uint8_t output_buffer[16];
8655 size_t buffer_size = 16;
8656 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008657 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008658
Gilles Peskine449bd832023-01-11 14:50:10 +01008659 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8660 output_buffer, buffer_size)
8661 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008662
Gilles Peskine449bd832023-01-11 14:50:10 +01008663 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8664 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008665
Gilles Peskine449bd832023-01-11 14:50:10 +01008666 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008667
Gilles Peskine449bd832023-01-11 14:50:10 +01008668 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8669 output_buffer, buffer_size)
8670 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008671
Gilles Peskine449bd832023-01-11 14:50:10 +01008672 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8673 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008674
8675exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008676 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008677}
8678/* END_CASE */
8679
8680/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008681void derive_output(int alg_arg,
8682 int step1_arg, data_t *input1, int expected_status_arg1,
8683 int step2_arg, data_t *input2, int expected_status_arg2,
8684 int step3_arg, data_t *input3, int expected_status_arg3,
8685 int step4_arg, data_t *input4, int expected_status_arg4,
8686 data_t *key_agreement_peer_key,
8687 int requested_capacity_arg,
8688 data_t *expected_output1,
8689 data_t *expected_output2,
8690 int other_key_input_type,
8691 int key_input_type,
8692 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008693{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008694 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008695 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8696 data_t *inputs[] = { input1, input2, input3, input4 };
8697 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8698 MBEDTLS_SVC_KEY_ID_INIT,
8699 MBEDTLS_SVC_KEY_ID_INIT,
8700 MBEDTLS_SVC_KEY_ID_INIT };
8701 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8702 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008703 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008704 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008705 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008706 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008707 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008708 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008709 size_t output_buffer_size = 0;
8710 uint8_t *output_buffer = NULL;
8711 size_t expected_capacity;
8712 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008713 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8714 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8715 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8716 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008717 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008718 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008719 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008720
Gilles Peskine449bd832023-01-11 14:50:10 +01008721 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8722 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008723 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008724 }
8725 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008726 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008727 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008728 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008729 ASSERT_ALLOC(output_buffer, output_buffer_size);
8730 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008731
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008732 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008733 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8734 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8735 requested_capacity));
8736 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8737 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008738 case 0:
8739 break;
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308740 case PSA_KEY_DERIVATION_INPUT_COST:
8741 TEST_EQUAL(psa_key_derivation_input_integer(
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308742 &operation, steps[i],
8743 parse_binary_string(inputs[i])),
8744 statuses[i]);
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308745 if (statuses[i] != PSA_SUCCESS) {
8746 goto exit;
8747 }
8748 break;
8749 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Gilles Peskine1468da72019-05-29 17:35:49 +02008750 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008751 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008752 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008753 TEST_EQUAL(psa_key_derivation_input_bytes(
8754 &operation, steps[i],
8755 inputs[i]->x, inputs[i]->len),
8756 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008757
Gilles Peskine449bd832023-01-11 14:50:10 +01008758 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008759 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008760 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008761 break;
8762 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008763 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8764 psa_set_key_algorithm(&attributes1, alg);
8765 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008766
Gilles Peskine449bd832023-01-11 14:50:10 +01008767 PSA_ASSERT(psa_import_key(&attributes1,
8768 inputs[i]->x, inputs[i]->len,
8769 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008770
Gilles Peskine449bd832023-01-11 14:50:10 +01008771 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8772 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8773 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8774 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008775 }
8776
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308777 TEST_EQUAL(psa_key_derivation_input_key(&operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01008778 steps[i],
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308779 keys[i]),
8780 statuses[i]);
8781
8782 if (statuses[i] != PSA_SUCCESS) {
8783 goto exit;
8784 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008785 break;
8786 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008787 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008788 break;
8789 }
8790 break;
8791 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008792 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008793 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008794 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8795 steps[i],
8796 inputs[i]->x,
8797 inputs[i]->len),
8798 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008799 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008800 case 1: // input key, type DERIVE
8801 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008802 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8803 psa_set_key_algorithm(&attributes2, alg);
8804 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008805
8806 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008807 if (other_key_input_type == 11) {
8808 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8809 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008810
Gilles Peskine449bd832023-01-11 14:50:10 +01008811 PSA_ASSERT(psa_import_key(&attributes2,
8812 inputs[i]->x, inputs[i]->len,
8813 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008814
Gilles Peskine449bd832023-01-11 14:50:10 +01008815 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8816 steps[i],
8817 keys[i]),
8818 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008819 break;
8820 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8822 psa_set_key_algorithm(&attributes3, alg);
8823 psa_set_key_type(&attributes3,
8824 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008825
Gilles Peskine449bd832023-01-11 14:50:10 +01008826 PSA_ASSERT(psa_import_key(&attributes3,
8827 inputs[i]->x, inputs[i]->len,
8828 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008829
Gilles Peskine449bd832023-01-11 14:50:10 +01008830 TEST_EQUAL(psa_key_derivation_key_agreement(
8831 &operation,
8832 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8833 keys[i], key_agreement_peer_key->x,
8834 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008835 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008836 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008837 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008838 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008839 }
8840
Gilles Peskine449bd832023-01-11 14:50:10 +01008841 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008842 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008843 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008844 break;
8845 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008846 TEST_EQUAL(psa_key_derivation_input_bytes(
8847 &operation, steps[i],
8848 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008849
Gilles Peskine449bd832023-01-11 14:50:10 +01008850 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008851 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008852 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008853 break;
8854 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008855 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008856
Gilles Peskine449bd832023-01-11 14:50:10 +01008857 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8858 &current_capacity));
8859 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008860 expected_capacity = requested_capacity;
8861
Gilles Peskine449bd832023-01-11 14:50:10 +01008862 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008863 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8864
8865 /* For output key derivation secret must be provided using
8866 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008867 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008868 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008869 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008870
Gilles Peskine449bd832023-01-11 14:50:10 +01008871 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8872 psa_set_key_algorithm(&attributes4, alg);
8873 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8874 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008875
Gilles Peskine449bd832023-01-11 14:50:10 +01008876 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8877 &derived_key), expected_status);
8878 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008879 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008880 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008881 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008882 status = psa_key_derivation_output_bytes(&operation,
8883 output_buffer, output_sizes[i]);
8884 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008885 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008886 TEST_ASSERT(status == PSA_SUCCESS ||
8887 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008888 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008889 } else if (expected_capacity == 0 ||
8890 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008891 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008892 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008893 expected_capacity = 0;
8894 continue;
8895 }
8896 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008897 PSA_ASSERT(status);
8898 if (output_sizes[i] != 0) {
8899 ASSERT_COMPARE(output_buffer, output_sizes[i],
8900 expected_outputs[i], output_sizes[i]);
8901 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008902 /* Check the operation status. */
8903 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008904 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8905 &current_capacity));
8906 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008907 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008908 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008909 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008910
8911exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008912 mbedtls_free(output_buffer);
8913 psa_key_derivation_abort(&operation);
8914 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8915 psa_destroy_key(keys[i]);
8916 }
8917 psa_destroy_key(derived_key);
8918 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008919}
8920/* END_CASE */
8921
8922/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008923void derive_full(int alg_arg,
8924 data_t *key_data,
8925 data_t *input1,
8926 data_t *input2,
8927 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008928{
Ronald Cron5425a212020-08-04 14:58:35 +02008929 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008930 psa_algorithm_t alg = alg_arg;
8931 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008932 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008933 unsigned char output_buffer[16];
8934 size_t expected_capacity = requested_capacity;
8935 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008936 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008937
Gilles Peskine449bd832023-01-11 14:50:10 +01008938 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008939
Gilles Peskine449bd832023-01-11 14:50:10 +01008940 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8941 psa_set_key_algorithm(&attributes, alg);
8942 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008943
Gilles Peskine449bd832023-01-11 14:50:10 +01008944 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8945 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008946
Gilles Peskine449bd832023-01-11 14:50:10 +01008947 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8948 input1->x, input1->len,
8949 input2->x, input2->len,
8950 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008951 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008952 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008953
Gilles Peskine449bd832023-01-11 14:50:10 +01008954 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8955 &current_capacity));
8956 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008957
8958 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008959 while (current_capacity > 0) {
8960 size_t read_size = sizeof(output_buffer);
8961 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008962 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008963 }
8964 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8965 output_buffer,
8966 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008967 expected_capacity -= read_size;
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
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008973 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008974 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8975 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008976
Gilles Peskine449bd832023-01-11 14:50:10 +01008977 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008978
8979exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008980 psa_key_derivation_abort(&operation);
8981 psa_destroy_key(key);
8982 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008983}
8984/* END_CASE */
8985
Stephan Koch78109f52023-04-12 14:19:36 +02008986/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008987void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8988 int derivation_step,
8989 int capacity, int expected_capacity_status_arg,
8990 data_t *expected_output,
8991 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008992{
8993 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
8994 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04008995 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008996 uint8_t *output_buffer = NULL;
8997 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04008998 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
8999 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
9000 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009001
Gilles Peskine449bd832023-01-11 14:50:10 +01009002 ASSERT_ALLOC(output_buffer, expected_output->len);
9003 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009004
Gilles Peskine449bd832023-01-11 14:50:10 +01009005 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9006 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
9007 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009008
Gilles Peskine449bd832023-01-11 14:50:10 +01009009 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
9010 step, input->x, input->len),
9011 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009012
Gilles Peskine449bd832023-01-11 14:50:10 +01009013 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009014 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009015 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009016
Gilles Peskine449bd832023-01-11 14:50:10 +01009017 status = psa_key_derivation_output_bytes(&operation, output_buffer,
9018 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009019
Gilles Peskine449bd832023-01-11 14:50:10 +01009020 TEST_EQUAL(status, expected_output_status);
9021 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
9022 ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x,
9023 expected_output->len);
9024 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009025
9026exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009027 mbedtls_free(output_buffer);
9028 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009029 PSA_DONE();
9030}
9031/* END_CASE */
9032
Janos Follathe60c9052019-07-03 13:51:30 +01009033/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009034void derive_key_exercise(int alg_arg,
9035 data_t *key_data,
9036 data_t *input1,
9037 data_t *input2,
9038 int derived_type_arg,
9039 int derived_bits_arg,
9040 int derived_usage_arg,
9041 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009042{
Ronald Cron5425a212020-08-04 14:58:35 +02009043 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9044 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009045 psa_algorithm_t alg = alg_arg;
9046 psa_key_type_t derived_type = derived_type_arg;
9047 size_t derived_bits = derived_bits_arg;
9048 psa_key_usage_t derived_usage = derived_usage_arg;
9049 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009050 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009051 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009052 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009053 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009054
Gilles Peskine449bd832023-01-11 14:50:10 +01009055 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009056
Gilles Peskine449bd832023-01-11 14:50:10 +01009057 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9058 psa_set_key_algorithm(&attributes, alg);
9059 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9060 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9061 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009062
9063 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009064 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9065 input1->x, input1->len,
9066 input2->x, input2->len,
9067 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009068 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009069 }
Janos Follathe60c9052019-07-03 13:51:30 +01009070
Gilles Peskine449bd832023-01-11 14:50:10 +01009071 psa_set_key_usage_flags(&attributes, derived_usage);
9072 psa_set_key_algorithm(&attributes, derived_alg);
9073 psa_set_key_type(&attributes, derived_type);
9074 psa_set_key_bits(&attributes, derived_bits);
9075 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9076 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009077
9078 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009079 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9080 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9081 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009082
9083 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009084 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009085 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009086 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009087
9088exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009089 /*
9090 * Key attributes may have been returned by psa_get_key_attributes()
9091 * thus reset them as required.
9092 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009093 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009094
Gilles Peskine449bd832023-01-11 14:50:10 +01009095 psa_key_derivation_abort(&operation);
9096 psa_destroy_key(base_key);
9097 psa_destroy_key(derived_key);
9098 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009099}
9100/* END_CASE */
9101
Janos Follath42fd8882019-07-03 14:17:09 +01009102/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009103void derive_key_export(int alg_arg,
9104 data_t *key_data,
9105 data_t *input1,
9106 data_t *input2,
9107 int bytes1_arg,
9108 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009109{
Ronald Cron5425a212020-08-04 14:58:35 +02009110 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9111 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009112 psa_algorithm_t alg = alg_arg;
9113 size_t bytes1 = bytes1_arg;
9114 size_t bytes2 = bytes2_arg;
9115 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009116 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009117 uint8_t *output_buffer = NULL;
9118 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009119 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9120 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009121 size_t length;
9122
Gilles Peskine449bd832023-01-11 14:50:10 +01009123 ASSERT_ALLOC(output_buffer, capacity);
9124 ASSERT_ALLOC(export_buffer, capacity);
9125 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009126
Gilles Peskine449bd832023-01-11 14:50:10 +01009127 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9128 psa_set_key_algorithm(&base_attributes, alg);
9129 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9130 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9131 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009132
9133 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009134 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9135 input1->x, input1->len,
9136 input2->x, input2->len,
9137 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009138 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009139 }
Janos Follath42fd8882019-07-03 14:17:09 +01009140
Gilles Peskine449bd832023-01-11 14:50:10 +01009141 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9142 output_buffer,
9143 capacity));
9144 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009145
9146 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009147 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9148 input1->x, input1->len,
9149 input2->x, input2->len,
9150 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009151 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009152 }
Janos Follath42fd8882019-07-03 14:17:09 +01009153
Gilles Peskine449bd832023-01-11 14:50:10 +01009154 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9155 psa_set_key_algorithm(&derived_attributes, 0);
9156 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9157 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9158 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9159 &derived_key));
9160 PSA_ASSERT(psa_export_key(derived_key,
9161 export_buffer, bytes1,
9162 &length));
9163 TEST_EQUAL(length, bytes1);
9164 PSA_ASSERT(psa_destroy_key(derived_key));
9165 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9166 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9167 &derived_key));
9168 PSA_ASSERT(psa_export_key(derived_key,
9169 export_buffer + bytes1, bytes2,
9170 &length));
9171 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009172
9173 /* Compare the outputs from the two runs. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009174 ASSERT_COMPARE(output_buffer, bytes1 + bytes2,
9175 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009176
9177exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009178 mbedtls_free(output_buffer);
9179 mbedtls_free(export_buffer);
9180 psa_key_derivation_abort(&operation);
9181 psa_destroy_key(base_key);
9182 psa_destroy_key(derived_key);
9183 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009184}
9185/* END_CASE */
9186
9187/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009188void derive_key_type(int alg_arg,
9189 data_t *key_data,
9190 data_t *input1,
9191 data_t *input2,
9192 int key_type_arg, int bits_arg,
9193 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009194{
9195 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9196 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9197 const psa_algorithm_t alg = alg_arg;
9198 const psa_key_type_t key_type = key_type_arg;
9199 const size_t bits = bits_arg;
9200 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9201 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009202 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009203 uint8_t *export_buffer = NULL;
9204 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9205 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9206 size_t export_length;
9207
Gilles Peskine449bd832023-01-11 14:50:10 +01009208 ASSERT_ALLOC(export_buffer, export_buffer_size);
9209 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009210
Gilles Peskine449bd832023-01-11 14:50:10 +01009211 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9212 psa_set_key_algorithm(&base_attributes, alg);
9213 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9214 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9215 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009216
Gilles Peskine449bd832023-01-11 14:50:10 +01009217 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009218 &operation, base_key, alg,
9219 input1->x, input1->len,
9220 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009221 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009222 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009223 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009224
Gilles Peskine449bd832023-01-11 14:50:10 +01009225 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9226 psa_set_key_algorithm(&derived_attributes, 0);
9227 psa_set_key_type(&derived_attributes, key_type);
9228 psa_set_key_bits(&derived_attributes, bits);
9229 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9230 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009231
Gilles Peskine449bd832023-01-11 14:50:10 +01009232 PSA_ASSERT(psa_export_key(derived_key,
9233 export_buffer, export_buffer_size,
9234 &export_length));
9235 ASSERT_COMPARE(export_buffer, export_length,
9236 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009237
9238exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009239 mbedtls_free(export_buffer);
9240 psa_key_derivation_abort(&operation);
9241 psa_destroy_key(base_key);
9242 psa_destroy_key(derived_key);
9243 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009244}
9245/* END_CASE */
9246
9247/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009248void derive_key(int alg_arg,
9249 data_t *key_data, data_t *input1, data_t *input2,
9250 int type_arg, int bits_arg,
9251 int expected_status_arg,
9252 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009253{
Ronald Cron5425a212020-08-04 14:58:35 +02009254 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9255 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009256 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009257 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009258 size_t bits = bits_arg;
9259 psa_status_t expected_status = expected_status_arg;
9260 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9261 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9262 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9263
Gilles Peskine449bd832023-01-11 14:50:10 +01009264 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009265
Gilles Peskine449bd832023-01-11 14:50:10 +01009266 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9267 psa_set_key_algorithm(&base_attributes, alg);
9268 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9269 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9270 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009271
Gilles Peskine449bd832023-01-11 14:50:10 +01009272 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9273 input1->x, input1->len,
9274 input2->x, input2->len,
9275 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009276 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009277 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009278
Gilles Peskine449bd832023-01-11 14:50:10 +01009279 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9280 psa_set_key_algorithm(&derived_attributes, 0);
9281 psa_set_key_type(&derived_attributes, type);
9282 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009283
9284 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009285 psa_key_derivation_output_key(&derived_attributes,
9286 &operation,
9287 &derived_key);
9288 if (is_large_output > 0) {
9289 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9290 }
9291 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009292
9293exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009294 psa_key_derivation_abort(&operation);
9295 psa_destroy_key(base_key);
9296 psa_destroy_key(derived_key);
9297 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009298}
9299/* END_CASE */
9300
9301/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009302void key_agreement_setup(int alg_arg,
9303 int our_key_type_arg, int our_key_alg_arg,
9304 data_t *our_key_data, data_t *peer_key_data,
9305 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009306{
Ronald Cron5425a212020-08-04 14:58:35 +02009307 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009308 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009309 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009310 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009311 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009312 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009313 psa_status_t expected_status = expected_status_arg;
9314 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009315
Gilles Peskine449bd832023-01-11 14:50:10 +01009316 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009317
Gilles Peskine449bd832023-01-11 14:50:10 +01009318 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9319 psa_set_key_algorithm(&attributes, our_key_alg);
9320 psa_set_key_type(&attributes, our_key_type);
9321 PSA_ASSERT(psa_import_key(&attributes,
9322 our_key_data->x, our_key_data->len,
9323 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009324
Gilles Peskine77f40d82019-04-11 21:27:06 +02009325 /* The tests currently include inputs that should fail at either step.
9326 * Test cases that fail at the setup step should be changed to call
9327 * key_derivation_setup instead, and this function should be renamed
9328 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009329 status = psa_key_derivation_setup(&operation, alg);
9330 if (status == PSA_SUCCESS) {
9331 TEST_EQUAL(psa_key_derivation_key_agreement(
9332 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9333 our_key,
9334 peer_key_data->x, peer_key_data->len),
9335 expected_status);
9336 } else {
9337 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009338 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009339
9340exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009341 psa_key_derivation_abort(&operation);
9342 psa_destroy_key(our_key);
9343 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009344}
9345/* END_CASE */
9346
9347/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009348void raw_key_agreement(int alg_arg,
9349 int our_key_type_arg, data_t *our_key_data,
9350 data_t *peer_key_data,
9351 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009352{
Ronald Cron5425a212020-08-04 14:58:35 +02009353 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009354 psa_algorithm_t alg = alg_arg;
9355 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009357 unsigned char *output = NULL;
9358 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009359 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009360
Gilles Peskine449bd832023-01-11 14:50:10 +01009361 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009362
Gilles Peskine449bd832023-01-11 14:50:10 +01009363 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9364 psa_set_key_algorithm(&attributes, alg);
9365 psa_set_key_type(&attributes, our_key_type);
9366 PSA_ASSERT(psa_import_key(&attributes,
9367 our_key_data->x, our_key_data->len,
9368 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009369
Gilles Peskine449bd832023-01-11 14:50:10 +01009370 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9371 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009372
Gilles Peskine992bee82022-04-13 23:25:52 +02009373 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009374 TEST_LE_U(expected_output->len,
9375 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9376 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9377 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009378
9379 /* Good case with exact output size */
Gilles Peskine449bd832023-01-11 14:50:10 +01009380 ASSERT_ALLOC(output, expected_output->len);
9381 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9382 peer_key_data->x, peer_key_data->len,
9383 output, expected_output->len,
9384 &output_length));
9385 ASSERT_COMPARE(output, output_length,
9386 expected_output->x, expected_output->len);
9387 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009388 output = NULL;
9389 output_length = ~0;
9390
9391 /* Larger buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009392 ASSERT_ALLOC(output, expected_output->len + 1);
9393 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9394 peer_key_data->x, peer_key_data->len,
9395 output, expected_output->len + 1,
9396 &output_length));
9397 ASSERT_COMPARE(output, output_length,
9398 expected_output->x, expected_output->len);
9399 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009400 output = NULL;
9401 output_length = ~0;
9402
9403 /* Buffer too small */
Gilles Peskine449bd832023-01-11 14:50:10 +01009404 ASSERT_ALLOC(output, expected_output->len - 1);
9405 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9406 peer_key_data->x, peer_key_data->len,
9407 output, expected_output->len - 1,
9408 &output_length),
9409 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009410 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009411 TEST_LE_U(output_length, expected_output->len - 1);
9412 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009413 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009414
9415exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009416 mbedtls_free(output);
9417 psa_destroy_key(our_key);
9418 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009419}
9420/* END_CASE */
9421
9422/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009423void key_agreement_capacity(int alg_arg,
9424 int our_key_type_arg, data_t *our_key_data,
9425 data_t *peer_key_data,
9426 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009427{
Ronald Cron5425a212020-08-04 14:58:35 +02009428 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009429 psa_algorithm_t alg = alg_arg;
9430 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009431 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009432 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009433 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009434 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009435
Gilles Peskine449bd832023-01-11 14:50:10 +01009436 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009437
Gilles Peskine449bd832023-01-11 14:50:10 +01009438 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9439 psa_set_key_algorithm(&attributes, alg);
9440 psa_set_key_type(&attributes, our_key_type);
9441 PSA_ASSERT(psa_import_key(&attributes,
9442 our_key_data->x, our_key_data->len,
9443 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009444
Gilles Peskine449bd832023-01-11 14:50:10 +01009445 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9446 PSA_ASSERT(psa_key_derivation_key_agreement(
9447 &operation,
9448 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9449 peer_key_data->x, peer_key_data->len));
9450 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009451 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009452 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9453 PSA_KEY_DERIVATION_INPUT_INFO,
9454 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009455 }
Gilles Peskine59685592018-09-18 12:11:34 +02009456
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009457 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009458 PSA_ASSERT(psa_key_derivation_get_capacity(
9459 &operation, &actual_capacity));
9460 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009461
Gilles Peskinebf491972018-10-25 22:36:12 +02009462 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009463 while (actual_capacity > sizeof(output)) {
9464 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9465 output, sizeof(output)));
9466 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009467 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009468 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9469 output, actual_capacity));
9470 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9471 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009472
Gilles Peskine59685592018-09-18 12:11:34 +02009473exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009474 psa_key_derivation_abort(&operation);
9475 psa_destroy_key(our_key);
9476 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009477}
9478/* END_CASE */
9479
9480/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009481void key_agreement_output(int alg_arg,
9482 int our_key_type_arg, data_t *our_key_data,
9483 data_t *peer_key_data,
9484 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009485{
Ronald Cron5425a212020-08-04 14:58:35 +02009486 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009487 psa_algorithm_t alg = alg_arg;
9488 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009489 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009490 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009491 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009492
Gilles Peskine449bd832023-01-11 14:50:10 +01009493 ASSERT_ALLOC(actual_output, MAX(expected_output1->len,
9494 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009495
Gilles Peskine449bd832023-01-11 14:50:10 +01009496 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009497
Gilles Peskine449bd832023-01-11 14:50:10 +01009498 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9499 psa_set_key_algorithm(&attributes, alg);
9500 psa_set_key_type(&attributes, our_key_type);
9501 PSA_ASSERT(psa_import_key(&attributes,
9502 our_key_data->x, our_key_data->len,
9503 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009504
Gilles Peskine449bd832023-01-11 14:50:10 +01009505 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9506 PSA_ASSERT(psa_key_derivation_key_agreement(
9507 &operation,
9508 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9509 peer_key_data->x, peer_key_data->len));
9510 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009511 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009512 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9513 PSA_KEY_DERIVATION_INPUT_INFO,
9514 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009515 }
Gilles Peskine59685592018-09-18 12:11:34 +02009516
Gilles Peskine449bd832023-01-11 14:50:10 +01009517 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9518 actual_output,
9519 expected_output1->len));
9520 ASSERT_COMPARE(actual_output, expected_output1->len,
9521 expected_output1->x, expected_output1->len);
9522 if (expected_output2->len != 0) {
9523 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9524 actual_output,
9525 expected_output2->len));
9526 ASSERT_COMPARE(actual_output, expected_output2->len,
9527 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009528 }
Gilles Peskine59685592018-09-18 12:11:34 +02009529
9530exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009531 psa_key_derivation_abort(&operation);
9532 psa_destroy_key(our_key);
9533 PSA_DONE();
9534 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009535}
9536/* END_CASE */
9537
9538/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009539void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009540{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009541 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009542 unsigned char *output = NULL;
9543 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009544 size_t i;
9545 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009546
Gilles Peskine449bd832023-01-11 14:50:10 +01009547 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009548
Gilles Peskine449bd832023-01-11 14:50:10 +01009549 ASSERT_ALLOC(output, bytes);
9550 ASSERT_ALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009551
Gilles Peskine449bd832023-01-11 14:50:10 +01009552 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009553
Gilles Peskinea50d7392018-06-21 10:22:13 +02009554 /* Run several times, to ensure that every output byte will be
9555 * nonzero at least once with overwhelming probability
9556 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009557 for (run = 0; run < 10; run++) {
9558 if (bytes != 0) {
9559 memset(output, 0, bytes);
9560 }
9561 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009562
Gilles Peskine449bd832023-01-11 14:50:10 +01009563 for (i = 0; i < bytes; i++) {
9564 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009565 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009566 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009567 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009568 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009569
9570 /* Check that every byte was changed to nonzero at least once. This
9571 * validates that psa_generate_random is overwriting every byte of
9572 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009573 for (i = 0; i < bytes; i++) {
9574 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009575 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009576
9577exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009578 PSA_DONE();
9579 mbedtls_free(output);
9580 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009581}
9582/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009583
9584/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009585void generate_key(int type_arg,
9586 int bits_arg,
9587 int usage_arg,
9588 int alg_arg,
9589 int expected_status_arg,
9590 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009591{
Ronald Cron5425a212020-08-04 14:58:35 +02009592 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009593 psa_key_type_t type = type_arg;
9594 psa_key_usage_t usage = usage_arg;
9595 size_t bits = bits_arg;
9596 psa_algorithm_t alg = alg_arg;
9597 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009598 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009599 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009600
Gilles Peskine449bd832023-01-11 14:50:10 +01009601 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009602
Gilles Peskine449bd832023-01-11 14:50:10 +01009603 psa_set_key_usage_flags(&attributes, usage);
9604 psa_set_key_algorithm(&attributes, alg);
9605 psa_set_key_type(&attributes, type);
9606 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009607
9608 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009609 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009610
Gilles Peskine449bd832023-01-11 14:50:10 +01009611 if (is_large_key > 0) {
9612 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9613 }
9614 TEST_EQUAL(status, expected_status);
9615 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009616 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009617 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009618
9619 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009620 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9621 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9622 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009623
Gilles Peskine818ca122018-06-20 18:16:48 +02009624 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009625 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009626 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009627 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009628
9629exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009630 /*
9631 * Key attributes may have been returned by psa_get_key_attributes()
9632 * thus reset them as required.
9633 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009634 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009635
Gilles Peskine449bd832023-01-11 14:50:10 +01009636 psa_destroy_key(key);
9637 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009638}
9639/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009640
Valerio Setti19fec542023-07-25 12:31:50 +02009641/* 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 +01009642void generate_key_rsa(int bits_arg,
9643 data_t *e_arg,
9644 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009645{
Ronald Cron5425a212020-08-04 14:58:35 +02009646 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009647 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009648 size_t bits = bits_arg;
9649 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9650 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9651 psa_status_t expected_status = expected_status_arg;
9652 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9653 uint8_t *exported = NULL;
9654 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009655 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009656 size_t exported_length = SIZE_MAX;
9657 uint8_t *e_read_buffer = NULL;
9658 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009659 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009660 size_t e_read_length = SIZE_MAX;
9661
Gilles Peskine449bd832023-01-11 14:50:10 +01009662 if (e_arg->len == 0 ||
9663 (e_arg->len == 3 &&
9664 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009665 is_default_public_exponent = 1;
9666 e_read_size = 0;
9667 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009668 ASSERT_ALLOC(e_read_buffer, e_read_size);
9669 ASSERT_ALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009670
Gilles Peskine449bd832023-01-11 14:50:10 +01009671 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009672
Gilles Peskine449bd832023-01-11 14:50:10 +01009673 psa_set_key_usage_flags(&attributes, usage);
9674 psa_set_key_algorithm(&attributes, alg);
9675 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9676 e_arg->x, e_arg->len));
9677 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009678
9679 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009680 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9681 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009682 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009683 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009684
9685 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009686 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9687 TEST_EQUAL(psa_get_key_type(&attributes), type);
9688 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9689 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
9690 e_read_buffer, e_read_size,
9691 &e_read_length));
9692 if (is_default_public_exponent) {
9693 TEST_EQUAL(e_read_length, 0);
9694 } else {
9695 ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
9696 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009697
9698 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009699 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009700 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009701 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009702
9703 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009704 PSA_ASSERT(psa_export_public_key(key,
9705 exported, exported_size,
9706 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009707 {
9708 uint8_t *p = exported;
9709 uint8_t *end = exported + exported_length;
9710 size_t len;
9711 /* RSAPublicKey ::= SEQUENCE {
9712 * modulus INTEGER, -- n
9713 * publicExponent INTEGER } -- e
9714 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009715 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9716 MBEDTLS_ASN1_SEQUENCE |
9717 MBEDTLS_ASN1_CONSTRUCTED));
9718 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9719 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9720 MBEDTLS_ASN1_INTEGER));
9721 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009722 ++p;
9723 --len;
9724 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009725 if (e_arg->len == 0) {
9726 TEST_EQUAL(len, 3);
9727 TEST_EQUAL(p[0], 1);
9728 TEST_EQUAL(p[1], 0);
9729 TEST_EQUAL(p[2], 1);
9730 } else {
9731 ASSERT_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009732 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009733 }
9734
9735exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009736 /*
9737 * Key attributes may have been returned by psa_get_key_attributes() or
9738 * set by psa_set_key_domain_parameters() thus reset them as required.
9739 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009740 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009741
Gilles Peskine449bd832023-01-11 14:50:10 +01009742 psa_destroy_key(key);
9743 PSA_DONE();
9744 mbedtls_free(e_read_buffer);
9745 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009746}
9747/* END_CASE */
9748
Darryl Greend49a4992018-06-18 17:27:26 +01009749/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009750void persistent_key_load_key_from_storage(data_t *data,
9751 int type_arg, int bits_arg,
9752 int usage_flags_arg, int alg_arg,
9753 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009754{
Gilles Peskine449bd832023-01-11 14:50:10 +01009755 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009756 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009757 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9758 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009759 psa_key_type_t type = type_arg;
9760 size_t bits = bits_arg;
9761 psa_key_usage_t usage_flags = usage_flags_arg;
9762 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009763 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009764 unsigned char *first_export = NULL;
9765 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009766 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009767 size_t first_exported_length;
9768 size_t second_exported_length;
9769
Gilles Peskine449bd832023-01-11 14:50:10 +01009770 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9771 ASSERT_ALLOC(first_export, export_size);
9772 ASSERT_ALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009773 }
Darryl Greend49a4992018-06-18 17:27:26 +01009774
Gilles Peskine449bd832023-01-11 14:50:10 +01009775 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009776
Gilles Peskine449bd832023-01-11 14:50:10 +01009777 psa_set_key_id(&attributes, key_id);
9778 psa_set_key_usage_flags(&attributes, usage_flags);
9779 psa_set_key_algorithm(&attributes, alg);
9780 psa_set_key_type(&attributes, type);
9781 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009782
Gilles Peskine449bd832023-01-11 14:50:10 +01009783 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009784 case IMPORT_KEY:
9785 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009786 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9787 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009788 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009789
Darryl Green0c6575a2018-11-07 16:05:30 +00009790 case GENERATE_KEY:
9791 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009792 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009793 break;
9794
9795 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009796#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009797 {
9798 /* Create base key */
9799 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9800 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9801 psa_set_key_usage_flags(&base_attributes,
9802 PSA_KEY_USAGE_DERIVE);
9803 psa_set_key_algorithm(&base_attributes, derive_alg);
9804 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9805 PSA_ASSERT(psa_import_key(&base_attributes,
9806 data->x, data->len,
9807 &base_key));
9808 /* Derive a key. */
9809 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9810 PSA_ASSERT(psa_key_derivation_input_key(
9811 &operation,
9812 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9813 PSA_ASSERT(psa_key_derivation_input_bytes(
9814 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9815 NULL, 0));
9816 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9817 &operation,
9818 &key));
9819 PSA_ASSERT(psa_key_derivation_abort(&operation));
9820 PSA_ASSERT(psa_destroy_key(base_key));
9821 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9822 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009823#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009824 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009825#endif
9826 break;
9827
9828 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009829 TEST_ASSERT(!"generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009830 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009831 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009832 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009833
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009834 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009835 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9836 PSA_ASSERT(psa_export_key(key,
9837 first_export, export_size,
9838 &first_exported_length));
9839 if (generation_method == IMPORT_KEY) {
9840 ASSERT_COMPARE(data->x, data->len,
9841 first_export, first_exported_length);
9842 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009843 }
Darryl Greend49a4992018-06-18 17:27:26 +01009844
9845 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009846 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009847 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009848 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009849
Darryl Greend49a4992018-06-18 17:27:26 +01009850 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009851 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9852 TEST_ASSERT(mbedtls_svc_key_id_equal(
9853 psa_get_key_id(&attributes), key_id));
9854 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9855 PSA_KEY_LIFETIME_PERSISTENT);
9856 TEST_EQUAL(psa_get_key_type(&attributes), type);
9857 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9858 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9859 mbedtls_test_update_key_usage_flags(usage_flags));
9860 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009861
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009862 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009863 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9864 PSA_ASSERT(psa_export_key(key,
9865 second_export, export_size,
9866 &second_exported_length));
9867 ASSERT_COMPARE(first_export, first_exported_length,
9868 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009869 }
9870
9871 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009872 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009873 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009874 }
Darryl Greend49a4992018-06-18 17:27:26 +01009875
9876exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009877 /*
9878 * Key attributes may have been returned by psa_get_key_attributes()
9879 * thus reset them as required.
9880 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009881 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009882
Gilles Peskine449bd832023-01-11 14:50:10 +01009883 mbedtls_free(first_export);
9884 mbedtls_free(second_export);
9885 psa_key_derivation_abort(&operation);
9886 psa_destroy_key(base_key);
9887 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009888 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009889}
9890/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009891
Neil Armstronga557cb82022-06-10 08:58:32 +02009892/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009893void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9894 int primitive_arg, int hash_arg, int role_arg,
9895 int test_input, data_t *pw_data,
9896 int inj_err_type_arg,
9897 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009898{
9899 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9900 psa_pake_operation_t operation = psa_pake_operation_init();
9901 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009902 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009903 psa_key_type_t key_type_pw = key_type_pw_arg;
9904 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009905 psa_algorithm_t hash_alg = hash_arg;
9906 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009907 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9908 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009909 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9910 psa_status_t expected_error = expected_error_arg;
9911 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009912 unsigned char *output_buffer = NULL;
9913 size_t output_len = 0;
9914
Gilles Peskine449bd832023-01-11 14:50:10 +01009915 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009916
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009917 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009918 PSA_PAKE_STEP_KEY_SHARE);
9919 ASSERT_ALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009920
Gilles Peskine449bd832023-01-11 14:50:10 +01009921 if (pw_data->len > 0) {
9922 psa_set_key_usage_flags(&attributes, key_usage_pw);
9923 psa_set_key_algorithm(&attributes, alg);
9924 psa_set_key_type(&attributes, key_type_pw);
9925 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9926 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009927 }
9928
Gilles Peskine449bd832023-01-11 14:50:10 +01009929 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9930 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9931 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009932
Gilles Peskine449bd832023-01-11 14:50:10 +01009933 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009934
Gilles Peskine449bd832023-01-11 14:50:10 +01009935 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9936 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9937 expected_error);
9938 PSA_ASSERT(psa_pake_abort(&operation));
9939 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9940 expected_error);
9941 PSA_ASSERT(psa_pake_abort(&operation));
9942 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9943 expected_error);
9944 PSA_ASSERT(psa_pake_abort(&operation));
9945 TEST_EQUAL(psa_pake_set_role(&operation, role),
9946 expected_error);
9947 PSA_ASSERT(psa_pake_abort(&operation));
9948 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9949 NULL, 0, NULL),
9950 expected_error);
9951 PSA_ASSERT(psa_pake_abort(&operation));
9952 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9953 expected_error);
9954 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009955 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009956 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009957
Gilles Peskine449bd832023-01-11 14:50:10 +01009958 status = psa_pake_setup(&operation, &cipher_suite);
9959 if (status != PSA_SUCCESS) {
9960 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009961 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009962 }
9963
Gilles Peskine449bd832023-01-11 14:50:10 +01009964 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9965 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9966 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009967 goto exit;
9968 }
9969
Gilles Peskine449bd832023-01-11 14:50:10 +01009970 status = psa_pake_set_role(&operation, role);
9971 if (status != PSA_SUCCESS) {
9972 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009973 goto exit;
9974 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009975
Gilles Peskine449bd832023-01-11 14:50:10 +01009976 if (pw_data->len > 0) {
9977 status = psa_pake_set_password_key(&operation, key);
9978 if (status != PSA_SUCCESS) {
9979 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009980 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009981 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009982 }
9983
Gilles Peskine449bd832023-01-11 14:50:10 +01009984 if (inj_err_type == INJECT_ERR_INVALID_USER) {
9985 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9986 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009987 goto exit;
9988 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009989
Gilles Peskine449bd832023-01-11 14:50:10 +01009990 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
9991 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9992 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009993 goto exit;
9994 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009995
Gilles Peskine449bd832023-01-11 14:50:10 +01009996 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009997 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009998 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
9999 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010000 goto exit;
10001 }
10002
Gilles Peskine449bd832023-01-11 14:50:10 +010010003 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010004 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010005 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
10006 PSA_ERROR_NOT_SUPPORTED);
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 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
10011 PSA_PAKE_STEP_KEY_SHARE);
10012 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
10013 PSA_PAKE_STEP_ZK_PUBLIC);
10014 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
10015 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010016
Gilles Peskine449bd832023-01-11 14:50:10 +010010017 if (test_input) {
10018 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10019 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
10020 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010021 goto exit;
10022 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010023
Gilles Peskine449bd832023-01-11 14:50:10 +010010024 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10025 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10026 output_buffer, size_zk_proof),
10027 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010028 goto exit;
10029 }
10030
Gilles Peskine449bd832023-01-11 14:50:10 +010010031 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10032 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
10033 output_buffer, size_zk_proof),
10034 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010035 goto exit;
10036 }
10037
Gilles Peskine449bd832023-01-11 14:50:10 +010010038 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
10039 output_buffer, size_key_share);
10040 if (status != PSA_SUCCESS) {
10041 TEST_EQUAL(status, expected_error);
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_WRONG_BUFFER_SIZE) {
10046 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10047 output_buffer, size_zk_public + 1),
10048 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010049 goto exit;
10050 }
10051
Gilles Peskine449bd832023-01-11 14:50:10 +010010052 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010053 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010054 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10055 output_buffer, size_zk_public + 1);
10056 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10057 output_buffer, size_zk_public),
10058 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010059 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010060 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010061 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010062 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10063 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10064 NULL, 0, NULL),
10065 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010066 goto exit;
10067 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010068
Gilles Peskine449bd832023-01-11 14:50:10 +010010069 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10070 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10071 output_buffer, buf_size, &output_len),
10072 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010073 goto exit;
10074 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010075
Gilles Peskine449bd832023-01-11 14:50:10 +010010076 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10077 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10078 output_buffer, buf_size, &output_len),
10079 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010080 goto exit;
10081 }
10082
Gilles Peskine449bd832023-01-11 14:50:10 +010010083 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10084 output_buffer, buf_size, &output_len);
10085 if (status != PSA_SUCCESS) {
10086 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010087 goto exit;
10088 }
10089
Gilles Peskine449bd832023-01-11 14:50:10 +010010090 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010091
Gilles Peskine449bd832023-01-11 14:50:10 +010010092 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10093 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10094 output_buffer, size_zk_public - 1, &output_len),
10095 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010096 goto exit;
10097 }
10098
Gilles Peskine449bd832023-01-11 14:50:10 +010010099 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010100 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010101 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10102 output_buffer, size_zk_public - 1, &output_len);
10103 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10104 output_buffer, buf_size, &output_len),
10105 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010106 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010107 }
10108 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010109
10110exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010111 PSA_ASSERT(psa_destroy_key(key));
10112 PSA_ASSERT(psa_pake_abort(&operation));
10113 mbedtls_free(output_buffer);
10114 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010115}
10116/* END_CASE */
10117
Neil Armstronga557cb82022-06-10 08:58:32 +020010118/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010119void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10120 int client_input_first, int inject_error,
10121 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010122{
10123 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10124 psa_pake_operation_t server = psa_pake_operation_init();
10125 psa_pake_operation_t client = psa_pake_operation_init();
10126 psa_algorithm_t alg = alg_arg;
10127 psa_algorithm_t hash_alg = hash_arg;
10128 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10129 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10130
Gilles Peskine449bd832023-01-11 14:50:10 +010010131 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010132
Gilles Peskine449bd832023-01-11 14:50:10 +010010133 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10134 psa_set_key_algorithm(&attributes, alg);
10135 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10136 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10137 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010138
Gilles Peskine449bd832023-01-11 14:50:10 +010010139 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10140 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10141 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010142
10143
Gilles Peskine449bd832023-01-11 14:50:10 +010010144 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10145 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010146
Gilles Peskine449bd832023-01-11 14:50:10 +010010147 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10148 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010149
Gilles Peskine449bd832023-01-11 14:50:10 +010010150 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10151 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010152
Gilles Peskine449bd832023-01-11 14:50:10 +010010153 ecjpake_do_round(alg, primitive_arg, &server, &client,
10154 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010155
Gilles Peskine449bd832023-01-11 14:50:10 +010010156 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010157 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010158 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010159
Gilles Peskine449bd832023-01-11 14:50:10 +010010160 ecjpake_do_round(alg, primitive_arg, &server, &client,
10161 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010162
10163exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010164 psa_destroy_key(key);
10165 psa_pake_abort(&server);
10166 psa_pake_abort(&client);
10167 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010168}
10169/* END_CASE */
10170
10171/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010172void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10173 int derive_alg_arg, data_t *pw_data,
10174 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010175{
10176 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10177 psa_pake_operation_t server = psa_pake_operation_init();
10178 psa_pake_operation_t client = psa_pake_operation_init();
10179 psa_algorithm_t alg = alg_arg;
10180 psa_algorithm_t hash_alg = hash_arg;
10181 psa_algorithm_t derive_alg = derive_alg_arg;
10182 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10184 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010185 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010186 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010187 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010188 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010189
Gilles Peskine449bd832023-01-11 14:50:10 +010010190 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010191
Gilles Peskine449bd832023-01-11 14:50:10 +010010192 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10193 psa_set_key_algorithm(&attributes, alg);
10194 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10195 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10196 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010197
Gilles Peskine449bd832023-01-11 14:50:10 +010010198 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10199 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10200 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010201
Neil Armstrong1e855602022-06-15 11:32:11 +020010202 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010203 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10204 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010205
Gilles Peskine449bd832023-01-11 14:50:10 +010010206 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10207 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10208 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10209 PSA_KEY_DERIVATION_INPUT_SEED,
10210 (const uint8_t *) "", 0));
10211 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10212 PSA_KEY_DERIVATION_INPUT_SEED,
10213 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010214 }
10215
Gilles Peskine449bd832023-01-11 14:50:10 +010010216 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10217 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010218
Gilles Peskine449bd832023-01-11 14:50:10 +010010219 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10220 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010221
Gilles Peskine449bd832023-01-11 14:50:10 +010010222 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10223 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010224
Gilles Peskine449bd832023-01-11 14:50:10 +010010225 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10226 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10227 PSA_ERROR_BAD_STATE);
10228 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10229 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010230 goto exit;
10231 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010232
Neil Armstrongf983caf2022-06-15 15:27:48 +020010233 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010234 ecjpake_do_round(alg, primitive_arg, &server, &client,
10235 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010236
Gilles Peskine449bd832023-01-11 14:50:10 +010010237 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10238 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10239 PSA_ERROR_BAD_STATE);
10240 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10241 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010242 goto exit;
10243 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010244
Neil Armstrongf983caf2022-06-15 15:27:48 +020010245 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010246 ecjpake_do_round(alg, primitive_arg, &server, &client,
10247 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010248
Gilles Peskine449bd832023-01-11 14:50:10 +010010249 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10250 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010251
10252exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010253 psa_key_derivation_abort(&server_derive);
10254 psa_key_derivation_abort(&client_derive);
10255 psa_destroy_key(key);
10256 psa_pake_abort(&server);
10257 psa_pake_abort(&client);
10258 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010259}
10260/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010261
10262/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010263void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010264{
10265 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10266 const size_t bits = 256;
10267 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010268 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010269 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010270 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010271
10272 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10273 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010274 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10275 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10276 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10277 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010278 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010279 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10280 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010281
10282 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010283 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10284 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10285 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10286 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10287 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10288 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010289
10290 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010291 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10292 PSA_PAKE_OUTPUT_MAX_SIZE);
10293 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10294 PSA_PAKE_OUTPUT_MAX_SIZE);
10295 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10296 PSA_PAKE_OUTPUT_MAX_SIZE);
10297 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10298 PSA_PAKE_INPUT_MAX_SIZE);
10299 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10300 PSA_PAKE_INPUT_MAX_SIZE);
10301 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10302 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010303}
10304/* END_CASE */