blob: b0123d9c8f455f675654abd2e263654e9bd17bd6 [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)));
1593 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001594
1595destroy:
1596 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 PSA_ASSERT(psa_destroy_key(key));
1598 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001599
1600exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001601 /*
1602 * Key attributes may have been returned by psa_get_key_attributes()
1603 * thus reset them as required.
1604 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 psa_reset_key_attributes(&got_attributes);
1606 psa_destroy_key(key);
1607 mbedtls_free(exported);
1608 mbedtls_free(reexported);
1609 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001610}
1611/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001612
Moran Pekerf709f4a2018-06-06 17:26:04 +03001613/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001614void import_export_public_key(data_t *data,
1615 int type_arg, // key pair or public key
1616 int alg_arg,
1617 int lifetime_arg,
1618 int export_size_delta,
1619 int expected_export_status_arg,
1620 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001621{
Ronald Cron5425a212020-08-04 14:58:35 +02001622 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001623 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001624 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001625 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001626 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301627 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001628 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001629 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001630 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001631 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001634
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 psa_set_key_lifetime(&attributes, lifetime);
1636 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1637 psa_set_key_algorithm(&attributes, alg);
1638 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001639
1640 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001642
Gilles Peskine49c25912018-10-29 15:15:31 +01001643 /* Export the public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 ASSERT_ALLOC(exported, export_size);
1645 status = psa_export_public_key(key,
1646 exported, export_size,
1647 &exported_length);
1648 TEST_EQUAL(status, expected_export_status);
1649 if (status == PSA_SUCCESS) {
1650 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001651 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1653 bits = psa_get_key_bits(&attributes);
1654 TEST_LE_U(expected_public_key->len,
1655 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1656 TEST_LE_U(expected_public_key->len,
1657 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1658 TEST_LE_U(expected_public_key->len,
1659 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1660 ASSERT_COMPARE(expected_public_key->x, expected_public_key->len,
1661 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001662 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001663exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001664 /*
1665 * Key attributes may have been returned by psa_get_key_attributes()
1666 * thus reset them as required.
1667 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 mbedtls_free(exported);
1671 psa_destroy_key(key);
1672 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001673}
1674/* END_CASE */
1675
Gilles Peskine20035e32018-02-03 22:44:14 +01001676/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001677void import_and_exercise_key(data_t *data,
1678 int type_arg,
1679 int bits_arg,
1680 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001681{
Ronald Cron5425a212020-08-04 14:58:35 +02001682 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001683 psa_key_type_t type = type_arg;
1684 size_t bits = bits_arg;
1685 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001687 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001688 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 psa_set_key_usage_flags(&attributes, usage);
1693 psa_set_key_algorithm(&attributes, alg);
1694 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001695
1696 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001698
1699 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1701 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1702 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001703
1704 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001706 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001708
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 PSA_ASSERT(psa_destroy_key(key));
1710 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001711
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001712exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001713 /*
1714 * Key attributes may have been returned by psa_get_key_attributes()
1715 * thus reset them as required.
1716 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001718
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 psa_reset_key_attributes(&attributes);
1720 psa_destroy_key(key);
1721 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001722}
1723/* END_CASE */
1724
1725/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001726void effective_key_attributes(int type_arg, int expected_type_arg,
1727 int bits_arg, int expected_bits_arg,
1728 int usage_arg, int expected_usage_arg,
1729 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001730{
Ronald Cron5425a212020-08-04 14:58:35 +02001731 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001732 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001733 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001734 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001735 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001736 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001737 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001738 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001739 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001740 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001741
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001743
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 psa_set_key_usage_flags(&attributes, usage);
1745 psa_set_key_algorithm(&attributes, alg);
1746 psa_set_key_type(&attributes, key_type);
1747 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001748
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 PSA_ASSERT(psa_generate_key(&attributes, &key));
1750 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001751
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1753 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1754 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1755 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1756 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001757
1758exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001759 /*
1760 * Key attributes may have been returned by psa_get_key_attributes()
1761 * thus reset them as required.
1762 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001764
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 psa_destroy_key(key);
1766 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001767}
1768/* END_CASE */
1769
1770/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001771void check_key_policy(int type_arg, int bits_arg,
1772 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001773{
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1775 usage_arg,
1776 mbedtls_test_update_key_usage_flags(usage_arg),
1777 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001778 goto exit;
1779}
1780/* END_CASE */
1781
1782/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001783void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001784{
1785 /* Test each valid way of initializing the object, except for `= {0}`, as
1786 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1787 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001788 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001790 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1791 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001792
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001794
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1796 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1797 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001798
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 TEST_EQUAL(psa_get_key_type(&func), 0);
1800 TEST_EQUAL(psa_get_key_type(&init), 0);
1801 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 TEST_EQUAL(psa_get_key_bits(&func), 0);
1804 TEST_EQUAL(psa_get_key_bits(&init), 0);
1805 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001806
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1808 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1809 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001810
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1812 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1813 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001814}
1815/* END_CASE */
1816
1817/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001818void mac_key_policy(int policy_usage_arg,
1819 int policy_alg_arg,
1820 int key_type_arg,
1821 data_t *key_data,
1822 int exercise_alg_arg,
1823 int expected_status_sign_arg,
1824 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001825{
Ronald Cron5425a212020-08-04 14:58:35 +02001826 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001827 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001828 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001829 psa_key_type_t key_type = key_type_arg;
1830 psa_algorithm_t policy_alg = policy_alg_arg;
1831 psa_algorithm_t exercise_alg = exercise_alg_arg;
1832 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001833 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001834 psa_status_t expected_status_sign = expected_status_sign_arg;
1835 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001836 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001837
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001839
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 psa_set_key_usage_flags(&attributes, policy_usage);
1841 psa_set_key_algorithm(&attributes, policy_alg);
1842 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1845 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1848 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001849
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1851 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001852
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001853 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001855 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1857 input, 128,
1858 mac, PSA_MAC_MAX_SIZE, &mac_len),
1859 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001860
Neil Armstrong3af9b972022-02-07 12:20:21 +01001861 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 PSA_ASSERT(psa_mac_abort(&operation));
1863 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1864 if (status == PSA_SUCCESS) {
1865 status = psa_mac_update(&operation, input, 128);
1866 if (status == PSA_SUCCESS) {
1867 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1868 &mac_len),
1869 expected_status_sign);
1870 } else {
1871 TEST_EQUAL(status, expected_status_sign);
1872 }
1873 } else {
1874 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001875 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001877
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001878 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 status = psa_mac_verify(key, exercise_alg, input, 128,
1880 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001881
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1883 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1884 } else {
1885 TEST_EQUAL(status, expected_status_verify);
1886 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001887
Neil Armstrong3af9b972022-02-07 12:20:21 +01001888 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1890 if (status == PSA_SUCCESS) {
1891 status = psa_mac_update(&operation, input, 128);
1892 if (status == PSA_SUCCESS) {
1893 status = psa_mac_verify_finish(&operation, mac, mac_len);
1894 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1895 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1896 } else {
1897 TEST_EQUAL(status, expected_status_verify);
1898 }
1899 } else {
1900 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001901 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 } else {
1903 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001904 }
1905
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 memset(mac, 0, sizeof(mac));
1909 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1910 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001911
1912exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 psa_mac_abort(&operation);
1914 psa_destroy_key(key);
1915 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001916}
1917/* END_CASE */
1918
1919/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001920void cipher_key_policy(int policy_usage_arg,
1921 int policy_alg,
1922 int key_type,
1923 data_t *key_data,
1924 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001925{
Ronald Cron5425a212020-08-04 14:58:35 +02001926 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001927 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001928 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001929 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001930 size_t output_buffer_size = 0;
1931 size_t input_buffer_size = 0;
1932 size_t output_length = 0;
1933 uint8_t *output = NULL;
1934 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001935 psa_status_t status;
1936
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1938 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1939 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001940
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 ASSERT_ALLOC(input, input_buffer_size);
1942 ASSERT_ALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001943
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 psa_set_key_usage_flags(&attributes, policy_usage);
1947 psa_set_key_algorithm(&attributes, policy_alg);
1948 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001949
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1951 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001952
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001953 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 TEST_EQUAL(policy_usage,
1955 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001956
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001957 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1959 output, output_buffer_size,
1960 &output_length);
1961 if (policy_alg == exercise_alg &&
1962 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1963 PSA_ASSERT(status);
1964 } else {
1965 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1966 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001967
1968 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1970 if (policy_alg == exercise_alg &&
1971 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1972 PSA_ASSERT(status);
1973 } else {
1974 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1975 }
1976 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001977
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001978 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1980 input, input_buffer_size,
1981 &output_length);
1982 if (policy_alg == exercise_alg &&
1983 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1984 PSA_ASSERT(status);
1985 } else {
1986 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1987 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001988
1989 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
1991 if (policy_alg == exercise_alg &&
1992 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1993 PSA_ASSERT(status);
1994 } else {
1995 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1996 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001997
1998exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 psa_cipher_abort(&operation);
2000 mbedtls_free(input);
2001 mbedtls_free(output);
2002 psa_destroy_key(key);
2003 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002004}
2005/* END_CASE */
2006
2007/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002008void aead_key_policy(int policy_usage_arg,
2009 int policy_alg,
2010 int key_type,
2011 data_t *key_data,
2012 int nonce_length_arg,
2013 int tag_length_arg,
2014 int exercise_alg,
2015 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002016{
Ronald Cron5425a212020-08-04 14:58:35 +02002017 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002018 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002019 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002020 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002021 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002022 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002024 size_t nonce_length = nonce_length_arg;
2025 unsigned char tag[16];
2026 size_t tag_length = tag_length_arg;
2027 size_t output_length;
2028
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 TEST_LE_U(nonce_length, sizeof(nonce));
2030 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002031
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 psa_set_key_usage_flags(&attributes, policy_usage);
2035 psa_set_key_algorithm(&attributes, policy_alg);
2036 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2039 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002040
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002041 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 TEST_EQUAL(policy_usage,
2043 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002044
Neil Armstrong752d8112022-02-07 14:51:11 +01002045 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 status = psa_aead_encrypt(key, exercise_alg,
2047 nonce, nonce_length,
2048 NULL, 0,
2049 NULL, 0,
2050 tag, tag_length,
2051 &output_length);
2052 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2053 TEST_EQUAL(status, expected_status);
2054 } else {
2055 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2056 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002057
Neil Armstrong752d8112022-02-07 14:51:11 +01002058 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2060 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2061 TEST_EQUAL(status, expected_status);
2062 } else {
2063 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2064 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002065
2066 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 memset(tag, 0, sizeof(tag));
2068 status = psa_aead_decrypt(key, exercise_alg,
2069 nonce, nonce_length,
2070 NULL, 0,
2071 tag, tag_length,
2072 NULL, 0,
2073 &output_length);
2074 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2075 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2076 } else if (expected_status == PSA_SUCCESS) {
2077 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2078 } else {
2079 TEST_EQUAL(status, expected_status);
2080 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002081
Neil Armstrong752d8112022-02-07 14:51:11 +01002082 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 PSA_ASSERT(psa_aead_abort(&operation));
2084 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2085 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2086 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2087 } else {
2088 TEST_EQUAL(status, expected_status);
2089 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002090
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002091exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 PSA_ASSERT(psa_aead_abort(&operation));
2093 psa_destroy_key(key);
2094 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002095}
2096/* END_CASE */
2097
2098/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002099void asymmetric_encryption_key_policy(int policy_usage_arg,
2100 int policy_alg,
2101 int key_type,
2102 data_t *key_data,
2103 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002104{
Ronald Cron5425a212020-08-04 14:58:35 +02002105 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002106 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002107 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002108 psa_status_t status;
2109 size_t key_bits;
2110 size_t buffer_length;
2111 unsigned char *buffer = NULL;
2112 size_t output_length;
2113
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 psa_set_key_usage_flags(&attributes, policy_usage);
2117 psa_set_key_algorithm(&attributes, policy_alg);
2118 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2121 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002122
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002123 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 TEST_EQUAL(policy_usage,
2125 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2128 key_bits = psa_get_key_bits(&attributes);
2129 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2130 exercise_alg);
2131 ASSERT_ALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 status = psa_asymmetric_encrypt(key, exercise_alg,
2134 NULL, 0,
2135 NULL, 0,
2136 buffer, buffer_length,
2137 &output_length);
2138 if (policy_alg == exercise_alg &&
2139 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2140 PSA_ASSERT(status);
2141 } else {
2142 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2143 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002144
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 if (buffer_length != 0) {
2146 memset(buffer, 0, buffer_length);
2147 }
2148 status = psa_asymmetric_decrypt(key, exercise_alg,
2149 buffer, buffer_length,
2150 NULL, 0,
2151 buffer, buffer_length,
2152 &output_length);
2153 if (policy_alg == exercise_alg &&
2154 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2155 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2156 } else {
2157 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2158 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002159
2160exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002161 /*
2162 * Key attributes may have been returned by psa_get_key_attributes()
2163 * thus reset them as required.
2164 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002166
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 psa_destroy_key(key);
2168 PSA_DONE();
2169 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002170}
2171/* END_CASE */
2172
2173/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002174void asymmetric_signature_key_policy(int policy_usage_arg,
2175 int policy_alg,
2176 int key_type,
2177 data_t *key_data,
2178 int exercise_alg,
2179 int payload_length_arg,
2180 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002181{
Ronald Cron5425a212020-08-04 14:58:35 +02002182 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002184 psa_key_usage_t policy_usage = policy_usage_arg;
2185 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002186 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002188 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2189 * compatible with the policy and `payload_length_arg` is supposed to be
2190 * a valid input length to sign. If `payload_length_arg <= 0`,
2191 * `exercise_alg` is supposed to be forbidden by the policy. */
2192 int compatible_alg = payload_length_arg > 0;
2193 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002195 size_t signature_length;
2196
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002197 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002198 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 TEST_EQUAL(expected_usage,
2200 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002201
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002203
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 psa_set_key_usage_flags(&attributes, policy_usage);
2205 psa_set_key_algorithm(&attributes, policy_alg);
2206 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2209 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 status = psa_sign_hash(key, exercise_alg,
2214 payload, payload_length,
2215 signature, sizeof(signature),
2216 &signature_length);
2217 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2218 PSA_ASSERT(status);
2219 } else {
2220 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2221 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002222
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 memset(signature, 0, sizeof(signature));
2224 status = psa_verify_hash(key, exercise_alg,
2225 payload, payload_length,
2226 signature, sizeof(signature));
2227 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2228 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2229 } else {
2230 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2231 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002232
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2234 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2235 status = psa_sign_message(key, exercise_alg,
2236 payload, payload_length,
2237 signature, sizeof(signature),
2238 &signature_length);
2239 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2240 PSA_ASSERT(status);
2241 } else {
2242 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2243 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 memset(signature, 0, sizeof(signature));
2246 status = psa_verify_message(key, exercise_alg,
2247 payload, payload_length,
2248 signature, sizeof(signature));
2249 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2250 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2251 } else {
2252 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2253 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002254 }
2255
Gilles Peskined5b33222018-06-18 22:20:03 +02002256exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 psa_destroy_key(key);
2258 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002259}
2260/* END_CASE */
2261
Janos Follathba3fab92019-06-11 14:50:16 +01002262/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002263void derive_key_policy(int policy_usage,
2264 int policy_alg,
2265 int key_type,
2266 data_t *key_data,
2267 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002268{
Ronald Cron5425a212020-08-04 14:58:35 +02002269 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002270 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002271 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002272 psa_status_t status;
2273
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002275
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 psa_set_key_usage_flags(&attributes, policy_usage);
2277 psa_set_key_algorithm(&attributes, policy_alg);
2278 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2281 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002284
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2286 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2287 PSA_ASSERT(psa_key_derivation_input_bytes(
2288 &operation,
2289 PSA_KEY_DERIVATION_INPUT_SEED,
2290 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002291 }
Janos Follathba3fab92019-06-11 14:50:16 +01002292
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 status = psa_key_derivation_input_key(&operation,
2294 PSA_KEY_DERIVATION_INPUT_SECRET,
2295 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 if (policy_alg == exercise_alg &&
2298 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2299 PSA_ASSERT(status);
2300 } else {
2301 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2302 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002303
2304exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 psa_key_derivation_abort(&operation);
2306 psa_destroy_key(key);
2307 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002308}
2309/* END_CASE */
2310
2311/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002312void agreement_key_policy(int policy_usage,
2313 int policy_alg,
2314 int key_type_arg,
2315 data_t *key_data,
2316 int exercise_alg,
2317 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002318{
Ronald Cron5425a212020-08-04 14:58:35 +02002319 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002320 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002321 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002322 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002323 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002324 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002327
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 psa_set_key_usage_flags(&attributes, policy_usage);
2329 psa_set_key_algorithm(&attributes, policy_alg);
2330 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2333 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2336 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002339
2340exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 psa_key_derivation_abort(&operation);
2342 psa_destroy_key(key);
2343 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002344}
2345/* END_CASE */
2346
2347/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002348void key_policy_alg2(int key_type_arg, data_t *key_data,
2349 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002350{
Ronald Cron5425a212020-08-04 14:58:35 +02002351 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002352 psa_key_type_t key_type = key_type_arg;
2353 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2354 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2355 psa_key_usage_t usage = usage_arg;
2356 psa_algorithm_t alg = alg_arg;
2357 psa_algorithm_t alg2 = alg2_arg;
2358
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002360
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 psa_set_key_usage_flags(&attributes, usage);
2362 psa_set_key_algorithm(&attributes, alg);
2363 psa_set_key_enrollment_algorithm(&attributes, alg2);
2364 psa_set_key_type(&attributes, key_type);
2365 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2366 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002367
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002368 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 usage = mbedtls_test_update_key_usage_flags(usage);
2370 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2371 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2372 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2373 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002374
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002376 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 }
2378 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002379 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002381
2382exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002383 /*
2384 * Key attributes may have been returned by psa_get_key_attributes()
2385 * thus reset them as required.
2386 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 psa_destroy_key(key);
2390 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002391}
2392/* END_CASE */
2393
2394/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002395void raw_agreement_key_policy(int policy_usage,
2396 int policy_alg,
2397 int key_type_arg,
2398 data_t *key_data,
2399 int exercise_alg,
2400 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002401{
Ronald Cron5425a212020-08-04 14:58:35 +02002402 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002403 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002404 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002405 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002406 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002407 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002408
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 psa_set_key_usage_flags(&attributes, policy_usage);
2412 psa_set_key_algorithm(&attributes, policy_alg);
2413 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2416 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002421
2422exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 psa_key_derivation_abort(&operation);
2424 psa_destroy_key(key);
2425 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002426}
2427/* END_CASE */
2428
2429/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002430void copy_success(int source_usage_arg,
2431 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002432 int source_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 int type_arg, data_t *material,
2434 int copy_attributes,
2435 int target_usage_arg,
2436 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002437 int target_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 int expected_usage_arg,
2439 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002440{
Gilles Peskineca25db92019-04-19 11:43:08 +02002441 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2442 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002443 psa_key_usage_t expected_usage = expected_usage_arg;
2444 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002445 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302446 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2447 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002448 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2449 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002450 uint8_t *export_buffer = NULL;
2451
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002453
Gilles Peskineca25db92019-04-19 11:43:08 +02002454 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2456 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2457 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2458 psa_set_key_type(&source_attributes, type_arg);
2459 psa_set_key_lifetime(&source_attributes, source_lifetime);
2460 PSA_ASSERT(psa_import_key(&source_attributes,
2461 material->x, material->len,
2462 &source_key));
2463 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002464
Gilles Peskineca25db92019-04-19 11:43:08 +02002465 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002467 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002468 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002470
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 if (target_usage_arg != -1) {
2472 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2473 }
2474 if (target_alg_arg != -1) {
2475 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2476 }
2477 if (target_alg2_arg != -1) {
2478 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2479 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002480
Archana8a180362021-07-05 02:18:48 +05302481
Gilles Peskine57ab7212019-01-28 13:03:09 +01002482 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 PSA_ASSERT(psa_copy_key(source_key,
2484 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002485
2486 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002488
2489 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2491 TEST_EQUAL(psa_get_key_type(&source_attributes),
2492 psa_get_key_type(&target_attributes));
2493 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2494 psa_get_key_bits(&target_attributes));
2495 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2496 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2497 TEST_EQUAL(expected_alg2,
2498 psa_get_key_enrollment_algorithm(&target_attributes));
2499 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002500 size_t length;
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 ASSERT_ALLOC(export_buffer, material->len);
2502 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2503 material->len, &length));
2504 ASSERT_COMPARE(material->x, material->len,
2505 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002506 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 if (!psa_key_lifetime_is_external(target_lifetime)) {
2509 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302510 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 }
2512 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302513 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 }
Archana8a180362021-07-05 02:18:48 +05302515 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002516
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002518
2519exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002520 /*
2521 * Source and target key attributes may have been returned by
2522 * psa_get_key_attributes() thus reset them as required.
2523 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 psa_reset_key_attributes(&source_attributes);
2525 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002526
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 PSA_DONE();
2528 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002529}
2530/* END_CASE */
2531
2532/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002533void copy_fail(int source_usage_arg,
2534 int source_alg_arg, int source_alg2_arg,
2535 int source_lifetime_arg,
2536 int type_arg, data_t *material,
2537 int target_type_arg, int target_bits_arg,
2538 int target_usage_arg,
2539 int target_alg_arg, int target_alg2_arg,
2540 int target_id_arg, int target_lifetime_arg,
2541 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002542{
2543 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2544 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002545 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2546 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002548
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002550
2551 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2553 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2554 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2555 psa_set_key_type(&source_attributes, type_arg);
2556 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2557 PSA_ASSERT(psa_import_key(&source_attributes,
2558 material->x, material->len,
2559 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002560
2561 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 psa_set_key_id(&target_attributes, key_id);
2563 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2564 psa_set_key_type(&target_attributes, target_type_arg);
2565 psa_set_key_bits(&target_attributes, target_bits_arg);
2566 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2567 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2568 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002569
2570 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 TEST_EQUAL(psa_copy_key(source_key,
2572 &target_attributes, &target_key),
2573 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002576
Gilles Peskine4a644642019-05-03 17:14:08 +02002577exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 psa_reset_key_attributes(&source_attributes);
2579 psa_reset_key_attributes(&target_attributes);
2580 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002581}
2582/* END_CASE */
2583
2584/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002585void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002586{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002587 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002588 /* Test each valid way of initializing the object, except for `= {0}`, as
2589 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2590 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002591 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002593 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2594 psa_hash_operation_t zero;
2595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002597
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002598 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2600 PSA_ERROR_BAD_STATE);
2601 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2602 PSA_ERROR_BAD_STATE);
2603 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2604 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002605
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002606 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 PSA_ASSERT(psa_hash_abort(&func));
2608 PSA_ASSERT(psa_hash_abort(&init));
2609 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002610}
2611/* END_CASE */
2612
2613/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002614void hash_setup(int alg_arg,
2615 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002616{
2617 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002618 uint8_t *output = NULL;
2619 size_t output_size = 0;
2620 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002621 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002622 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002623 psa_status_t status;
2624
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002626
Neil Armstrongedb20862022-02-07 15:47:44 +01002627 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 output_size = PSA_HASH_LENGTH(alg);
2629 ASSERT_ALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002630
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 status = psa_hash_compute(alg, NULL, 0,
2632 output, output_size, &output_length);
2633 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002634
2635 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 status = psa_hash_setup(&operation, alg);
2637 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002638
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002639 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002641
2642 /* If setup failed, reproduce the failure, so as to
2643 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 if (status != PSA_SUCCESS) {
2645 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2646 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002647
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002648 /* Now the operation object should be reusable. */
2649#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2651 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002652#endif
2653
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002654exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 mbedtls_free(output);
2656 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002657}
2658/* END_CASE */
2659
2660/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002661void hash_compute_fail(int alg_arg, data_t *input,
2662 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002663{
2664 psa_algorithm_t alg = alg_arg;
2665 uint8_t *output = NULL;
2666 size_t output_size = output_size_arg;
2667 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002668 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002669 psa_status_t expected_status = expected_status_arg;
2670 psa_status_t status;
2671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 ASSERT_ALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002675
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002676 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 status = psa_hash_compute(alg, input->x, input->len,
2678 output, output_size, &output_length);
2679 TEST_EQUAL(status, expected_status);
2680 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002681
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002682 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 status = psa_hash_setup(&operation, alg);
2684 if (status == PSA_SUCCESS) {
2685 status = psa_hash_update(&operation, input->x, input->len);
2686 if (status == PSA_SUCCESS) {
2687 status = psa_hash_finish(&operation, output, output_size,
2688 &output_length);
2689 if (status == PSA_SUCCESS) {
2690 TEST_LE_U(output_length, output_size);
2691 } else {
2692 TEST_EQUAL(status, expected_status);
2693 }
2694 } else {
2695 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002696 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 } else {
2698 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002699 }
2700
Gilles Peskine0a749c82019-11-28 19:33:58 +01002701exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 PSA_ASSERT(psa_hash_abort(&operation));
2703 mbedtls_free(output);
2704 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002705}
2706/* END_CASE */
2707
2708/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002709void hash_compare_fail(int alg_arg, data_t *input,
2710 data_t *reference_hash,
2711 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002712{
2713 psa_algorithm_t alg = alg_arg;
2714 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002715 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002716 psa_status_t status;
2717
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002719
Neil Armstrong55a1be12022-02-07 11:23:20 +01002720 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 status = psa_hash_compare(alg, input->x, input->len,
2722 reference_hash->x, reference_hash->len);
2723 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002724
Neil Armstrong55a1be12022-02-07 11:23:20 +01002725 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 status = psa_hash_setup(&operation, alg);
2727 if (status == PSA_SUCCESS) {
2728 status = psa_hash_update(&operation, input->x, input->len);
2729 if (status == PSA_SUCCESS) {
2730 status = psa_hash_verify(&operation, reference_hash->x,
2731 reference_hash->len);
2732 TEST_EQUAL(status, expected_status);
2733 } else {
2734 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002735 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 } else {
2737 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002738 }
2739
Gilles Peskine88e08462020-01-28 20:43:00 +01002740exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 PSA_ASSERT(psa_hash_abort(&operation));
2742 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002743}
2744/* END_CASE */
2745
2746/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002747void hash_compute_compare(int alg_arg, data_t *input,
2748 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002749{
2750 psa_algorithm_t alg = alg_arg;
2751 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2752 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002753 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002754 size_t i;
2755
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002757
Neil Armstrongca30a002022-02-07 11:40:23 +01002758 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2760 output, PSA_HASH_LENGTH(alg),
2761 &output_length));
2762 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2763 ASSERT_COMPARE(output, output_length,
2764 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002765
Neil Armstrongca30a002022-02-07 11:40:23 +01002766 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 PSA_ASSERT(psa_hash_setup(&operation, alg));
2768 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2769 PSA_ASSERT(psa_hash_finish(&operation, output,
2770 PSA_HASH_LENGTH(alg),
2771 &output_length));
2772 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2773 ASSERT_COMPARE(output, output_length,
2774 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002775
2776 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2778 output, sizeof(output),
2779 &output_length));
2780 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2781 ASSERT_COMPARE(output, output_length,
2782 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002783
Neil Armstrongca30a002022-02-07 11:40:23 +01002784 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 PSA_ASSERT(psa_hash_setup(&operation, alg));
2786 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2787 PSA_ASSERT(psa_hash_finish(&operation, output,
2788 sizeof(output), &output_length));
2789 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2790 ASSERT_COMPARE(output, output_length,
2791 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002792
2793 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2795 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002796
Neil Armstrongca30a002022-02-07 11:40:23 +01002797 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 PSA_ASSERT(psa_hash_setup(&operation, alg));
2799 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2800 PSA_ASSERT(psa_hash_verify(&operation, output,
2801 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002802
2803 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2805 output, output_length + 1),
2806 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002807
Neil Armstrongca30a002022-02-07 11:40:23 +01002808 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 PSA_ASSERT(psa_hash_setup(&operation, alg));
2810 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2811 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2812 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002813
2814 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2816 output, output_length - 1),
2817 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002818
Neil Armstrongca30a002022-02-07 11:40:23 +01002819 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 PSA_ASSERT(psa_hash_setup(&operation, alg));
2821 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2822 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2823 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002824
Gilles Peskine0a749c82019-11-28 19:33:58 +01002825 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002826 for (i = 0; i < output_length; i++) {
2827 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002828 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002829
2830 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2832 output, output_length),
2833 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002834
2835 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 PSA_ASSERT(psa_hash_setup(&operation, alg));
2837 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2838 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2839 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002840
Gilles Peskine0a749c82019-11-28 19:33:58 +01002841 output[i] ^= 1;
2842 }
2843
2844exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 PSA_ASSERT(psa_hash_abort(&operation));
2846 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002847}
2848/* END_CASE */
2849
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002850/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002851void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002852{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002853 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002854 unsigned char input[] = "";
2855 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002856 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002857 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2858 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2860 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002861 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002862 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002863 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002864
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002866
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002867 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 PSA_ASSERT(psa_hash_setup(&operation, alg));
2869 ASSERT_OPERATION_IS_ACTIVE(operation);
2870 TEST_EQUAL(psa_hash_setup(&operation, alg),
2871 PSA_ERROR_BAD_STATE);
2872 ASSERT_OPERATION_IS_INACTIVE(operation);
2873 PSA_ASSERT(psa_hash_abort(&operation));
2874 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002875
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002876 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2878 PSA_ERROR_BAD_STATE);
2879 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002880
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002881 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002883 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 ASSERT_OPERATION_IS_ACTIVE(operation);
2885 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2886 PSA_ERROR_BAD_STATE);
2887 ASSERT_OPERATION_IS_INACTIVE(operation);
2888 PSA_ASSERT(psa_hash_abort(&operation));
2889 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002890
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002891 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 PSA_ASSERT(psa_hash_setup(&operation, alg));
2893 PSA_ASSERT(psa_hash_finish(&operation,
2894 hash, sizeof(hash), &hash_len));
2895 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2896 PSA_ERROR_BAD_STATE);
2897 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002898
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002899 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 TEST_EQUAL(psa_hash_verify(&operation,
2901 valid_hash, sizeof(valid_hash)),
2902 PSA_ERROR_BAD_STATE);
2903 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002904
2905 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 PSA_ASSERT(psa_hash_setup(&operation, alg));
2907 PSA_ASSERT(psa_hash_finish(&operation,
2908 hash, sizeof(hash), &hash_len));
2909 TEST_EQUAL(psa_hash_verify(&operation,
2910 valid_hash, sizeof(valid_hash)),
2911 PSA_ERROR_BAD_STATE);
2912 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002913
2914 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 PSA_ASSERT(psa_hash_setup(&operation, alg));
2916 ASSERT_OPERATION_IS_ACTIVE(operation);
2917 PSA_ASSERT(psa_hash_verify(&operation,
2918 valid_hash, sizeof(valid_hash)));
2919 ASSERT_OPERATION_IS_INACTIVE(operation);
2920 TEST_EQUAL(psa_hash_verify(&operation,
2921 valid_hash, sizeof(valid_hash)),
2922 PSA_ERROR_BAD_STATE);
2923 ASSERT_OPERATION_IS_INACTIVE(operation);
2924 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002925
2926 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 TEST_EQUAL(psa_hash_finish(&operation,
2928 hash, sizeof(hash), &hash_len),
2929 PSA_ERROR_BAD_STATE);
2930 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002931
2932 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 PSA_ASSERT(psa_hash_setup(&operation, alg));
2934 PSA_ASSERT(psa_hash_finish(&operation,
2935 hash, sizeof(hash), &hash_len));
2936 TEST_EQUAL(psa_hash_finish(&operation,
2937 hash, sizeof(hash), &hash_len),
2938 PSA_ERROR_BAD_STATE);
2939 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002940
2941 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 PSA_ASSERT(psa_hash_setup(&operation, alg));
2943 PSA_ASSERT(psa_hash_verify(&operation,
2944 valid_hash, sizeof(valid_hash)));
2945 TEST_EQUAL(psa_hash_finish(&operation,
2946 hash, sizeof(hash), &hash_len),
2947 PSA_ERROR_BAD_STATE);
2948 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002949
2950exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002952}
2953/* END_CASE */
2954
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002955/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002956void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002957{
2958 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002959 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2960 * appended to it */
2961 unsigned char hash[] = {
2962 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2963 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2965 };
2966 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002967 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002968
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002970
itayzafrir27e69452018-11-01 14:26:34 +02002971 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 PSA_ASSERT(psa_hash_setup(&operation, alg));
2973 ASSERT_OPERATION_IS_ACTIVE(operation);
2974 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2975 PSA_ERROR_INVALID_SIGNATURE);
2976 ASSERT_OPERATION_IS_INACTIVE(operation);
2977 PSA_ASSERT(psa_hash_abort(&operation));
2978 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002979
itayzafrir27e69452018-11-01 14:26:34 +02002980 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 PSA_ASSERT(psa_hash_setup(&operation, alg));
2982 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
2983 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03002984
itayzafrir27e69452018-11-01 14:26:34 +02002985 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 PSA_ASSERT(psa_hash_setup(&operation, alg));
2987 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
2988 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03002989
itayzafrirec93d302018-10-18 18:01:10 +03002990exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03002992}
2993/* END_CASE */
2994
Ronald Cronee414c72021-03-18 18:50:08 +01002995/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002996void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03002997{
2998 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002999 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003001 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003002 size_t hash_len;
3003
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03003005
itayzafrir58028322018-10-25 10:22:01 +03003006 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 PSA_ASSERT(psa_hash_setup(&operation, alg));
3008 TEST_EQUAL(psa_hash_finish(&operation,
3009 hash, expected_size - 1, &hash_len),
3010 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003011
3012exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003014}
3015/* END_CASE */
3016
Ronald Cronee414c72021-03-18 18:50:08 +01003017/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003018void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003019{
3020 psa_algorithm_t alg = PSA_ALG_SHA_256;
3021 unsigned char hash[PSA_HASH_MAX_SIZE];
3022 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3023 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3024 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3025 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3026 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3027 size_t hash_len;
3028
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 PSA_ASSERT(psa_crypto_init());
3030 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003031
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3033 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3034 PSA_ASSERT(psa_hash_finish(&op_finished,
3035 hash, sizeof(hash), &hash_len));
3036 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3037 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003038
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3040 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003041
Gilles Peskine449bd832023-01-11 14:50:10 +01003042 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3043 PSA_ASSERT(psa_hash_finish(&op_init,
3044 hash, sizeof(hash), &hash_len));
3045 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3046 PSA_ASSERT(psa_hash_finish(&op_finished,
3047 hash, sizeof(hash), &hash_len));
3048 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3049 PSA_ASSERT(psa_hash_finish(&op_aborted,
3050 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003051
3052exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 psa_hash_abort(&op_source);
3054 psa_hash_abort(&op_init);
3055 psa_hash_abort(&op_setup);
3056 psa_hash_abort(&op_finished);
3057 psa_hash_abort(&op_aborted);
3058 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003059}
3060/* END_CASE */
3061
Ronald Cronee414c72021-03-18 18:50:08 +01003062/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003063void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003064{
3065 psa_algorithm_t alg = PSA_ALG_SHA_256;
3066 unsigned char hash[PSA_HASH_MAX_SIZE];
3067 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3068 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3069 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3070 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3071 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3072 size_t hash_len;
3073
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003075
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3077 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3078 PSA_ASSERT(psa_hash_finish(&op_finished,
3079 hash, sizeof(hash), &hash_len));
3080 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3081 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3084 PSA_ASSERT(psa_hash_finish(&op_target,
3085 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003086
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3088 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3089 PSA_ERROR_BAD_STATE);
3090 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3091 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003092
3093exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 psa_hash_abort(&op_target);
3095 psa_hash_abort(&op_init);
3096 psa_hash_abort(&op_setup);
3097 psa_hash_abort(&op_finished);
3098 psa_hash_abort(&op_aborted);
3099 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003100}
3101/* END_CASE */
3102
itayzafrir58028322018-10-25 10:22:01 +03003103/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003104void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003105{
Jaeden Amero252ef282019-02-15 14:05:35 +00003106 const uint8_t input[1] = { 0 };
3107
Jaeden Amero769ce272019-01-04 11:48:03 +00003108 /* Test each valid way of initializing the object, except for `= {0}`, as
3109 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3110 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003111 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003113 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3114 psa_mac_operation_t zero;
3115
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003117
Jaeden Amero252ef282019-02-15 14:05:35 +00003118 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 TEST_EQUAL(psa_mac_update(&func,
3120 input, sizeof(input)),
3121 PSA_ERROR_BAD_STATE);
3122 TEST_EQUAL(psa_mac_update(&init,
3123 input, sizeof(input)),
3124 PSA_ERROR_BAD_STATE);
3125 TEST_EQUAL(psa_mac_update(&zero,
3126 input, sizeof(input)),
3127 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003128
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003129 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 PSA_ASSERT(psa_mac_abort(&func));
3131 PSA_ASSERT(psa_mac_abort(&init));
3132 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003133}
3134/* END_CASE */
3135
3136/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003137void mac_setup(int key_type_arg,
3138 data_t *key,
3139 int alg_arg,
3140 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003141{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003142 psa_key_type_t key_type = key_type_arg;
3143 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003144 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003145 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003146 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3147#if defined(KNOWN_SUPPORTED_MAC_ALG)
3148 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3149#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003150
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003152
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3154 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003155 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 }
3157 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003158
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003159 /* The operation object should be reusable. */
3160#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3162 smoke_test_key_data,
3163 sizeof(smoke_test_key_data),
3164 KNOWN_SUPPORTED_MAC_ALG,
3165 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003166 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 }
3168 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003169#endif
3170
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003171exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003172 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003173}
3174/* END_CASE */
3175
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003176/* 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 +01003177void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003178{
Ronald Cron5425a212020-08-04 14:58:35 +02003179 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003180 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3181 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003182 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003183 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3184 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003185 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3186 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003187 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003188 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3189 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3190 size_t sign_mac_length = 0;
3191 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3192 const uint8_t verify_mac[] = {
3193 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3194 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3196 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003197
Gilles Peskine449bd832023-01-11 14:50:10 +01003198 PSA_ASSERT(psa_crypto_init());
3199 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3200 psa_set_key_algorithm(&attributes, alg);
3201 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003202
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3204 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003205
Jaeden Amero252ef282019-02-15 14:05:35 +00003206 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3208 PSA_ERROR_BAD_STATE);
3209 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003210
3211 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3213 &sign_mac_length),
3214 PSA_ERROR_BAD_STATE);
3215 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003216
3217 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003218 TEST_EQUAL(psa_mac_verify_finish(&operation,
3219 verify_mac, sizeof(verify_mac)),
3220 PSA_ERROR_BAD_STATE);
3221 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003222
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003223 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3225 ASSERT_OPERATION_IS_ACTIVE(operation);
3226 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3227 PSA_ERROR_BAD_STATE);
3228 ASSERT_OPERATION_IS_INACTIVE(operation);
3229 PSA_ASSERT(psa_mac_abort(&operation));
3230 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003231
Jaeden Amero252ef282019-02-15 14:05:35 +00003232 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3234 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3235 PSA_ASSERT(psa_mac_sign_finish(&operation,
3236 sign_mac, sizeof(sign_mac),
3237 &sign_mac_length));
3238 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3239 PSA_ERROR_BAD_STATE);
3240 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003241
3242 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003243 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3244 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3245 PSA_ASSERT(psa_mac_verify_finish(&operation,
3246 verify_mac, sizeof(verify_mac)));
3247 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3248 PSA_ERROR_BAD_STATE);
3249 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003250
3251 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3253 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3254 PSA_ASSERT(psa_mac_sign_finish(&operation,
3255 sign_mac, sizeof(sign_mac),
3256 &sign_mac_length));
3257 TEST_EQUAL(psa_mac_sign_finish(&operation,
3258 sign_mac, sizeof(sign_mac),
3259 &sign_mac_length),
3260 PSA_ERROR_BAD_STATE);
3261 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003262
3263 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3265 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3266 PSA_ASSERT(psa_mac_verify_finish(&operation,
3267 verify_mac, sizeof(verify_mac)));
3268 TEST_EQUAL(psa_mac_verify_finish(&operation,
3269 verify_mac, sizeof(verify_mac)),
3270 PSA_ERROR_BAD_STATE);
3271 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003272
3273 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3275 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3276 ASSERT_OPERATION_IS_ACTIVE(operation);
3277 TEST_EQUAL(psa_mac_verify_finish(&operation,
3278 verify_mac, sizeof(verify_mac)),
3279 PSA_ERROR_BAD_STATE);
3280 ASSERT_OPERATION_IS_INACTIVE(operation);
3281 PSA_ASSERT(psa_mac_abort(&operation));
3282 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003283
3284 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003285 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3286 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3287 ASSERT_OPERATION_IS_ACTIVE(operation);
3288 TEST_EQUAL(psa_mac_sign_finish(&operation,
3289 sign_mac, sizeof(sign_mac),
3290 &sign_mac_length),
3291 PSA_ERROR_BAD_STATE);
3292 ASSERT_OPERATION_IS_INACTIVE(operation);
3293 PSA_ASSERT(psa_mac_abort(&operation));
3294 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003295
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003297
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003298exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003299 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003300}
3301/* END_CASE */
3302
3303/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003304void mac_sign_verify_multi(int key_type_arg,
3305 data_t *key_data,
3306 int alg_arg,
3307 data_t *input,
3308 int is_verify,
3309 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003310{
3311 size_t data_part_len = 0;
3312
Gilles Peskine449bd832023-01-11 14:50:10 +01003313 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003314 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003315 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003316
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 if (mac_multipart_internal_func(key_type_arg, key_data,
3318 alg_arg,
3319 input, data_part_len,
3320 expected_mac,
3321 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003322 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003324
3325 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003326 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003327
Gilles Peskine449bd832023-01-11 14:50:10 +01003328 if (mac_multipart_internal_func(key_type_arg, key_data,
3329 alg_arg,
3330 input, data_part_len,
3331 expected_mac,
3332 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003333 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003335 }
3336
3337 /* Goto is required to silence warnings about unused labels, as we
3338 * don't actually do any test assertions in this function. */
3339 goto exit;
3340}
3341/* END_CASE */
3342
3343/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003344void mac_sign(int key_type_arg,
3345 data_t *key_data,
3346 int alg_arg,
3347 data_t *input,
3348 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003349{
Ronald Cron5425a212020-08-04 14:58:35 +02003350 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003351 psa_key_type_t key_type = key_type_arg;
3352 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003353 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003354 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003355 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003356 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003357 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003358 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003359 const size_t output_sizes_to_test[] = {
3360 0,
3361 1,
3362 expected_mac->len - 1,
3363 expected_mac->len,
3364 expected_mac->len + 1,
3365 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003366
Gilles Peskine449bd832023-01-11 14:50:10 +01003367 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003368 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003370
Gilles Peskine449bd832023-01-11 14:50:10 +01003371 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003372
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3374 psa_set_key_algorithm(&attributes, alg);
3375 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003376
Gilles Peskine449bd832023-01-11 14:50:10 +01003377 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3378 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003379
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003381 const size_t output_size = output_sizes_to_test[i];
3382 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003383 (output_size >= expected_mac->len ? PSA_SUCCESS :
3384 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003385
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 mbedtls_test_set_step(output_size);
3387 ASSERT_ALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003388
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003389 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003390 TEST_EQUAL(psa_mac_compute(key, alg,
3391 input->x, input->len,
3392 actual_mac, output_size, &mac_length),
3393 expected_status);
3394 if (expected_status == PSA_SUCCESS) {
3395 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3396 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003397 }
3398
Gilles Peskine449bd832023-01-11 14:50:10 +01003399 if (output_size > 0) {
3400 memset(actual_mac, 0, output_size);
3401 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003402
3403 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003404 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3405 PSA_ASSERT(psa_mac_update(&operation,
3406 input->x, input->len));
3407 TEST_EQUAL(psa_mac_sign_finish(&operation,
3408 actual_mac, output_size,
3409 &mac_length),
3410 expected_status);
3411 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003412
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 if (expected_status == PSA_SUCCESS) {
3414 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3415 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003416 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003417 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003418 actual_mac = NULL;
3419 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003420
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003421exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003422 psa_mac_abort(&operation);
3423 psa_destroy_key(key);
3424 PSA_DONE();
3425 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003426}
3427/* END_CASE */
3428
3429/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003430void mac_verify(int key_type_arg,
3431 data_t *key_data,
3432 int alg_arg,
3433 data_t *input,
3434 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003435{
Ronald Cron5425a212020-08-04 14:58:35 +02003436 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003437 psa_key_type_t key_type = key_type_arg;
3438 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003439 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003440 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003441 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003442
Gilles Peskine449bd832023-01-11 14:50:10 +01003443 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003444
Gilles Peskine449bd832023-01-11 14:50:10 +01003445 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003446
Gilles Peskine449bd832023-01-11 14:50:10 +01003447 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3448 psa_set_key_algorithm(&attributes, alg);
3449 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003450
Gilles Peskine449bd832023-01-11 14:50:10 +01003451 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3452 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003453
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003454 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003455 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3456 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003457
3458 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003459 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3460 PSA_ASSERT(psa_mac_update(&operation,
3461 input->x, input->len));
3462 PSA_ASSERT(psa_mac_verify_finish(&operation,
3463 expected_mac->x,
3464 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003465
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003466 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003467 TEST_EQUAL(psa_mac_verify(key, alg,
3468 input->x, input->len,
3469 expected_mac->x,
3470 expected_mac->len - 1),
3471 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003472
3473 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003474 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3475 PSA_ASSERT(psa_mac_update(&operation,
3476 input->x, input->len));
3477 TEST_EQUAL(psa_mac_verify_finish(&operation,
3478 expected_mac->x,
3479 expected_mac->len - 1),
3480 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003481
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003482 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003483 ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1);
3484 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3485 TEST_EQUAL(psa_mac_verify(key, alg,
3486 input->x, input->len,
3487 perturbed_mac, expected_mac->len + 1),
3488 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003489
3490 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003491 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3492 PSA_ASSERT(psa_mac_update(&operation,
3493 input->x, input->len));
3494 TEST_EQUAL(psa_mac_verify_finish(&operation,
3495 perturbed_mac,
3496 expected_mac->len + 1),
3497 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003498
3499 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003500 for (size_t i = 0; i < expected_mac->len; i++) {
3501 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003502 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003503
Gilles Peskine449bd832023-01-11 14:50:10 +01003504 TEST_EQUAL(psa_mac_verify(key, alg,
3505 input->x, input->len,
3506 perturbed_mac, expected_mac->len),
3507 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003508
Gilles Peskine449bd832023-01-11 14:50:10 +01003509 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3510 PSA_ASSERT(psa_mac_update(&operation,
3511 input->x, input->len));
3512 TEST_EQUAL(psa_mac_verify_finish(&operation,
3513 perturbed_mac,
3514 expected_mac->len),
3515 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003516 perturbed_mac[i] ^= 1;
3517 }
3518
Gilles Peskine8c9def32018-02-08 10:02:12 +01003519exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003520 psa_mac_abort(&operation);
3521 psa_destroy_key(key);
3522 PSA_DONE();
3523 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003524}
3525/* END_CASE */
3526
3527/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003528void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003529{
Jaeden Ameroab439972019-02-15 14:12:05 +00003530 const uint8_t input[1] = { 0 };
3531 unsigned char output[1] = { 0 };
3532 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003533 /* Test each valid way of initializing the object, except for `= {0}`, as
3534 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3535 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003536 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003537 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003538 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3539 psa_cipher_operation_t zero;
3540
Gilles Peskine449bd832023-01-11 14:50:10 +01003541 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003542
Jaeden Ameroab439972019-02-15 14:12:05 +00003543 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003544 TEST_EQUAL(psa_cipher_update(&func,
3545 input, sizeof(input),
3546 output, sizeof(output),
3547 &output_length),
3548 PSA_ERROR_BAD_STATE);
3549 TEST_EQUAL(psa_cipher_update(&init,
3550 input, sizeof(input),
3551 output, sizeof(output),
3552 &output_length),
3553 PSA_ERROR_BAD_STATE);
3554 TEST_EQUAL(psa_cipher_update(&zero,
3555 input, sizeof(input),
3556 output, sizeof(output),
3557 &output_length),
3558 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003559
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003560 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003561 PSA_ASSERT(psa_cipher_abort(&func));
3562 PSA_ASSERT(psa_cipher_abort(&init));
3563 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003564}
3565/* END_CASE */
3566
3567/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003568void cipher_setup(int key_type_arg,
3569 data_t *key,
3570 int alg_arg,
3571 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003572{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003573 psa_key_type_t key_type = key_type_arg;
3574 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003575 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003576 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003577 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003578#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003579 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3580#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003581
Gilles Peskine449bd832023-01-11 14:50:10 +01003582 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003583
Gilles Peskine449bd832023-01-11 14:50:10 +01003584 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3585 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003586 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003587 }
3588 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003589
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003590 /* The operation object should be reusable. */
3591#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003592 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3593 smoke_test_key_data,
3594 sizeof(smoke_test_key_data),
3595 KNOWN_SUPPORTED_CIPHER_ALG,
3596 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003597 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003598 }
3599 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003600#endif
3601
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003602exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003603 psa_cipher_abort(&operation);
3604 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003605}
3606/* END_CASE */
3607
Ronald Cronee414c72021-03-18 18:50:08 +01003608/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003609void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003610{
Ronald Cron5425a212020-08-04 14:58:35 +02003611 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003612 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3613 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003614 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003615 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003616 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003617 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003618 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003619 0xaa, 0xaa, 0xaa, 0xaa
3620 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003621 const uint8_t text[] = {
3622 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003623 0xbb, 0xbb, 0xbb, 0xbb
3624 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003625 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003626 size_t length = 0;
3627
Gilles Peskine449bd832023-01-11 14:50:10 +01003628 PSA_ASSERT(psa_crypto_init());
3629 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3630 psa_set_key_algorithm(&attributes, alg);
3631 psa_set_key_type(&attributes, key_type);
3632 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3633 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003634
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003635 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003636 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3637 ASSERT_OPERATION_IS_ACTIVE(operation);
3638 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3639 PSA_ERROR_BAD_STATE);
3640 ASSERT_OPERATION_IS_INACTIVE(operation);
3641 PSA_ASSERT(psa_cipher_abort(&operation));
3642 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003643
3644 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003645 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3646 ASSERT_OPERATION_IS_ACTIVE(operation);
3647 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3648 PSA_ERROR_BAD_STATE);
3649 ASSERT_OPERATION_IS_INACTIVE(operation);
3650 PSA_ASSERT(psa_cipher_abort(&operation));
3651 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003652
Jaeden Ameroab439972019-02-15 14:12:05 +00003653 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003654 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3655 buffer, sizeof(buffer),
3656 &length),
3657 PSA_ERROR_BAD_STATE);
3658 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003659
3660 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003661 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3662 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3663 buffer, sizeof(buffer),
3664 &length));
3665 ASSERT_OPERATION_IS_ACTIVE(operation);
3666 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3667 buffer, sizeof(buffer),
3668 &length),
3669 PSA_ERROR_BAD_STATE);
3670 ASSERT_OPERATION_IS_INACTIVE(operation);
3671 PSA_ASSERT(psa_cipher_abort(&operation));
3672 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003673
3674 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003675 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3676 PSA_ASSERT(psa_cipher_set_iv(&operation,
3677 iv, sizeof(iv)));
3678 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3679 buffer, sizeof(buffer),
3680 &length),
3681 PSA_ERROR_BAD_STATE);
3682 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003683
3684 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003685 TEST_EQUAL(psa_cipher_set_iv(&operation,
3686 iv, sizeof(iv)),
3687 PSA_ERROR_BAD_STATE);
3688 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003689
3690 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003691 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3692 PSA_ASSERT(psa_cipher_set_iv(&operation,
3693 iv, sizeof(iv)));
3694 ASSERT_OPERATION_IS_ACTIVE(operation);
3695 TEST_EQUAL(psa_cipher_set_iv(&operation,
3696 iv, sizeof(iv)),
3697 PSA_ERROR_BAD_STATE);
3698 ASSERT_OPERATION_IS_INACTIVE(operation);
3699 PSA_ASSERT(psa_cipher_abort(&operation));
3700 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003701
3702 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003703 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3704 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3705 buffer, sizeof(buffer),
3706 &length));
3707 TEST_EQUAL(psa_cipher_set_iv(&operation,
3708 iv, sizeof(iv)),
3709 PSA_ERROR_BAD_STATE);
3710 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003711
3712 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003713 TEST_EQUAL(psa_cipher_update(&operation,
3714 text, sizeof(text),
3715 buffer, sizeof(buffer),
3716 &length),
3717 PSA_ERROR_BAD_STATE);
3718 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003719
3720 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003721 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3722 ASSERT_OPERATION_IS_ACTIVE(operation);
3723 TEST_EQUAL(psa_cipher_update(&operation,
3724 text, sizeof(text),
3725 buffer, sizeof(buffer),
3726 &length),
3727 PSA_ERROR_BAD_STATE);
3728 ASSERT_OPERATION_IS_INACTIVE(operation);
3729 PSA_ASSERT(psa_cipher_abort(&operation));
3730 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003731
3732 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003733 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3734 PSA_ASSERT(psa_cipher_set_iv(&operation,
3735 iv, sizeof(iv)));
3736 PSA_ASSERT(psa_cipher_finish(&operation,
3737 buffer, sizeof(buffer), &length));
3738 TEST_EQUAL(psa_cipher_update(&operation,
3739 text, sizeof(text),
3740 buffer, sizeof(buffer),
3741 &length),
3742 PSA_ERROR_BAD_STATE);
3743 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003744
3745 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003746 TEST_EQUAL(psa_cipher_finish(&operation,
3747 buffer, sizeof(buffer), &length),
3748 PSA_ERROR_BAD_STATE);
3749 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003750
3751 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003752 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003753 /* Not calling update means we are encrypting an empty buffer, which is OK
3754 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003755 ASSERT_OPERATION_IS_ACTIVE(operation);
3756 TEST_EQUAL(psa_cipher_finish(&operation,
3757 buffer, sizeof(buffer), &length),
3758 PSA_ERROR_BAD_STATE);
3759 ASSERT_OPERATION_IS_INACTIVE(operation);
3760 PSA_ASSERT(psa_cipher_abort(&operation));
3761 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003762
3763 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003764 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3765 PSA_ASSERT(psa_cipher_set_iv(&operation,
3766 iv, sizeof(iv)));
3767 PSA_ASSERT(psa_cipher_finish(&operation,
3768 buffer, sizeof(buffer), &length));
3769 TEST_EQUAL(psa_cipher_finish(&operation,
3770 buffer, sizeof(buffer), &length),
3771 PSA_ERROR_BAD_STATE);
3772 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003773
Gilles Peskine449bd832023-01-11 14:50:10 +01003774 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003775
Jaeden Ameroab439972019-02-15 14:12:05 +00003776exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003777 psa_cipher_abort(&operation);
3778 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003779}
3780/* END_CASE */
3781
3782/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003783void cipher_encrypt_fail(int alg_arg,
3784 int key_type_arg,
3785 data_t *key_data,
3786 data_t *input,
3787 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003788{
Ronald Cron5425a212020-08-04 14:58:35 +02003789 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003790 psa_status_t status;
3791 psa_key_type_t key_type = key_type_arg;
3792 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003793 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003794 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003795 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3796 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003797 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003798 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003799 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003800 size_t function_output_length;
3801 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003802 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3803
Gilles Peskine449bd832023-01-11 14:50:10 +01003804 if (PSA_ERROR_BAD_STATE != expected_status) {
3805 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003806
Gilles Peskine449bd832023-01-11 14:50:10 +01003807 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3808 psa_set_key_algorithm(&attributes, alg);
3809 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003810
Gilles Peskine449bd832023-01-11 14:50:10 +01003811 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3812 input->len);
3813 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003814
Gilles Peskine449bd832023-01-11 14:50:10 +01003815 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3816 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003817 }
3818
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003819 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003820 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3821 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003822
Gilles Peskine449bd832023-01-11 14:50:10 +01003823 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003824
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003825 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003826 status = psa_cipher_encrypt_setup(&operation, key, alg);
3827 if (status == PSA_SUCCESS) {
3828 if (alg != PSA_ALG_ECB_NO_PADDING) {
3829 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3830 iv, iv_size,
3831 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003832 }
3833
Gilles Peskine449bd832023-01-11 14:50:10 +01003834 status = psa_cipher_update(&operation, input->x, input->len,
3835 output, output_buffer_size,
3836 &function_output_length);
3837 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003838 output_length += function_output_length;
3839
Gilles Peskine449bd832023-01-11 14:50:10 +01003840 status = psa_cipher_finish(&operation, output + output_length,
3841 output_buffer_size - output_length,
3842 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003843
Gilles Peskine449bd832023-01-11 14:50:10 +01003844 TEST_EQUAL(status, expected_status);
3845 } else {
3846 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003847 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003848 } else {
3849 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003850 }
3851
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003852exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003853 psa_cipher_abort(&operation);
3854 mbedtls_free(output);
3855 psa_destroy_key(key);
3856 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003857}
3858/* END_CASE */
3859
3860/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003861void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3862 data_t *input, int iv_length,
3863 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003864{
3865 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3866 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3867 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3868 size_t output_buffer_size = 0;
3869 unsigned char *output = NULL;
3870
Gilles Peskine449bd832023-01-11 14:50:10 +01003871 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
3872 ASSERT_ALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003873
Gilles Peskine449bd832023-01-11 14:50:10 +01003874 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003875
Gilles Peskine449bd832023-01-11 14:50:10 +01003876 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3877 psa_set_key_algorithm(&attributes, alg);
3878 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003879
Gilles Peskine449bd832023-01-11 14:50:10 +01003880 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3881 &key));
3882 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3883 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3884 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003885
3886exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003887 psa_cipher_abort(&operation);
3888 mbedtls_free(output);
3889 psa_destroy_key(key);
3890 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003891}
3892/* END_CASE */
3893
3894/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003895void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3896 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003897{
3898 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3899 psa_key_type_t key_type = key_type_arg;
3900 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003901 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3902 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003903 unsigned char *output = NULL;
3904 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003905 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003906 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3907
Gilles Peskine449bd832023-01-11 14:50:10 +01003908 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003909
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003910 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003911 TEST_LE_U(ciphertext->len,
3912 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3913 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3914 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3915 TEST_LE_U(plaintext->len,
3916 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3917 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3918 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003919
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003920
3921 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003922 psa_set_key_usage_flags(&attributes,
3923 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3924 psa_set_key_algorithm(&attributes, alg);
3925 psa_set_key_type(&attributes, key_type);
3926 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3927 &key));
3928 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3929 plaintext->len);
3930 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003931
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003932 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003933 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3934 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3935 PSA_ERROR_BAD_STATE);
3936 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3937 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3938 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003939
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003940 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003941 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3942 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3943 &length),
3944 PSA_ERROR_BAD_STATE);
3945 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3946 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3947 &length),
3948 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003949
Gilles Peskine286c3142022-04-20 17:09:38 +02003950 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003951 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003952 output_length = 0;
3953 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003954 PSA_ASSERT(psa_cipher_update(&operation,
3955 plaintext->x, plaintext->len,
3956 output, output_buffer_size,
3957 &length));
3958 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003959 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003960 PSA_ASSERT(psa_cipher_finish(&operation,
3961 mbedtls_buffer_offset(output, output_length),
3962 output_buffer_size - output_length,
3963 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003964 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003965 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3966 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003967
Gilles Peskine286c3142022-04-20 17:09:38 +02003968 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003969 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003970 output_length = 0;
3971 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003972 PSA_ASSERT(psa_cipher_update(&operation,
3973 ciphertext->x, ciphertext->len,
3974 output, output_buffer_size,
3975 &length));
3976 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003977 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003978 PSA_ASSERT(psa_cipher_finish(&operation,
3979 mbedtls_buffer_offset(output, output_length),
3980 output_buffer_size - output_length,
3981 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003982 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003983 ASSERT_COMPARE(plaintext->x, plaintext->len,
3984 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003985
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003986 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003987 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003988 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
3989 output, output_buffer_size,
3990 &output_length));
3991 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3992 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003993
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003994 /* One-shot decryption */
3995 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003996 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
3997 output, output_buffer_size,
3998 &output_length));
3999 ASSERT_COMPARE(plaintext->x, plaintext->len,
4000 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004001
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004002exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004003 PSA_ASSERT(psa_cipher_abort(&operation));
4004 mbedtls_free(output);
4005 psa_cipher_abort(&operation);
4006 psa_destroy_key(key);
4007 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004008}
4009/* END_CASE */
4010
4011/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004012void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004013{
4014 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4015 psa_algorithm_t alg = alg_arg;
4016 psa_key_type_t key_type = key_type_arg;
4017 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4018 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4019 psa_status_t status;
4020
Gilles Peskine449bd832023-01-11 14:50:10 +01004021 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004022
Gilles Peskine449bd832023-01-11 14:50:10 +01004023 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4024 psa_set_key_algorithm(&attributes, alg);
4025 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004026
4027 /* Usage of either of these two size macros would cause divide by zero
4028 * with incorrect key types previously. Input length should be irrelevant
4029 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004030 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4031 0);
4032 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004033
4034
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4036 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004037
4038 /* Should fail due to invalid alg type (to support invalid key type).
4039 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004040 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004041
Gilles Peskine449bd832023-01-11 14:50:10 +01004042 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004043
4044exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 psa_cipher_abort(&operation);
4046 psa_destroy_key(key);
4047 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004048}
4049/* END_CASE */
4050
4051/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004052void cipher_encrypt_validation(int alg_arg,
4053 int key_type_arg,
4054 data_t *key_data,
4055 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004056{
4057 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4058 psa_key_type_t key_type = key_type_arg;
4059 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004060 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004061 unsigned char *output1 = NULL;
4062 size_t output1_buffer_size = 0;
4063 size_t output1_length = 0;
4064 unsigned char *output2 = NULL;
4065 size_t output2_buffer_size = 0;
4066 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004067 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004068 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004069 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004070
Gilles Peskine449bd832023-01-11 14:50:10 +01004071 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004072
Gilles Peskine449bd832023-01-11 14:50:10 +01004073 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4074 psa_set_key_algorithm(&attributes, alg);
4075 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004076
Gilles Peskine449bd832023-01-11 14:50:10 +01004077 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4078 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4079 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4080 ASSERT_ALLOC(output1, output1_buffer_size);
4081 ASSERT_ALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004082
Gilles Peskine449bd832023-01-11 14:50:10 +01004083 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4084 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004085
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004086 /* The one-shot cipher encryption uses generated iv so validating
4087 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004088 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4089 output1_buffer_size, &output1_length));
4090 TEST_LE_U(output1_length,
4091 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4092 TEST_LE_U(output1_length,
4093 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004094
Gilles Peskine449bd832023-01-11 14:50:10 +01004095 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4096 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004097
Gilles Peskine449bd832023-01-11 14:50:10 +01004098 PSA_ASSERT(psa_cipher_update(&operation,
4099 input->x, input->len,
4100 output2, output2_buffer_size,
4101 &function_output_length));
4102 TEST_LE_U(function_output_length,
4103 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4104 TEST_LE_U(function_output_length,
4105 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004106 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004107
Gilles Peskine449bd832023-01-11 14:50:10 +01004108 PSA_ASSERT(psa_cipher_finish(&operation,
4109 output2 + output2_length,
4110 output2_buffer_size - output2_length,
4111 &function_output_length));
4112 TEST_LE_U(function_output_length,
4113 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4114 TEST_LE_U(function_output_length,
4115 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004116 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004117
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 PSA_ASSERT(psa_cipher_abort(&operation));
4119 ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size,
4120 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004121
Gilles Peskine50e586b2018-06-08 14:28:46 +02004122exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 psa_cipher_abort(&operation);
4124 mbedtls_free(output1);
4125 mbedtls_free(output2);
4126 psa_destroy_key(key);
4127 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004128}
4129/* END_CASE */
4130
4131/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004132void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4133 data_t *key_data, data_t *iv,
4134 data_t *input,
4135 int first_part_size_arg,
4136 int output1_length_arg, int output2_length_arg,
4137 data_t *expected_output,
4138 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004139{
Ronald Cron5425a212020-08-04 14:58:35 +02004140 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004141 psa_key_type_t key_type = key_type_arg;
4142 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004143 psa_status_t status;
4144 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004145 size_t first_part_size = first_part_size_arg;
4146 size_t output1_length = output1_length_arg;
4147 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004148 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004149 size_t output_buffer_size = 0;
4150 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004151 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004152 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004153 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004154
Gilles Peskine449bd832023-01-11 14:50:10 +01004155 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004156
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4158 psa_set_key_algorithm(&attributes, alg);
4159 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004160
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4162 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004163
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004165
Gilles Peskine449bd832023-01-11 14:50:10 +01004166 if (iv->len > 0) {
4167 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004168 }
4169
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4171 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4172 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004173
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 TEST_LE_U(first_part_size, input->len);
4175 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4176 output, output_buffer_size,
4177 &function_output_length));
4178 TEST_ASSERT(function_output_length == output1_length);
4179 TEST_LE_U(function_output_length,
4180 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4181 TEST_LE_U(function_output_length,
4182 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004183 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004184
Gilles Peskine449bd832023-01-11 14:50:10 +01004185 if (first_part_size < input->len) {
4186 PSA_ASSERT(psa_cipher_update(&operation,
4187 input->x + first_part_size,
4188 input->len - first_part_size,
4189 (output_buffer_size == 0 ? NULL :
4190 output + total_output_length),
4191 output_buffer_size - total_output_length,
4192 &function_output_length));
4193 TEST_ASSERT(function_output_length == output2_length);
4194 TEST_LE_U(function_output_length,
4195 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4196 alg,
4197 input->len - first_part_size));
4198 TEST_LE_U(function_output_length,
4199 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004200 total_output_length += function_output_length;
4201 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004202
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 status = psa_cipher_finish(&operation,
4204 (output_buffer_size == 0 ? NULL :
4205 output + total_output_length),
4206 output_buffer_size - total_output_length,
4207 &function_output_length);
4208 TEST_LE_U(function_output_length,
4209 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4210 TEST_LE_U(function_output_length,
4211 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004212 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004214
Gilles Peskine449bd832023-01-11 14:50:10 +01004215 if (expected_status == PSA_SUCCESS) {
4216 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004217
Gilles Peskine449bd832023-01-11 14:50:10 +01004218 ASSERT_COMPARE(expected_output->x, expected_output->len,
4219 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004220 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004221
4222exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 psa_cipher_abort(&operation);
4224 mbedtls_free(output);
4225 psa_destroy_key(key);
4226 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004227}
4228/* END_CASE */
4229
4230/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004231void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4232 data_t *key_data, data_t *iv,
4233 data_t *input,
4234 int first_part_size_arg,
4235 int output1_length_arg, int output2_length_arg,
4236 data_t *expected_output,
4237 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004238{
Ronald Cron5425a212020-08-04 14:58:35 +02004239 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004240 psa_key_type_t key_type = key_type_arg;
4241 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004242 psa_status_t status;
4243 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004244 size_t first_part_size = first_part_size_arg;
4245 size_t output1_length = output1_length_arg;
4246 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004247 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004248 size_t output_buffer_size = 0;
4249 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004250 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004251 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004252 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004253
Gilles Peskine449bd832023-01-11 14:50:10 +01004254 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004255
Gilles Peskine449bd832023-01-11 14:50:10 +01004256 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4257 psa_set_key_algorithm(&attributes, alg);
4258 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004259
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4261 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004262
Gilles Peskine449bd832023-01-11 14:50:10 +01004263 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004264
Gilles Peskine449bd832023-01-11 14:50:10 +01004265 if (iv->len > 0) {
4266 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004267 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004268
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4270 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4271 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004272
Gilles Peskine449bd832023-01-11 14:50:10 +01004273 TEST_LE_U(first_part_size, input->len);
4274 PSA_ASSERT(psa_cipher_update(&operation,
4275 input->x, first_part_size,
4276 output, output_buffer_size,
4277 &function_output_length));
4278 TEST_ASSERT(function_output_length == output1_length);
4279 TEST_LE_U(function_output_length,
4280 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4281 TEST_LE_U(function_output_length,
4282 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004283 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004284
Gilles Peskine449bd832023-01-11 14:50:10 +01004285 if (first_part_size < input->len) {
4286 PSA_ASSERT(psa_cipher_update(&operation,
4287 input->x + first_part_size,
4288 input->len - first_part_size,
4289 (output_buffer_size == 0 ? NULL :
4290 output + total_output_length),
4291 output_buffer_size - total_output_length,
4292 &function_output_length));
4293 TEST_ASSERT(function_output_length == output2_length);
4294 TEST_LE_U(function_output_length,
4295 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4296 alg,
4297 input->len - first_part_size));
4298 TEST_LE_U(function_output_length,
4299 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004300 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004301 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004302
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 status = psa_cipher_finish(&operation,
4304 (output_buffer_size == 0 ? NULL :
4305 output + total_output_length),
4306 output_buffer_size - total_output_length,
4307 &function_output_length);
4308 TEST_LE_U(function_output_length,
4309 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4310 TEST_LE_U(function_output_length,
4311 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004312 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004313 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004314
Gilles Peskine449bd832023-01-11 14:50:10 +01004315 if (expected_status == PSA_SUCCESS) {
4316 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004317
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 ASSERT_COMPARE(expected_output->x, expected_output->len,
4319 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004320 }
4321
Gilles Peskine50e586b2018-06-08 14:28:46 +02004322exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 psa_cipher_abort(&operation);
4324 mbedtls_free(output);
4325 psa_destroy_key(key);
4326 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004327}
4328/* END_CASE */
4329
Gilles Peskine50e586b2018-06-08 14:28:46 +02004330/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004331void cipher_decrypt_fail(int alg_arg,
4332 int key_type_arg,
4333 data_t *key_data,
4334 data_t *iv,
4335 data_t *input_arg,
4336 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004337{
4338 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4339 psa_status_t status;
4340 psa_key_type_t key_type = key_type_arg;
4341 psa_algorithm_t alg = alg_arg;
4342 psa_status_t expected_status = expected_status_arg;
4343 unsigned char *input = NULL;
4344 size_t input_buffer_size = 0;
4345 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004346 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004347 size_t output_buffer_size = 0;
4348 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004349 size_t function_output_length;
4350 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004351 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4352
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 if (PSA_ERROR_BAD_STATE != expected_status) {
4354 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004355
Gilles Peskine449bd832023-01-11 14:50:10 +01004356 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4357 psa_set_key_algorithm(&attributes, alg);
4358 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004359
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4361 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004362 }
4363
4364 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4366 if (input_buffer_size > 0) {
4367 ASSERT_ALLOC(input, input_buffer_size);
4368 memcpy(input, iv->x, iv->len);
4369 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004370 }
4371
Gilles Peskine449bd832023-01-11 14:50:10 +01004372 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4373 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004374
Neil Armstrong66a479f2022-02-07 15:41:19 +01004375 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4377 output_buffer_size, &output_length);
4378 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004379
Neil Armstrong66a479f2022-02-07 15:41:19 +01004380 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004381 status = psa_cipher_decrypt_setup(&operation, key, alg);
4382 if (status == PSA_SUCCESS) {
4383 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4384 input_arg->len) +
4385 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4386 ASSERT_ALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004387
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 if (iv->len > 0) {
4389 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004390
Gilles Peskine449bd832023-01-11 14:50:10 +01004391 if (status != PSA_SUCCESS) {
4392 TEST_EQUAL(status, expected_status);
4393 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004394 }
4395
Gilles Peskine449bd832023-01-11 14:50:10 +01004396 if (status == PSA_SUCCESS) {
4397 status = psa_cipher_update(&operation,
4398 input_arg->x, input_arg->len,
4399 output_multi, output_buffer_size,
4400 &function_output_length);
4401 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004402 output_length = function_output_length;
4403
Gilles Peskine449bd832023-01-11 14:50:10 +01004404 status = psa_cipher_finish(&operation,
4405 output_multi + output_length,
4406 output_buffer_size - output_length,
4407 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004408
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 TEST_EQUAL(status, expected_status);
4410 } else {
4411 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004412 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004413 } else {
4414 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004415 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004416 } else {
4417 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004418 }
4419
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004420exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004421 psa_cipher_abort(&operation);
4422 mbedtls_free(input);
4423 mbedtls_free(output);
4424 mbedtls_free(output_multi);
4425 psa_destroy_key(key);
4426 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004427}
4428/* END_CASE */
4429
4430/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004431void cipher_decrypt(int alg_arg,
4432 int key_type_arg,
4433 data_t *key_data,
4434 data_t *iv,
4435 data_t *input_arg,
4436 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004437{
4438 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4439 psa_key_type_t key_type = key_type_arg;
4440 psa_algorithm_t alg = alg_arg;
4441 unsigned char *input = NULL;
4442 size_t input_buffer_size = 0;
4443 unsigned char *output = NULL;
4444 size_t output_buffer_size = 0;
4445 size_t output_length = 0;
4446 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4447
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004449
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4451 psa_set_key_algorithm(&attributes, alg);
4452 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004453
4454 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4456 if (input_buffer_size > 0) {
4457 ASSERT_ALLOC(input, input_buffer_size);
4458 memcpy(input, iv->x, iv->len);
4459 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004460 }
4461
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4463 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004464
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4466 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004467
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4469 output_buffer_size, &output_length));
4470 TEST_LE_U(output_length,
4471 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4472 TEST_LE_U(output_length,
4473 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004474
Gilles Peskine449bd832023-01-11 14:50:10 +01004475 ASSERT_COMPARE(expected_output->x, expected_output->len,
4476 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004477exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 mbedtls_free(input);
4479 mbedtls_free(output);
4480 psa_destroy_key(key);
4481 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004482}
4483/* END_CASE */
4484
4485/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004486void cipher_verify_output(int alg_arg,
4487 int key_type_arg,
4488 data_t *key_data,
4489 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004490{
Ronald Cron5425a212020-08-04 14:58:35 +02004491 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004492 psa_key_type_t key_type = key_type_arg;
4493 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004494 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004495 size_t output1_size = 0;
4496 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004497 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004498 size_t output2_size = 0;
4499 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004500 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004501
Gilles Peskine449bd832023-01-11 14:50:10 +01004502 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004503
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4505 psa_set_key_algorithm(&attributes, alg);
4506 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004507
Gilles Peskine449bd832023-01-11 14:50:10 +01004508 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4509 &key));
4510 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4511 ASSERT_ALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004512
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4514 output1, output1_size,
4515 &output1_length));
4516 TEST_LE_U(output1_length,
4517 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4518 TEST_LE_U(output1_length,
4519 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004520
4521 output2_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 ASSERT_ALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004523
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4525 output2, output2_size,
4526 &output2_length));
4527 TEST_LE_U(output2_length,
4528 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4529 TEST_LE_U(output2_length,
4530 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004531
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004533
4534exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004535 mbedtls_free(output1);
4536 mbedtls_free(output2);
4537 psa_destroy_key(key);
4538 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004539}
4540/* END_CASE */
4541
4542/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004543void cipher_verify_output_multipart(int alg_arg,
4544 int key_type_arg,
4545 data_t *key_data,
4546 data_t *input,
4547 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004548{
Ronald Cron5425a212020-08-04 14:58:35 +02004549 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004550 psa_key_type_t key_type = key_type_arg;
4551 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004552 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004554 size_t iv_size = 16;
4555 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004556 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004557 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004558 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004559 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004560 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004561 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004562 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004563 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4564 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004565 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004566
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004568
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4570 psa_set_key_algorithm(&attributes, alg);
4571 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004572
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4574 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004575
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4577 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004578
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 if (alg != PSA_ALG_ECB_NO_PADDING) {
4580 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4581 iv, iv_size,
4582 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004583 }
4584
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4586 TEST_LE_U(output1_buffer_size,
4587 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
4588 ASSERT_ALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004589
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004591
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4593 output1, output1_buffer_size,
4594 &function_output_length));
4595 TEST_LE_U(function_output_length,
4596 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4597 TEST_LE_U(function_output_length,
4598 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004599 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004600
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 PSA_ASSERT(psa_cipher_update(&operation1,
4602 input->x + first_part_size,
4603 input->len - first_part_size,
4604 output1, output1_buffer_size,
4605 &function_output_length));
4606 TEST_LE_U(function_output_length,
4607 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4608 alg,
4609 input->len - first_part_size));
4610 TEST_LE_U(function_output_length,
4611 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004612 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004613
Gilles Peskine449bd832023-01-11 14:50:10 +01004614 PSA_ASSERT(psa_cipher_finish(&operation1,
4615 output1 + output1_length,
4616 output1_buffer_size - output1_length,
4617 &function_output_length));
4618 TEST_LE_U(function_output_length,
4619 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4620 TEST_LE_U(function_output_length,
4621 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004622 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004623
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004625
Gilles Peskine048b7f02018-06-08 14:20:49 +02004626 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 TEST_LE_U(output2_buffer_size,
4628 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4629 TEST_LE_U(output2_buffer_size,
4630 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
4631 ASSERT_ALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004632
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 if (iv_length > 0) {
4634 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4635 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004636 }
Moran Pekerded84402018-06-06 16:36:50 +03004637
Gilles Peskine449bd832023-01-11 14:50:10 +01004638 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4639 output2, output2_buffer_size,
4640 &function_output_length));
4641 TEST_LE_U(function_output_length,
4642 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4643 TEST_LE_U(function_output_length,
4644 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004645 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004646
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 PSA_ASSERT(psa_cipher_update(&operation2,
4648 output1 + first_part_size,
4649 output1_length - first_part_size,
4650 output2, output2_buffer_size,
4651 &function_output_length));
4652 TEST_LE_U(function_output_length,
4653 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4654 alg,
4655 output1_length - first_part_size));
4656 TEST_LE_U(function_output_length,
4657 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004658 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004659
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 PSA_ASSERT(psa_cipher_finish(&operation2,
4661 output2 + output2_length,
4662 output2_buffer_size - output2_length,
4663 &function_output_length));
4664 TEST_LE_U(function_output_length,
4665 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4666 TEST_LE_U(function_output_length,
4667 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004668 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004669
Gilles Peskine449bd832023-01-11 14:50:10 +01004670 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004671
Gilles Peskine449bd832023-01-11 14:50:10 +01004672 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004673
4674exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004675 psa_cipher_abort(&operation1);
4676 psa_cipher_abort(&operation2);
4677 mbedtls_free(output1);
4678 mbedtls_free(output2);
4679 psa_destroy_key(key);
4680 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004681}
4682/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004683
Gilles Peskine20035e32018-02-03 22:44:14 +01004684/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004685void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4686 int alg_arg,
4687 data_t *nonce,
4688 data_t *additional_data,
4689 data_t *input_data,
4690 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004691{
Ronald Cron5425a212020-08-04 14:58:35 +02004692 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004693 psa_key_type_t key_type = key_type_arg;
4694 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004695 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004696 unsigned char *output_data = NULL;
4697 size_t output_size = 0;
4698 size_t output_length = 0;
4699 unsigned char *output_data2 = NULL;
4700 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004701 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004702 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004703 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004704
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004706
Gilles Peskine449bd832023-01-11 14:50:10 +01004707 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4708 psa_set_key_algorithm(&attributes, alg);
4709 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004710
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4712 &key));
4713 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4714 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004715
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4717 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004718 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4719 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4721 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4722 TEST_EQUAL(output_size,
4723 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4724 TEST_LE_U(output_size,
4725 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004726 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004728
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 status = psa_aead_encrypt(key, alg,
4730 nonce->x, nonce->len,
4731 additional_data->x,
4732 additional_data->len,
4733 input_data->x, input_data->len,
4734 output_data, output_size,
4735 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004736
4737 /* If the operation is not supported, just skip and not fail in case the
4738 * encryption involves a common limitation of cryptography hardwares and
4739 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 if (status == PSA_ERROR_NOT_SUPPORTED) {
4741 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4742 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004743 }
4744
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004746
Gilles Peskine449bd832023-01-11 14:50:10 +01004747 if (PSA_SUCCESS == expected_result) {
4748 ASSERT_ALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004749
Gilles Peskine003a4a92019-05-14 16:09:40 +02004750 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4751 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 TEST_EQUAL(input_data->len,
4753 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004754
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 TEST_LE_U(input_data->len,
4756 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004757
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 TEST_EQUAL(psa_aead_decrypt(key, alg,
4759 nonce->x, nonce->len,
4760 additional_data->x,
4761 additional_data->len,
4762 output_data, output_length,
4763 output_data2, output_length,
4764 &output_length2),
4765 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004766
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 ASSERT_COMPARE(input_data->x, input_data->len,
4768 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004769 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004770
Gilles Peskinea1cac842018-06-11 19:33:02 +02004771exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 psa_destroy_key(key);
4773 mbedtls_free(output_data);
4774 mbedtls_free(output_data2);
4775 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004776}
4777/* END_CASE */
4778
4779/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004780void aead_encrypt(int key_type_arg, data_t *key_data,
4781 int alg_arg,
4782 data_t *nonce,
4783 data_t *additional_data,
4784 data_t *input_data,
4785 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004786{
Ronald Cron5425a212020-08-04 14:58:35 +02004787 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004788 psa_key_type_t key_type = key_type_arg;
4789 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004790 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004791 unsigned char *output_data = NULL;
4792 size_t output_size = 0;
4793 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004794 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004795 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004796
Gilles Peskine449bd832023-01-11 14:50:10 +01004797 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004798
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4800 psa_set_key_algorithm(&attributes, alg);
4801 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004802
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4804 &key));
4805 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4806 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004807
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4809 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004810 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4811 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 TEST_EQUAL(output_size,
4813 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4814 TEST_LE_U(output_size,
4815 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
4816 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004817
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 status = psa_aead_encrypt(key, alg,
4819 nonce->x, nonce->len,
4820 additional_data->x, additional_data->len,
4821 input_data->x, input_data->len,
4822 output_data, output_size,
4823 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004824
Ronald Cron28a45ed2021-02-09 20:35:42 +01004825 /* If the operation is not supported, just skip and not fail in case the
4826 * encryption involves a common limitation of cryptography hardwares and
4827 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004828 if (status == PSA_ERROR_NOT_SUPPORTED) {
4829 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4830 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004831 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004832
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 PSA_ASSERT(status);
4834 ASSERT_COMPARE(expected_result->x, expected_result->len,
4835 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004836
Gilles Peskinea1cac842018-06-11 19:33:02 +02004837exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004838 psa_destroy_key(key);
4839 mbedtls_free(output_data);
4840 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004841}
4842/* END_CASE */
4843
4844/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004845void aead_decrypt(int key_type_arg, data_t *key_data,
4846 int alg_arg,
4847 data_t *nonce,
4848 data_t *additional_data,
4849 data_t *input_data,
4850 data_t *expected_data,
4851 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004852{
Ronald Cron5425a212020-08-04 14:58:35 +02004853 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004854 psa_key_type_t key_type = key_type_arg;
4855 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004856 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004857 unsigned char *output_data = NULL;
4858 size_t output_size = 0;
4859 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004860 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004861 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004862 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004863
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004865
Gilles Peskine449bd832023-01-11 14:50:10 +01004866 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4867 psa_set_key_algorithm(&attributes, alg);
4868 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004869
Gilles Peskine449bd832023-01-11 14:50:10 +01004870 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4871 &key));
4872 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4873 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004874
Gilles Peskine449bd832023-01-11 14:50:10 +01004875 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4876 alg);
4877 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4878 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004879 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4880 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 TEST_EQUAL(output_size,
4882 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4883 TEST_LE_U(output_size,
4884 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004885 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004887
Gilles Peskine449bd832023-01-11 14:50:10 +01004888 status = psa_aead_decrypt(key, alg,
4889 nonce->x, nonce->len,
4890 additional_data->x,
4891 additional_data->len,
4892 input_data->x, input_data->len,
4893 output_data, output_size,
4894 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004895
Ronald Cron28a45ed2021-02-09 20:35:42 +01004896 /* If the operation is not supported, just skip and not fail in case the
4897 * decryption involves a common limitation of cryptography hardwares and
4898 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 if (status == PSA_ERROR_NOT_SUPPORTED) {
4900 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4901 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004902 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004903
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004905
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 if (expected_result == PSA_SUCCESS) {
4907 ASSERT_COMPARE(expected_data->x, expected_data->len,
4908 output_data, output_length);
4909 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004910
Gilles Peskinea1cac842018-06-11 19:33:02 +02004911exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 psa_destroy_key(key);
4913 mbedtls_free(output_data);
4914 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004915}
4916/* END_CASE */
4917
4918/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004919void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4920 int alg_arg,
4921 data_t *nonce,
4922 data_t *additional_data,
4923 data_t *input_data,
4924 int do_set_lengths,
4925 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004926{
Paul Elliottd3f82412021-06-16 16:52:21 +01004927 size_t ad_part_len = 0;
4928 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004929 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004930
Gilles Peskine449bd832023-01-11 14:50:10 +01004931 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4932 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004933
Gilles Peskine449bd832023-01-11 14:50:10 +01004934 if (do_set_lengths) {
4935 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004936 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004937 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004938 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004939 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004940 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004941
4942 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 if (!aead_multipart_internal_func(key_type_arg, key_data,
4944 alg_arg, nonce,
4945 additional_data,
4946 ad_part_len,
4947 input_data, -1,
4948 set_lengths_method,
4949 expected_output,
4950 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004951 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004952 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004953
Gilles Peskine449bd832023-01-11 14:50:10 +01004954 /* length(0) part, length(ad_part_len) part, length(0) part... */
4955 mbedtls_test_set_step(1000 + ad_part_len);
4956
4957 if (!aead_multipart_internal_func(key_type_arg, key_data,
4958 alg_arg, nonce,
4959 additional_data,
4960 ad_part_len,
4961 input_data, -1,
4962 set_lengths_method,
4963 expected_output,
4964 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004965 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004966 }
4967 }
4968
4969 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4970 /* Split data into length(data_part_len) parts. */
4971 mbedtls_test_set_step(2000 + data_part_len);
4972
4973 if (do_set_lengths) {
4974 if (data_part_len & 0x01) {
4975 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4976 } else {
4977 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4978 }
4979 }
4980
4981 if (!aead_multipart_internal_func(key_type_arg, key_data,
4982 alg_arg, nonce,
4983 additional_data, -1,
4984 input_data, data_part_len,
4985 set_lengths_method,
4986 expected_output,
4987 1, 0)) {
4988 break;
4989 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004990
4991 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01004992 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004993
Gilles Peskine449bd832023-01-11 14:50:10 +01004994 if (!aead_multipart_internal_func(key_type_arg, key_data,
4995 alg_arg, nonce,
4996 additional_data, -1,
4997 input_data, data_part_len,
4998 set_lengths_method,
4999 expected_output,
5000 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005001 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005003 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005004
Paul Elliott8fc45162021-06-23 16:06:01 +01005005 /* Goto is required to silence warnings about unused labels, as we
5006 * don't actually do any test assertions in this function. */
5007 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005008}
5009/* END_CASE */
5010
5011/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005012void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5013 int alg_arg,
5014 data_t *nonce,
5015 data_t *additional_data,
5016 data_t *input_data,
5017 int do_set_lengths,
5018 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005019{
Paul Elliottd3f82412021-06-16 16:52:21 +01005020 size_t ad_part_len = 0;
5021 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005022 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005023
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005025 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005026 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005027
Gilles Peskine449bd832023-01-11 14:50:10 +01005028 if (do_set_lengths) {
5029 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005030 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005031 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005032 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005034 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005035
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 if (!aead_multipart_internal_func(key_type_arg, key_data,
5037 alg_arg, nonce,
5038 additional_data,
5039 ad_part_len,
5040 input_data, -1,
5041 set_lengths_method,
5042 expected_output,
5043 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005044 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005046
5047 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005049
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 if (!aead_multipart_internal_func(key_type_arg, key_data,
5051 alg_arg, nonce,
5052 additional_data,
5053 ad_part_len,
5054 input_data, -1,
5055 set_lengths_method,
5056 expected_output,
5057 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005058 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005059 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005060 }
5061
Gilles Peskine449bd832023-01-11 14:50:10 +01005062 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005063 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005065
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 if (do_set_lengths) {
5067 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005068 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005070 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005072 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005073
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 if (!aead_multipart_internal_func(key_type_arg, key_data,
5075 alg_arg, nonce,
5076 additional_data, -1,
5077 input_data, data_part_len,
5078 set_lengths_method,
5079 expected_output,
5080 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005081 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005082 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005083
5084 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005085 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005086
Gilles Peskine449bd832023-01-11 14:50:10 +01005087 if (!aead_multipart_internal_func(key_type_arg, key_data,
5088 alg_arg, nonce,
5089 additional_data, -1,
5090 input_data, data_part_len,
5091 set_lengths_method,
5092 expected_output,
5093 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005094 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005096 }
5097
Paul Elliott8fc45162021-06-23 16:06:01 +01005098 /* Goto is required to silence warnings about unused labels, as we
5099 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005100 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005101}
5102/* END_CASE */
5103
5104/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005105void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5106 int alg_arg,
5107 int nonce_length,
5108 int expected_nonce_length_arg,
5109 data_t *additional_data,
5110 data_t *input_data,
5111 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005112{
5113
5114 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5115 psa_key_type_t key_type = key_type_arg;
5116 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005117 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005118 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5119 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5120 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005121 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005122 size_t actual_nonce_length = 0;
5123 size_t expected_nonce_length = expected_nonce_length_arg;
5124 unsigned char *output = NULL;
5125 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005126 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005127 size_t ciphertext_size = 0;
5128 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005129 size_t tag_length = 0;
5130 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005131
Gilles Peskine449bd832023-01-11 14:50:10 +01005132 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005133
Gilles Peskine449bd832023-01-11 14:50:10 +01005134 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5135 psa_set_key_algorithm(&attributes, alg);
5136 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005137
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5139 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005140
Gilles Peskine449bd832023-01-11 14:50:10 +01005141 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005142
Gilles Peskine449bd832023-01-11 14:50:10 +01005143 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005144
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 ASSERT_ALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005146
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005148
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005150
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005152
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005154
5155 /* If the operation is not supported, just skip and not fail in case the
5156 * encryption involves a common limitation of cryptography hardwares and
5157 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 if (status == PSA_ERROR_NOT_SUPPORTED) {
5159 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5160 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005161 }
5162
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005164
Gilles Peskine449bd832023-01-11 14:50:10 +01005165 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5166 nonce_length,
5167 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005168
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005170
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005172
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 if (expected_status == PSA_SUCCESS) {
5174 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5175 alg));
5176 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005177
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005179
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005181 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5183 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005184
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5186 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005187
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5189 output, output_size,
5190 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005191
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5193 &ciphertext_length, tag_buffer,
5194 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005195 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005196
5197exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 psa_destroy_key(key);
5199 mbedtls_free(output);
5200 mbedtls_free(ciphertext);
5201 psa_aead_abort(&operation);
5202 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005203}
5204/* END_CASE */
5205
5206/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005207void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5208 int alg_arg,
5209 int nonce_length_arg,
5210 int set_lengths_method_arg,
5211 data_t *additional_data,
5212 data_t *input_data,
5213 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005214{
5215
5216 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5217 psa_key_type_t key_type = key_type_arg;
5218 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005219 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005220 uint8_t *nonce_buffer = NULL;
5221 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5222 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5223 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005224 unsigned char *output = NULL;
5225 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005226 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005227 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005228 size_t ciphertext_size = 0;
5229 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005230 size_t tag_length = 0;
5231 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005232 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005233 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005234
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005236
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5238 psa_set_key_algorithm(&attributes, alg);
5239 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005240
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5242 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005243
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005245
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005247
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 ASSERT_ALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005249
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005251
Gilles Peskine449bd832023-01-11 14:50:10 +01005252 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005253
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005255
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005257
5258 /* If the operation is not supported, just skip and not fail in case the
5259 * encryption involves a common limitation of cryptography hardwares and
5260 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 if (status == PSA_ERROR_NOT_SUPPORTED) {
5262 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5263 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005264 }
5265
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005267
Paul Elliott4023ffd2021-09-10 16:21:22 +01005268 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005269 if (nonce_length_arg == -1) {
5270 /* Arbitrary size buffer, to test zero length valid buffer. */
5271 ASSERT_ALLOC(nonce_buffer, 4);
5272 nonce_length = 0;
5273 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005274 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 nonce_length = (size_t) nonce_length_arg;
5276 ASSERT_ALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005277
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 if (nonce_buffer) {
5279 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005280 nonce_buffer[index] = 'a' + index;
5281 }
Paul Elliott66696b52021-08-16 18:42:41 +01005282 }
Paul Elliott863864a2021-07-23 17:28:31 +01005283 }
5284
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5286 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5287 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005288 }
5289
Gilles Peskine449bd832023-01-11 14:50:10 +01005290 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005291
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005293
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 if (expected_status == PSA_SUCCESS) {
5295 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5296 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5297 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005298 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005299 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005300 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 }
Paul Elliott863864a2021-07-23 17:28:31 +01005302
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005303 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5305 additional_data->len),
5306 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005307
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5309 output, output_size,
5310 &ciphertext_length),
5311 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005312
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5314 &ciphertext_length, tag_buffer,
5315 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5316 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005317 }
5318
5319exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 psa_destroy_key(key);
5321 mbedtls_free(output);
5322 mbedtls_free(ciphertext);
5323 mbedtls_free(nonce_buffer);
5324 psa_aead_abort(&operation);
5325 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005326}
5327/* END_CASE */
5328
5329/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005330void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005331 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005332 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005333 data_t *nonce,
5334 data_t *additional_data,
5335 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005337{
5338
5339 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5340 psa_key_type_t key_type = key_type_arg;
5341 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005342 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5344 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5345 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005346 unsigned char *output = NULL;
5347 unsigned char *ciphertext = NULL;
5348 size_t output_size = output_size_arg;
5349 size_t ciphertext_size = 0;
5350 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005351 size_t tag_length = 0;
5352 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5353
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005355
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5357 psa_set_key_algorithm(&attributes, alg);
5358 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005359
Gilles Peskine449bd832023-01-11 14:50:10 +01005360 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5361 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005362
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005364
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 ASSERT_ALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005366
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005368
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005370
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005372
5373 /* If the operation is not supported, just skip and not fail in case the
5374 * encryption involves a common limitation of cryptography hardwares and
5375 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 if (status == PSA_ERROR_NOT_SUPPORTED) {
5377 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5378 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005379 }
5380
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005382
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5384 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005385
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005387
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5389 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005390
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 status = psa_aead_update(&operation, input_data->x, input_data->len,
5392 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005393
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005395
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005397 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005398 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5399 &ciphertext_length, tag_buffer,
5400 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005401 }
5402
5403exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 psa_destroy_key(key);
5405 mbedtls_free(output);
5406 mbedtls_free(ciphertext);
5407 psa_aead_abort(&operation);
5408 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005409}
5410/* END_CASE */
5411
Paul Elliott91b021e2021-07-23 18:52:31 +01005412/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005413void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5414 int alg_arg,
5415 int finish_ciphertext_size_arg,
5416 int tag_size_arg,
5417 data_t *nonce,
5418 data_t *additional_data,
5419 data_t *input_data,
5420 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005421{
5422
5423 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5424 psa_key_type_t key_type = key_type_arg;
5425 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005426 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005427 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5428 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5429 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005430 unsigned char *ciphertext = NULL;
5431 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005432 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005433 size_t ciphertext_size = 0;
5434 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5436 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005437 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005438
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005440
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5442 psa_set_key_algorithm(&attributes, alg);
5443 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005444
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5446 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005447
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005449
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005451
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005453
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005455
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 ASSERT_ALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005457
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005459
5460 /* If the operation is not supported, just skip and not fail in case the
5461 * encryption involves a common limitation of cryptography hardwares and
5462 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005463 if (status == PSA_ERROR_NOT_SUPPORTED) {
5464 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5465 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005466 }
5467
Gilles Peskine449bd832023-01-11 14:50:10 +01005468 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005469
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005471
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5473 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005474
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5476 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005477
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5479 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005480
5481 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 status = psa_aead_finish(&operation, finish_ciphertext,
5483 finish_ciphertext_size,
5484 &ciphertext_length, tag_buffer,
5485 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005486
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005488
5489exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005490 psa_destroy_key(key);
5491 mbedtls_free(ciphertext);
5492 mbedtls_free(finish_ciphertext);
5493 mbedtls_free(tag_buffer);
5494 psa_aead_abort(&operation);
5495 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005496}
5497/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005498
5499/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005500void aead_multipart_verify(int key_type_arg, data_t *key_data,
5501 int alg_arg,
5502 data_t *nonce,
5503 data_t *additional_data,
5504 data_t *input_data,
5505 data_t *tag,
5506 int tag_usage_arg,
5507 int expected_setup_status_arg,
5508 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005509{
5510 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5511 psa_key_type_t key_type = key_type_arg;
5512 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005513 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005514 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5515 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5516 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005517 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005518 unsigned char *plaintext = NULL;
5519 unsigned char *finish_plaintext = NULL;
5520 size_t plaintext_size = 0;
5521 size_t plaintext_length = 0;
5522 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005523 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005524 unsigned char *tag_buffer = NULL;
5525 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005526
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005528
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5530 psa_set_key_algorithm(&attributes, alg);
5531 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005532
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5534 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005535
Gilles Peskine449bd832023-01-11 14:50:10 +01005536 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005537
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5539 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005540
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 ASSERT_ALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005542
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005544
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 ASSERT_ALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005546
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005548
5549 /* If the operation is not supported, just skip and not fail in case the
5550 * encryption involves a common limitation of cryptography hardwares and
5551 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 if (status == PSA_ERROR_NOT_SUPPORTED) {
5553 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5554 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005555 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005556 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005557
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005559 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 }
Paul Elliott9961a662021-09-17 19:19:02 +01005561
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005563
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005565
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 status = psa_aead_set_lengths(&operation, additional_data->len,
5567 input_data->len);
5568 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005569
Gilles Peskine449bd832023-01-11 14:50:10 +01005570 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5571 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005572
Gilles Peskine449bd832023-01-11 14:50:10 +01005573 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5574 input_data->len,
5575 plaintext, plaintext_size,
5576 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005577
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005579 tag_buffer = tag->x;
5580 tag_size = tag->len;
5581 }
5582
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 status = psa_aead_verify(&operation, finish_plaintext,
5584 verify_plaintext_size,
5585 &plaintext_length,
5586 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005587
Gilles Peskine449bd832023-01-11 14:50:10 +01005588 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005589
5590exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005591 psa_destroy_key(key);
5592 mbedtls_free(plaintext);
5593 mbedtls_free(finish_plaintext);
5594 psa_aead_abort(&operation);
5595 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005596}
5597/* END_CASE */
5598
Paul Elliott9961a662021-09-17 19:19:02 +01005599/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005600void aead_multipart_setup(int key_type_arg, data_t *key_data,
5601 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005602{
5603 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5604 psa_key_type_t key_type = key_type_arg;
5605 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005606 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005607 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5608 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5609 psa_status_t expected_status = expected_status_arg;
5610
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005612
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 psa_set_key_usage_flags(&attributes,
5614 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5615 psa_set_key_algorithm(&attributes, alg);
5616 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005617
Gilles Peskine449bd832023-01-11 14:50:10 +01005618 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5619 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005620
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005622
Gilles Peskine449bd832023-01-11 14:50:10 +01005623 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005624
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005626
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005628
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005630
5631exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 psa_destroy_key(key);
5633 psa_aead_abort(&operation);
5634 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005635}
5636/* END_CASE */
5637
5638/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005639void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5640 int alg_arg,
5641 data_t *nonce,
5642 data_t *additional_data,
5643 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005644{
5645 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5646 psa_key_type_t key_type = key_type_arg;
5647 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005648 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005649 unsigned char *output_data = NULL;
5650 unsigned char *final_data = NULL;
5651 size_t output_size = 0;
5652 size_t finish_output_size = 0;
5653 size_t output_length = 0;
5654 size_t key_bits = 0;
5655 size_t tag_length = 0;
5656 size_t tag_size = 0;
5657 size_t nonce_length = 0;
5658 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5659 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5660 size_t output_part_length = 0;
5661 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5662
Gilles Peskine449bd832023-01-11 14:50:10 +01005663 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005664
Gilles Peskine449bd832023-01-11 14:50:10 +01005665 psa_set_key_usage_flags(&attributes,
5666 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5667 psa_set_key_algorithm(&attributes, alg);
5668 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005669
Gilles Peskine449bd832023-01-11 14:50:10 +01005670 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5671 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005672
Gilles Peskine449bd832023-01-11 14:50:10 +01005673 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5674 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005675
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005677
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005679
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005681
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 ASSERT_ALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005683
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005685
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005687
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 ASSERT_ALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005689
5690 /* Test all operations error without calling setup first. */
5691
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5693 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005694
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005696
Gilles Peskine449bd832023-01-11 14:50:10 +01005697 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5698 PSA_AEAD_NONCE_MAX_SIZE,
5699 &nonce_length),
5700 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005701
Gilles Peskine449bd832023-01-11 14:50:10 +01005702 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005703
Paul Elliott481be342021-07-16 17:38:47 +01005704 /* ------------------------------------------------------- */
5705
Gilles Peskine449bd832023-01-11 14:50:10 +01005706 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5707 input_data->len),
5708 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005709
Gilles Peskine449bd832023-01-11 14:50:10 +01005710 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005711
Paul Elliott481be342021-07-16 17:38:47 +01005712 /* ------------------------------------------------------- */
5713
Gilles Peskine449bd832023-01-11 14:50:10 +01005714 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5715 additional_data->len),
5716 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005717
Gilles Peskine449bd832023-01-11 14:50:10 +01005718 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005719
Paul Elliott481be342021-07-16 17:38:47 +01005720 /* ------------------------------------------------------- */
5721
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5723 input_data->len, output_data,
5724 output_size, &output_length),
5725 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005726
Gilles Peskine449bd832023-01-11 14:50:10 +01005727 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005728
Paul Elliott481be342021-07-16 17:38:47 +01005729 /* ------------------------------------------------------- */
5730
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5732 finish_output_size,
5733 &output_part_length,
5734 tag_buffer, tag_length,
5735 &tag_size),
5736 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005737
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005739
Paul Elliott481be342021-07-16 17:38:47 +01005740 /* ------------------------------------------------------- */
5741
Gilles Peskine449bd832023-01-11 14:50:10 +01005742 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5743 finish_output_size,
5744 &output_part_length,
5745 tag_buffer,
5746 tag_length),
5747 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005748
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005750
5751 /* Test for double setups. */
5752
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005754
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5756 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005757
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005759
Paul Elliott481be342021-07-16 17:38:47 +01005760 /* ------------------------------------------------------- */
5761
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005763
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5765 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005766
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005768
Paul Elliott374a2be2021-07-16 17:53:40 +01005769 /* ------------------------------------------------------- */
5770
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005772
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5774 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005775
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005777
5778 /* ------------------------------------------------------- */
5779
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005781
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5783 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005784
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005786
Paul Elliottc23a9a02021-06-21 18:32:46 +01005787 /* Test for not setting a nonce. */
5788
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005790
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5792 additional_data->len),
5793 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005794
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005796
Paul Elliott7f628422021-09-01 12:08:29 +01005797 /* ------------------------------------------------------- */
5798
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005800
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5802 input_data->len, output_data,
5803 output_size, &output_length),
5804 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005805
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005807
Paul Elliottbdc2c682021-09-21 18:37:10 +01005808 /* ------------------------------------------------------- */
5809
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005811
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5813 finish_output_size,
5814 &output_part_length,
5815 tag_buffer, tag_length,
5816 &tag_size),
5817 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005818
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005820
5821 /* ------------------------------------------------------- */
5822
Gilles Peskine449bd832023-01-11 14:50:10 +01005823 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005824
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5826 finish_output_size,
5827 &output_part_length,
5828 tag_buffer,
5829 tag_length),
5830 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005831
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005833
Paul Elliottc23a9a02021-06-21 18:32:46 +01005834 /* Test for double setting nonce. */
5835
Gilles Peskine449bd832023-01-11 14:50:10 +01005836 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005837
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005839
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5841 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005842
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005844
Paul Elliott374a2be2021-07-16 17:53:40 +01005845 /* Test for double generating nonce. */
5846
Gilles Peskine449bd832023-01-11 14:50:10 +01005847 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005848
Gilles Peskine449bd832023-01-11 14:50:10 +01005849 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5850 PSA_AEAD_NONCE_MAX_SIZE,
5851 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005852
Gilles Peskine449bd832023-01-11 14:50:10 +01005853 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5854 PSA_AEAD_NONCE_MAX_SIZE,
5855 &nonce_length),
5856 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005857
5858
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005860
5861 /* Test for generate nonce then set and vice versa */
5862
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005864
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5866 PSA_AEAD_NONCE_MAX_SIZE,
5867 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005868
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5870 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005871
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005873
Andrzej Kurekad837522021-12-15 15:28:49 +01005874 /* Test for generating nonce after calling set lengths */
5875
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005877
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5879 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005880
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5882 PSA_AEAD_NONCE_MAX_SIZE,
5883 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005884
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005886
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005887 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005888
Gilles Peskine449bd832023-01-11 14:50:10 +01005889 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005890
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 if (operation.alg == PSA_ALG_CCM) {
5892 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5893 input_data->len),
5894 PSA_ERROR_INVALID_ARGUMENT);
5895 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5896 PSA_AEAD_NONCE_MAX_SIZE,
5897 &nonce_length),
5898 PSA_ERROR_BAD_STATE);
5899 } else {
5900 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5901 input_data->len));
5902 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5903 PSA_AEAD_NONCE_MAX_SIZE,
5904 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005905 }
5906
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005908
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005909 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005910#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005911 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005912
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5914 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5915 input_data->len),
5916 PSA_ERROR_INVALID_ARGUMENT);
5917 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5918 PSA_AEAD_NONCE_MAX_SIZE,
5919 &nonce_length),
5920 PSA_ERROR_BAD_STATE);
5921 } else {
5922 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5923 input_data->len));
5924 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5925 PSA_AEAD_NONCE_MAX_SIZE,
5926 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005927 }
5928
Gilles Peskine449bd832023-01-11 14:50:10 +01005929 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005930#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005931
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005932 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005933
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005935
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5937 PSA_AEAD_NONCE_MAX_SIZE,
5938 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005939
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 if (operation.alg == PSA_ALG_CCM) {
5941 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5942 input_data->len),
5943 PSA_ERROR_INVALID_ARGUMENT);
5944 } else {
5945 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5946 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005947 }
5948
Gilles Peskine449bd832023-01-11 14:50:10 +01005949 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005950
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005951 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005952 /* Test for setting nonce after calling set lengths */
5953
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005955
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5957 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005958
Gilles Peskine449bd832023-01-11 14:50:10 +01005959 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005960
Gilles Peskine449bd832023-01-11 14:50:10 +01005961 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005962
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005963 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005964
Gilles Peskine449bd832023-01-11 14:50:10 +01005965 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005966
Gilles Peskine449bd832023-01-11 14:50:10 +01005967 if (operation.alg == PSA_ALG_CCM) {
5968 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5969 input_data->len),
5970 PSA_ERROR_INVALID_ARGUMENT);
5971 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5972 PSA_ERROR_BAD_STATE);
5973 } else {
5974 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5975 input_data->len));
5976 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005977 }
5978
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005980
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005981 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005982#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005984
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5986 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5987 input_data->len),
5988 PSA_ERROR_INVALID_ARGUMENT);
5989 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5990 PSA_ERROR_BAD_STATE);
5991 } else {
5992 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5993 input_data->len));
5994 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005995 }
5996
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00005998#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005999
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006000 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006001
Gilles Peskine449bd832023-01-11 14:50:10 +01006002 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006003
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006005
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 if (operation.alg == PSA_ALG_CCM) {
6007 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6008 input_data->len),
6009 PSA_ERROR_INVALID_ARGUMENT);
6010 } else {
6011 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6012 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006013 }
6014
Gilles Peskine449bd832023-01-11 14:50:10 +01006015 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006016
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006017 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006018#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006020
Gilles Peskine449bd832023-01-11 14:50:10 +01006021 if (operation.alg == PSA_ALG_GCM) {
6022 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6023 SIZE_MAX),
6024 PSA_ERROR_INVALID_ARGUMENT);
6025 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6026 PSA_ERROR_BAD_STATE);
6027 } else if (operation.alg != PSA_ALG_CCM) {
6028 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6029 SIZE_MAX));
6030 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006031 }
6032
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006034#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006035
Tom Cosgrove1797b052022-12-04 17:19:59 +00006036 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006037#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006038 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006039
Gilles Peskine449bd832023-01-11 14:50:10 +01006040 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006041
Gilles Peskine449bd832023-01-11 14:50:10 +01006042 if (operation.alg == PSA_ALG_GCM) {
6043 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6044 SIZE_MAX),
6045 PSA_ERROR_INVALID_ARGUMENT);
6046 } else if (operation.alg != PSA_ALG_CCM) {
6047 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6048 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006049 }
6050
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006052#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006053
6054 /* ------------------------------------------------------- */
6055
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006057
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006059
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6061 PSA_AEAD_NONCE_MAX_SIZE,
6062 &nonce_length),
6063 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006064
Gilles Peskine449bd832023-01-11 14:50:10 +01006065 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006066
Paul Elliott7220cae2021-06-22 17:25:57 +01006067 /* Test for generating nonce in decrypt setup. */
6068
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006070
Gilles Peskine449bd832023-01-11 14:50:10 +01006071 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6072 PSA_AEAD_NONCE_MAX_SIZE,
6073 &nonce_length),
6074 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006075
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006077
Paul Elliottc23a9a02021-06-21 18:32:46 +01006078 /* Test for setting lengths twice. */
6079
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006081
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006083
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6085 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006086
Gilles Peskine449bd832023-01-11 14:50:10 +01006087 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6088 input_data->len),
6089 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006090
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006092
Andrzej Kurekad837522021-12-15 15:28:49 +01006093 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006094
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006096
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006098
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006100
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6102 additional_data->len),
6103 PSA_ERROR_BAD_STATE);
6104 } else {
6105 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6106 additional_data->len));
6107
6108 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6109 input_data->len),
6110 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006111 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006113
6114 /* ------------------------------------------------------- */
6115
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006117
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006119
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 if (operation.alg == PSA_ALG_CCM) {
6121 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6122 input_data->len, output_data,
6123 output_size, &output_length),
6124 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006125
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 } else {
6127 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6128 input_data->len, output_data,
6129 output_size, &output_length));
6130
6131 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6132 input_data->len),
6133 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006134 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006136
6137 /* ------------------------------------------------------- */
6138
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006140
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006142
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 if (operation.alg == PSA_ALG_CCM) {
6144 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6145 finish_output_size,
6146 &output_part_length,
6147 tag_buffer, tag_length,
6148 &tag_size));
6149 } else {
6150 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6151 finish_output_size,
6152 &output_part_length,
6153 tag_buffer, tag_length,
6154 &tag_size));
6155
6156 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6157 input_data->len),
6158 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006159 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006161
6162 /* Test for setting lengths after generating nonce + already starting data. */
6163
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006165
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6167 PSA_AEAD_NONCE_MAX_SIZE,
6168 &nonce_length));
6169 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006170
Gilles Peskine449bd832023-01-11 14:50:10 +01006171 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6172 additional_data->len),
6173 PSA_ERROR_BAD_STATE);
6174 } else {
6175 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6176 additional_data->len));
6177
6178 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6179 input_data->len),
6180 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006181 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006183
6184 /* ------------------------------------------------------- */
6185
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006187
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6189 PSA_AEAD_NONCE_MAX_SIZE,
6190 &nonce_length));
6191 if (operation.alg == PSA_ALG_CCM) {
6192 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6193 input_data->len, output_data,
6194 output_size, &output_length),
6195 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006196
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 } else {
6198 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6199 input_data->len, output_data,
6200 output_size, &output_length));
6201
6202 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6203 input_data->len),
6204 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006205 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006207
6208 /* ------------------------------------------------------- */
6209
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006211
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6213 PSA_AEAD_NONCE_MAX_SIZE,
6214 &nonce_length));
6215 if (operation.alg == PSA_ALG_CCM) {
6216 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6217 finish_output_size,
6218 &output_part_length,
6219 tag_buffer, tag_length,
6220 &tag_size));
6221 } else {
6222 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6223 finish_output_size,
6224 &output_part_length,
6225 tag_buffer, tag_length,
6226 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006227
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6229 input_data->len),
6230 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006231 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006233
Paul Elliott243080c2021-07-21 19:01:17 +01006234 /* Test for not sending any additional data or data after setting non zero
6235 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006236
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006238
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006240
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6242 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006243
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6245 finish_output_size,
6246 &output_part_length,
6247 tag_buffer, tag_length,
6248 &tag_size),
6249 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006250
Gilles Peskine449bd832023-01-11 14:50:10 +01006251 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006252
Paul Elliott243080c2021-07-21 19:01:17 +01006253 /* Test for not sending any additional data or data after setting non-zero
6254 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006257
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6261 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006262
Gilles Peskine449bd832023-01-11 14:50:10 +01006263 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6264 finish_output_size,
6265 &output_part_length,
6266 tag_buffer,
6267 tag_length),
6268 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006269
Gilles Peskine449bd832023-01-11 14:50:10 +01006270 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006271
Paul Elliott243080c2021-07-21 19:01:17 +01006272 /* Test for not sending any additional data after setting a non-zero length
6273 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006274
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006276
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006278
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6280 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006281
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6283 input_data->len, output_data,
6284 output_size, &output_length),
6285 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006286
Gilles Peskine449bd832023-01-11 14:50:10 +01006287 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006288
Paul Elliottf94bd992021-09-19 18:15:59 +01006289 /* Test for not sending any data after setting a non-zero length for it.*/
6290
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006292
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006294
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6296 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006297
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6299 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006300
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6302 finish_output_size,
6303 &output_part_length,
6304 tag_buffer, tag_length,
6305 &tag_size),
6306 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006307
Gilles Peskine449bd832023-01-11 14:50:10 +01006308 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006309
Paul Elliottb0450fe2021-09-01 15:06:26 +01006310 /* Test for sending too much additional data after setting lengths. */
6311
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006313
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006315
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006317
6318
Gilles Peskine449bd832023-01-11 14:50:10 +01006319 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6320 additional_data->len),
6321 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006322
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006324
Paul Elliotta2a09b02021-09-22 14:56:40 +01006325 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006326
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006328
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006330
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6332 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006333
Gilles Peskine449bd832023-01-11 14:50:10 +01006334 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6335 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006336
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6338 1),
6339 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006340
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006342
Paul Elliottb0450fe2021-09-01 15:06:26 +01006343 /* Test for sending too much data after setting lengths. */
6344
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006346
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006348
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006350
Gilles Peskine449bd832023-01-11 14:50:10 +01006351 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6352 input_data->len, output_data,
6353 output_size, &output_length),
6354 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006355
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006357
Paul Elliotta2a09b02021-09-22 14:56:40 +01006358 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006359
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006361
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006363
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6365 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006366
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6368 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006369
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6371 input_data->len, output_data,
6372 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006373
Gilles Peskine449bd832023-01-11 14:50:10 +01006374 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6375 1, output_data,
6376 output_size, &output_length),
6377 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006378
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006380
Paul Elliottc23a9a02021-06-21 18:32:46 +01006381 /* Test sending additional data after data. */
6382
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006384
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006386
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 if (operation.alg != PSA_ALG_CCM) {
6388 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6389 input_data->len, output_data,
6390 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006391
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6393 additional_data->len),
6394 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006395 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006397
Paul Elliott534d0b42021-06-22 19:15:20 +01006398 /* Test calling finish on decryption. */
6399
Gilles Peskine449bd832023-01-11 14:50:10 +01006400 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006401
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006403
Gilles Peskine449bd832023-01-11 14:50:10 +01006404 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6405 finish_output_size,
6406 &output_part_length,
6407 tag_buffer, tag_length,
6408 &tag_size),
6409 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006410
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006412
6413 /* Test calling verify on encryption. */
6414
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006416
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006418
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6420 finish_output_size,
6421 &output_part_length,
6422 tag_buffer,
6423 tag_length),
6424 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006425
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006427
6428
Paul Elliottc23a9a02021-06-21 18:32:46 +01006429exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 psa_destroy_key(key);
6431 psa_aead_abort(&operation);
6432 mbedtls_free(output_data);
6433 mbedtls_free(final_data);
6434 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006435}
6436/* END_CASE */
6437
6438/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006439void signature_size(int type_arg,
6440 int bits,
6441 int alg_arg,
6442 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006443{
6444 psa_key_type_t type = type_arg;
6445 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006447
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006449
Gilles Peskinee59236f2018-01-27 23:32:46 +01006450exit:
6451 ;
6452}
6453/* END_CASE */
6454
6455/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006456void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6457 int alg_arg, data_t *input_data,
6458 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006459{
Ronald Cron5425a212020-08-04 14:58:35 +02006460 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006461 psa_key_type_t key_type = key_type_arg;
6462 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006463 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006464 unsigned char *signature = NULL;
6465 size_t signature_size;
6466 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006467 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006468
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006470
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6472 psa_set_key_algorithm(&attributes, alg);
6473 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006474
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6476 &key));
6477 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6478 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006479
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006480 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006481 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006482 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6483 key_bits, alg);
6484 TEST_ASSERT(signature_size != 0);
6485 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6486 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006487
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006488 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 PSA_ASSERT(psa_sign_hash(key, alg,
6490 input_data->x, input_data->len,
6491 signature, signature_size,
6492 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006493 /* Verify that the signature is what is expected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 ASSERT_COMPARE(output_data->x, output_data->len,
6495 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006496
6497exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006498 /*
6499 * Key attributes may have been returned by psa_get_key_attributes()
6500 * thus reset them as required.
6501 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006502 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006503
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 psa_destroy_key(key);
6505 mbedtls_free(signature);
6506 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006507}
6508/* END_CASE */
6509
Paul Elliott712d5122022-12-07 14:03:10 +00006510/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006511/**
6512 * sign_hash_interruptible() test intentions:
6513 *
6514 * Note: This test can currently only handle ECDSA.
6515 *
6516 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006517 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006518 *
6519 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6520 * expected for different max_ops values.
6521 *
6522 * 3. Test that the number of ops done prior to start and after abort is zero
6523 * and that each successful stage completes some ops (this is not mandated by
6524 * the PSA specification, but is currently the case).
6525 *
6526 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6527 * complete() calls does not alter the number of ops returned.
6528 */
Paul Elliott712d5122022-12-07 14:03:10 +00006529void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6530 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006531 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006532{
6533 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6534 psa_key_type_t key_type = key_type_arg;
6535 psa_algorithm_t alg = alg_arg;
6536 size_t key_bits;
6537 unsigned char *signature = NULL;
6538 size_t signature_size;
6539 size_t signature_length = 0xdeadbeef;
6540 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6541 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006542 uint32_t num_ops = 0;
6543 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006544 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006545 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006546 size_t min_completes = 0;
6547 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006548
Paul Elliott712d5122022-12-07 14:03:10 +00006549 psa_sign_hash_interruptible_operation_t operation =
6550 psa_sign_hash_interruptible_operation_init();
6551
6552 PSA_ASSERT(psa_crypto_init());
6553
6554 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6555 psa_set_key_algorithm(&attributes, alg);
6556 psa_set_key_type(&attributes, key_type);
6557
6558 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6559 &key));
6560 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6561 key_bits = psa_get_key_bits(&attributes);
6562
6563 /* Allocate a buffer which has the size advertised by the
6564 * library. */
6565 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6566 key_bits, alg);
6567 TEST_ASSERT(signature_size != 0);
6568 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6569 ASSERT_ALLOC(signature, signature_size);
6570
Paul Elliott0c683352022-12-16 19:16:56 +00006571 psa_interruptible_set_max_ops(max_ops);
6572
Paul Elliott6f600372023-02-06 18:41:05 +00006573 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6574 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006575
Paul Elliott712d5122022-12-07 14:03:10 +00006576 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6577 TEST_ASSERT(num_ops_prior == 0);
6578
6579 /* Start performing the signature. */
6580 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6581 input_data->x, input_data->len));
6582
6583 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6584 TEST_ASSERT(num_ops_prior == 0);
6585
6586 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006587 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006588 status = psa_sign_hash_complete(&operation, signature, signature_size,
6589 &signature_length);
6590
Paul Elliott0c683352022-12-16 19:16:56 +00006591 num_completes++;
6592
Paul Elliott712d5122022-12-07 14:03:10 +00006593 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6594 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006595 /* We are asserting here that every complete makes progress
6596 * (completes some ops), which is true of the internal
6597 * implementation and probably any implementation, however this is
6598 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006599 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006600
Paul Elliott712d5122022-12-07 14:03:10 +00006601 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006602
6603 /* Ensure calling get_num_ops() twice still returns the same
6604 * number of ops as previously reported. */
6605 num_ops = psa_sign_hash_get_num_ops(&operation);
6606
6607 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006608 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006609 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006610
6611 TEST_ASSERT(status == PSA_SUCCESS);
6612
Paul Elliott0c683352022-12-16 19:16:56 +00006613 TEST_LE_U(min_completes, num_completes);
6614 TEST_LE_U(num_completes, max_completes);
6615
Paul Elliott712d5122022-12-07 14:03:10 +00006616 /* Verify that the signature is what is expected. */
6617 ASSERT_COMPARE(output_data->x, output_data->len,
6618 signature, signature_length);
6619
6620 PSA_ASSERT(psa_sign_hash_abort(&operation));
6621
Paul Elliott59ad9452022-12-18 15:09:02 +00006622 num_ops = psa_sign_hash_get_num_ops(&operation);
6623 TEST_ASSERT(num_ops == 0);
6624
Paul Elliott712d5122022-12-07 14:03:10 +00006625exit:
6626
6627 /*
6628 * Key attributes may have been returned by psa_get_key_attributes()
6629 * thus reset them as required.
6630 */
6631 psa_reset_key_attributes(&attributes);
6632
6633 psa_destroy_key(key);
6634 mbedtls_free(signature);
6635 PSA_DONE();
6636}
6637/* END_CASE */
6638
Gilles Peskine20035e32018-02-03 22:44:14 +01006639/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006640void sign_hash_fail(int key_type_arg, data_t *key_data,
6641 int alg_arg, data_t *input_data,
6642 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006643{
Ronald Cron5425a212020-08-04 14:58:35 +02006644 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006645 psa_key_type_t key_type = key_type_arg;
6646 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006647 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006648 psa_status_t actual_status;
6649 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006650 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006651 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006652 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006653
Gilles Peskine449bd832023-01-11 14:50:10 +01006654 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006655
Gilles Peskine449bd832023-01-11 14:50:10 +01006656 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006657
Gilles Peskine449bd832023-01-11 14:50:10 +01006658 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6659 psa_set_key_algorithm(&attributes, alg);
6660 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006661
Gilles Peskine449bd832023-01-11 14:50:10 +01006662 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6663 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006664
Gilles Peskine449bd832023-01-11 14:50:10 +01006665 actual_status = psa_sign_hash(key, alg,
6666 input_data->x, input_data->len,
6667 signature, signature_size,
6668 &signature_length);
6669 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006670 /* The value of *signature_length is unspecified on error, but
6671 * whatever it is, it should be less than signature_size, so that
6672 * if the caller tries to read *signature_length bytes without
6673 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006674 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006675
6676exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006677 psa_reset_key_attributes(&attributes);
6678 psa_destroy_key(key);
6679 mbedtls_free(signature);
6680 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006681}
6682/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006683
Paul Elliott91007972022-12-16 12:21:24 +00006684/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006685/**
6686 * sign_hash_fail_interruptible() test intentions:
6687 *
6688 * Note: This test can currently only handle ECDSA.
6689 *
6690 * 1. Test that various failure cases for interruptible sign hash fail with the
6691 * correct error codes, and at the correct point (at start or during
6692 * complete).
6693 *
6694 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6695 * expected for different max_ops values.
6696 *
6697 * 3. Test that the number of ops done prior to start and after abort is zero
6698 * and that each successful stage completes some ops (this is not mandated by
6699 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006700 *
6701 * 4. Check that calling complete() when start() fails and complete()
6702 * after completion results in a BAD_STATE error.
6703 *
6704 * 5. Check that calling start() again after start fails results in a BAD_STATE
6705 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006706 */
Paul Elliott91007972022-12-16 12:21:24 +00006707void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6708 int alg_arg, data_t *input_data,
6709 int signature_size_arg,
6710 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006711 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006712 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006713{
6714 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6715 psa_key_type_t key_type = key_type_arg;
6716 psa_algorithm_t alg = alg_arg;
6717 size_t signature_size = signature_size_arg;
6718 psa_status_t actual_status;
6719 psa_status_t expected_start_status = expected_start_status_arg;
6720 psa_status_t expected_complete_status = expected_complete_status_arg;
6721 unsigned char *signature = NULL;
6722 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006723 uint32_t num_ops = 0;
6724 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006725 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006726 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006727 size_t min_completes = 0;
6728 size_t max_completes = 0;
6729
Paul Elliott91007972022-12-16 12:21:24 +00006730 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6731 psa_sign_hash_interruptible_operation_t operation =
6732 psa_sign_hash_interruptible_operation_init();
6733
6734 ASSERT_ALLOC(signature, signature_size);
6735
6736 PSA_ASSERT(psa_crypto_init());
6737
6738 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6739 psa_set_key_algorithm(&attributes, alg);
6740 psa_set_key_type(&attributes, key_type);
6741
6742 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6743 &key));
6744
Paul Elliott0c683352022-12-16 19:16:56 +00006745 psa_interruptible_set_max_ops(max_ops);
6746
Paul Elliott6f600372023-02-06 18:41:05 +00006747 interruptible_signverify_get_minmax_completes(max_ops,
6748 expected_complete_status,
6749 &min_completes,
6750 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006751
Paul Elliott91007972022-12-16 12:21:24 +00006752 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6753 TEST_ASSERT(num_ops_prior == 0);
6754
6755 /* Start performing the signature. */
6756 actual_status = psa_sign_hash_start(&operation, key, alg,
6757 input_data->x, input_data->len);
6758
6759 TEST_EQUAL(actual_status, expected_start_status);
6760
Paul Elliottc9774412023-02-06 15:14:07 +00006761 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006762 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006763 * start failed. */
6764 actual_status = psa_sign_hash_complete(&operation, signature,
6765 signature_size,
6766 &signature_length);
6767
6768 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6769
6770 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006771 actual_status = psa_sign_hash_start(&operation, key, alg,
6772 input_data->x, input_data->len);
6773
6774 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6775 }
6776
Paul Elliott91007972022-12-16 12:21:24 +00006777 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6778 TEST_ASSERT(num_ops_prior == 0);
6779
Paul Elliott91007972022-12-16 12:21:24 +00006780 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006781 do {
Paul Elliott91007972022-12-16 12:21:24 +00006782 actual_status = psa_sign_hash_complete(&operation, signature,
6783 signature_size,
6784 &signature_length);
6785
Paul Elliott0c683352022-12-16 19:16:56 +00006786 num_completes++;
6787
Paul Elliott334d7262023-01-20 17:29:41 +00006788 if (actual_status == PSA_SUCCESS ||
6789 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006790 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006791 /* We are asserting here that every complete makes progress
6792 * (completes some ops), which is true of the internal
6793 * implementation and probably any implementation, however this is
6794 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006795 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006796
Paul Elliott91007972022-12-16 12:21:24 +00006797 num_ops_prior = num_ops;
6798 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006799 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006800
Paul Elliottc9774412023-02-06 15:14:07 +00006801 TEST_EQUAL(actual_status, expected_complete_status);
6802
Paul Elliottefebad02023-02-15 16:56:45 +00006803 /* Check that another complete returns BAD_STATE. */
6804 actual_status = psa_sign_hash_complete(&operation, signature,
6805 signature_size,
6806 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006807
Paul Elliottefebad02023-02-15 16:56:45 +00006808 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006809
Paul Elliott91007972022-12-16 12:21:24 +00006810 PSA_ASSERT(psa_sign_hash_abort(&operation));
6811
Paul Elliott59ad9452022-12-18 15:09:02 +00006812 num_ops = psa_sign_hash_get_num_ops(&operation);
6813 TEST_ASSERT(num_ops == 0);
6814
Paul Elliott91007972022-12-16 12:21:24 +00006815 /* The value of *signature_length is unspecified on error, but
6816 * whatever it is, it should be less than signature_size, so that
6817 * if the caller tries to read *signature_length bytes without
6818 * checking the error code then they don't overflow a buffer. */
6819 TEST_LE_U(signature_length, signature_size);
6820
Paul Elliott0c683352022-12-16 19:16:56 +00006821 TEST_LE_U(min_completes, num_completes);
6822 TEST_LE_U(num_completes, max_completes);
6823
Paul Elliott91007972022-12-16 12:21:24 +00006824exit:
6825 psa_reset_key_attributes(&attributes);
6826 psa_destroy_key(key);
6827 mbedtls_free(signature);
6828 PSA_DONE();
6829}
6830/* END_CASE */
6831
mohammad16038cc1cee2018-03-28 01:21:33 +03006832/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006833void sign_verify_hash(int key_type_arg, data_t *key_data,
6834 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006835{
Ronald Cron5425a212020-08-04 14:58:35 +02006836 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006837 psa_key_type_t key_type = key_type_arg;
6838 psa_algorithm_t alg = alg_arg;
6839 size_t key_bits;
6840 unsigned char *signature = NULL;
6841 size_t signature_size;
6842 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006843 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006844
Gilles Peskine449bd832023-01-11 14:50:10 +01006845 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006846
Gilles Peskine449bd832023-01-11 14:50:10 +01006847 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6848 psa_set_key_algorithm(&attributes, alg);
6849 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006850
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6852 &key));
6853 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6854 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006855
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006856 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006857 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006858 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6859 key_bits, alg);
6860 TEST_ASSERT(signature_size != 0);
6861 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6862 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006863
6864 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006865 PSA_ASSERT(psa_sign_hash(key, alg,
6866 input_data->x, input_data->len,
6867 signature, signature_size,
6868 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006869 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006870 TEST_LE_U(signature_length, signature_size);
6871 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006872
6873 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006874 PSA_ASSERT(psa_verify_hash(key, alg,
6875 input_data->x, input_data->len,
6876 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006877
Gilles Peskine449bd832023-01-11 14:50:10 +01006878 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006879 /* Flip a bit in the input and verify that the signature is now
6880 * detected as invalid. Flip a bit at the beginning, not at the end,
6881 * because ECDSA may ignore the last few bits of the input. */
6882 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006883 TEST_EQUAL(psa_verify_hash(key, alg,
6884 input_data->x, input_data->len,
6885 signature, signature_length),
6886 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006887 }
6888
6889exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006890 /*
6891 * Key attributes may have been returned by psa_get_key_attributes()
6892 * thus reset them as required.
6893 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006894 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006895
Gilles Peskine449bd832023-01-11 14:50:10 +01006896 psa_destroy_key(key);
6897 mbedtls_free(signature);
6898 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006899}
6900/* END_CASE */
6901
Paul Elliott712d5122022-12-07 14:03:10 +00006902/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006903/**
6904 * sign_verify_hash_interruptible() test intentions:
6905 *
6906 * Note: This test can currently only handle ECDSA.
6907 *
Paul Elliott8c092052023-03-06 17:49:14 +00006908 * 1. Test that we can sign an input hash with the given keypair and then
6909 * afterwards verify that signature. This is currently the only way to test
6910 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00006911 *
6912 * 2. Test that after corrupting the hash, the verification detects an invalid
6913 * signature.
6914 *
6915 * 3. Test the number of calls to psa_sign_hash_complete() required are as
6916 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00006917 *
6918 * 4. Test that the number of ops done prior to starting signing and after abort
6919 * is zero and that each successful signing stage completes some ops (this is
6920 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00006921 */
Paul Elliott712d5122022-12-07 14:03:10 +00006922void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006923 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006924 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006925{
6926 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6927 psa_key_type_t key_type = key_type_arg;
6928 psa_algorithm_t alg = alg_arg;
6929 size_t key_bits;
6930 unsigned char *signature = NULL;
6931 size_t signature_size;
6932 size_t signature_length = 0xdeadbeef;
6933 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6934 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006935 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00006936 uint32_t num_ops = 0;
6937 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006938 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006939 size_t min_completes = 0;
6940 size_t max_completes = 0;
6941
Paul Elliott712d5122022-12-07 14:03:10 +00006942 psa_sign_hash_interruptible_operation_t sign_operation =
6943 psa_sign_hash_interruptible_operation_init();
6944 psa_verify_hash_interruptible_operation_t verify_operation =
6945 psa_verify_hash_interruptible_operation_init();
6946
6947 PSA_ASSERT(psa_crypto_init());
6948
Paul Elliott0c683352022-12-16 19:16:56 +00006949 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6950 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006951 psa_set_key_algorithm(&attributes, alg);
6952 psa_set_key_type(&attributes, key_type);
6953
6954 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6955 &key));
6956 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6957 key_bits = psa_get_key_bits(&attributes);
6958
6959 /* Allocate a buffer which has the size advertised by the
6960 * library. */
6961 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6962 key_bits, alg);
6963 TEST_ASSERT(signature_size != 0);
6964 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6965 ASSERT_ALLOC(signature, signature_size);
6966
Paul Elliott0c683352022-12-16 19:16:56 +00006967 psa_interruptible_set_max_ops(max_ops);
6968
Paul Elliott6f600372023-02-06 18:41:05 +00006969 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6970 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006971
Paul Elliott7c173082023-02-26 18:44:45 +00006972 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6973 TEST_ASSERT(num_ops_prior == 0);
6974
Paul Elliott712d5122022-12-07 14:03:10 +00006975 /* Start performing the signature. */
6976 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6977 input_data->x, input_data->len));
6978
Paul Elliott7c173082023-02-26 18:44:45 +00006979 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6980 TEST_ASSERT(num_ops_prior == 0);
6981
Paul Elliott712d5122022-12-07 14:03:10 +00006982 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006983 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006984
Paul Elliott0c683352022-12-16 19:16:56 +00006985 status = psa_sign_hash_complete(&sign_operation, signature,
6986 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00006987 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00006988
6989 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00006990
6991 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6992 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
6993 /* We are asserting here that every complete makes progress
6994 * (completes some ops), which is true of the internal
6995 * implementation and probably any implementation, however this is
6996 * not mandated by the PSA specification. */
6997 TEST_ASSERT(num_ops > num_ops_prior);
6998
6999 num_ops_prior = num_ops;
7000 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007001 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007002
7003 TEST_ASSERT(status == PSA_SUCCESS);
7004
Paul Elliott0c683352022-12-16 19:16:56 +00007005 TEST_LE_U(min_completes, num_completes);
7006 TEST_LE_U(num_completes, max_completes);
7007
Paul Elliott712d5122022-12-07 14:03:10 +00007008 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7009
Paul Elliott7c173082023-02-26 18:44:45 +00007010 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7011 TEST_ASSERT(num_ops == 0);
7012
Paul Elliott712d5122022-12-07 14:03:10 +00007013 /* Check that the signature length looks sensible. */
7014 TEST_LE_U(signature_length, signature_size);
7015 TEST_ASSERT(signature_length > 0);
7016
Paul Elliott0c683352022-12-16 19:16:56 +00007017 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007018
7019 /* Start verification. */
7020 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7021 input_data->x, input_data->len,
7022 signature, signature_length));
7023
7024 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007025 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007026 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007027
7028 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007029 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007030
7031 TEST_ASSERT(status == PSA_SUCCESS);
7032
Paul Elliott0c683352022-12-16 19:16:56 +00007033 TEST_LE_U(min_completes, num_completes);
7034 TEST_LE_U(num_completes, max_completes);
7035
Paul Elliott712d5122022-12-07 14:03:10 +00007036 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7037
7038 verify_operation = psa_verify_hash_interruptible_operation_init();
7039
7040 if (input_data->len != 0) {
7041 /* Flip a bit in the input and verify that the signature is now
7042 * detected as invalid. Flip a bit at the beginning, not at the end,
7043 * because ECDSA may ignore the last few bits of the input. */
7044 input_data->x[0] ^= 1;
7045
Paul Elliott712d5122022-12-07 14:03:10 +00007046 /* Start verification. */
7047 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7048 input_data->x, input_data->len,
7049 signature, signature_length));
7050
7051 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007052 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007053 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007054 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007055
7056 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7057 }
7058
7059 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7060
7061exit:
7062 /*
7063 * Key attributes may have been returned by psa_get_key_attributes()
7064 * thus reset them as required.
7065 */
7066 psa_reset_key_attributes(&attributes);
7067
7068 psa_destroy_key(key);
7069 mbedtls_free(signature);
7070 PSA_DONE();
7071}
7072/* END_CASE */
7073
Gilles Peskine9911b022018-06-29 17:30:48 +02007074/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007075void verify_hash(int key_type_arg, data_t *key_data,
7076 int alg_arg, data_t *hash_data,
7077 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007078{
Ronald Cron5425a212020-08-04 14:58:35 +02007079 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007080 psa_key_type_t key_type = key_type_arg;
7081 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007082 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007083
Gilles Peskine449bd832023-01-11 14:50:10 +01007084 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007085
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007087
Gilles Peskine449bd832023-01-11 14:50:10 +01007088 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7089 psa_set_key_algorithm(&attributes, alg);
7090 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007091
Gilles Peskine449bd832023-01-11 14:50:10 +01007092 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7093 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007094
Gilles Peskine449bd832023-01-11 14:50:10 +01007095 PSA_ASSERT(psa_verify_hash(key, alg,
7096 hash_data->x, hash_data->len,
7097 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007098
itayzafrir5c753392018-05-08 11:18:38 +03007099exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007100 psa_reset_key_attributes(&attributes);
7101 psa_destroy_key(key);
7102 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007103}
7104/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007105
Paul Elliott712d5122022-12-07 14:03:10 +00007106/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007107/**
7108 * verify_hash_interruptible() test intentions:
7109 *
7110 * Note: This test can currently only handle ECDSA.
7111 *
7112 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007113 * only). Given this test only does verification it can accept public keys as
7114 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007115 *
7116 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7117 * expected for different max_ops values.
7118 *
7119 * 3. Test that the number of ops done prior to start and after abort is zero
7120 * and that each successful stage completes some ops (this is not mandated by
7121 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007122 *
7123 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7124 * complete() calls does not alter the number of ops returned.
7125 *
7126 * 5. Test that after corrupting the hash, the verification detects an invalid
7127 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007128 */
Paul Elliott712d5122022-12-07 14:03:10 +00007129void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7130 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007131 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007132{
7133 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7134 psa_key_type_t key_type = key_type_arg;
7135 psa_algorithm_t alg = alg_arg;
7136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7137 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007138 uint32_t num_ops = 0;
7139 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007140 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007141 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007142 size_t min_completes = 0;
7143 size_t max_completes = 0;
7144
Paul Elliott712d5122022-12-07 14:03:10 +00007145 psa_verify_hash_interruptible_operation_t operation =
7146 psa_verify_hash_interruptible_operation_init();
7147
7148 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7149
7150 PSA_ASSERT(psa_crypto_init());
7151
7152 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7153 psa_set_key_algorithm(&attributes, alg);
7154 psa_set_key_type(&attributes, key_type);
7155
7156 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7157 &key));
7158
Paul Elliott0c683352022-12-16 19:16:56 +00007159 psa_interruptible_set_max_ops(max_ops);
7160
Paul Elliott6f600372023-02-06 18:41:05 +00007161 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7162 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007163
Paul Elliott712d5122022-12-07 14:03:10 +00007164 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7165
7166 TEST_ASSERT(num_ops_prior == 0);
7167
7168 /* Start verification. */
7169 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7170 hash_data->x, hash_data->len,
7171 signature_data->x, signature_data->len)
7172 );
7173
7174 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7175
7176 TEST_ASSERT(num_ops_prior == 0);
7177
7178 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007179 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007180 status = psa_verify_hash_complete(&operation);
7181
Paul Elliott0c683352022-12-16 19:16:56 +00007182 num_completes++;
7183
Paul Elliott712d5122022-12-07 14:03:10 +00007184 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7185 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007186 /* We are asserting here that every complete makes progress
7187 * (completes some ops), which is true of the internal
7188 * implementation and probably any implementation, however this is
7189 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007190 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007191
Paul Elliott712d5122022-12-07 14:03:10 +00007192 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007193
7194 /* Ensure calling get_num_ops() twice still returns the same
7195 * number of ops as previously reported. */
7196 num_ops = psa_verify_hash_get_num_ops(&operation);
7197
7198 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007199 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007200 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007201
7202 TEST_ASSERT(status == PSA_SUCCESS);
7203
Paul Elliott0c683352022-12-16 19:16:56 +00007204 TEST_LE_U(min_completes, num_completes);
7205 TEST_LE_U(num_completes, max_completes);
7206
Paul Elliott712d5122022-12-07 14:03:10 +00007207 PSA_ASSERT(psa_verify_hash_abort(&operation));
7208
Paul Elliott59ad9452022-12-18 15:09:02 +00007209 num_ops = psa_verify_hash_get_num_ops(&operation);
7210 TEST_ASSERT(num_ops == 0);
7211
Paul Elliott8359c142023-02-24 18:40:10 +00007212 if (hash_data->len != 0) {
7213 /* Flip a bit in the hash and verify that the signature is now detected
7214 * as invalid. Flip a bit at the beginning, not at the end, because
7215 * ECDSA may ignore the last few bits of the input. */
7216 hash_data->x[0] ^= 1;
7217
7218 /* Start verification. */
7219 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7220 hash_data->x, hash_data->len,
7221 signature_data->x, signature_data->len));
7222
7223 /* Continue performing the signature until complete. */
7224 do {
7225 status = psa_verify_hash_complete(&operation);
7226 } while (status == PSA_OPERATION_INCOMPLETE);
7227
7228 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7229 }
7230
Paul Elliott712d5122022-12-07 14:03:10 +00007231exit:
7232 psa_reset_key_attributes(&attributes);
7233 psa_destroy_key(key);
7234 PSA_DONE();
7235}
7236/* END_CASE */
7237
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007238/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007239void verify_hash_fail(int key_type_arg, data_t *key_data,
7240 int alg_arg, data_t *hash_data,
7241 data_t *signature_data,
7242 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007243{
Ronald Cron5425a212020-08-04 14:58:35 +02007244 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007245 psa_key_type_t key_type = key_type_arg;
7246 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007247 psa_status_t actual_status;
7248 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007249 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007250
Gilles Peskine449bd832023-01-11 14:50:10 +01007251 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007252
Gilles Peskine449bd832023-01-11 14:50:10 +01007253 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7254 psa_set_key_algorithm(&attributes, alg);
7255 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007256
Gilles Peskine449bd832023-01-11 14:50:10 +01007257 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7258 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007259
Gilles Peskine449bd832023-01-11 14:50:10 +01007260 actual_status = psa_verify_hash(key, alg,
7261 hash_data->x, hash_data->len,
7262 signature_data->x, signature_data->len);
7263 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007264
7265exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007266 psa_reset_key_attributes(&attributes);
7267 psa_destroy_key(key);
7268 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007269}
7270/* END_CASE */
7271
Paul Elliott91007972022-12-16 12:21:24 +00007272/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007273/**
7274 * verify_hash_fail_interruptible() test intentions:
7275 *
7276 * Note: This test can currently only handle ECDSA.
7277 *
7278 * 1. Test that various failure cases for interruptible verify hash fail with
7279 * the correct error codes, and at the correct point (at start or during
7280 * complete).
7281 *
7282 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7283 * expected for different max_ops values.
7284 *
7285 * 3. Test that the number of ops done prior to start and after abort is zero
7286 * and that each successful stage completes some ops (this is not mandated by
7287 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007288 *
7289 * 4. Check that calling complete() when start() fails and complete()
7290 * after completion results in a BAD_STATE error.
7291 *
7292 * 5. Check that calling start() again after start fails results in a BAD_STATE
7293 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007294 */
Paul Elliott91007972022-12-16 12:21:24 +00007295void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7296 int alg_arg, data_t *hash_data,
7297 data_t *signature_data,
7298 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007299 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007300 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007301{
7302 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7303 psa_key_type_t key_type = key_type_arg;
7304 psa_algorithm_t alg = alg_arg;
7305 psa_status_t actual_status;
7306 psa_status_t expected_start_status = expected_start_status_arg;
7307 psa_status_t expected_complete_status = expected_complete_status_arg;
7308 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007309 uint32_t num_ops = 0;
7310 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007311 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007312 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007313 size_t min_completes = 0;
7314 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007315 psa_verify_hash_interruptible_operation_t operation =
7316 psa_verify_hash_interruptible_operation_init();
7317
7318 PSA_ASSERT(psa_crypto_init());
7319
7320 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7321 psa_set_key_algorithm(&attributes, alg);
7322 psa_set_key_type(&attributes, key_type);
7323
7324 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7325 &key));
7326
Paul Elliott0c683352022-12-16 19:16:56 +00007327 psa_interruptible_set_max_ops(max_ops);
7328
Paul Elliott6f600372023-02-06 18:41:05 +00007329 interruptible_signverify_get_minmax_completes(max_ops,
7330 expected_complete_status,
7331 &min_completes,
7332 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007333
Paul Elliott91007972022-12-16 12:21:24 +00007334 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7335 TEST_ASSERT(num_ops_prior == 0);
7336
7337 /* Start verification. */
7338 actual_status = psa_verify_hash_start(&operation, key, alg,
7339 hash_data->x, hash_data->len,
7340 signature_data->x,
7341 signature_data->len);
7342
7343 TEST_EQUAL(actual_status, expected_start_status);
7344
Paul Elliottc9774412023-02-06 15:14:07 +00007345 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007346 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007347 * start failed. */
7348 actual_status = psa_verify_hash_complete(&operation);
7349
7350 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7351
7352 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007353 actual_status = psa_verify_hash_start(&operation, key, alg,
7354 hash_data->x, hash_data->len,
7355 signature_data->x,
7356 signature_data->len);
7357
7358 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7359 }
7360
Paul Elliott91007972022-12-16 12:21:24 +00007361 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7362 TEST_ASSERT(num_ops_prior == 0);
7363
Paul Elliott91007972022-12-16 12:21:24 +00007364 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007365 do {
Paul Elliott91007972022-12-16 12:21:24 +00007366 actual_status = psa_verify_hash_complete(&operation);
7367
Paul Elliott0c683352022-12-16 19:16:56 +00007368 num_completes++;
7369
Paul Elliott334d7262023-01-20 17:29:41 +00007370 if (actual_status == PSA_SUCCESS ||
7371 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007372 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007373 /* We are asserting here that every complete makes progress
7374 * (completes some ops), which is true of the internal
7375 * implementation and probably any implementation, however this is
7376 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007377 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007378
Paul Elliott91007972022-12-16 12:21:24 +00007379 num_ops_prior = num_ops;
7380 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007381 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007382
Paul Elliottc9774412023-02-06 15:14:07 +00007383 TEST_EQUAL(actual_status, expected_complete_status);
7384
Paul Elliottefebad02023-02-15 16:56:45 +00007385 /* Check that another complete returns BAD_STATE. */
7386 actual_status = psa_verify_hash_complete(&operation);
7387 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007388
Paul Elliott0c683352022-12-16 19:16:56 +00007389 TEST_LE_U(min_completes, num_completes);
7390 TEST_LE_U(num_completes, max_completes);
7391
Paul Elliott91007972022-12-16 12:21:24 +00007392 PSA_ASSERT(psa_verify_hash_abort(&operation));
7393
Paul Elliott59ad9452022-12-18 15:09:02 +00007394 num_ops = psa_verify_hash_get_num_ops(&operation);
7395 TEST_ASSERT(num_ops == 0);
7396
Paul Elliott91007972022-12-16 12:21:24 +00007397exit:
7398 psa_reset_key_attributes(&attributes);
7399 psa_destroy_key(key);
7400 PSA_DONE();
7401}
7402/* END_CASE */
7403
Paul Elliott20a36062022-12-18 13:21:25 +00007404/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007405/**
7406 * interruptible_signverify_hash_state_test() test intentions:
7407 *
7408 * Note: This test can currently only handle ECDSA.
7409 *
7410 * 1. Test that calling the various interruptible sign and verify hash functions
7411 * in incorrect orders returns BAD_STATE errors.
7412 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007413void interruptible_signverify_hash_state_test(int key_type_arg,
7414 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007415{
7416 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7417 psa_key_type_t key_type = key_type_arg;
7418 psa_algorithm_t alg = alg_arg;
7419 size_t key_bits;
7420 unsigned char *signature = NULL;
7421 size_t signature_size;
7422 size_t signature_length = 0xdeadbeef;
7423 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7424 psa_sign_hash_interruptible_operation_t sign_operation =
7425 psa_sign_hash_interruptible_operation_init();
7426 psa_verify_hash_interruptible_operation_t verify_operation =
7427 psa_verify_hash_interruptible_operation_init();
7428
7429 PSA_ASSERT(psa_crypto_init());
7430
7431 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7432 PSA_KEY_USAGE_VERIFY_HASH);
7433 psa_set_key_algorithm(&attributes, alg);
7434 psa_set_key_type(&attributes, key_type);
7435
7436 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7437 &key));
7438 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7439 key_bits = psa_get_key_bits(&attributes);
7440
7441 /* Allocate a buffer which has the size advertised by the
7442 * library. */
7443 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7444 key_bits, alg);
7445 TEST_ASSERT(signature_size != 0);
7446 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7447 ASSERT_ALLOC(signature, signature_size);
7448
7449 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7450
7451 /* --- Attempt completes prior to starts --- */
7452 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7453 signature_size,
7454 &signature_length),
7455 PSA_ERROR_BAD_STATE);
7456
7457 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7458
7459 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7460 PSA_ERROR_BAD_STATE);
7461
7462 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7463
7464 /* --- Aborts in all other places. --- */
7465 psa_sign_hash_abort(&sign_operation);
7466
7467 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7468 input_data->x, input_data->len));
7469
7470 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7471
7472 psa_interruptible_set_max_ops(1);
7473
7474 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7475 input_data->x, input_data->len));
7476
7477 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7478 signature_size,
7479 &signature_length),
7480 PSA_OPERATION_INCOMPLETE);
7481
7482 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7483
7484 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7485
7486 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7487 input_data->x, input_data->len));
7488
7489 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7490 signature_size,
7491 &signature_length));
7492
7493 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7494
7495 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7496
7497 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7498 input_data->x, input_data->len,
7499 signature, signature_length));
7500
7501 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7502
7503 psa_interruptible_set_max_ops(1);
7504
7505 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7506 input_data->x, input_data->len,
7507 signature, signature_length));
7508
7509 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7510 PSA_OPERATION_INCOMPLETE);
7511
7512 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7513
7514 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7515
7516 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7517 input_data->x, input_data->len,
7518 signature, signature_length));
7519
7520 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7521
7522 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7523
7524 /* --- Attempt double starts. --- */
7525
7526 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7527 input_data->x, input_data->len));
7528
7529 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7530 input_data->x, input_data->len),
7531 PSA_ERROR_BAD_STATE);
7532
7533 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7534
7535 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7536 input_data->x, input_data->len,
7537 signature, signature_length));
7538
7539 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7540 input_data->x, input_data->len,
7541 signature, signature_length),
7542 PSA_ERROR_BAD_STATE);
7543
7544 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7545
Paul Elliott76d671a2023-02-07 17:45:18 +00007546exit:
7547 /*
7548 * Key attributes may have been returned by psa_get_key_attributes()
7549 * thus reset them as required.
7550 */
7551 psa_reset_key_attributes(&attributes);
7552
7553 psa_destroy_key(key);
7554 mbedtls_free(signature);
7555 PSA_DONE();
7556}
7557/* END_CASE */
7558
7559/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007560/**
Paul Elliottc2033502023-02-26 17:09:14 +00007561 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007562 *
7563 * Note: This test can currently only handle ECDSA.
7564 *
7565 * 1. Test various edge cases in the interruptible sign and verify hash
7566 * interfaces.
7567 */
Paul Elliottc2033502023-02-26 17:09:14 +00007568void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007569 data_t *key_data, int alg_arg, data_t *input_data)
7570{
7571 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7572 psa_key_type_t key_type = key_type_arg;
7573 psa_algorithm_t alg = alg_arg;
7574 size_t key_bits;
7575 unsigned char *signature = NULL;
7576 size_t signature_size;
7577 size_t signature_length = 0xdeadbeef;
7578 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7579 uint8_t *input_buffer = NULL;
7580 psa_sign_hash_interruptible_operation_t sign_operation =
7581 psa_sign_hash_interruptible_operation_init();
7582 psa_verify_hash_interruptible_operation_t verify_operation =
7583 psa_verify_hash_interruptible_operation_init();
7584
7585 PSA_ASSERT(psa_crypto_init());
7586
7587 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7588 PSA_KEY_USAGE_VERIFY_HASH);
7589 psa_set_key_algorithm(&attributes, alg);
7590 psa_set_key_type(&attributes, key_type);
7591
7592 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7593 &key));
7594 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7595 key_bits = psa_get_key_bits(&attributes);
7596
7597 /* Allocate a buffer which has the size advertised by the
7598 * library. */
7599 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7600 key_bits, alg);
7601 TEST_ASSERT(signature_size != 0);
7602 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7603 ASSERT_ALLOC(signature, signature_size);
7604
Paul Elliott20a36062022-12-18 13:21:25 +00007605 /* --- Change function inputs mid run, to cause an error (sign only,
7606 * verify passes all inputs to start. --- */
7607
7608 psa_interruptible_set_max_ops(1);
7609
7610 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7611 input_data->x, input_data->len));
7612
7613 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7614 signature_size,
7615 &signature_length),
7616 PSA_OPERATION_INCOMPLETE);
7617
7618 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7619 0,
7620 &signature_length),
7621 PSA_ERROR_BUFFER_TOO_SMALL);
7622
Paul Elliottc9774412023-02-06 15:14:07 +00007623 /* And test that this invalidates the operation. */
7624 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7625 0,
7626 &signature_length),
7627 PSA_ERROR_BAD_STATE);
7628
Paul Elliott20a36062022-12-18 13:21:25 +00007629 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7630
Paul Elliottf9c91a72023-02-05 18:06:38 +00007631 /* Trash the hash buffer in between start and complete, to ensure
7632 * no reliance on external buffers. */
7633 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7634
7635 input_buffer = mbedtls_calloc(1, input_data->len);
7636 TEST_ASSERT(input_buffer != NULL);
7637
7638 memcpy(input_buffer, input_data->x, input_data->len);
7639
7640 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7641 input_buffer, input_data->len));
7642
7643 memset(input_buffer, '!', input_data->len);
7644 mbedtls_free(input_buffer);
7645 input_buffer = NULL;
7646
7647 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7648 signature_size,
7649 &signature_length));
7650
7651 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7652
7653 input_buffer = mbedtls_calloc(1, input_data->len);
7654 TEST_ASSERT(input_buffer != NULL);
7655
7656 memcpy(input_buffer, input_data->x, input_data->len);
7657
7658 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7659 input_buffer, input_data->len,
7660 signature, signature_length));
7661
7662 memset(input_buffer, '!', input_data->len);
7663 mbedtls_free(input_buffer);
7664 input_buffer = NULL;
7665
7666 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7667
7668 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7669
Paul Elliott20a36062022-12-18 13:21:25 +00007670exit:
7671 /*
7672 * Key attributes may have been returned by psa_get_key_attributes()
7673 * thus reset them as required.
7674 */
7675 psa_reset_key_attributes(&attributes);
7676
7677 psa_destroy_key(key);
7678 mbedtls_free(signature);
7679 PSA_DONE();
7680}
7681/* END_CASE */
7682
Paul Elliotta4cb9092023-02-07 18:01:55 +00007683/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007684/**
Paul Elliott57702242023-02-26 20:36:10 +00007685 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007686 *
7687 * Note: This test can currently only handle ECDSA.
7688 *
7689 * 1. Test that setting max ops is reflected in both interruptible sign and
7690 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007691 * 2. Test that changing the value of max_ops to unlimited during an operation
7692 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007693 *
7694 * 3. Test that calling get_num_ops() between complete calls gives the same
7695 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007696 */
Paul Elliott57702242023-02-26 20:36:10 +00007697void interruptible_signverify_hash_ops_tests(int key_type_arg,
7698 data_t *key_data, int alg_arg,
7699 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007700{
7701 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7702 psa_key_type_t key_type = key_type_arg;
7703 psa_algorithm_t alg = alg_arg;
7704 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007705 size_t key_bits;
7706 unsigned char *signature = NULL;
7707 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007708 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007709 uint32_t num_ops = 0;
7710 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7711
Paul Elliotta4cb9092023-02-07 18:01:55 +00007712 psa_sign_hash_interruptible_operation_t sign_operation =
7713 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007714 psa_verify_hash_interruptible_operation_t verify_operation =
7715 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007716
7717 PSA_ASSERT(psa_crypto_init());
7718
7719 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7720 PSA_KEY_USAGE_VERIFY_HASH);
7721 psa_set_key_algorithm(&attributes, alg);
7722 psa_set_key_type(&attributes, key_type);
7723
Paul Elliottf1743e22023-02-15 18:44:16 +00007724 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7725 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7726 key_bits = psa_get_key_bits(&attributes);
7727
7728 /* Allocate a buffer which has the size advertised by the
7729 * library. */
7730 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7731
7732 TEST_ASSERT(signature_size != 0);
7733 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7734 ASSERT_ALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007735
7736 /* Check that default max ops gets set if we don't set it. */
7737 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7738 input_data->x, input_data->len));
7739
7740 TEST_EQUAL(psa_interruptible_get_max_ops(),
7741 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7742
7743 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7744
Paul Elliottf1743e22023-02-15 18:44:16 +00007745 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7746 input_data->x, input_data->len,
7747 signature, signature_size));
7748
7749 TEST_EQUAL(psa_interruptible_get_max_ops(),
7750 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7751
7752 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7753
Paul Elliotta4cb9092023-02-07 18:01:55 +00007754 /* Check that max ops gets set properly. */
7755
7756 psa_interruptible_set_max_ops(0xbeef);
7757
Paul Elliottf1743e22023-02-15 18:44:16 +00007758 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007759
Paul Elliott9e8819f2023-02-26 19:01:35 +00007760 /* --- Ensure changing the max ops mid operation works (operation should
7761 * complete successfully after setting max ops to unlimited --- */
7762 psa_interruptible_set_max_ops(1);
7763
7764 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7765 input_data->x, input_data->len));
7766
7767 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7768 signature_size,
7769 &signature_length),
7770 PSA_OPERATION_INCOMPLETE);
7771
7772 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7773
7774 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7775 signature_size,
7776 &signature_length));
7777
7778 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7779
7780 psa_interruptible_set_max_ops(1);
7781
7782 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7783 input_data->x, input_data->len,
7784 signature, signature_length));
7785
7786 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7787 PSA_OPERATION_INCOMPLETE);
7788
7789 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7790
7791 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7792
7793 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7794
Paul Elliottc1e04002023-02-26 20:27:23 +00007795 /* --- Test that not calling get_num_ops inbetween complete calls does not
7796 * result in lost ops. ---*/
7797
7798 psa_interruptible_set_max_ops(1);
7799
7800 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7801 input_data->x, input_data->len));
7802
7803 /* Continue performing the signature until complete. */
7804 do {
7805 status = psa_sign_hash_complete(&sign_operation, signature,
7806 signature_size,
7807 &signature_length);
7808
7809 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7810
7811 } while (status == PSA_OPERATION_INCOMPLETE);
7812
7813 PSA_ASSERT(status);
7814
7815 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7816
7817 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7818 input_data->x, input_data->len));
7819
7820 /* Continue performing the signature until complete. */
7821 do {
7822 status = psa_sign_hash_complete(&sign_operation, signature,
7823 signature_size,
7824 &signature_length);
7825 } while (status == PSA_OPERATION_INCOMPLETE);
7826
7827 PSA_ASSERT(status);
7828
7829 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7830
7831 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7832
7833 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7834 input_data->x, input_data->len,
7835 signature, signature_length));
7836
7837 /* Continue performing the verification until complete. */
7838 do {
7839 status = psa_verify_hash_complete(&verify_operation);
7840
7841 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7842
7843 } while (status == PSA_OPERATION_INCOMPLETE);
7844
7845 PSA_ASSERT(status);
7846
7847 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7848
7849 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7850 input_data->x, input_data->len,
7851 signature, signature_length));
7852
7853 /* Continue performing the verification until complete. */
7854 do {
7855 status = psa_verify_hash_complete(&verify_operation);
7856
7857 } while (status == PSA_OPERATION_INCOMPLETE);
7858
7859 PSA_ASSERT(status);
7860
7861 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7862
7863 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7864
Paul Elliotta4cb9092023-02-07 18:01:55 +00007865exit:
7866 /*
7867 * Key attributes may have been returned by psa_get_key_attributes()
7868 * thus reset them as required.
7869 */
7870 psa_reset_key_attributes(&attributes);
7871
7872 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007873 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007874 PSA_DONE();
7875}
7876/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007877
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007878/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007879void sign_message_deterministic(int key_type_arg,
7880 data_t *key_data,
7881 int alg_arg,
7882 data_t *input_data,
7883 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007884{
7885 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7886 psa_key_type_t key_type = key_type_arg;
7887 psa_algorithm_t alg = alg_arg;
7888 size_t key_bits;
7889 unsigned char *signature = NULL;
7890 size_t signature_size;
7891 size_t signature_length = 0xdeadbeef;
7892 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7893
Gilles Peskine449bd832023-01-11 14:50:10 +01007894 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007895
Gilles Peskine449bd832023-01-11 14:50:10 +01007896 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7897 psa_set_key_algorithm(&attributes, alg);
7898 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007899
Gilles Peskine449bd832023-01-11 14:50:10 +01007900 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7901 &key));
7902 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7903 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007904
Gilles Peskine449bd832023-01-11 14:50:10 +01007905 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7906 TEST_ASSERT(signature_size != 0);
7907 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7908 ASSERT_ALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007909
Gilles Peskine449bd832023-01-11 14:50:10 +01007910 PSA_ASSERT(psa_sign_message(key, alg,
7911 input_data->x, input_data->len,
7912 signature, signature_size,
7913 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007914
Gilles Peskine449bd832023-01-11 14:50:10 +01007915 ASSERT_COMPARE(output_data->x, output_data->len,
7916 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007917
7918exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007919 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007920
Gilles Peskine449bd832023-01-11 14:50:10 +01007921 psa_destroy_key(key);
7922 mbedtls_free(signature);
7923 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007924
7925}
7926/* END_CASE */
7927
7928/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007929void sign_message_fail(int key_type_arg,
7930 data_t *key_data,
7931 int alg_arg,
7932 data_t *input_data,
7933 int signature_size_arg,
7934 int expected_status_arg)
7935{
7936 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7937 psa_key_type_t key_type = key_type_arg;
7938 psa_algorithm_t alg = alg_arg;
7939 size_t signature_size = signature_size_arg;
7940 psa_status_t actual_status;
7941 psa_status_t expected_status = expected_status_arg;
7942 unsigned char *signature = NULL;
7943 size_t signature_length = 0xdeadbeef;
7944 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7945
7946 ASSERT_ALLOC(signature, signature_size);
7947
7948 PSA_ASSERT(psa_crypto_init());
7949
7950 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7951 psa_set_key_algorithm(&attributes, alg);
7952 psa_set_key_type(&attributes, key_type);
7953
7954 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7955 &key));
7956
7957 actual_status = psa_sign_message(key, alg,
7958 input_data->x, input_data->len,
7959 signature, signature_size,
7960 &signature_length);
7961 TEST_EQUAL(actual_status, expected_status);
7962 /* The value of *signature_length is unspecified on error, but
7963 * whatever it is, it should be less than signature_size, so that
7964 * if the caller tries to read *signature_length bytes without
7965 * checking the error code then they don't overflow a buffer. */
7966 TEST_LE_U(signature_length, signature_size);
7967
7968exit:
7969 psa_reset_key_attributes(&attributes);
7970 psa_destroy_key(key);
7971 mbedtls_free(signature);
7972 PSA_DONE();
7973}
7974/* END_CASE */
7975
7976/* BEGIN_CASE */
7977void sign_verify_message(int key_type_arg,
7978 data_t *key_data,
7979 int alg_arg,
7980 data_t *input_data)
7981{
7982 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7983 psa_key_type_t key_type = key_type_arg;
7984 psa_algorithm_t alg = alg_arg;
7985 size_t key_bits;
7986 unsigned char *signature = NULL;
7987 size_t signature_size;
7988 size_t signature_length = 0xdeadbeef;
7989 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7990
7991 PSA_ASSERT(psa_crypto_init());
7992
7993 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
7994 PSA_KEY_USAGE_VERIFY_MESSAGE);
7995 psa_set_key_algorithm(&attributes, alg);
7996 psa_set_key_type(&attributes, key_type);
7997
7998 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7999 &key));
8000 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8001 key_bits = psa_get_key_bits(&attributes);
8002
8003 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8004 TEST_ASSERT(signature_size != 0);
8005 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
8006 ASSERT_ALLOC(signature, signature_size);
8007
8008 PSA_ASSERT(psa_sign_message(key, alg,
8009 input_data->x, input_data->len,
8010 signature, signature_size,
8011 &signature_length));
8012 TEST_LE_U(signature_length, signature_size);
8013 TEST_ASSERT(signature_length > 0);
8014
8015 PSA_ASSERT(psa_verify_message(key, alg,
8016 input_data->x, input_data->len,
8017 signature, signature_length));
8018
8019 if (input_data->len != 0) {
8020 /* Flip a bit in the input and verify that the signature is now
8021 * detected as invalid. Flip a bit at the beginning, not at the end,
8022 * because ECDSA may ignore the last few bits of the input. */
8023 input_data->x[0] ^= 1;
8024 TEST_EQUAL(psa_verify_message(key, alg,
8025 input_data->x, input_data->len,
8026 signature, signature_length),
8027 PSA_ERROR_INVALID_SIGNATURE);
8028 }
8029
8030exit:
8031 psa_reset_key_attributes(&attributes);
8032
8033 psa_destroy_key(key);
8034 mbedtls_free(signature);
8035 PSA_DONE();
8036}
8037/* END_CASE */
8038
8039/* BEGIN_CASE */
8040void verify_message(int key_type_arg,
8041 data_t *key_data,
8042 int alg_arg,
8043 data_t *input_data,
8044 data_t *signature_data)
8045{
8046 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8047 psa_key_type_t key_type = key_type_arg;
8048 psa_algorithm_t alg = alg_arg;
8049 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8050
8051 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8052
8053 PSA_ASSERT(psa_crypto_init());
8054
8055 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8056 psa_set_key_algorithm(&attributes, alg);
8057 psa_set_key_type(&attributes, key_type);
8058
8059 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8060 &key));
8061
8062 PSA_ASSERT(psa_verify_message(key, alg,
8063 input_data->x, input_data->len,
8064 signature_data->x, signature_data->len));
8065
8066exit:
8067 psa_reset_key_attributes(&attributes);
8068 psa_destroy_key(key);
8069 PSA_DONE();
8070}
8071/* END_CASE */
8072
8073/* BEGIN_CASE */
8074void verify_message_fail(int key_type_arg,
8075 data_t *key_data,
8076 int alg_arg,
8077 data_t *hash_data,
8078 data_t *signature_data,
8079 int expected_status_arg)
8080{
8081 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8082 psa_key_type_t key_type = key_type_arg;
8083 psa_algorithm_t alg = alg_arg;
8084 psa_status_t actual_status;
8085 psa_status_t expected_status = expected_status_arg;
8086 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8087
8088 PSA_ASSERT(psa_crypto_init());
8089
8090 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8091 psa_set_key_algorithm(&attributes, alg);
8092 psa_set_key_type(&attributes, key_type);
8093
8094 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8095 &key));
8096
8097 actual_status = psa_verify_message(key, alg,
8098 hash_data->x, hash_data->len,
8099 signature_data->x,
8100 signature_data->len);
8101 TEST_EQUAL(actual_status, expected_status);
8102
8103exit:
8104 psa_reset_key_attributes(&attributes);
8105 psa_destroy_key(key);
8106 PSA_DONE();
8107}
8108/* END_CASE */
8109
8110/* BEGIN_CASE */
8111void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008112 data_t *key_data,
8113 int alg_arg,
8114 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008115 data_t *label,
8116 int expected_output_length_arg,
8117 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008118{
Ronald Cron5425a212020-08-04 14:58:35 +02008119 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008120 psa_key_type_t key_type = key_type_arg;
8121 psa_algorithm_t alg = alg_arg;
8122 size_t expected_output_length = expected_output_length_arg;
8123 size_t key_bits;
8124 unsigned char *output = NULL;
8125 size_t output_size;
8126 size_t output_length = ~0;
8127 psa_status_t actual_status;
8128 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008129 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008130
Gilles Peskine449bd832023-01-11 14:50:10 +01008131 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008132
Gilles Peskine656896e2018-06-29 19:12:28 +02008133 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008134 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8135 psa_set_key_algorithm(&attributes, alg);
8136 psa_set_key_type(&attributes, key_type);
8137 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8138 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008139
8140 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008141 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8142 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008143
Gilles Peskine449bd832023-01-11 14:50:10 +01008144 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8145 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
8146 ASSERT_ALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008147
8148 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008149 actual_status = psa_asymmetric_encrypt(key, alg,
8150 input_data->x, input_data->len,
8151 label->x, label->len,
8152 output, output_size,
8153 &output_length);
8154 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008155 if (actual_status == PSA_SUCCESS) {
8156 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008157 } else {
8158 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008159 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008160
Gilles Peskine68428122018-06-30 18:42:41 +02008161 /* If the label is empty, the test framework puts a non-null pointer
8162 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008163 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008164 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008165 if (output_size != 0) {
8166 memset(output, 0, output_size);
8167 }
8168 actual_status = psa_asymmetric_encrypt(key, alg,
8169 input_data->x, input_data->len,
8170 NULL, label->len,
8171 output, output_size,
8172 &output_length);
8173 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008174 if (actual_status == PSA_SUCCESS) {
8175 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008176 } else {
8177 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008178 }
Gilles Peskine68428122018-06-30 18:42:41 +02008179 }
8180
Gilles Peskine656896e2018-06-29 19:12:28 +02008181exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008182 /*
8183 * Key attributes may have been returned by psa_get_key_attributes()
8184 * thus reset them as required.
8185 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008186 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008187
Gilles Peskine449bd832023-01-11 14:50:10 +01008188 psa_destroy_key(key);
8189 mbedtls_free(output);
8190 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008191}
8192/* END_CASE */
8193
8194/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008195void asymmetric_encrypt_decrypt(int key_type_arg,
8196 data_t *key_data,
8197 int alg_arg,
8198 data_t *input_data,
8199 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008200{
Ronald Cron5425a212020-08-04 14:58:35 +02008201 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008202 psa_key_type_t key_type = key_type_arg;
8203 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008204 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008205 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008206 size_t output_size;
8207 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008208 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008209 size_t output2_size;
8210 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008211 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008212
Gilles Peskine449bd832023-01-11 14:50:10 +01008213 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008214
Gilles Peskine449bd832023-01-11 14:50:10 +01008215 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8216 psa_set_key_algorithm(&attributes, alg);
8217 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008218
Gilles Peskine449bd832023-01-11 14:50:10 +01008219 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8220 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008221
8222 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008223 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8224 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008225
Gilles Peskine449bd832023-01-11 14:50:10 +01008226 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8227 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
8228 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008229
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008230 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008231 TEST_LE_U(output2_size,
8232 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8233 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
8234 ASSERT_ALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008235
Gilles Peskineeebd7382018-06-08 18:11:54 +02008236 /* We test encryption by checking that encrypt-then-decrypt gives back
8237 * the original plaintext because of the non-optional random
8238 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008239 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8240 input_data->x, input_data->len,
8241 label->x, label->len,
8242 output, output_size,
8243 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008244 /* We don't know what ciphertext length to expect, but check that
8245 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008246 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008247
Gilles Peskine449bd832023-01-11 14:50:10 +01008248 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8249 output, output_length,
8250 label->x, label->len,
8251 output2, output2_size,
8252 &output2_length));
8253 ASSERT_COMPARE(input_data->x, input_data->len,
8254 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008255
8256exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008257 /*
8258 * Key attributes may have been returned by psa_get_key_attributes()
8259 * thus reset them as required.
8260 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008261 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008262
Gilles Peskine449bd832023-01-11 14:50:10 +01008263 psa_destroy_key(key);
8264 mbedtls_free(output);
8265 mbedtls_free(output2);
8266 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008267}
8268/* END_CASE */
8269
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008270/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008271void asymmetric_decrypt(int key_type_arg,
8272 data_t *key_data,
8273 int alg_arg,
8274 data_t *input_data,
8275 data_t *label,
8276 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008277{
Ronald Cron5425a212020-08-04 14:58:35 +02008278 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008279 psa_key_type_t key_type = key_type_arg;
8280 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008281 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008282 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008283 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008284 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008285 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008286
Gilles Peskine449bd832023-01-11 14:50:10 +01008287 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008288
Gilles Peskine449bd832023-01-11 14:50:10 +01008289 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8290 psa_set_key_algorithm(&attributes, alg);
8291 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008292
Gilles Peskine449bd832023-01-11 14:50:10 +01008293 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8294 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008295
Gilles Peskine449bd832023-01-11 14:50:10 +01008296 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8297 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008298
8299 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008300 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8301 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
8302 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008303
Gilles Peskine449bd832023-01-11 14:50:10 +01008304 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8305 input_data->x, input_data->len,
8306 label->x, label->len,
8307 output,
8308 output_size,
8309 &output_length));
8310 ASSERT_COMPARE(expected_data->x, expected_data->len,
8311 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008312
Gilles Peskine68428122018-06-30 18:42:41 +02008313 /* If the label is empty, the test framework puts a non-null pointer
8314 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008315 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008316 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008317 if (output_size != 0) {
8318 memset(output, 0, output_size);
8319 }
8320 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8321 input_data->x, input_data->len,
8322 NULL, label->len,
8323 output,
8324 output_size,
8325 &output_length));
8326 ASSERT_COMPARE(expected_data->x, expected_data->len,
8327 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008328 }
8329
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008330exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008331 psa_reset_key_attributes(&attributes);
8332 psa_destroy_key(key);
8333 mbedtls_free(output);
8334 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008335}
8336/* END_CASE */
8337
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008338/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008339void asymmetric_decrypt_fail(int key_type_arg,
8340 data_t *key_data,
8341 int alg_arg,
8342 data_t *input_data,
8343 data_t *label,
8344 int output_size_arg,
8345 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008346{
Ronald Cron5425a212020-08-04 14:58:35 +02008347 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008348 psa_key_type_t key_type = key_type_arg;
8349 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008350 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008351 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008352 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008353 psa_status_t actual_status;
8354 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008355 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008356
Gilles Peskine449bd832023-01-11 14:50:10 +01008357 ASSERT_ALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008358
Gilles Peskine449bd832023-01-11 14:50:10 +01008359 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008360
Gilles Peskine449bd832023-01-11 14:50:10 +01008361 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8362 psa_set_key_algorithm(&attributes, alg);
8363 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008364
Gilles Peskine449bd832023-01-11 14:50:10 +01008365 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8366 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008367
Gilles Peskine449bd832023-01-11 14:50:10 +01008368 actual_status = psa_asymmetric_decrypt(key, alg,
8369 input_data->x, input_data->len,
8370 label->x, label->len,
8371 output, output_size,
8372 &output_length);
8373 TEST_EQUAL(actual_status, expected_status);
8374 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008375
Gilles Peskine68428122018-06-30 18:42:41 +02008376 /* If the label is empty, the test framework puts a non-null pointer
8377 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008378 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008379 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008380 if (output_size != 0) {
8381 memset(output, 0, output_size);
8382 }
8383 actual_status = psa_asymmetric_decrypt(key, alg,
8384 input_data->x, input_data->len,
8385 NULL, label->len,
8386 output, output_size,
8387 &output_length);
8388 TEST_EQUAL(actual_status, expected_status);
8389 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008390 }
8391
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008392exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008393 psa_reset_key_attributes(&attributes);
8394 psa_destroy_key(key);
8395 mbedtls_free(output);
8396 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008397}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008398/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008399
8400/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008401void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008402{
8403 /* Test each valid way of initializing the object, except for `= {0}`, as
8404 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8405 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008406 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008407 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008408 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008409 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8410 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008411
Gilles Peskine449bd832023-01-11 14:50:10 +01008412 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008413
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008414 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008415 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8416 PSA_ERROR_BAD_STATE);
8417 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8418 PSA_ERROR_BAD_STATE);
8419 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8420 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008421
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008422 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008423 PSA_ASSERT(psa_key_derivation_abort(&func));
8424 PSA_ASSERT(psa_key_derivation_abort(&init));
8425 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008426}
8427/* END_CASE */
8428
Janos Follath16de4a42019-06-13 16:32:24 +01008429/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008430void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008431{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008432 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008433 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008434 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008435
Gilles Peskine449bd832023-01-11 14:50:10 +01008436 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008437
Gilles Peskine449bd832023-01-11 14:50:10 +01008438 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8439 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008440
8441exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008442 psa_key_derivation_abort(&operation);
8443 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008444}
8445/* END_CASE */
8446
Janos Follathaf3c2a02019-06-12 12:34:34 +01008447/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008448void derive_set_capacity(int alg_arg, int capacity_arg,
8449 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008450{
8451 psa_algorithm_t alg = alg_arg;
8452 size_t capacity = capacity_arg;
8453 psa_status_t expected_status = expected_status_arg;
8454 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8455
Gilles Peskine449bd832023-01-11 14:50:10 +01008456 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008457
Gilles Peskine449bd832023-01-11 14:50:10 +01008458 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008459
Gilles Peskine449bd832023-01-11 14:50:10 +01008460 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8461 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008462
8463exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008464 psa_key_derivation_abort(&operation);
8465 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008466}
8467/* END_CASE */
8468
8469/* BEGIN_CASE */
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308470void parse_binary_string_test(data_t *input, int output)
8471{
8472 uint64_t value;
8473 value = parse_binary_string(input);
8474 TEST_EQUAL(value, output);
8475}
8476/* END_CASE */
8477
8478/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008479void derive_input(int alg_arg,
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308480 int step_arg1, int key_type_arg1, data_t *input1,
8481 int expected_status_arg1,
8482 int step_arg2, int key_type_arg2, data_t *input2,
8483 int expected_status_arg2,
8484 int step_arg3, int key_type_arg3, data_t *input3,
8485 int expected_status_arg3,
Gilles Peskine449bd832023-01-11 14:50:10 +01008486 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008487{
8488 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008489 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308490 uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
Gilles Peskine449bd832023-01-11 14:50:10 +01008491 psa_status_t expected_statuses[] = { expected_status_arg1,
8492 expected_status_arg2,
8493 expected_status_arg3 };
8494 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008495 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8496 MBEDTLS_SVC_KEY_ID_INIT,
8497 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008498 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8499 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8500 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008501 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008502 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008503 psa_status_t expected_output_status = expected_output_status_arg;
8504 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008505
Gilles Peskine449bd832023-01-11 14:50:10 +01008506 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008507
Gilles Peskine449bd832023-01-11 14:50:10 +01008508 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8509 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008510
Gilles Peskine449bd832023-01-11 14:50:10 +01008511 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008512
Gilles Peskine449bd832023-01-11 14:50:10 +01008513 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8514 mbedtls_test_set_step(i);
8515 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008516 /* Skip this step */
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308517 } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE &&
8518 key_types[i] != INPUT_INTEGER) {
8519 psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i]));
Gilles Peskine449bd832023-01-11 14:50:10 +01008520 PSA_ASSERT(psa_import_key(&attributes,
8521 inputs[i]->x, inputs[i]->len,
8522 &keys[i]));
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308523 if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008524 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008525 // When taking a private key as secret input, use key agreement
8526 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008527 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8528 &operation, keys[i]),
8529 expected_statuses[i]);
8530 } else {
8531 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8532 keys[i]),
8533 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008534 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008535 } else {
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308536 if (key_types[i] == INPUT_INTEGER) {
8537 TEST_EQUAL(psa_key_derivation_input_integer(
8538 &operation, steps[i],
8539 parse_binary_string(inputs[i])),
8540 expected_statuses[i]);
8541 } else {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308542 TEST_EQUAL(psa_key_derivation_input_bytes(
8543 &operation, steps[i],
8544 inputs[i]->x, inputs[i]->len),
8545 expected_statuses[i]);
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308546 }
Janos Follathaf3c2a02019-06-12 12:34:34 +01008547 }
8548 }
8549
Gilles Peskine449bd832023-01-11 14:50:10 +01008550 if (output_key_type != PSA_KEY_TYPE_NONE) {
8551 psa_reset_key_attributes(&attributes);
8552 psa_set_key_type(&attributes, output_key_type);
8553 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008554 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008555 psa_key_derivation_output_key(&attributes, &operation,
8556 &output_key);
8557 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008558 uint8_t buffer[1];
8559 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008560 psa_key_derivation_output_bytes(&operation,
8561 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008562 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008563 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008564
Janos Follathaf3c2a02019-06-12 12:34:34 +01008565exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008566 psa_key_derivation_abort(&operation);
8567 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8568 psa_destroy_key(keys[i]);
8569 }
8570 psa_destroy_key(output_key);
8571 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008572}
8573/* END_CASE */
8574
Janos Follathd958bb72019-07-03 15:02:16 +01008575/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008576void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008577{
Janos Follathd958bb72019-07-03 15:02:16 +01008578 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008579 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008580 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008581 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008582 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008583 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008584 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008585 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008586 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008587 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008588 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8589 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008590 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008591 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008592
Gilles Peskine449bd832023-01-11 14:50:10 +01008593 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008594
Gilles Peskine449bd832023-01-11 14:50:10 +01008595 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8596 psa_set_key_algorithm(&attributes, alg);
8597 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008598
Gilles Peskine449bd832023-01-11 14:50:10 +01008599 PSA_ASSERT(psa_import_key(&attributes,
8600 key_data, sizeof(key_data),
8601 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008602
8603 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008604 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8605 input1, input1_length,
8606 input2, input2_length,
8607 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008608 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008609 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008610
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008611 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008612 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8613 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008614
Gilles Peskine449bd832023-01-11 14:50:10 +01008615 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008616
Gilles Peskine449bd832023-01-11 14:50:10 +01008617 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8618 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008619
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008620exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008621 psa_key_derivation_abort(&operation);
8622 psa_destroy_key(key);
8623 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008624}
8625/* END_CASE */
8626
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008627/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008628void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008629{
8630 uint8_t output_buffer[16];
8631 size_t buffer_size = 16;
8632 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008633 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008634
Gilles Peskine449bd832023-01-11 14:50:10 +01008635 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8636 output_buffer, buffer_size)
8637 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008638
Gilles Peskine449bd832023-01-11 14:50:10 +01008639 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8640 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008641
Gilles Peskine449bd832023-01-11 14:50:10 +01008642 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008643
Gilles Peskine449bd832023-01-11 14:50:10 +01008644 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8645 output_buffer, buffer_size)
8646 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008647
Gilles Peskine449bd832023-01-11 14:50:10 +01008648 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8649 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008650
8651exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008652 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008653}
8654/* END_CASE */
8655
8656/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008657void derive_output(int alg_arg,
8658 int step1_arg, data_t *input1, int expected_status_arg1,
8659 int step2_arg, data_t *input2, int expected_status_arg2,
8660 int step3_arg, data_t *input3, int expected_status_arg3,
8661 int step4_arg, data_t *input4, int expected_status_arg4,
8662 data_t *key_agreement_peer_key,
8663 int requested_capacity_arg,
8664 data_t *expected_output1,
8665 data_t *expected_output2,
8666 int other_key_input_type,
8667 int key_input_type,
8668 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008669{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008670 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008671 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8672 data_t *inputs[] = { input1, input2, input3, input4 };
8673 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8674 MBEDTLS_SVC_KEY_ID_INIT,
8675 MBEDTLS_SVC_KEY_ID_INIT,
8676 MBEDTLS_SVC_KEY_ID_INIT };
8677 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8678 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008679 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008680 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008681 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008682 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008683 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008684 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008685 size_t output_buffer_size = 0;
8686 uint8_t *output_buffer = NULL;
8687 size_t expected_capacity;
8688 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008689 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8690 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8691 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8692 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008693 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008694 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008695 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008696
Gilles Peskine449bd832023-01-11 14:50:10 +01008697 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8698 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008699 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008700 }
8701 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008702 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008703 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008704 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008705 ASSERT_ALLOC(output_buffer, output_buffer_size);
8706 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008707
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008708 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008709 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8710 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8711 requested_capacity));
8712 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8713 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008714 case 0:
8715 break;
8716 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008717 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008718 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008719 TEST_EQUAL(psa_key_derivation_input_bytes(
8720 &operation, steps[i],
8721 inputs[i]->x, inputs[i]->len),
8722 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008723
Gilles Peskine449bd832023-01-11 14:50:10 +01008724 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008725 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008726 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008727 break;
8728 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008729 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8730 psa_set_key_algorithm(&attributes1, alg);
8731 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008732
Gilles Peskine449bd832023-01-11 14:50:10 +01008733 PSA_ASSERT(psa_import_key(&attributes1,
8734 inputs[i]->x, inputs[i]->len,
8735 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008736
Gilles Peskine449bd832023-01-11 14:50:10 +01008737 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8738 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8739 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8740 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008741 }
8742
Gilles Peskine449bd832023-01-11 14:50:10 +01008743 PSA_ASSERT(psa_key_derivation_input_key(&operation,
8744 steps[i],
8745 keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008746 break;
8747 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008748 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008749 break;
8750 }
8751 break;
8752 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008753 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008754 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008755 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8756 steps[i],
8757 inputs[i]->x,
8758 inputs[i]->len),
8759 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008760 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008761 case 1: // input key, type DERIVE
8762 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008763 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8764 psa_set_key_algorithm(&attributes2, alg);
8765 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008766
8767 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008768 if (other_key_input_type == 11) {
8769 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8770 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008771
Gilles Peskine449bd832023-01-11 14:50:10 +01008772 PSA_ASSERT(psa_import_key(&attributes2,
8773 inputs[i]->x, inputs[i]->len,
8774 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008775
Gilles Peskine449bd832023-01-11 14:50:10 +01008776 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8777 steps[i],
8778 keys[i]),
8779 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008780 break;
8781 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008782 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8783 psa_set_key_algorithm(&attributes3, alg);
8784 psa_set_key_type(&attributes3,
8785 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008786
Gilles Peskine449bd832023-01-11 14:50:10 +01008787 PSA_ASSERT(psa_import_key(&attributes3,
8788 inputs[i]->x, inputs[i]->len,
8789 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008790
Gilles Peskine449bd832023-01-11 14:50:10 +01008791 TEST_EQUAL(psa_key_derivation_key_agreement(
8792 &operation,
8793 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8794 keys[i], key_agreement_peer_key->x,
8795 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008796 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008797 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008798 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008799 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008800 }
8801
Gilles Peskine449bd832023-01-11 14:50:10 +01008802 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008803 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008804 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008805 break;
8806 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008807 TEST_EQUAL(psa_key_derivation_input_bytes(
8808 &operation, steps[i],
8809 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008810
Gilles Peskine449bd832023-01-11 14:50:10 +01008811 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008812 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008813 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008814 break;
8815 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008816 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008817
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8819 &current_capacity));
8820 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008821 expected_capacity = requested_capacity;
8822
Gilles Peskine449bd832023-01-11 14:50:10 +01008823 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008824 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8825
8826 /* For output key derivation secret must be provided using
8827 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008828 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008829 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008830 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008831
Gilles Peskine449bd832023-01-11 14:50:10 +01008832 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8833 psa_set_key_algorithm(&attributes4, alg);
8834 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8835 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008836
Gilles Peskine449bd832023-01-11 14:50:10 +01008837 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8838 &derived_key), expected_status);
8839 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008840 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008841 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008842 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008843 status = psa_key_derivation_output_bytes(&operation,
8844 output_buffer, output_sizes[i]);
8845 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008846 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008847 TEST_ASSERT(status == PSA_SUCCESS ||
8848 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008849 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008850 } else if (expected_capacity == 0 ||
8851 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008852 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008853 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008854 expected_capacity = 0;
8855 continue;
8856 }
8857 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008858 PSA_ASSERT(status);
8859 if (output_sizes[i] != 0) {
8860 ASSERT_COMPARE(output_buffer, output_sizes[i],
8861 expected_outputs[i], output_sizes[i]);
8862 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008863 /* Check the operation status. */
8864 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008865 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8866 &current_capacity));
8867 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008868 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008869 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008870 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008871
8872exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008873 mbedtls_free(output_buffer);
8874 psa_key_derivation_abort(&operation);
8875 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8876 psa_destroy_key(keys[i]);
8877 }
8878 psa_destroy_key(derived_key);
8879 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008880}
8881/* END_CASE */
8882
8883/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008884void derive_full(int alg_arg,
8885 data_t *key_data,
8886 data_t *input1,
8887 data_t *input2,
8888 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008889{
Ronald Cron5425a212020-08-04 14:58:35 +02008890 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008891 psa_algorithm_t alg = alg_arg;
8892 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008893 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008894 unsigned char output_buffer[16];
8895 size_t expected_capacity = requested_capacity;
8896 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008897 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008898
Gilles Peskine449bd832023-01-11 14:50:10 +01008899 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008900
Gilles Peskine449bd832023-01-11 14:50:10 +01008901 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8902 psa_set_key_algorithm(&attributes, alg);
8903 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008904
Gilles Peskine449bd832023-01-11 14:50:10 +01008905 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8906 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008907
Gilles Peskine449bd832023-01-11 14:50:10 +01008908 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8909 input1->x, input1->len,
8910 input2->x, input2->len,
8911 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008912 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008913 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008914
Gilles Peskine449bd832023-01-11 14:50:10 +01008915 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8916 &current_capacity));
8917 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008918
8919 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008920 while (current_capacity > 0) {
8921 size_t read_size = sizeof(output_buffer);
8922 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008923 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008924 }
8925 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8926 output_buffer,
8927 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008928 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008929 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8930 &current_capacity));
8931 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008932 }
8933
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008934 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008935 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8936 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008937
Gilles Peskine449bd832023-01-11 14:50:10 +01008938 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008939
8940exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008941 psa_key_derivation_abort(&operation);
8942 psa_destroy_key(key);
8943 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008944}
8945/* END_CASE */
8946
Przemek Stekiel8258ea72022-10-19 12:17:19 +02008947/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008948void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8949 int derivation_step,
8950 int capacity, int expected_capacity_status_arg,
8951 data_t *expected_output,
8952 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008953{
8954 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
8955 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04008956 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008957 uint8_t *output_buffer = NULL;
8958 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04008959 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
8960 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
8961 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008962
Gilles Peskine449bd832023-01-11 14:50:10 +01008963 ASSERT_ALLOC(output_buffer, expected_output->len);
8964 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008965
Gilles Peskine449bd832023-01-11 14:50:10 +01008966 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8967 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8968 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008969
Gilles Peskine449bd832023-01-11 14:50:10 +01008970 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8971 step, input->x, input->len),
8972 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008973
Gilles Peskine449bd832023-01-11 14:50:10 +01008974 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008975 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008976 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008977
Gilles Peskine449bd832023-01-11 14:50:10 +01008978 status = psa_key_derivation_output_bytes(&operation, output_buffer,
8979 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008980
Gilles Peskine449bd832023-01-11 14:50:10 +01008981 TEST_EQUAL(status, expected_output_status);
8982 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
8983 ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x,
8984 expected_output->len);
8985 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008986
8987exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008988 mbedtls_free(output_buffer);
8989 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008990 PSA_DONE();
8991}
8992/* END_CASE */
8993
Janos Follathe60c9052019-07-03 13:51:30 +01008994/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008995void derive_key_exercise(int alg_arg,
8996 data_t *key_data,
8997 data_t *input1,
8998 data_t *input2,
8999 int derived_type_arg,
9000 int derived_bits_arg,
9001 int derived_usage_arg,
9002 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009003{
Ronald Cron5425a212020-08-04 14:58:35 +02009004 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9005 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009006 psa_algorithm_t alg = alg_arg;
9007 psa_key_type_t derived_type = derived_type_arg;
9008 size_t derived_bits = derived_bits_arg;
9009 psa_key_usage_t derived_usage = derived_usage_arg;
9010 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009011 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009012 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009013 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009014 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009015
Gilles Peskine449bd832023-01-11 14:50:10 +01009016 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009017
Gilles Peskine449bd832023-01-11 14:50:10 +01009018 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9019 psa_set_key_algorithm(&attributes, alg);
9020 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9021 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9022 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009023
9024 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009025 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9026 input1->x, input1->len,
9027 input2->x, input2->len,
9028 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009029 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009030 }
Janos Follathe60c9052019-07-03 13:51:30 +01009031
Gilles Peskine449bd832023-01-11 14:50:10 +01009032 psa_set_key_usage_flags(&attributes, derived_usage);
9033 psa_set_key_algorithm(&attributes, derived_alg);
9034 psa_set_key_type(&attributes, derived_type);
9035 psa_set_key_bits(&attributes, derived_bits);
9036 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9037 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009038
9039 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009040 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9041 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9042 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009043
9044 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009045 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009046 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009047 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009048
9049exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009050 /*
9051 * Key attributes may have been returned by psa_get_key_attributes()
9052 * thus reset them as required.
9053 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009054 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009055
Gilles Peskine449bd832023-01-11 14:50:10 +01009056 psa_key_derivation_abort(&operation);
9057 psa_destroy_key(base_key);
9058 psa_destroy_key(derived_key);
9059 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009060}
9061/* END_CASE */
9062
Janos Follath42fd8882019-07-03 14:17:09 +01009063/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009064void derive_key_export(int alg_arg,
9065 data_t *key_data,
9066 data_t *input1,
9067 data_t *input2,
9068 int bytes1_arg,
9069 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009070{
Ronald Cron5425a212020-08-04 14:58:35 +02009071 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9072 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009073 psa_algorithm_t alg = alg_arg;
9074 size_t bytes1 = bytes1_arg;
9075 size_t bytes2 = bytes2_arg;
9076 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009077 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009078 uint8_t *output_buffer = NULL;
9079 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009080 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9081 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009082 size_t length;
9083
Gilles Peskine449bd832023-01-11 14:50:10 +01009084 ASSERT_ALLOC(output_buffer, capacity);
9085 ASSERT_ALLOC(export_buffer, capacity);
9086 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009087
Gilles Peskine449bd832023-01-11 14:50:10 +01009088 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9089 psa_set_key_algorithm(&base_attributes, alg);
9090 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9091 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9092 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009093
9094 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009095 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9096 input1->x, input1->len,
9097 input2->x, input2->len,
9098 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009099 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009100 }
Janos Follath42fd8882019-07-03 14:17:09 +01009101
Gilles Peskine449bd832023-01-11 14:50:10 +01009102 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9103 output_buffer,
9104 capacity));
9105 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009106
9107 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009108 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9109 input1->x, input1->len,
9110 input2->x, input2->len,
9111 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009112 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009113 }
Janos Follath42fd8882019-07-03 14:17:09 +01009114
Gilles Peskine449bd832023-01-11 14:50:10 +01009115 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9116 psa_set_key_algorithm(&derived_attributes, 0);
9117 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9118 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9119 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9120 &derived_key));
9121 PSA_ASSERT(psa_export_key(derived_key,
9122 export_buffer, bytes1,
9123 &length));
9124 TEST_EQUAL(length, bytes1);
9125 PSA_ASSERT(psa_destroy_key(derived_key));
9126 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9127 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9128 &derived_key));
9129 PSA_ASSERT(psa_export_key(derived_key,
9130 export_buffer + bytes1, bytes2,
9131 &length));
9132 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009133
9134 /* Compare the outputs from the two runs. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009135 ASSERT_COMPARE(output_buffer, bytes1 + bytes2,
9136 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009137
9138exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009139 mbedtls_free(output_buffer);
9140 mbedtls_free(export_buffer);
9141 psa_key_derivation_abort(&operation);
9142 psa_destroy_key(base_key);
9143 psa_destroy_key(derived_key);
9144 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009145}
9146/* END_CASE */
9147
9148/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009149void derive_key_type(int alg_arg,
9150 data_t *key_data,
9151 data_t *input1,
9152 data_t *input2,
9153 int key_type_arg, int bits_arg,
9154 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009155{
9156 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9157 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9158 const psa_algorithm_t alg = alg_arg;
9159 const psa_key_type_t key_type = key_type_arg;
9160 const size_t bits = bits_arg;
9161 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9162 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009163 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009164 uint8_t *export_buffer = NULL;
9165 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9166 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9167 size_t export_length;
9168
Gilles Peskine449bd832023-01-11 14:50:10 +01009169 ASSERT_ALLOC(export_buffer, export_buffer_size);
9170 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009171
Gilles Peskine449bd832023-01-11 14:50:10 +01009172 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9173 psa_set_key_algorithm(&base_attributes, alg);
9174 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9175 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9176 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009177
Gilles Peskine449bd832023-01-11 14:50:10 +01009178 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009179 &operation, base_key, alg,
9180 input1->x, input1->len,
9181 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009182 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009183 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009184 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009185
Gilles Peskine449bd832023-01-11 14:50:10 +01009186 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9187 psa_set_key_algorithm(&derived_attributes, 0);
9188 psa_set_key_type(&derived_attributes, key_type);
9189 psa_set_key_bits(&derived_attributes, bits);
9190 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9191 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009192
Gilles Peskine449bd832023-01-11 14:50:10 +01009193 PSA_ASSERT(psa_export_key(derived_key,
9194 export_buffer, export_buffer_size,
9195 &export_length));
9196 ASSERT_COMPARE(export_buffer, export_length,
9197 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009198
9199exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009200 mbedtls_free(export_buffer);
9201 psa_key_derivation_abort(&operation);
9202 psa_destroy_key(base_key);
9203 psa_destroy_key(derived_key);
9204 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009205}
9206/* END_CASE */
9207
9208/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009209void derive_key(int alg_arg,
9210 data_t *key_data, data_t *input1, data_t *input2,
9211 int type_arg, int bits_arg,
9212 int expected_status_arg,
9213 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009214{
Ronald Cron5425a212020-08-04 14:58:35 +02009215 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9216 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009217 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009218 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009219 size_t bits = bits_arg;
9220 psa_status_t expected_status = expected_status_arg;
9221 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9222 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9223 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9224
Gilles Peskine449bd832023-01-11 14:50:10 +01009225 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009226
Gilles Peskine449bd832023-01-11 14:50:10 +01009227 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9228 psa_set_key_algorithm(&base_attributes, alg);
9229 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9230 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9231 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009232
Gilles Peskine449bd832023-01-11 14:50:10 +01009233 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9234 input1->x, input1->len,
9235 input2->x, input2->len,
9236 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009237 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009238 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009239
Gilles Peskine449bd832023-01-11 14:50:10 +01009240 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9241 psa_set_key_algorithm(&derived_attributes, 0);
9242 psa_set_key_type(&derived_attributes, type);
9243 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009244
9245 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009246 psa_key_derivation_output_key(&derived_attributes,
9247 &operation,
9248 &derived_key);
9249 if (is_large_output > 0) {
9250 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9251 }
9252 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009253
9254exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009255 psa_key_derivation_abort(&operation);
9256 psa_destroy_key(base_key);
9257 psa_destroy_key(derived_key);
9258 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009259}
9260/* END_CASE */
9261
9262/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009263void key_agreement_setup(int alg_arg,
9264 int our_key_type_arg, int our_key_alg_arg,
9265 data_t *our_key_data, data_t *peer_key_data,
9266 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009267{
Ronald Cron5425a212020-08-04 14:58:35 +02009268 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009269 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009270 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009271 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009272 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009273 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009274 psa_status_t expected_status = expected_status_arg;
9275 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009276
Gilles Peskine449bd832023-01-11 14:50:10 +01009277 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009278
Gilles Peskine449bd832023-01-11 14:50:10 +01009279 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9280 psa_set_key_algorithm(&attributes, our_key_alg);
9281 psa_set_key_type(&attributes, our_key_type);
9282 PSA_ASSERT(psa_import_key(&attributes,
9283 our_key_data->x, our_key_data->len,
9284 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009285
Gilles Peskine77f40d82019-04-11 21:27:06 +02009286 /* The tests currently include inputs that should fail at either step.
9287 * Test cases that fail at the setup step should be changed to call
9288 * key_derivation_setup instead, and this function should be renamed
9289 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009290 status = psa_key_derivation_setup(&operation, alg);
9291 if (status == PSA_SUCCESS) {
9292 TEST_EQUAL(psa_key_derivation_key_agreement(
9293 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9294 our_key,
9295 peer_key_data->x, peer_key_data->len),
9296 expected_status);
9297 } else {
9298 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009299 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009300
9301exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009302 psa_key_derivation_abort(&operation);
9303 psa_destroy_key(our_key);
9304 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009305}
9306/* END_CASE */
9307
9308/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009309void raw_key_agreement(int alg_arg,
9310 int our_key_type_arg, data_t *our_key_data,
9311 data_t *peer_key_data,
9312 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009313{
Ronald Cron5425a212020-08-04 14:58:35 +02009314 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009315 psa_algorithm_t alg = alg_arg;
9316 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009317 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009318 unsigned char *output = NULL;
9319 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009320 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009321
Gilles Peskine449bd832023-01-11 14:50:10 +01009322 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009323
Gilles Peskine449bd832023-01-11 14:50:10 +01009324 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9325 psa_set_key_algorithm(&attributes, alg);
9326 psa_set_key_type(&attributes, our_key_type);
9327 PSA_ASSERT(psa_import_key(&attributes,
9328 our_key_data->x, our_key_data->len,
9329 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009330
Gilles Peskine449bd832023-01-11 14:50:10 +01009331 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9332 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009333
Gilles Peskine992bee82022-04-13 23:25:52 +02009334 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009335 TEST_LE_U(expected_output->len,
9336 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9337 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9338 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009339
9340 /* Good case with exact output size */
Gilles Peskine449bd832023-01-11 14:50:10 +01009341 ASSERT_ALLOC(output, expected_output->len);
9342 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9343 peer_key_data->x, peer_key_data->len,
9344 output, expected_output->len,
9345 &output_length));
9346 ASSERT_COMPARE(output, output_length,
9347 expected_output->x, expected_output->len);
9348 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009349 output = NULL;
9350 output_length = ~0;
9351
9352 /* Larger buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009353 ASSERT_ALLOC(output, expected_output->len + 1);
9354 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9355 peer_key_data->x, peer_key_data->len,
9356 output, expected_output->len + 1,
9357 &output_length));
9358 ASSERT_COMPARE(output, output_length,
9359 expected_output->x, expected_output->len);
9360 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009361 output = NULL;
9362 output_length = ~0;
9363
9364 /* Buffer too small */
Gilles Peskine449bd832023-01-11 14:50:10 +01009365 ASSERT_ALLOC(output, expected_output->len - 1);
9366 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9367 peer_key_data->x, peer_key_data->len,
9368 output, expected_output->len - 1,
9369 &output_length),
9370 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009371 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009372 TEST_LE_U(output_length, expected_output->len - 1);
9373 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009374 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009375
9376exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009377 mbedtls_free(output);
9378 psa_destroy_key(our_key);
9379 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009380}
9381/* END_CASE */
9382
9383/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009384void key_agreement_capacity(int alg_arg,
9385 int our_key_type_arg, data_t *our_key_data,
9386 data_t *peer_key_data,
9387 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009388{
Ronald Cron5425a212020-08-04 14:58:35 +02009389 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009390 psa_algorithm_t alg = alg_arg;
9391 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009392 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009393 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009394 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009395 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009396
Gilles Peskine449bd832023-01-11 14:50:10 +01009397 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009398
Gilles Peskine449bd832023-01-11 14:50:10 +01009399 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9400 psa_set_key_algorithm(&attributes, alg);
9401 psa_set_key_type(&attributes, our_key_type);
9402 PSA_ASSERT(psa_import_key(&attributes,
9403 our_key_data->x, our_key_data->len,
9404 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009405
Gilles Peskine449bd832023-01-11 14:50:10 +01009406 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9407 PSA_ASSERT(psa_key_derivation_key_agreement(
9408 &operation,
9409 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9410 peer_key_data->x, peer_key_data->len));
9411 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009412 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009413 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9414 PSA_KEY_DERIVATION_INPUT_INFO,
9415 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009416 }
Gilles Peskine59685592018-09-18 12:11:34 +02009417
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009418 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009419 PSA_ASSERT(psa_key_derivation_get_capacity(
9420 &operation, &actual_capacity));
9421 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009422
Gilles Peskinebf491972018-10-25 22:36:12 +02009423 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009424 while (actual_capacity > sizeof(output)) {
9425 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9426 output, sizeof(output)));
9427 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009428 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009429 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9430 output, actual_capacity));
9431 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9432 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009433
Gilles Peskine59685592018-09-18 12:11:34 +02009434exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009435 psa_key_derivation_abort(&operation);
9436 psa_destroy_key(our_key);
9437 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009438}
9439/* END_CASE */
9440
9441/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009442void key_agreement_output(int alg_arg,
9443 int our_key_type_arg, data_t *our_key_data,
9444 data_t *peer_key_data,
9445 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009446{
Ronald Cron5425a212020-08-04 14:58:35 +02009447 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009448 psa_algorithm_t alg = alg_arg;
9449 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009450 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009451 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009452 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009453
Gilles Peskine449bd832023-01-11 14:50:10 +01009454 ASSERT_ALLOC(actual_output, MAX(expected_output1->len,
9455 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009456
Gilles Peskine449bd832023-01-11 14:50:10 +01009457 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009458
Gilles Peskine449bd832023-01-11 14:50:10 +01009459 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9460 psa_set_key_algorithm(&attributes, alg);
9461 psa_set_key_type(&attributes, our_key_type);
9462 PSA_ASSERT(psa_import_key(&attributes,
9463 our_key_data->x, our_key_data->len,
9464 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009465
Gilles Peskine449bd832023-01-11 14:50:10 +01009466 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9467 PSA_ASSERT(psa_key_derivation_key_agreement(
9468 &operation,
9469 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9470 peer_key_data->x, peer_key_data->len));
9471 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009472 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009473 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9474 PSA_KEY_DERIVATION_INPUT_INFO,
9475 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009476 }
Gilles Peskine59685592018-09-18 12:11:34 +02009477
Gilles Peskine449bd832023-01-11 14:50:10 +01009478 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9479 actual_output,
9480 expected_output1->len));
9481 ASSERT_COMPARE(actual_output, expected_output1->len,
9482 expected_output1->x, expected_output1->len);
9483 if (expected_output2->len != 0) {
9484 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9485 actual_output,
9486 expected_output2->len));
9487 ASSERT_COMPARE(actual_output, expected_output2->len,
9488 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009489 }
Gilles Peskine59685592018-09-18 12:11:34 +02009490
9491exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009492 psa_key_derivation_abort(&operation);
9493 psa_destroy_key(our_key);
9494 PSA_DONE();
9495 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009496}
9497/* END_CASE */
9498
9499/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009500void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009501{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009502 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009503 unsigned char *output = NULL;
9504 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009505 size_t i;
9506 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009507
Gilles Peskine449bd832023-01-11 14:50:10 +01009508 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009509
Gilles Peskine449bd832023-01-11 14:50:10 +01009510 ASSERT_ALLOC(output, bytes);
9511 ASSERT_ALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009512
Gilles Peskine449bd832023-01-11 14:50:10 +01009513 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009514
Gilles Peskinea50d7392018-06-21 10:22:13 +02009515 /* Run several times, to ensure that every output byte will be
9516 * nonzero at least once with overwhelming probability
9517 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009518 for (run = 0; run < 10; run++) {
9519 if (bytes != 0) {
9520 memset(output, 0, bytes);
9521 }
9522 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009523
Gilles Peskine449bd832023-01-11 14:50:10 +01009524 for (i = 0; i < bytes; i++) {
9525 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009526 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009527 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009528 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009529 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009530
9531 /* Check that every byte was changed to nonzero at least once. This
9532 * validates that psa_generate_random is overwriting every byte of
9533 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009534 for (i = 0; i < bytes; i++) {
9535 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009536 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009537
9538exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009539 PSA_DONE();
9540 mbedtls_free(output);
9541 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009542}
9543/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009544
9545/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009546void generate_key(int type_arg,
9547 int bits_arg,
9548 int usage_arg,
9549 int alg_arg,
9550 int expected_status_arg,
9551 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009552{
Ronald Cron5425a212020-08-04 14:58:35 +02009553 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009554 psa_key_type_t type = type_arg;
9555 psa_key_usage_t usage = usage_arg;
9556 size_t bits = bits_arg;
9557 psa_algorithm_t alg = alg_arg;
9558 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009559 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009560 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009561
Gilles Peskine449bd832023-01-11 14:50:10 +01009562 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009563
Gilles Peskine449bd832023-01-11 14:50:10 +01009564 psa_set_key_usage_flags(&attributes, usage);
9565 psa_set_key_algorithm(&attributes, alg);
9566 psa_set_key_type(&attributes, type);
9567 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009568
9569 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009570 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009571
Gilles Peskine449bd832023-01-11 14:50:10 +01009572 if (is_large_key > 0) {
9573 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9574 }
9575 TEST_EQUAL(status, expected_status);
9576 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009577 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009578 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009579
9580 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009581 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9582 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9583 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009584
Gilles Peskine818ca122018-06-20 18:16:48 +02009585 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009586 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009587 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009588 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009589
9590exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009591 /*
9592 * Key attributes may have been returned by psa_get_key_attributes()
9593 * thus reset them as required.
9594 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009595 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009596
Gilles Peskine449bd832023-01-11 14:50:10 +01009597 psa_destroy_key(key);
9598 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009599}
9600/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009601
Ronald Cronee414c72021-03-18 18:50:08 +01009602/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN:MBEDTLS_GENPRIME */
Gilles Peskine449bd832023-01-11 14:50:10 +01009603void generate_key_rsa(int bits_arg,
9604 data_t *e_arg,
9605 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009606{
Ronald Cron5425a212020-08-04 14:58:35 +02009607 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009608 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009609 size_t bits = bits_arg;
9610 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9611 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9612 psa_status_t expected_status = expected_status_arg;
9613 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9614 uint8_t *exported = NULL;
9615 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009616 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009617 size_t exported_length = SIZE_MAX;
9618 uint8_t *e_read_buffer = NULL;
9619 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009620 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009621 size_t e_read_length = SIZE_MAX;
9622
Gilles Peskine449bd832023-01-11 14:50:10 +01009623 if (e_arg->len == 0 ||
9624 (e_arg->len == 3 &&
9625 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009626 is_default_public_exponent = 1;
9627 e_read_size = 0;
9628 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009629 ASSERT_ALLOC(e_read_buffer, e_read_size);
9630 ASSERT_ALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009631
Gilles Peskine449bd832023-01-11 14:50:10 +01009632 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009633
Gilles Peskine449bd832023-01-11 14:50:10 +01009634 psa_set_key_usage_flags(&attributes, usage);
9635 psa_set_key_algorithm(&attributes, alg);
9636 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9637 e_arg->x, e_arg->len));
9638 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009639
9640 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009641 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9642 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009643 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009644 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009645
9646 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009647 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9648 TEST_EQUAL(psa_get_key_type(&attributes), type);
9649 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9650 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
9651 e_read_buffer, e_read_size,
9652 &e_read_length));
9653 if (is_default_public_exponent) {
9654 TEST_EQUAL(e_read_length, 0);
9655 } else {
9656 ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
9657 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009658
9659 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009660 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009661 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009662 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009663
9664 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009665 PSA_ASSERT(psa_export_public_key(key,
9666 exported, exported_size,
9667 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009668 {
9669 uint8_t *p = exported;
9670 uint8_t *end = exported + exported_length;
9671 size_t len;
9672 /* RSAPublicKey ::= SEQUENCE {
9673 * modulus INTEGER, -- n
9674 * publicExponent INTEGER } -- e
9675 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009676 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9677 MBEDTLS_ASN1_SEQUENCE |
9678 MBEDTLS_ASN1_CONSTRUCTED));
9679 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9680 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9681 MBEDTLS_ASN1_INTEGER));
9682 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009683 ++p;
9684 --len;
9685 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009686 if (e_arg->len == 0) {
9687 TEST_EQUAL(len, 3);
9688 TEST_EQUAL(p[0], 1);
9689 TEST_EQUAL(p[1], 0);
9690 TEST_EQUAL(p[2], 1);
9691 } else {
9692 ASSERT_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009693 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009694 }
9695
9696exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009697 /*
9698 * Key attributes may have been returned by psa_get_key_attributes() or
9699 * set by psa_set_key_domain_parameters() thus reset them as required.
9700 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009701 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009702
Gilles Peskine449bd832023-01-11 14:50:10 +01009703 psa_destroy_key(key);
9704 PSA_DONE();
9705 mbedtls_free(e_read_buffer);
9706 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009707}
9708/* END_CASE */
9709
Darryl Greend49a4992018-06-18 17:27:26 +01009710/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009711void persistent_key_load_key_from_storage(data_t *data,
9712 int type_arg, int bits_arg,
9713 int usage_flags_arg, int alg_arg,
9714 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009715{
Gilles Peskine449bd832023-01-11 14:50:10 +01009716 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009717 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009718 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9719 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009720 psa_key_type_t type = type_arg;
9721 size_t bits = bits_arg;
9722 psa_key_usage_t usage_flags = usage_flags_arg;
9723 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009724 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009725 unsigned char *first_export = NULL;
9726 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009727 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009728 size_t first_exported_length;
9729 size_t second_exported_length;
9730
Gilles Peskine449bd832023-01-11 14:50:10 +01009731 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9732 ASSERT_ALLOC(first_export, export_size);
9733 ASSERT_ALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009734 }
Darryl Greend49a4992018-06-18 17:27:26 +01009735
Gilles Peskine449bd832023-01-11 14:50:10 +01009736 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009737
Gilles Peskine449bd832023-01-11 14:50:10 +01009738 psa_set_key_id(&attributes, key_id);
9739 psa_set_key_usage_flags(&attributes, usage_flags);
9740 psa_set_key_algorithm(&attributes, alg);
9741 psa_set_key_type(&attributes, type);
9742 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009743
Gilles Peskine449bd832023-01-11 14:50:10 +01009744 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009745 case IMPORT_KEY:
9746 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009747 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9748 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009749 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009750
Darryl Green0c6575a2018-11-07 16:05:30 +00009751 case GENERATE_KEY:
9752 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009753 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009754 break;
9755
9756 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009757#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009758 {
9759 /* Create base key */
9760 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9761 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9762 psa_set_key_usage_flags(&base_attributes,
9763 PSA_KEY_USAGE_DERIVE);
9764 psa_set_key_algorithm(&base_attributes, derive_alg);
9765 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9766 PSA_ASSERT(psa_import_key(&base_attributes,
9767 data->x, data->len,
9768 &base_key));
9769 /* Derive a key. */
9770 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9771 PSA_ASSERT(psa_key_derivation_input_key(
9772 &operation,
9773 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9774 PSA_ASSERT(psa_key_derivation_input_bytes(
9775 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9776 NULL, 0));
9777 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9778 &operation,
9779 &key));
9780 PSA_ASSERT(psa_key_derivation_abort(&operation));
9781 PSA_ASSERT(psa_destroy_key(base_key));
9782 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9783 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009784#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009785 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009786#endif
9787 break;
9788
9789 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009790 TEST_ASSERT(!"generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009791 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009792 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009793 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009794
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009795 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009796 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9797 PSA_ASSERT(psa_export_key(key,
9798 first_export, export_size,
9799 &first_exported_length));
9800 if (generation_method == IMPORT_KEY) {
9801 ASSERT_COMPARE(data->x, data->len,
9802 first_export, first_exported_length);
9803 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009804 }
Darryl Greend49a4992018-06-18 17:27:26 +01009805
9806 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009807 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009808 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009809 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009810
Darryl Greend49a4992018-06-18 17:27:26 +01009811 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009812 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9813 TEST_ASSERT(mbedtls_svc_key_id_equal(
9814 psa_get_key_id(&attributes), key_id));
9815 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9816 PSA_KEY_LIFETIME_PERSISTENT);
9817 TEST_EQUAL(psa_get_key_type(&attributes), type);
9818 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9819 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9820 mbedtls_test_update_key_usage_flags(usage_flags));
9821 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009822
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009823 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009824 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9825 PSA_ASSERT(psa_export_key(key,
9826 second_export, export_size,
9827 &second_exported_length));
9828 ASSERT_COMPARE(first_export, first_exported_length,
9829 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009830 }
9831
9832 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009833 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009834 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009835 }
Darryl Greend49a4992018-06-18 17:27:26 +01009836
9837exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009838 /*
9839 * Key attributes may have been returned by psa_get_key_attributes()
9840 * thus reset them as required.
9841 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009842 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009843
Gilles Peskine449bd832023-01-11 14:50:10 +01009844 mbedtls_free(first_export);
9845 mbedtls_free(second_export);
9846 psa_key_derivation_abort(&operation);
9847 psa_destroy_key(base_key);
9848 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009849 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009850}
9851/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009852
Neil Armstronga557cb82022-06-10 08:58:32 +02009853/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009854void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9855 int primitive_arg, int hash_arg, int role_arg,
9856 int test_input, data_t *pw_data,
9857 int inj_err_type_arg,
9858 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009859{
9860 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9861 psa_pake_operation_t operation = psa_pake_operation_init();
9862 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009863 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009864 psa_key_type_t key_type_pw = key_type_pw_arg;
9865 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009866 psa_algorithm_t hash_alg = hash_arg;
9867 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009868 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9869 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009870 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9871 psa_status_t expected_error = expected_error_arg;
9872 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009873 unsigned char *output_buffer = NULL;
9874 size_t output_len = 0;
9875
Gilles Peskine449bd832023-01-11 14:50:10 +01009876 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009877
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009878 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009879 PSA_PAKE_STEP_KEY_SHARE);
9880 ASSERT_ALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009881
Gilles Peskine449bd832023-01-11 14:50:10 +01009882 if (pw_data->len > 0) {
9883 psa_set_key_usage_flags(&attributes, key_usage_pw);
9884 psa_set_key_algorithm(&attributes, alg);
9885 psa_set_key_type(&attributes, key_type_pw);
9886 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9887 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009888 }
9889
Gilles Peskine449bd832023-01-11 14:50:10 +01009890 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9891 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9892 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009893
Gilles Peskine449bd832023-01-11 14:50:10 +01009894 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009895
Gilles Peskine449bd832023-01-11 14:50:10 +01009896 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9897 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9898 expected_error);
9899 PSA_ASSERT(psa_pake_abort(&operation));
9900 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9901 expected_error);
9902 PSA_ASSERT(psa_pake_abort(&operation));
9903 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9904 expected_error);
9905 PSA_ASSERT(psa_pake_abort(&operation));
9906 TEST_EQUAL(psa_pake_set_role(&operation, role),
9907 expected_error);
9908 PSA_ASSERT(psa_pake_abort(&operation));
9909 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9910 NULL, 0, NULL),
9911 expected_error);
9912 PSA_ASSERT(psa_pake_abort(&operation));
9913 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9914 expected_error);
9915 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009916 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009917 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009918
Gilles Peskine449bd832023-01-11 14:50:10 +01009919 status = psa_pake_setup(&operation, &cipher_suite);
9920 if (status != PSA_SUCCESS) {
9921 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009922 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009923 }
9924
Gilles Peskine449bd832023-01-11 14:50:10 +01009925 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9926 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9927 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009928 goto exit;
9929 }
9930
Gilles Peskine449bd832023-01-11 14:50:10 +01009931 status = psa_pake_set_role(&operation, role);
9932 if (status != PSA_SUCCESS) {
9933 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009934 goto exit;
9935 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009936
Gilles Peskine449bd832023-01-11 14:50:10 +01009937 if (pw_data->len > 0) {
9938 status = psa_pake_set_password_key(&operation, key);
9939 if (status != PSA_SUCCESS) {
9940 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009941 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009942 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009943 }
9944
Gilles Peskine449bd832023-01-11 14:50:10 +01009945 if (inj_err_type == INJECT_ERR_INVALID_USER) {
9946 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9947 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009948 goto exit;
9949 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009950
Gilles Peskine449bd832023-01-11 14:50:10 +01009951 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
9952 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9953 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009954 goto exit;
9955 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009956
Gilles Peskine449bd832023-01-11 14:50:10 +01009957 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009958 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009959 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
9960 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009961 goto exit;
9962 }
9963
Gilles Peskine449bd832023-01-11 14:50:10 +01009964 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009965 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009966 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
9967 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009968 goto exit;
9969 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009970
Gilles Peskine449bd832023-01-11 14:50:10 +01009971 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
9972 PSA_PAKE_STEP_KEY_SHARE);
9973 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
9974 PSA_PAKE_STEP_ZK_PUBLIC);
9975 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
9976 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009977
Gilles Peskine449bd832023-01-11 14:50:10 +01009978 if (test_input) {
9979 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9980 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
9981 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009982 goto exit;
9983 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009984
Gilles Peskine449bd832023-01-11 14:50:10 +01009985 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9986 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9987 output_buffer, size_zk_proof),
9988 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009989 goto exit;
9990 }
9991
Gilles Peskine449bd832023-01-11 14:50:10 +01009992 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9993 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
9994 output_buffer, size_zk_proof),
9995 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009996 goto exit;
9997 }
9998
Gilles Peskine449bd832023-01-11 14:50:10 +01009999 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
10000 output_buffer, size_key_share);
10001 if (status != PSA_SUCCESS) {
10002 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010003 goto exit;
10004 }
10005
Gilles Peskine449bd832023-01-11 14:50:10 +010010006 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10007 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10008 output_buffer, size_zk_public + 1),
10009 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010010 goto exit;
10011 }
10012
Gilles Peskine449bd832023-01-11 14:50:10 +010010013 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010014 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010015 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10016 output_buffer, size_zk_public + 1);
10017 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10018 output_buffer, size_zk_public),
10019 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010020 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010021 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010022 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010023 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10024 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10025 NULL, 0, NULL),
10026 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010027 goto exit;
10028 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010029
Gilles Peskine449bd832023-01-11 14:50:10 +010010030 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10031 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10032 output_buffer, buf_size, &output_len),
10033 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010034 goto exit;
10035 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010036
Gilles Peskine449bd832023-01-11 14:50:10 +010010037 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10038 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10039 output_buffer, buf_size, &output_len),
10040 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010041 goto exit;
10042 }
10043
Gilles Peskine449bd832023-01-11 14:50:10 +010010044 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10045 output_buffer, buf_size, &output_len);
10046 if (status != PSA_SUCCESS) {
10047 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010048 goto exit;
10049 }
10050
Gilles Peskine449bd832023-01-11 14:50:10 +010010051 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010052
Gilles Peskine449bd832023-01-11 14:50:10 +010010053 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10054 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10055 output_buffer, size_zk_public - 1, &output_len),
10056 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010057 goto exit;
10058 }
10059
Gilles Peskine449bd832023-01-11 14:50:10 +010010060 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010061 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010062 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10063 output_buffer, size_zk_public - 1, &output_len);
10064 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10065 output_buffer, buf_size, &output_len),
10066 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010067 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010068 }
10069 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010070
10071exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010072 PSA_ASSERT(psa_destroy_key(key));
10073 PSA_ASSERT(psa_pake_abort(&operation));
10074 mbedtls_free(output_buffer);
10075 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010076}
10077/* END_CASE */
10078
Neil Armstronga557cb82022-06-10 08:58:32 +020010079/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010080void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10081 int client_input_first, int inject_error,
10082 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010083{
10084 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10085 psa_pake_operation_t server = psa_pake_operation_init();
10086 psa_pake_operation_t client = psa_pake_operation_init();
10087 psa_algorithm_t alg = alg_arg;
10088 psa_algorithm_t hash_alg = hash_arg;
10089 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10090 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10091
Gilles Peskine449bd832023-01-11 14:50:10 +010010092 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010093
Gilles Peskine449bd832023-01-11 14:50:10 +010010094 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10095 psa_set_key_algorithm(&attributes, alg);
10096 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10097 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10098 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010099
Gilles Peskine449bd832023-01-11 14:50:10 +010010100 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10101 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10102 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010103
10104
Gilles Peskine449bd832023-01-11 14:50:10 +010010105 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10106 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010107
Gilles Peskine449bd832023-01-11 14:50:10 +010010108 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10109 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010110
Gilles Peskine449bd832023-01-11 14:50:10 +010010111 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10112 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010113
Gilles Peskine449bd832023-01-11 14:50:10 +010010114 ecjpake_do_round(alg, primitive_arg, &server, &client,
10115 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010116
Gilles Peskine449bd832023-01-11 14:50:10 +010010117 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010118 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010119 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010120
Gilles Peskine449bd832023-01-11 14:50:10 +010010121 ecjpake_do_round(alg, primitive_arg, &server, &client,
10122 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010123
10124exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010125 psa_destroy_key(key);
10126 psa_pake_abort(&server);
10127 psa_pake_abort(&client);
10128 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010129}
10130/* END_CASE */
10131
10132/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010133void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10134 int derive_alg_arg, data_t *pw_data,
10135 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010136{
10137 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10138 psa_pake_operation_t server = psa_pake_operation_init();
10139 psa_pake_operation_t client = psa_pake_operation_init();
10140 psa_algorithm_t alg = alg_arg;
10141 psa_algorithm_t hash_alg = hash_arg;
10142 psa_algorithm_t derive_alg = derive_alg_arg;
10143 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10145 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010146 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010147 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010148 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010149 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010150
Gilles Peskine449bd832023-01-11 14:50:10 +010010151 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010152
Gilles Peskine449bd832023-01-11 14:50:10 +010010153 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10154 psa_set_key_algorithm(&attributes, alg);
10155 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10156 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10157 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010158
Gilles Peskine449bd832023-01-11 14:50:10 +010010159 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10160 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10161 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010162
Neil Armstrong1e855602022-06-15 11:32:11 +020010163 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010164 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10165 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010166
Gilles Peskine449bd832023-01-11 14:50:10 +010010167 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10168 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10169 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10170 PSA_KEY_DERIVATION_INPUT_SEED,
10171 (const uint8_t *) "", 0));
10172 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10173 PSA_KEY_DERIVATION_INPUT_SEED,
10174 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010175 }
10176
Gilles Peskine449bd832023-01-11 14:50:10 +010010177 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10178 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010179
Gilles Peskine449bd832023-01-11 14:50:10 +010010180 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10181 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010182
Gilles Peskine449bd832023-01-11 14:50:10 +010010183 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10184 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010185
Gilles Peskine449bd832023-01-11 14:50:10 +010010186 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10187 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10188 PSA_ERROR_BAD_STATE);
10189 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10190 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010191 goto exit;
10192 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010193
Neil Armstrongf983caf2022-06-15 15:27:48 +020010194 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010195 ecjpake_do_round(alg, primitive_arg, &server, &client,
10196 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010197
Gilles Peskine449bd832023-01-11 14:50:10 +010010198 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10199 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10200 PSA_ERROR_BAD_STATE);
10201 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10202 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010203 goto exit;
10204 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010205
Neil Armstrongf983caf2022-06-15 15:27:48 +020010206 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010207 ecjpake_do_round(alg, primitive_arg, &server, &client,
10208 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010209
Gilles Peskine449bd832023-01-11 14:50:10 +010010210 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10211 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010212
10213exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010214 psa_key_derivation_abort(&server_derive);
10215 psa_key_derivation_abort(&client_derive);
10216 psa_destroy_key(key);
10217 psa_pake_abort(&server);
10218 psa_pake_abort(&client);
10219 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010220}
10221/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010222
10223/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010224void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010225{
10226 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10227 const size_t bits = 256;
10228 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010229 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010230 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010231 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010232
10233 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10234 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010235 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10236 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10237 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10238 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010239 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010240 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10241 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010242
10243 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010244 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10245 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10246 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10247 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10248 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10249 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010250
10251 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010252 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10253 PSA_PAKE_OUTPUT_MAX_SIZE);
10254 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10255 PSA_PAKE_OUTPUT_MAX_SIZE);
10256 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10257 PSA_PAKE_OUTPUT_MAX_SIZE);
10258 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10259 PSA_PAKE_INPUT_MAX_SIZE);
10260 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10261 PSA_PAKE_INPUT_MAX_SIZE);
10262 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10263 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010264}
10265/* END_CASE */