blob: 6f528f10c087682c7a4afe85caadaa3676eaab82 [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
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001533 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001535
1536 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1538 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1539 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1540 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001541
1542 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 status = psa_export_key(key, exported, export_size, &exported_length);
1544 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001545
1546 /* The exported length must be set by psa_export_key() to a value between 0
1547 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1549 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1550 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1553 export_size - exported_length));
1554 if (status != PSA_SUCCESS) {
1555 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001556 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001557 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001558
Gilles Peskineea38a922021-02-13 00:05:16 +01001559 /* Run sanity checks on the exported key. For non-canonical inputs,
1560 * this validates the canonical representations. For canonical inputs,
1561 * this doesn't directly validate the implementation, but it still helps
1562 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 if (!psa_key_lifetime_is_external(lifetime)) {
1564 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301565 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 }
Archana4d7ae1d2021-07-07 02:50:22 +05301567 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001568
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 if (canonical_input) {
1570 ASSERT_COMPARE(data->x, data->len, exported, exported_length);
1571 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001572 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1574 &key2));
1575 PSA_ASSERT(psa_export_key(key2,
1576 reexported,
1577 export_size,
1578 &reexported_length));
1579 ASSERT_COMPARE(exported, exported_length,
1580 reexported, reexported_length);
1581 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001582 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 TEST_LE_U(exported_length,
1584 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1585 psa_get_key_bits(&got_attributes)));
1586 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001587
1588destroy:
1589 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 PSA_ASSERT(psa_destroy_key(key));
1591 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001592
1593exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001594 /*
1595 * Key attributes may have been returned by psa_get_key_attributes()
1596 * thus reset them as required.
1597 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 psa_reset_key_attributes(&got_attributes);
1599 psa_destroy_key(key);
1600 mbedtls_free(exported);
1601 mbedtls_free(reexported);
1602 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001603}
1604/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001605
Moran Pekerf709f4a2018-06-06 17:26:04 +03001606/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001607void import_export_public_key(data_t *data,
1608 int type_arg, // key pair or public key
1609 int alg_arg,
1610 int lifetime_arg,
1611 int export_size_delta,
1612 int expected_export_status_arg,
1613 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001614{
Ronald Cron5425a212020-08-04 14:58:35 +02001615 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001616 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001617 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001618 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001619 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301620 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001621 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001622 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001623 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001625
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001627
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 psa_set_key_lifetime(&attributes, lifetime);
1629 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1630 psa_set_key_algorithm(&attributes, alg);
1631 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001632
1633 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001635
Gilles Peskine49c25912018-10-29 15:15:31 +01001636 /* Export the public key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 ASSERT_ALLOC(exported, export_size);
1638 status = psa_export_public_key(key,
1639 exported, export_size,
1640 &exported_length);
1641 TEST_EQUAL(status, expected_export_status);
1642 if (status == PSA_SUCCESS) {
1643 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001644 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1646 bits = psa_get_key_bits(&attributes);
1647 TEST_LE_U(expected_public_key->len,
1648 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1649 TEST_LE_U(expected_public_key->len,
1650 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1651 TEST_LE_U(expected_public_key->len,
1652 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1653 ASSERT_COMPARE(expected_public_key->x, expected_public_key->len,
1654 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001655 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001656exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001657 /*
1658 * Key attributes may have been returned by psa_get_key_attributes()
1659 * thus reset them as required.
1660 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001662
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 mbedtls_free(exported);
1664 psa_destroy_key(key);
1665 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001666}
1667/* END_CASE */
1668
Gilles Peskine20035e32018-02-03 22:44:14 +01001669/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001670void import_and_exercise_key(data_t *data,
1671 int type_arg,
1672 int bits_arg,
1673 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001674{
Ronald Cron5425a212020-08-04 14:58:35 +02001675 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001676 psa_key_type_t type = type_arg;
1677 size_t bits = bits_arg;
1678 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001680 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001681 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001682
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 psa_set_key_usage_flags(&attributes, usage);
1686 psa_set_key_algorithm(&attributes, alg);
1687 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001688
1689 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001691
1692 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1694 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1695 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001696
1697 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001699 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001701
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 PSA_ASSERT(psa_destroy_key(key));
1703 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001704
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001705exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001706 /*
1707 * Key attributes may have been returned by psa_get_key_attributes()
1708 * thus reset them as required.
1709 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001711
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 psa_reset_key_attributes(&attributes);
1713 psa_destroy_key(key);
1714 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001715}
1716/* END_CASE */
1717
1718/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001719void effective_key_attributes(int type_arg, int expected_type_arg,
1720 int bits_arg, int expected_bits_arg,
1721 int usage_arg, int expected_usage_arg,
1722 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001723{
Ronald Cron5425a212020-08-04 14:58:35 +02001724 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001725 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001726 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001727 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001728 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001729 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001730 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001731 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001732 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001733 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001734
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001736
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 psa_set_key_usage_flags(&attributes, usage);
1738 psa_set_key_algorithm(&attributes, alg);
1739 psa_set_key_type(&attributes, key_type);
1740 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001741
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 PSA_ASSERT(psa_generate_key(&attributes, &key));
1743 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001744
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1746 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1747 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1748 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1749 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001750
1751exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001752 /*
1753 * Key attributes may have been returned by psa_get_key_attributes()
1754 * thus reset them as required.
1755 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 psa_destroy_key(key);
1759 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001760}
1761/* END_CASE */
1762
1763/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001764void check_key_policy(int type_arg, int bits_arg,
1765 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001766{
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1768 usage_arg,
1769 mbedtls_test_update_key_usage_flags(usage_arg),
1770 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001771 goto exit;
1772}
1773/* END_CASE */
1774
1775/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001776void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001777{
1778 /* Test each valid way of initializing the object, except for `= {0}`, as
1779 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1780 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001781 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001783 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1784 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001785
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001787
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1789 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1790 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001791
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 TEST_EQUAL(psa_get_key_type(&func), 0);
1793 TEST_EQUAL(psa_get_key_type(&init), 0);
1794 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001795
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 TEST_EQUAL(psa_get_key_bits(&func), 0);
1797 TEST_EQUAL(psa_get_key_bits(&init), 0);
1798 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001799
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1801 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1802 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001803
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1805 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1806 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001807}
1808/* END_CASE */
1809
1810/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001811void mac_key_policy(int policy_usage_arg,
1812 int policy_alg_arg,
1813 int key_type_arg,
1814 data_t *key_data,
1815 int exercise_alg_arg,
1816 int expected_status_sign_arg,
1817 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001818{
Ronald Cron5425a212020-08-04 14:58:35 +02001819 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001820 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001821 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001822 psa_key_type_t key_type = key_type_arg;
1823 psa_algorithm_t policy_alg = policy_alg_arg;
1824 psa_algorithm_t exercise_alg = exercise_alg_arg;
1825 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001826 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001827 psa_status_t expected_status_sign = expected_status_sign_arg;
1828 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001829 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001830
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001832
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 psa_set_key_usage_flags(&attributes, policy_usage);
1834 psa_set_key_algorithm(&attributes, policy_alg);
1835 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001836
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1838 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001839
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1841 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001842
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1844 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001845
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001846 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001848 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1850 input, 128,
1851 mac, PSA_MAC_MAX_SIZE, &mac_len),
1852 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001853
Neil Armstrong3af9b972022-02-07 12:20:21 +01001854 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 PSA_ASSERT(psa_mac_abort(&operation));
1856 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1857 if (status == PSA_SUCCESS) {
1858 status = psa_mac_update(&operation, input, 128);
1859 if (status == PSA_SUCCESS) {
1860 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1861 &mac_len),
1862 expected_status_sign);
1863 } else {
1864 TEST_EQUAL(status, expected_status_sign);
1865 }
1866 } else {
1867 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001868 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001870
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001871 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 status = psa_mac_verify(key, exercise_alg, input, 128,
1873 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001874
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1876 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1877 } else {
1878 TEST_EQUAL(status, expected_status_verify);
1879 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001880
Neil Armstrong3af9b972022-02-07 12:20:21 +01001881 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1883 if (status == PSA_SUCCESS) {
1884 status = psa_mac_update(&operation, input, 128);
1885 if (status == PSA_SUCCESS) {
1886 status = psa_mac_verify_finish(&operation, mac, mac_len);
1887 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1888 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1889 } else {
1890 TEST_EQUAL(status, expected_status_verify);
1891 }
1892 } else {
1893 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001894 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 } else {
1896 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001897 }
1898
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001900
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 memset(mac, 0, sizeof(mac));
1902 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1903 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001904
1905exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 psa_mac_abort(&operation);
1907 psa_destroy_key(key);
1908 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001909}
1910/* END_CASE */
1911
1912/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001913void cipher_key_policy(int policy_usage_arg,
1914 int policy_alg,
1915 int key_type,
1916 data_t *key_data,
1917 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001918{
Ronald Cron5425a212020-08-04 14:58:35 +02001919 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001920 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001921 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001922 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001923 size_t output_buffer_size = 0;
1924 size_t input_buffer_size = 0;
1925 size_t output_length = 0;
1926 uint8_t *output = NULL;
1927 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001928 psa_status_t status;
1929
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1931 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1932 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001933
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 ASSERT_ALLOC(input, input_buffer_size);
1935 ASSERT_ALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001936
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001938
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 psa_set_key_usage_flags(&attributes, policy_usage);
1940 psa_set_key_algorithm(&attributes, policy_alg);
1941 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001942
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1944 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001945
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001946 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 TEST_EQUAL(policy_usage,
1948 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001949
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001950 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1952 output, output_buffer_size,
1953 &output_length);
1954 if (policy_alg == exercise_alg &&
1955 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1956 PSA_ASSERT(status);
1957 } else {
1958 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1959 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001960
1961 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1963 if (policy_alg == exercise_alg &&
1964 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1965 PSA_ASSERT(status);
1966 } else {
1967 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1968 }
1969 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001970
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001971 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1973 input, input_buffer_size,
1974 &output_length);
1975 if (policy_alg == exercise_alg &&
1976 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1977 PSA_ASSERT(status);
1978 } else {
1979 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1980 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001981
1982 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
1984 if (policy_alg == exercise_alg &&
1985 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1986 PSA_ASSERT(status);
1987 } else {
1988 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1989 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001990
1991exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 psa_cipher_abort(&operation);
1993 mbedtls_free(input);
1994 mbedtls_free(output);
1995 psa_destroy_key(key);
1996 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001997}
1998/* END_CASE */
1999
2000/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002001void aead_key_policy(int policy_usage_arg,
2002 int policy_alg,
2003 int key_type,
2004 data_t *key_data,
2005 int nonce_length_arg,
2006 int tag_length_arg,
2007 int exercise_alg,
2008 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002009{
Ronald Cron5425a212020-08-04 14:58:35 +02002010 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002011 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002012 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002013 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002014 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002015 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002017 size_t nonce_length = nonce_length_arg;
2018 unsigned char tag[16];
2019 size_t tag_length = tag_length_arg;
2020 size_t output_length;
2021
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 TEST_LE_U(nonce_length, sizeof(nonce));
2023 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002024
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002026
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 psa_set_key_usage_flags(&attributes, policy_usage);
2028 psa_set_key_algorithm(&attributes, policy_alg);
2029 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002030
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2032 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002033
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002034 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 TEST_EQUAL(policy_usage,
2036 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002037
Neil Armstrong752d8112022-02-07 14:51:11 +01002038 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 status = psa_aead_encrypt(key, exercise_alg,
2040 nonce, nonce_length,
2041 NULL, 0,
2042 NULL, 0,
2043 tag, tag_length,
2044 &output_length);
2045 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2046 TEST_EQUAL(status, expected_status);
2047 } else {
2048 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2049 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002050
Neil Armstrong752d8112022-02-07 14:51:11 +01002051 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2053 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2054 TEST_EQUAL(status, expected_status);
2055 } else {
2056 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2057 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002058
2059 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 memset(tag, 0, sizeof(tag));
2061 status = psa_aead_decrypt(key, exercise_alg,
2062 nonce, nonce_length,
2063 NULL, 0,
2064 tag, tag_length,
2065 NULL, 0,
2066 &output_length);
2067 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2068 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2069 } else if (expected_status == PSA_SUCCESS) {
2070 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2071 } else {
2072 TEST_EQUAL(status, expected_status);
2073 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002074
Neil Armstrong752d8112022-02-07 14:51:11 +01002075 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 PSA_ASSERT(psa_aead_abort(&operation));
2077 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2078 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2079 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2080 } else {
2081 TEST_EQUAL(status, expected_status);
2082 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002083
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002084exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 PSA_ASSERT(psa_aead_abort(&operation));
2086 psa_destroy_key(key);
2087 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002088}
2089/* END_CASE */
2090
2091/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002092void asymmetric_encryption_key_policy(int policy_usage_arg,
2093 int policy_alg,
2094 int key_type,
2095 data_t *key_data,
2096 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002097{
Ronald Cron5425a212020-08-04 14:58:35 +02002098 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002099 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002100 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002101 psa_status_t status;
2102 size_t key_bits;
2103 size_t buffer_length;
2104 unsigned char *buffer = NULL;
2105 size_t output_length;
2106
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002108
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 psa_set_key_usage_flags(&attributes, policy_usage);
2110 psa_set_key_algorithm(&attributes, policy_alg);
2111 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2114 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002115
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002116 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 TEST_EQUAL(policy_usage,
2118 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2121 key_bits = psa_get_key_bits(&attributes);
2122 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2123 exercise_alg);
2124 ASSERT_ALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 status = psa_asymmetric_encrypt(key, exercise_alg,
2127 NULL, 0,
2128 NULL, 0,
2129 buffer, buffer_length,
2130 &output_length);
2131 if (policy_alg == exercise_alg &&
2132 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2133 PSA_ASSERT(status);
2134 } else {
2135 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2136 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 if (buffer_length != 0) {
2139 memset(buffer, 0, buffer_length);
2140 }
2141 status = psa_asymmetric_decrypt(key, exercise_alg,
2142 buffer, buffer_length,
2143 NULL, 0,
2144 buffer, buffer_length,
2145 &output_length);
2146 if (policy_alg == exercise_alg &&
2147 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2148 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2149 } else {
2150 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2151 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002152
2153exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002154 /*
2155 * Key attributes may have been returned by psa_get_key_attributes()
2156 * thus reset them as required.
2157 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 psa_destroy_key(key);
2161 PSA_DONE();
2162 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002163}
2164/* END_CASE */
2165
2166/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002167void asymmetric_signature_key_policy(int policy_usage_arg,
2168 int policy_alg,
2169 int key_type,
2170 data_t *key_data,
2171 int exercise_alg,
2172 int payload_length_arg,
2173 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002174{
Ronald Cron5425a212020-08-04 14:58:35 +02002175 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002176 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002177 psa_key_usage_t policy_usage = policy_usage_arg;
2178 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002179 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002181 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2182 * compatible with the policy and `payload_length_arg` is supposed to be
2183 * a valid input length to sign. If `payload_length_arg <= 0`,
2184 * `exercise_alg` is supposed to be forbidden by the policy. */
2185 int compatible_alg = payload_length_arg > 0;
2186 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002188 size_t signature_length;
2189
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002190 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002191 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 TEST_EQUAL(expected_usage,
2193 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002194
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 psa_set_key_usage_flags(&attributes, policy_usage);
2198 psa_set_key_algorithm(&attributes, policy_alg);
2199 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002200
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2202 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002203
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002205
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 status = psa_sign_hash(key, exercise_alg,
2207 payload, payload_length,
2208 signature, sizeof(signature),
2209 &signature_length);
2210 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2211 PSA_ASSERT(status);
2212 } else {
2213 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2214 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 memset(signature, 0, sizeof(signature));
2217 status = psa_verify_hash(key, exercise_alg,
2218 payload, payload_length,
2219 signature, sizeof(signature));
2220 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2221 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2222 } else {
2223 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2224 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002225
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2227 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2228 status = psa_sign_message(key, exercise_alg,
2229 payload, payload_length,
2230 signature, sizeof(signature),
2231 &signature_length);
2232 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2233 PSA_ASSERT(status);
2234 } else {
2235 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2236 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002237
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 memset(signature, 0, sizeof(signature));
2239 status = psa_verify_message(key, exercise_alg,
2240 payload, payload_length,
2241 signature, sizeof(signature));
2242 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2243 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2244 } else {
2245 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2246 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002247 }
2248
Gilles Peskined5b33222018-06-18 22:20:03 +02002249exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 psa_destroy_key(key);
2251 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002252}
2253/* END_CASE */
2254
Janos Follathba3fab92019-06-11 14:50:16 +01002255/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002256void derive_key_policy(int policy_usage,
2257 int policy_alg,
2258 int key_type,
2259 data_t *key_data,
2260 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002261{
Ronald Cron5425a212020-08-04 14:58:35 +02002262 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002263 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002264 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002265 psa_status_t status;
2266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 psa_set_key_usage_flags(&attributes, policy_usage);
2270 psa_set_key_algorithm(&attributes, policy_alg);
2271 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002272
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2274 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002275
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002277
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2279 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2280 PSA_ASSERT(psa_key_derivation_input_bytes(
2281 &operation,
2282 PSA_KEY_DERIVATION_INPUT_SEED,
2283 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002284 }
Janos Follathba3fab92019-06-11 14:50:16 +01002285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 status = psa_key_derivation_input_key(&operation,
2287 PSA_KEY_DERIVATION_INPUT_SECRET,
2288 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 if (policy_alg == exercise_alg &&
2291 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2292 PSA_ASSERT(status);
2293 } else {
2294 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2295 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002296
2297exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 psa_key_derivation_abort(&operation);
2299 psa_destroy_key(key);
2300 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002301}
2302/* END_CASE */
2303
2304/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002305void agreement_key_policy(int policy_usage,
2306 int policy_alg,
2307 int key_type_arg,
2308 data_t *key_data,
2309 int exercise_alg,
2310 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002311{
Ronald Cron5425a212020-08-04 14:58:35 +02002312 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002313 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002314 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002315 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002316 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002317 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002320
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 psa_set_key_usage_flags(&attributes, policy_usage);
2322 psa_set_key_algorithm(&attributes, policy_alg);
2323 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2326 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002327
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2329 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002330
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002332
2333exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 psa_key_derivation_abort(&operation);
2335 psa_destroy_key(key);
2336 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002337}
2338/* END_CASE */
2339
2340/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002341void key_policy_alg2(int key_type_arg, data_t *key_data,
2342 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002343{
Ronald Cron5425a212020-08-04 14:58:35 +02002344 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002345 psa_key_type_t key_type = key_type_arg;
2346 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2347 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2348 psa_key_usage_t usage = usage_arg;
2349 psa_algorithm_t alg = alg_arg;
2350 psa_algorithm_t alg2 = alg2_arg;
2351
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 psa_set_key_usage_flags(&attributes, usage);
2355 psa_set_key_algorithm(&attributes, alg);
2356 psa_set_key_enrollment_algorithm(&attributes, alg2);
2357 psa_set_key_type(&attributes, key_type);
2358 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2359 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002360
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002361 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 usage = mbedtls_test_update_key_usage_flags(usage);
2363 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2364 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2365 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2366 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002369 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 }
2371 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002372 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002374
2375exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002376 /*
2377 * Key attributes may have been returned by psa_get_key_attributes()
2378 * thus reset them as required.
2379 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002381
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 psa_destroy_key(key);
2383 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002384}
2385/* END_CASE */
2386
2387/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002388void raw_agreement_key_policy(int policy_usage,
2389 int policy_alg,
2390 int key_type_arg,
2391 data_t *key_data,
2392 int exercise_alg,
2393 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002394{
Ronald Cron5425a212020-08-04 14:58:35 +02002395 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002396 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002397 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002398 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002399 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002400 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002403
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 psa_set_key_usage_flags(&attributes, policy_usage);
2405 psa_set_key_algorithm(&attributes, policy_alg);
2406 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002407
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2409 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002412
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002414
2415exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 psa_key_derivation_abort(&operation);
2417 psa_destroy_key(key);
2418 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002419}
2420/* END_CASE */
2421
2422/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002423void copy_success(int source_usage_arg,
2424 int source_alg_arg, int source_alg2_arg,
2425 unsigned int source_lifetime_arg,
2426 int type_arg, data_t *material,
2427 int copy_attributes,
2428 int target_usage_arg,
2429 int target_alg_arg, int target_alg2_arg,
2430 unsigned int target_lifetime_arg,
2431 int expected_usage_arg,
2432 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002433{
Gilles Peskineca25db92019-04-19 11:43:08 +02002434 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2435 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002436 psa_key_usage_t expected_usage = expected_usage_arg;
2437 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002438 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302439 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2440 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002441 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2442 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002443 uint8_t *export_buffer = NULL;
2444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002446
Gilles Peskineca25db92019-04-19 11:43:08 +02002447 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2449 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2450 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2451 psa_set_key_type(&source_attributes, type_arg);
2452 psa_set_key_lifetime(&source_attributes, source_lifetime);
2453 PSA_ASSERT(psa_import_key(&source_attributes,
2454 material->x, material->len,
2455 &source_key));
2456 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002457
Gilles Peskineca25db92019-04-19 11:43:08 +02002458 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002460 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002461 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002463
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 if (target_usage_arg != -1) {
2465 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2466 }
2467 if (target_alg_arg != -1) {
2468 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2469 }
2470 if (target_alg2_arg != -1) {
2471 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2472 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002473
Archana8a180362021-07-05 02:18:48 +05302474
Gilles Peskine57ab7212019-01-28 13:03:09 +01002475 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 PSA_ASSERT(psa_copy_key(source_key,
2477 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002478
2479 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002481
2482 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2484 TEST_EQUAL(psa_get_key_type(&source_attributes),
2485 psa_get_key_type(&target_attributes));
2486 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2487 psa_get_key_bits(&target_attributes));
2488 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2489 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2490 TEST_EQUAL(expected_alg2,
2491 psa_get_key_enrollment_algorithm(&target_attributes));
2492 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002493 size_t length;
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 ASSERT_ALLOC(export_buffer, material->len);
2495 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2496 material->len, &length));
2497 ASSERT_COMPARE(material->x, material->len,
2498 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002499 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002500
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 if (!psa_key_lifetime_is_external(target_lifetime)) {
2502 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302503 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 }
2505 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302506 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 }
Archana8a180362021-07-05 02:18:48 +05302508 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002511
2512exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002513 /*
2514 * Source and target key attributes may have been returned by
2515 * psa_get_key_attributes() thus reset them as required.
2516 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 psa_reset_key_attributes(&source_attributes);
2518 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 PSA_DONE();
2521 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002522}
2523/* END_CASE */
2524
2525/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002526void copy_fail(int source_usage_arg,
2527 int source_alg_arg, int source_alg2_arg,
2528 int source_lifetime_arg,
2529 int type_arg, data_t *material,
2530 int target_type_arg, int target_bits_arg,
2531 int target_usage_arg,
2532 int target_alg_arg, int target_alg2_arg,
2533 int target_id_arg, int target_lifetime_arg,
2534 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002535{
2536 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2537 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002538 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2539 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002541
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002543
2544 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2546 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2547 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2548 psa_set_key_type(&source_attributes, type_arg);
2549 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2550 PSA_ASSERT(psa_import_key(&source_attributes,
2551 material->x, material->len,
2552 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002553
2554 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 psa_set_key_id(&target_attributes, key_id);
2556 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2557 psa_set_key_type(&target_attributes, target_type_arg);
2558 psa_set_key_bits(&target_attributes, target_bits_arg);
2559 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2560 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2561 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002562
2563 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 TEST_EQUAL(psa_copy_key(source_key,
2565 &target_attributes, &target_key),
2566 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002569
Gilles Peskine4a644642019-05-03 17:14:08 +02002570exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 psa_reset_key_attributes(&source_attributes);
2572 psa_reset_key_attributes(&target_attributes);
2573 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002574}
2575/* END_CASE */
2576
2577/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002578void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002579{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002580 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002581 /* Test each valid way of initializing the object, except for `= {0}`, as
2582 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2583 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002584 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002586 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2587 psa_hash_operation_t zero;
2588
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002590
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002591 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2593 PSA_ERROR_BAD_STATE);
2594 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2595 PSA_ERROR_BAD_STATE);
2596 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2597 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002598
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002599 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 PSA_ASSERT(psa_hash_abort(&func));
2601 PSA_ASSERT(psa_hash_abort(&init));
2602 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002603}
2604/* END_CASE */
2605
2606/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002607void hash_setup(int alg_arg,
2608 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002609{
2610 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002611 uint8_t *output = NULL;
2612 size_t output_size = 0;
2613 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002614 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002615 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002616 psa_status_t status;
2617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002619
Neil Armstrongedb20862022-02-07 15:47:44 +01002620 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 output_size = PSA_HASH_LENGTH(alg);
2622 ASSERT_ALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002623
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 status = psa_hash_compute(alg, NULL, 0,
2625 output, output_size, &output_length);
2626 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002627
2628 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 status = psa_hash_setup(&operation, alg);
2630 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002631
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002632 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002634
2635 /* If setup failed, reproduce the failure, so as to
2636 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 if (status != PSA_SUCCESS) {
2638 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2639 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002640
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002641 /* Now the operation object should be reusable. */
2642#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2644 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002645#endif
2646
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002647exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 mbedtls_free(output);
2649 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002650}
2651/* END_CASE */
2652
2653/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002654void hash_compute_fail(int alg_arg, data_t *input,
2655 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002656{
2657 psa_algorithm_t alg = alg_arg;
2658 uint8_t *output = NULL;
2659 size_t output_size = output_size_arg;
2660 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002661 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002662 psa_status_t expected_status = expected_status_arg;
2663 psa_status_t status;
2664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 ASSERT_ALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002666
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002668
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002669 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 status = psa_hash_compute(alg, input->x, input->len,
2671 output, output_size, &output_length);
2672 TEST_EQUAL(status, expected_status);
2673 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002674
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002675 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 status = psa_hash_setup(&operation, alg);
2677 if (status == PSA_SUCCESS) {
2678 status = psa_hash_update(&operation, input->x, input->len);
2679 if (status == PSA_SUCCESS) {
2680 status = psa_hash_finish(&operation, output, output_size,
2681 &output_length);
2682 if (status == PSA_SUCCESS) {
2683 TEST_LE_U(output_length, output_size);
2684 } else {
2685 TEST_EQUAL(status, expected_status);
2686 }
2687 } else {
2688 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002689 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 } else {
2691 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002692 }
2693
Gilles Peskine0a749c82019-11-28 19:33:58 +01002694exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 PSA_ASSERT(psa_hash_abort(&operation));
2696 mbedtls_free(output);
2697 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002698}
2699/* END_CASE */
2700
2701/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002702void hash_compare_fail(int alg_arg, data_t *input,
2703 data_t *reference_hash,
2704 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002705{
2706 psa_algorithm_t alg = alg_arg;
2707 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002708 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002709 psa_status_t status;
2710
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002712
Neil Armstrong55a1be12022-02-07 11:23:20 +01002713 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 status = psa_hash_compare(alg, input->x, input->len,
2715 reference_hash->x, reference_hash->len);
2716 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002717
Neil Armstrong55a1be12022-02-07 11:23:20 +01002718 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 status = psa_hash_setup(&operation, alg);
2720 if (status == PSA_SUCCESS) {
2721 status = psa_hash_update(&operation, input->x, input->len);
2722 if (status == PSA_SUCCESS) {
2723 status = psa_hash_verify(&operation, reference_hash->x,
2724 reference_hash->len);
2725 TEST_EQUAL(status, expected_status);
2726 } else {
2727 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002728 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 } else {
2730 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002731 }
2732
Gilles Peskine88e08462020-01-28 20:43:00 +01002733exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 PSA_ASSERT(psa_hash_abort(&operation));
2735 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002736}
2737/* END_CASE */
2738
2739/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002740void hash_compute_compare(int alg_arg, data_t *input,
2741 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002742{
2743 psa_algorithm_t alg = alg_arg;
2744 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2745 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002746 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002747 size_t i;
2748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002750
Neil Armstrongca30a002022-02-07 11:40:23 +01002751 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2753 output, PSA_HASH_LENGTH(alg),
2754 &output_length));
2755 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2756 ASSERT_COMPARE(output, output_length,
2757 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002758
Neil Armstrongca30a002022-02-07 11:40:23 +01002759 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 PSA_ASSERT(psa_hash_setup(&operation, alg));
2761 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2762 PSA_ASSERT(psa_hash_finish(&operation, output,
2763 PSA_HASH_LENGTH(alg),
2764 &output_length));
2765 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2766 ASSERT_COMPARE(output, output_length,
2767 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002768
2769 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2771 output, sizeof(output),
2772 &output_length));
2773 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2774 ASSERT_COMPARE(output, output_length,
2775 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002776
Neil Armstrongca30a002022-02-07 11:40:23 +01002777 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 PSA_ASSERT(psa_hash_setup(&operation, alg));
2779 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2780 PSA_ASSERT(psa_hash_finish(&operation, output,
2781 sizeof(output), &output_length));
2782 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
2783 ASSERT_COMPARE(output, output_length,
2784 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002785
2786 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2788 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002789
Neil Armstrongca30a002022-02-07 11:40:23 +01002790 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 PSA_ASSERT(psa_hash_setup(&operation, alg));
2792 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2793 PSA_ASSERT(psa_hash_verify(&operation, output,
2794 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002795
2796 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002797 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2798 output, output_length + 1),
2799 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002800
Neil Armstrongca30a002022-02-07 11:40:23 +01002801 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 PSA_ASSERT(psa_hash_setup(&operation, alg));
2803 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2804 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2805 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002806
2807 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2809 output, output_length - 1),
2810 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002811
Neil Armstrongca30a002022-02-07 11:40:23 +01002812 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 PSA_ASSERT(psa_hash_setup(&operation, alg));
2814 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2815 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2816 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002817
Gilles Peskine0a749c82019-11-28 19:33:58 +01002818 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 for (i = 0; i < output_length; i++) {
2820 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002821 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002822
2823 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2825 output, output_length),
2826 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002827
2828 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 PSA_ASSERT(psa_hash_setup(&operation, alg));
2830 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2831 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2832 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002833
Gilles Peskine0a749c82019-11-28 19:33:58 +01002834 output[i] ^= 1;
2835 }
2836
2837exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 PSA_ASSERT(psa_hash_abort(&operation));
2839 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002840}
2841/* END_CASE */
2842
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002843/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002844void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002845{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002846 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002847 unsigned char input[] = "";
2848 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002849 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002850 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2851 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2853 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002854 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002855 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002856 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002857
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002859
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002860 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 PSA_ASSERT(psa_hash_setup(&operation, alg));
2862 ASSERT_OPERATION_IS_ACTIVE(operation);
2863 TEST_EQUAL(psa_hash_setup(&operation, alg),
2864 PSA_ERROR_BAD_STATE);
2865 ASSERT_OPERATION_IS_INACTIVE(operation);
2866 PSA_ASSERT(psa_hash_abort(&operation));
2867 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002868
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002869 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2871 PSA_ERROR_BAD_STATE);
2872 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002873
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002874 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002876 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 ASSERT_OPERATION_IS_ACTIVE(operation);
2878 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2879 PSA_ERROR_BAD_STATE);
2880 ASSERT_OPERATION_IS_INACTIVE(operation);
2881 PSA_ASSERT(psa_hash_abort(&operation));
2882 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002883
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002884 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 PSA_ASSERT(psa_hash_setup(&operation, alg));
2886 PSA_ASSERT(psa_hash_finish(&operation,
2887 hash, sizeof(hash), &hash_len));
2888 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2889 PSA_ERROR_BAD_STATE);
2890 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002891
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002892 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 TEST_EQUAL(psa_hash_verify(&operation,
2894 valid_hash, sizeof(valid_hash)),
2895 PSA_ERROR_BAD_STATE);
2896 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002897
2898 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 PSA_ASSERT(psa_hash_setup(&operation, alg));
2900 PSA_ASSERT(psa_hash_finish(&operation,
2901 hash, sizeof(hash), &hash_len));
2902 TEST_EQUAL(psa_hash_verify(&operation,
2903 valid_hash, sizeof(valid_hash)),
2904 PSA_ERROR_BAD_STATE);
2905 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002906
2907 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002908 PSA_ASSERT(psa_hash_setup(&operation, alg));
2909 ASSERT_OPERATION_IS_ACTIVE(operation);
2910 PSA_ASSERT(psa_hash_verify(&operation,
2911 valid_hash, sizeof(valid_hash)));
2912 ASSERT_OPERATION_IS_INACTIVE(operation);
2913 TEST_EQUAL(psa_hash_verify(&operation,
2914 valid_hash, sizeof(valid_hash)),
2915 PSA_ERROR_BAD_STATE);
2916 ASSERT_OPERATION_IS_INACTIVE(operation);
2917 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002918
2919 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 TEST_EQUAL(psa_hash_finish(&operation,
2921 hash, sizeof(hash), &hash_len),
2922 PSA_ERROR_BAD_STATE);
2923 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002924
2925 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 PSA_ASSERT(psa_hash_setup(&operation, alg));
2927 PSA_ASSERT(psa_hash_finish(&operation,
2928 hash, sizeof(hash), &hash_len));
2929 TEST_EQUAL(psa_hash_finish(&operation,
2930 hash, sizeof(hash), &hash_len),
2931 PSA_ERROR_BAD_STATE);
2932 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002933
2934 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 PSA_ASSERT(psa_hash_setup(&operation, alg));
2936 PSA_ASSERT(psa_hash_verify(&operation,
2937 valid_hash, sizeof(valid_hash)));
2938 TEST_EQUAL(psa_hash_finish(&operation,
2939 hash, sizeof(hash), &hash_len),
2940 PSA_ERROR_BAD_STATE);
2941 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002942
2943exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002945}
2946/* END_CASE */
2947
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002948/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002949void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002950{
2951 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002952 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2953 * appended to it */
2954 unsigned char hash[] = {
2955 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2956 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2958 };
2959 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002960 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002961
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002963
itayzafrir27e69452018-11-01 14:26:34 +02002964 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 PSA_ASSERT(psa_hash_setup(&operation, alg));
2966 ASSERT_OPERATION_IS_ACTIVE(operation);
2967 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2968 PSA_ERROR_INVALID_SIGNATURE);
2969 ASSERT_OPERATION_IS_INACTIVE(operation);
2970 PSA_ASSERT(psa_hash_abort(&operation));
2971 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002972
itayzafrir27e69452018-11-01 14:26:34 +02002973 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 PSA_ASSERT(psa_hash_setup(&operation, alg));
2975 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
2976 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03002977
itayzafrir27e69452018-11-01 14:26:34 +02002978 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 PSA_ASSERT(psa_hash_setup(&operation, alg));
2980 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
2981 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03002982
itayzafrirec93d302018-10-18 18:01:10 +03002983exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002984 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03002985}
2986/* END_CASE */
2987
Ronald Cronee414c72021-03-18 18:50:08 +01002988/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002989void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03002990{
2991 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002992 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002994 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002995 size_t hash_len;
2996
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03002998
itayzafrir58028322018-10-25 10:22:01 +03002999 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 PSA_ASSERT(psa_hash_setup(&operation, alg));
3001 TEST_EQUAL(psa_hash_finish(&operation,
3002 hash, expected_size - 1, &hash_len),
3003 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003004
3005exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003007}
3008/* END_CASE */
3009
Ronald Cronee414c72021-03-18 18:50:08 +01003010/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003011void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003012{
3013 psa_algorithm_t alg = PSA_ALG_SHA_256;
3014 unsigned char hash[PSA_HASH_MAX_SIZE];
3015 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3016 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3017 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3018 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3019 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3020 size_t hash_len;
3021
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 PSA_ASSERT(psa_crypto_init());
3023 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3026 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3027 PSA_ASSERT(psa_hash_finish(&op_finished,
3028 hash, sizeof(hash), &hash_len));
3029 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3030 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003031
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3033 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003034
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3036 PSA_ASSERT(psa_hash_finish(&op_init,
3037 hash, sizeof(hash), &hash_len));
3038 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3039 PSA_ASSERT(psa_hash_finish(&op_finished,
3040 hash, sizeof(hash), &hash_len));
3041 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3042 PSA_ASSERT(psa_hash_finish(&op_aborted,
3043 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003044
3045exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 psa_hash_abort(&op_source);
3047 psa_hash_abort(&op_init);
3048 psa_hash_abort(&op_setup);
3049 psa_hash_abort(&op_finished);
3050 psa_hash_abort(&op_aborted);
3051 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003052}
3053/* END_CASE */
3054
Ronald Cronee414c72021-03-18 18:50:08 +01003055/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003056void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003057{
3058 psa_algorithm_t alg = PSA_ALG_SHA_256;
3059 unsigned char hash[PSA_HASH_MAX_SIZE];
3060 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3061 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3062 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3063 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3064 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3065 size_t hash_len;
3066
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003068
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3070 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3071 PSA_ASSERT(psa_hash_finish(&op_finished,
3072 hash, sizeof(hash), &hash_len));
3073 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3074 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003075
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3077 PSA_ASSERT(psa_hash_finish(&op_target,
3078 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3081 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3082 PSA_ERROR_BAD_STATE);
3083 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3084 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003085
3086exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 psa_hash_abort(&op_target);
3088 psa_hash_abort(&op_init);
3089 psa_hash_abort(&op_setup);
3090 psa_hash_abort(&op_finished);
3091 psa_hash_abort(&op_aborted);
3092 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003093}
3094/* END_CASE */
3095
itayzafrir58028322018-10-25 10:22:01 +03003096/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003097void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003098{
Jaeden Amero252ef282019-02-15 14:05:35 +00003099 const uint8_t input[1] = { 0 };
3100
Jaeden Amero769ce272019-01-04 11:48:03 +00003101 /* Test each valid way of initializing the object, except for `= {0}`, as
3102 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3103 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003104 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003106 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3107 psa_mac_operation_t zero;
3108
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003110
Jaeden Amero252ef282019-02-15 14:05:35 +00003111 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 TEST_EQUAL(psa_mac_update(&func,
3113 input, sizeof(input)),
3114 PSA_ERROR_BAD_STATE);
3115 TEST_EQUAL(psa_mac_update(&init,
3116 input, sizeof(input)),
3117 PSA_ERROR_BAD_STATE);
3118 TEST_EQUAL(psa_mac_update(&zero,
3119 input, sizeof(input)),
3120 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003121
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003122 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 PSA_ASSERT(psa_mac_abort(&func));
3124 PSA_ASSERT(psa_mac_abort(&init));
3125 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003126}
3127/* END_CASE */
3128
3129/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003130void mac_setup(int key_type_arg,
3131 data_t *key,
3132 int alg_arg,
3133 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003134{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003135 psa_key_type_t key_type = key_type_arg;
3136 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003137 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003138 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003139 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3140#if defined(KNOWN_SUPPORTED_MAC_ALG)
3141 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3142#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003143
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003145
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3147 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003148 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 }
3150 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003151
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003152 /* The operation object should be reusable. */
3153#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3155 smoke_test_key_data,
3156 sizeof(smoke_test_key_data),
3157 KNOWN_SUPPORTED_MAC_ALG,
3158 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003159 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 }
3161 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003162#endif
3163
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003164exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003166}
3167/* END_CASE */
3168
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003169/* 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 +01003170void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003171{
Ronald Cron5425a212020-08-04 14:58:35 +02003172 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003173 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3174 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003175 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003176 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3177 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3179 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003180 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003181 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3182 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3183 size_t sign_mac_length = 0;
3184 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3185 const uint8_t verify_mac[] = {
3186 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3187 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3189 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003190
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 PSA_ASSERT(psa_crypto_init());
3192 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3193 psa_set_key_algorithm(&attributes, alg);
3194 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003195
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3197 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003198
Jaeden Amero252ef282019-02-15 14:05:35 +00003199 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3201 PSA_ERROR_BAD_STATE);
3202 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003203
3204 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3206 &sign_mac_length),
3207 PSA_ERROR_BAD_STATE);
3208 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003209
3210 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 TEST_EQUAL(psa_mac_verify_finish(&operation,
3212 verify_mac, sizeof(verify_mac)),
3213 PSA_ERROR_BAD_STATE);
3214 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003215
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003216 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003217 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3218 ASSERT_OPERATION_IS_ACTIVE(operation);
3219 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3220 PSA_ERROR_BAD_STATE);
3221 ASSERT_OPERATION_IS_INACTIVE(operation);
3222 PSA_ASSERT(psa_mac_abort(&operation));
3223 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003224
Jaeden Amero252ef282019-02-15 14:05:35 +00003225 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003226 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3227 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3228 PSA_ASSERT(psa_mac_sign_finish(&operation,
3229 sign_mac, sizeof(sign_mac),
3230 &sign_mac_length));
3231 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3232 PSA_ERROR_BAD_STATE);
3233 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003234
3235 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3237 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3238 PSA_ASSERT(psa_mac_verify_finish(&operation,
3239 verify_mac, sizeof(verify_mac)));
3240 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3241 PSA_ERROR_BAD_STATE);
3242 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003243
3244 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003245 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3246 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3247 PSA_ASSERT(psa_mac_sign_finish(&operation,
3248 sign_mac, sizeof(sign_mac),
3249 &sign_mac_length));
3250 TEST_EQUAL(psa_mac_sign_finish(&operation,
3251 sign_mac, sizeof(sign_mac),
3252 &sign_mac_length),
3253 PSA_ERROR_BAD_STATE);
3254 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003255
3256 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003257 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3258 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3259 PSA_ASSERT(psa_mac_verify_finish(&operation,
3260 verify_mac, sizeof(verify_mac)));
3261 TEST_EQUAL(psa_mac_verify_finish(&operation,
3262 verify_mac, sizeof(verify_mac)),
3263 PSA_ERROR_BAD_STATE);
3264 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003265
3266 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3268 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3269 ASSERT_OPERATION_IS_ACTIVE(operation);
3270 TEST_EQUAL(psa_mac_verify_finish(&operation,
3271 verify_mac, sizeof(verify_mac)),
3272 PSA_ERROR_BAD_STATE);
3273 ASSERT_OPERATION_IS_INACTIVE(operation);
3274 PSA_ASSERT(psa_mac_abort(&operation));
3275 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003276
3277 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3279 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3280 ASSERT_OPERATION_IS_ACTIVE(operation);
3281 TEST_EQUAL(psa_mac_sign_finish(&operation,
3282 sign_mac, sizeof(sign_mac),
3283 &sign_mac_length),
3284 PSA_ERROR_BAD_STATE);
3285 ASSERT_OPERATION_IS_INACTIVE(operation);
3286 PSA_ASSERT(psa_mac_abort(&operation));
3287 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003288
Gilles Peskine449bd832023-01-11 14:50:10 +01003289 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003290
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003291exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003293}
3294/* END_CASE */
3295
3296/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003297void mac_sign_verify_multi(int key_type_arg,
3298 data_t *key_data,
3299 int alg_arg,
3300 data_t *input,
3301 int is_verify,
3302 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003303{
3304 size_t data_part_len = 0;
3305
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003307 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003309
Gilles Peskine449bd832023-01-11 14:50:10 +01003310 if (mac_multipart_internal_func(key_type_arg, key_data,
3311 alg_arg,
3312 input, data_part_len,
3313 expected_mac,
3314 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003315 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003316 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003317
3318 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003320
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 if (mac_multipart_internal_func(key_type_arg, key_data,
3322 alg_arg,
3323 input, data_part_len,
3324 expected_mac,
3325 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003326 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003328 }
3329
3330 /* Goto is required to silence warnings about unused labels, as we
3331 * don't actually do any test assertions in this function. */
3332 goto exit;
3333}
3334/* END_CASE */
3335
3336/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003337void mac_sign(int key_type_arg,
3338 data_t *key_data,
3339 int alg_arg,
3340 data_t *input,
3341 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003342{
Ronald Cron5425a212020-08-04 14:58:35 +02003343 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003344 psa_key_type_t key_type = key_type_arg;
3345 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003346 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003347 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003348 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003349 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003351 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003352 const size_t output_sizes_to_test[] = {
3353 0,
3354 1,
3355 expected_mac->len - 1,
3356 expected_mac->len,
3357 expected_mac->len + 1,
3358 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003359
Gilles Peskine449bd832023-01-11 14:50:10 +01003360 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003361 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003362 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003363
Gilles Peskine449bd832023-01-11 14:50:10 +01003364 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003365
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3367 psa_set_key_algorithm(&attributes, alg);
3368 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003369
Gilles Peskine449bd832023-01-11 14:50:10 +01003370 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3371 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003372
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003374 const size_t output_size = output_sizes_to_test[i];
3375 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003376 (output_size >= expected_mac->len ? PSA_SUCCESS :
3377 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003378
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 mbedtls_test_set_step(output_size);
3380 ASSERT_ALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003381
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003382 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003383 TEST_EQUAL(psa_mac_compute(key, alg,
3384 input->x, input->len,
3385 actual_mac, output_size, &mac_length),
3386 expected_status);
3387 if (expected_status == PSA_SUCCESS) {
3388 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3389 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003390 }
3391
Gilles Peskine449bd832023-01-11 14:50:10 +01003392 if (output_size > 0) {
3393 memset(actual_mac, 0, output_size);
3394 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003395
3396 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003397 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3398 PSA_ASSERT(psa_mac_update(&operation,
3399 input->x, input->len));
3400 TEST_EQUAL(psa_mac_sign_finish(&operation,
3401 actual_mac, output_size,
3402 &mac_length),
3403 expected_status);
3404 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003405
Gilles Peskine449bd832023-01-11 14:50:10 +01003406 if (expected_status == PSA_SUCCESS) {
3407 ASSERT_COMPARE(expected_mac->x, expected_mac->len,
3408 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003409 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003410 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003411 actual_mac = NULL;
3412 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003413
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003414exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003415 psa_mac_abort(&operation);
3416 psa_destroy_key(key);
3417 PSA_DONE();
3418 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003419}
3420/* END_CASE */
3421
3422/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003423void mac_verify(int key_type_arg,
3424 data_t *key_data,
3425 int alg_arg,
3426 data_t *input,
3427 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003428{
Ronald Cron5425a212020-08-04 14:58:35 +02003429 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003430 psa_key_type_t key_type = key_type_arg;
3431 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003432 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003433 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003434 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003435
Gilles Peskine449bd832023-01-11 14:50:10 +01003436 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003437
Gilles Peskine449bd832023-01-11 14:50:10 +01003438 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003439
Gilles Peskine449bd832023-01-11 14:50:10 +01003440 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3441 psa_set_key_algorithm(&attributes, alg);
3442 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003443
Gilles Peskine449bd832023-01-11 14:50:10 +01003444 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3445 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003446
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003447 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003448 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3449 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003450
3451 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003452 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3453 PSA_ASSERT(psa_mac_update(&operation,
3454 input->x, input->len));
3455 PSA_ASSERT(psa_mac_verify_finish(&operation,
3456 expected_mac->x,
3457 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003458
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003459 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003460 TEST_EQUAL(psa_mac_verify(key, alg,
3461 input->x, input->len,
3462 expected_mac->x,
3463 expected_mac->len - 1),
3464 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003465
3466 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003467 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3468 PSA_ASSERT(psa_mac_update(&operation,
3469 input->x, input->len));
3470 TEST_EQUAL(psa_mac_verify_finish(&operation,
3471 expected_mac->x,
3472 expected_mac->len - 1),
3473 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003474
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003475 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003476 ASSERT_ALLOC(perturbed_mac, expected_mac->len + 1);
3477 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3478 TEST_EQUAL(psa_mac_verify(key, alg,
3479 input->x, input->len,
3480 perturbed_mac, expected_mac->len + 1),
3481 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003482
3483 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003484 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3485 PSA_ASSERT(psa_mac_update(&operation,
3486 input->x, input->len));
3487 TEST_EQUAL(psa_mac_verify_finish(&operation,
3488 perturbed_mac,
3489 expected_mac->len + 1),
3490 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003491
3492 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003493 for (size_t i = 0; i < expected_mac->len; i++) {
3494 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003495 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003496
Gilles Peskine449bd832023-01-11 14:50:10 +01003497 TEST_EQUAL(psa_mac_verify(key, alg,
3498 input->x, input->len,
3499 perturbed_mac, expected_mac->len),
3500 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003501
Gilles Peskine449bd832023-01-11 14:50:10 +01003502 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3503 PSA_ASSERT(psa_mac_update(&operation,
3504 input->x, input->len));
3505 TEST_EQUAL(psa_mac_verify_finish(&operation,
3506 perturbed_mac,
3507 expected_mac->len),
3508 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003509 perturbed_mac[i] ^= 1;
3510 }
3511
Gilles Peskine8c9def32018-02-08 10:02:12 +01003512exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003513 psa_mac_abort(&operation);
3514 psa_destroy_key(key);
3515 PSA_DONE();
3516 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003517}
3518/* END_CASE */
3519
3520/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003521void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003522{
Jaeden Ameroab439972019-02-15 14:12:05 +00003523 const uint8_t input[1] = { 0 };
3524 unsigned char output[1] = { 0 };
3525 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003526 /* Test each valid way of initializing the object, except for `= {0}`, as
3527 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3528 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003529 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003530 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003531 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3532 psa_cipher_operation_t zero;
3533
Gilles Peskine449bd832023-01-11 14:50:10 +01003534 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003535
Jaeden Ameroab439972019-02-15 14:12:05 +00003536 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003537 TEST_EQUAL(psa_cipher_update(&func,
3538 input, sizeof(input),
3539 output, sizeof(output),
3540 &output_length),
3541 PSA_ERROR_BAD_STATE);
3542 TEST_EQUAL(psa_cipher_update(&init,
3543 input, sizeof(input),
3544 output, sizeof(output),
3545 &output_length),
3546 PSA_ERROR_BAD_STATE);
3547 TEST_EQUAL(psa_cipher_update(&zero,
3548 input, sizeof(input),
3549 output, sizeof(output),
3550 &output_length),
3551 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003552
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003553 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003554 PSA_ASSERT(psa_cipher_abort(&func));
3555 PSA_ASSERT(psa_cipher_abort(&init));
3556 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003557}
3558/* END_CASE */
3559
3560/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003561void cipher_setup(int key_type_arg,
3562 data_t *key,
3563 int alg_arg,
3564 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003565{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003566 psa_key_type_t key_type = key_type_arg;
3567 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003568 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003569 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003570 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003571#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003572 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3573#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003574
Gilles Peskine449bd832023-01-11 14:50:10 +01003575 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003576
Gilles Peskine449bd832023-01-11 14:50:10 +01003577 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3578 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003579 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003580 }
3581 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003582
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003583 /* The operation object should be reusable. */
3584#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003585 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3586 smoke_test_key_data,
3587 sizeof(smoke_test_key_data),
3588 KNOWN_SUPPORTED_CIPHER_ALG,
3589 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003590 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003591 }
3592 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003593#endif
3594
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003595exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003596 psa_cipher_abort(&operation);
3597 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003598}
3599/* END_CASE */
3600
Ronald Cronee414c72021-03-18 18:50:08 +01003601/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003602void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003603{
Ronald Cron5425a212020-08-04 14:58:35 +02003604 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003605 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3606 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003607 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003608 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003609 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003610 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003611 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003612 0xaa, 0xaa, 0xaa, 0xaa
3613 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003614 const uint8_t text[] = {
3615 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003616 0xbb, 0xbb, 0xbb, 0xbb
3617 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003618 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003619 size_t length = 0;
3620
Gilles Peskine449bd832023-01-11 14:50:10 +01003621 PSA_ASSERT(psa_crypto_init());
3622 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3623 psa_set_key_algorithm(&attributes, alg);
3624 psa_set_key_type(&attributes, key_type);
3625 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3626 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003627
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003628 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003629 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3630 ASSERT_OPERATION_IS_ACTIVE(operation);
3631 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3632 PSA_ERROR_BAD_STATE);
3633 ASSERT_OPERATION_IS_INACTIVE(operation);
3634 PSA_ASSERT(psa_cipher_abort(&operation));
3635 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003636
3637 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003638 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3639 ASSERT_OPERATION_IS_ACTIVE(operation);
3640 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3641 PSA_ERROR_BAD_STATE);
3642 ASSERT_OPERATION_IS_INACTIVE(operation);
3643 PSA_ASSERT(psa_cipher_abort(&operation));
3644 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003645
Jaeden Ameroab439972019-02-15 14:12:05 +00003646 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003647 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3648 buffer, sizeof(buffer),
3649 &length),
3650 PSA_ERROR_BAD_STATE);
3651 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003652
3653 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003654 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3655 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3656 buffer, sizeof(buffer),
3657 &length));
3658 ASSERT_OPERATION_IS_ACTIVE(operation);
3659 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3660 buffer, sizeof(buffer),
3661 &length),
3662 PSA_ERROR_BAD_STATE);
3663 ASSERT_OPERATION_IS_INACTIVE(operation);
3664 PSA_ASSERT(psa_cipher_abort(&operation));
3665 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003666
3667 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003668 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3669 PSA_ASSERT(psa_cipher_set_iv(&operation,
3670 iv, sizeof(iv)));
3671 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3672 buffer, sizeof(buffer),
3673 &length),
3674 PSA_ERROR_BAD_STATE);
3675 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003676
3677 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003678 TEST_EQUAL(psa_cipher_set_iv(&operation,
3679 iv, sizeof(iv)),
3680 PSA_ERROR_BAD_STATE);
3681 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003682
3683 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003684 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3685 PSA_ASSERT(psa_cipher_set_iv(&operation,
3686 iv, sizeof(iv)));
3687 ASSERT_OPERATION_IS_ACTIVE(operation);
3688 TEST_EQUAL(psa_cipher_set_iv(&operation,
3689 iv, sizeof(iv)),
3690 PSA_ERROR_BAD_STATE);
3691 ASSERT_OPERATION_IS_INACTIVE(operation);
3692 PSA_ASSERT(psa_cipher_abort(&operation));
3693 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003694
3695 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003696 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3697 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3698 buffer, sizeof(buffer),
3699 &length));
3700 TEST_EQUAL(psa_cipher_set_iv(&operation,
3701 iv, sizeof(iv)),
3702 PSA_ERROR_BAD_STATE);
3703 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003704
3705 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003706 TEST_EQUAL(psa_cipher_update(&operation,
3707 text, sizeof(text),
3708 buffer, sizeof(buffer),
3709 &length),
3710 PSA_ERROR_BAD_STATE);
3711 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003712
3713 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003714 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3715 ASSERT_OPERATION_IS_ACTIVE(operation);
3716 TEST_EQUAL(psa_cipher_update(&operation,
3717 text, sizeof(text),
3718 buffer, sizeof(buffer),
3719 &length),
3720 PSA_ERROR_BAD_STATE);
3721 ASSERT_OPERATION_IS_INACTIVE(operation);
3722 PSA_ASSERT(psa_cipher_abort(&operation));
3723 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003724
3725 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003726 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3727 PSA_ASSERT(psa_cipher_set_iv(&operation,
3728 iv, sizeof(iv)));
3729 PSA_ASSERT(psa_cipher_finish(&operation,
3730 buffer, sizeof(buffer), &length));
3731 TEST_EQUAL(psa_cipher_update(&operation,
3732 text, sizeof(text),
3733 buffer, sizeof(buffer),
3734 &length),
3735 PSA_ERROR_BAD_STATE);
3736 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003737
3738 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003739 TEST_EQUAL(psa_cipher_finish(&operation,
3740 buffer, sizeof(buffer), &length),
3741 PSA_ERROR_BAD_STATE);
3742 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003743
3744 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003745 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003746 /* Not calling update means we are encrypting an empty buffer, which is OK
3747 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003748 ASSERT_OPERATION_IS_ACTIVE(operation);
3749 TEST_EQUAL(psa_cipher_finish(&operation,
3750 buffer, sizeof(buffer), &length),
3751 PSA_ERROR_BAD_STATE);
3752 ASSERT_OPERATION_IS_INACTIVE(operation);
3753 PSA_ASSERT(psa_cipher_abort(&operation));
3754 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003755
3756 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003757 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3758 PSA_ASSERT(psa_cipher_set_iv(&operation,
3759 iv, sizeof(iv)));
3760 PSA_ASSERT(psa_cipher_finish(&operation,
3761 buffer, sizeof(buffer), &length));
3762 TEST_EQUAL(psa_cipher_finish(&operation,
3763 buffer, sizeof(buffer), &length),
3764 PSA_ERROR_BAD_STATE);
3765 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003766
Gilles Peskine449bd832023-01-11 14:50:10 +01003767 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003768
Jaeden Ameroab439972019-02-15 14:12:05 +00003769exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003770 psa_cipher_abort(&operation);
3771 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003772}
3773/* END_CASE */
3774
3775/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003776void cipher_encrypt_fail(int alg_arg,
3777 int key_type_arg,
3778 data_t *key_data,
3779 data_t *input,
3780 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003781{
Ronald Cron5425a212020-08-04 14:58:35 +02003782 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003783 psa_status_t status;
3784 psa_key_type_t key_type = key_type_arg;
3785 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003786 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003787 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003788 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3789 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003790 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003791 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003792 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003793 size_t function_output_length;
3794 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003795 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3796
Gilles Peskine449bd832023-01-11 14:50:10 +01003797 if (PSA_ERROR_BAD_STATE != expected_status) {
3798 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003799
Gilles Peskine449bd832023-01-11 14:50:10 +01003800 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3801 psa_set_key_algorithm(&attributes, alg);
3802 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003803
Gilles Peskine449bd832023-01-11 14:50:10 +01003804 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3805 input->len);
3806 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003807
Gilles Peskine449bd832023-01-11 14:50:10 +01003808 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3809 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003810 }
3811
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003812 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003813 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3814 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003815
Gilles Peskine449bd832023-01-11 14:50:10 +01003816 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003817
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003818 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003819 status = psa_cipher_encrypt_setup(&operation, key, alg);
3820 if (status == PSA_SUCCESS) {
3821 if (alg != PSA_ALG_ECB_NO_PADDING) {
3822 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3823 iv, iv_size,
3824 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003825 }
3826
Gilles Peskine449bd832023-01-11 14:50:10 +01003827 status = psa_cipher_update(&operation, input->x, input->len,
3828 output, output_buffer_size,
3829 &function_output_length);
3830 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003831 output_length += function_output_length;
3832
Gilles Peskine449bd832023-01-11 14:50:10 +01003833 status = psa_cipher_finish(&operation, output + output_length,
3834 output_buffer_size - output_length,
3835 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003836
Gilles Peskine449bd832023-01-11 14:50:10 +01003837 TEST_EQUAL(status, expected_status);
3838 } else {
3839 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003840 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003841 } else {
3842 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003843 }
3844
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003845exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003846 psa_cipher_abort(&operation);
3847 mbedtls_free(output);
3848 psa_destroy_key(key);
3849 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003850}
3851/* END_CASE */
3852
3853/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003854void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3855 data_t *input, int iv_length,
3856 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003857{
3858 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3859 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3860 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3861 size_t output_buffer_size = 0;
3862 unsigned char *output = NULL;
3863
Gilles Peskine449bd832023-01-11 14:50:10 +01003864 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
3865 ASSERT_ALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003866
Gilles Peskine449bd832023-01-11 14:50:10 +01003867 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003868
Gilles Peskine449bd832023-01-11 14:50:10 +01003869 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3870 psa_set_key_algorithm(&attributes, alg);
3871 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003872
Gilles Peskine449bd832023-01-11 14:50:10 +01003873 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3874 &key));
3875 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3876 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3877 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003878
3879exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003880 psa_cipher_abort(&operation);
3881 mbedtls_free(output);
3882 psa_destroy_key(key);
3883 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003884}
3885/* END_CASE */
3886
3887/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003888void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3889 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003890{
3891 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3892 psa_key_type_t key_type = key_type_arg;
3893 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003894 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3895 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003896 unsigned char *output = NULL;
3897 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003898 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003899 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3900
Gilles Peskine449bd832023-01-11 14:50:10 +01003901 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003902
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003903 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003904 TEST_LE_U(ciphertext->len,
3905 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3906 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3907 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3908 TEST_LE_U(plaintext->len,
3909 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3910 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3911 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003912
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003913
3914 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003915 psa_set_key_usage_flags(&attributes,
3916 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3917 psa_set_key_algorithm(&attributes, alg);
3918 psa_set_key_type(&attributes, key_type);
3919 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3920 &key));
3921 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3922 plaintext->len);
3923 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003924
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003925 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003926 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3927 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3928 PSA_ERROR_BAD_STATE);
3929 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3930 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3931 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003932
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003933 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003934 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3935 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3936 &length),
3937 PSA_ERROR_BAD_STATE);
3938 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3939 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3940 &length),
3941 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003942
Gilles Peskine286c3142022-04-20 17:09:38 +02003943 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003944 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003945 output_length = 0;
3946 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003947 PSA_ASSERT(psa_cipher_update(&operation,
3948 plaintext->x, plaintext->len,
3949 output, output_buffer_size,
3950 &length));
3951 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003952 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003953 PSA_ASSERT(psa_cipher_finish(&operation,
3954 mbedtls_buffer_offset(output, output_length),
3955 output_buffer_size - output_length,
3956 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003957 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3959 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003960
Gilles Peskine286c3142022-04-20 17:09:38 +02003961 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003962 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003963 output_length = 0;
3964 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003965 PSA_ASSERT(psa_cipher_update(&operation,
3966 ciphertext->x, ciphertext->len,
3967 output, output_buffer_size,
3968 &length));
3969 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003970 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003971 PSA_ASSERT(psa_cipher_finish(&operation,
3972 mbedtls_buffer_offset(output, output_length),
3973 output_buffer_size - output_length,
3974 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003975 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003976 ASSERT_COMPARE(plaintext->x, plaintext->len,
3977 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003978
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003979 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003980 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003981 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
3982 output, output_buffer_size,
3983 &output_length));
3984 ASSERT_COMPARE(ciphertext->x, ciphertext->len,
3985 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003986
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003987 /* One-shot decryption */
3988 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003989 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
3990 output, output_buffer_size,
3991 &output_length));
3992 ASSERT_COMPARE(plaintext->x, plaintext->len,
3993 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003994
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003995exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003996 PSA_ASSERT(psa_cipher_abort(&operation));
3997 mbedtls_free(output);
3998 psa_cipher_abort(&operation);
3999 psa_destroy_key(key);
4000 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004001}
4002/* END_CASE */
4003
4004/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004005void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004006{
4007 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4008 psa_algorithm_t alg = alg_arg;
4009 psa_key_type_t key_type = key_type_arg;
4010 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4011 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4012 psa_status_t status;
4013
Gilles Peskine449bd832023-01-11 14:50:10 +01004014 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004015
Gilles Peskine449bd832023-01-11 14:50:10 +01004016 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4017 psa_set_key_algorithm(&attributes, alg);
4018 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004019
4020 /* Usage of either of these two size macros would cause divide by zero
4021 * with incorrect key types previously. Input length should be irrelevant
4022 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004023 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4024 0);
4025 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004026
4027
Gilles Peskine449bd832023-01-11 14:50:10 +01004028 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4029 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004030
4031 /* Should fail due to invalid alg type (to support invalid key type).
4032 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004033 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004034
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004036
4037exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004038 psa_cipher_abort(&operation);
4039 psa_destroy_key(key);
4040 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004041}
4042/* END_CASE */
4043
4044/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004045void cipher_encrypt_validation(int alg_arg,
4046 int key_type_arg,
4047 data_t *key_data,
4048 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004049{
4050 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4051 psa_key_type_t key_type = key_type_arg;
4052 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004053 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004054 unsigned char *output1 = NULL;
4055 size_t output1_buffer_size = 0;
4056 size_t output1_length = 0;
4057 unsigned char *output2 = NULL;
4058 size_t output2_buffer_size = 0;
4059 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004060 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004061 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004062 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004063
Gilles Peskine449bd832023-01-11 14:50:10 +01004064 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004065
Gilles Peskine449bd832023-01-11 14:50:10 +01004066 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4067 psa_set_key_algorithm(&attributes, alg);
4068 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004069
Gilles Peskine449bd832023-01-11 14:50:10 +01004070 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4071 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4072 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4073 ASSERT_ALLOC(output1, output1_buffer_size);
4074 ASSERT_ALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004075
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4077 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004078
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004079 /* The one-shot cipher encryption uses generated iv so validating
4080 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4082 output1_buffer_size, &output1_length));
4083 TEST_LE_U(output1_length,
4084 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4085 TEST_LE_U(output1_length,
4086 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004087
Gilles Peskine449bd832023-01-11 14:50:10 +01004088 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4089 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004090
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 PSA_ASSERT(psa_cipher_update(&operation,
4092 input->x, input->len,
4093 output2, output2_buffer_size,
4094 &function_output_length));
4095 TEST_LE_U(function_output_length,
4096 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4097 TEST_LE_U(function_output_length,
4098 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004099 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004100
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 PSA_ASSERT(psa_cipher_finish(&operation,
4102 output2 + output2_length,
4103 output2_buffer_size - output2_length,
4104 &function_output_length));
4105 TEST_LE_U(function_output_length,
4106 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4107 TEST_LE_U(function_output_length,
4108 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004109 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004110
Gilles Peskine449bd832023-01-11 14:50:10 +01004111 PSA_ASSERT(psa_cipher_abort(&operation));
4112 ASSERT_COMPARE(output1 + iv_size, output1_length - iv_size,
4113 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004114
Gilles Peskine50e586b2018-06-08 14:28:46 +02004115exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004116 psa_cipher_abort(&operation);
4117 mbedtls_free(output1);
4118 mbedtls_free(output2);
4119 psa_destroy_key(key);
4120 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004121}
4122/* END_CASE */
4123
4124/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004125void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4126 data_t *key_data, data_t *iv,
4127 data_t *input,
4128 int first_part_size_arg,
4129 int output1_length_arg, int output2_length_arg,
4130 data_t *expected_output,
4131 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004132{
Ronald Cron5425a212020-08-04 14:58:35 +02004133 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004134 psa_key_type_t key_type = key_type_arg;
4135 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004136 psa_status_t status;
4137 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004138 size_t first_part_size = first_part_size_arg;
4139 size_t output1_length = output1_length_arg;
4140 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004141 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004142 size_t output_buffer_size = 0;
4143 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004144 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004145 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004146 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004147
Gilles Peskine449bd832023-01-11 14:50:10 +01004148 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004149
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4151 psa_set_key_algorithm(&attributes, alg);
4152 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004153
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4155 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004156
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004158
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 if (iv->len > 0) {
4160 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004161 }
4162
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4164 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4165 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004166
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 TEST_LE_U(first_part_size, input->len);
4168 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4169 output, output_buffer_size,
4170 &function_output_length));
4171 TEST_ASSERT(function_output_length == output1_length);
4172 TEST_LE_U(function_output_length,
4173 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4174 TEST_LE_U(function_output_length,
4175 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004176 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004177
Gilles Peskine449bd832023-01-11 14:50:10 +01004178 if (first_part_size < input->len) {
4179 PSA_ASSERT(psa_cipher_update(&operation,
4180 input->x + first_part_size,
4181 input->len - first_part_size,
4182 (output_buffer_size == 0 ? NULL :
4183 output + total_output_length),
4184 output_buffer_size - total_output_length,
4185 &function_output_length));
4186 TEST_ASSERT(function_output_length == output2_length);
4187 TEST_LE_U(function_output_length,
4188 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4189 alg,
4190 input->len - first_part_size));
4191 TEST_LE_U(function_output_length,
4192 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004193 total_output_length += function_output_length;
4194 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004195
Gilles Peskine449bd832023-01-11 14:50:10 +01004196 status = psa_cipher_finish(&operation,
4197 (output_buffer_size == 0 ? NULL :
4198 output + total_output_length),
4199 output_buffer_size - total_output_length,
4200 &function_output_length);
4201 TEST_LE_U(function_output_length,
4202 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4203 TEST_LE_U(function_output_length,
4204 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004205 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004206 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004207
Gilles Peskine449bd832023-01-11 14:50:10 +01004208 if (expected_status == PSA_SUCCESS) {
4209 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004210
Gilles Peskine449bd832023-01-11 14:50:10 +01004211 ASSERT_COMPARE(expected_output->x, expected_output->len,
4212 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004213 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004214
4215exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004216 psa_cipher_abort(&operation);
4217 mbedtls_free(output);
4218 psa_destroy_key(key);
4219 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004220}
4221/* END_CASE */
4222
4223/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004224void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4225 data_t *key_data, data_t *iv,
4226 data_t *input,
4227 int first_part_size_arg,
4228 int output1_length_arg, int output2_length_arg,
4229 data_t *expected_output,
4230 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004231{
Ronald Cron5425a212020-08-04 14:58:35 +02004232 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004233 psa_key_type_t key_type = key_type_arg;
4234 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004235 psa_status_t status;
4236 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004237 size_t first_part_size = first_part_size_arg;
4238 size_t output1_length = output1_length_arg;
4239 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004240 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004241 size_t output_buffer_size = 0;
4242 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004243 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004244 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004245 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004246
Gilles Peskine449bd832023-01-11 14:50:10 +01004247 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004248
Gilles Peskine449bd832023-01-11 14:50:10 +01004249 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4250 psa_set_key_algorithm(&attributes, alg);
4251 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004252
Gilles Peskine449bd832023-01-11 14:50:10 +01004253 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4254 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004255
Gilles Peskine449bd832023-01-11 14:50:10 +01004256 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004257
Gilles Peskine449bd832023-01-11 14:50:10 +01004258 if (iv->len > 0) {
4259 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004260 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004261
Gilles Peskine449bd832023-01-11 14:50:10 +01004262 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4263 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4264 ASSERT_ALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004265
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 TEST_LE_U(first_part_size, input->len);
4267 PSA_ASSERT(psa_cipher_update(&operation,
4268 input->x, first_part_size,
4269 output, output_buffer_size,
4270 &function_output_length));
4271 TEST_ASSERT(function_output_length == output1_length);
4272 TEST_LE_U(function_output_length,
4273 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4274 TEST_LE_U(function_output_length,
4275 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004276 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004277
Gilles Peskine449bd832023-01-11 14:50:10 +01004278 if (first_part_size < input->len) {
4279 PSA_ASSERT(psa_cipher_update(&operation,
4280 input->x + first_part_size,
4281 input->len - first_part_size,
4282 (output_buffer_size == 0 ? NULL :
4283 output + total_output_length),
4284 output_buffer_size - total_output_length,
4285 &function_output_length));
4286 TEST_ASSERT(function_output_length == output2_length);
4287 TEST_LE_U(function_output_length,
4288 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4289 alg,
4290 input->len - first_part_size));
4291 TEST_LE_U(function_output_length,
4292 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004293 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004294 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004295
Gilles Peskine449bd832023-01-11 14:50:10 +01004296 status = psa_cipher_finish(&operation,
4297 (output_buffer_size == 0 ? NULL :
4298 output + total_output_length),
4299 output_buffer_size - total_output_length,
4300 &function_output_length);
4301 TEST_LE_U(function_output_length,
4302 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4303 TEST_LE_U(function_output_length,
4304 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004305 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004306 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004307
Gilles Peskine449bd832023-01-11 14:50:10 +01004308 if (expected_status == PSA_SUCCESS) {
4309 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004310
Gilles Peskine449bd832023-01-11 14:50:10 +01004311 ASSERT_COMPARE(expected_output->x, expected_output->len,
4312 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004313 }
4314
Gilles Peskine50e586b2018-06-08 14:28:46 +02004315exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 psa_cipher_abort(&operation);
4317 mbedtls_free(output);
4318 psa_destroy_key(key);
4319 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004320}
4321/* END_CASE */
4322
Gilles Peskine50e586b2018-06-08 14:28:46 +02004323/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004324void cipher_decrypt_fail(int alg_arg,
4325 int key_type_arg,
4326 data_t *key_data,
4327 data_t *iv,
4328 data_t *input_arg,
4329 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004330{
4331 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4332 psa_status_t status;
4333 psa_key_type_t key_type = key_type_arg;
4334 psa_algorithm_t alg = alg_arg;
4335 psa_status_t expected_status = expected_status_arg;
4336 unsigned char *input = NULL;
4337 size_t input_buffer_size = 0;
4338 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004339 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004340 size_t output_buffer_size = 0;
4341 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004342 size_t function_output_length;
4343 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004344 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4345
Gilles Peskine449bd832023-01-11 14:50:10 +01004346 if (PSA_ERROR_BAD_STATE != expected_status) {
4347 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004348
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4350 psa_set_key_algorithm(&attributes, alg);
4351 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004352
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4354 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004355 }
4356
4357 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4359 if (input_buffer_size > 0) {
4360 ASSERT_ALLOC(input, input_buffer_size);
4361 memcpy(input, iv->x, iv->len);
4362 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004363 }
4364
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4366 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004367
Neil Armstrong66a479f2022-02-07 15:41:19 +01004368 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4370 output_buffer_size, &output_length);
4371 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004372
Neil Armstrong66a479f2022-02-07 15:41:19 +01004373 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 status = psa_cipher_decrypt_setup(&operation, key, alg);
4375 if (status == PSA_SUCCESS) {
4376 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4377 input_arg->len) +
4378 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
4379 ASSERT_ALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004380
Gilles Peskine449bd832023-01-11 14:50:10 +01004381 if (iv->len > 0) {
4382 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004383
Gilles Peskine449bd832023-01-11 14:50:10 +01004384 if (status != PSA_SUCCESS) {
4385 TEST_EQUAL(status, expected_status);
4386 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004387 }
4388
Gilles Peskine449bd832023-01-11 14:50:10 +01004389 if (status == PSA_SUCCESS) {
4390 status = psa_cipher_update(&operation,
4391 input_arg->x, input_arg->len,
4392 output_multi, output_buffer_size,
4393 &function_output_length);
4394 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004395 output_length = function_output_length;
4396
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 status = psa_cipher_finish(&operation,
4398 output_multi + output_length,
4399 output_buffer_size - output_length,
4400 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004401
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 TEST_EQUAL(status, expected_status);
4403 } else {
4404 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004405 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 } else {
4407 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004408 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 } else {
4410 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004411 }
4412
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004413exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004414 psa_cipher_abort(&operation);
4415 mbedtls_free(input);
4416 mbedtls_free(output);
4417 mbedtls_free(output_multi);
4418 psa_destroy_key(key);
4419 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004420}
4421/* END_CASE */
4422
4423/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004424void cipher_decrypt(int alg_arg,
4425 int key_type_arg,
4426 data_t *key_data,
4427 data_t *iv,
4428 data_t *input_arg,
4429 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004430{
4431 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4432 psa_key_type_t key_type = key_type_arg;
4433 psa_algorithm_t alg = alg_arg;
4434 unsigned char *input = NULL;
4435 size_t input_buffer_size = 0;
4436 unsigned char *output = NULL;
4437 size_t output_buffer_size = 0;
4438 size_t output_length = 0;
4439 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4440
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004442
Gilles Peskine449bd832023-01-11 14:50:10 +01004443 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4444 psa_set_key_algorithm(&attributes, alg);
4445 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004446
4447 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4449 if (input_buffer_size > 0) {
4450 ASSERT_ALLOC(input, input_buffer_size);
4451 memcpy(input, iv->x, iv->len);
4452 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004453 }
4454
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
4456 ASSERT_ALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004457
Gilles Peskine449bd832023-01-11 14:50:10 +01004458 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4459 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004460
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4462 output_buffer_size, &output_length));
4463 TEST_LE_U(output_length,
4464 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4465 TEST_LE_U(output_length,
4466 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004467
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 ASSERT_COMPARE(expected_output->x, expected_output->len,
4469 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004470exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 mbedtls_free(input);
4472 mbedtls_free(output);
4473 psa_destroy_key(key);
4474 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004475}
4476/* END_CASE */
4477
4478/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004479void cipher_verify_output(int alg_arg,
4480 int key_type_arg,
4481 data_t *key_data,
4482 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004483{
Ronald Cron5425a212020-08-04 14:58:35 +02004484 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004485 psa_key_type_t key_type = key_type_arg;
4486 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004487 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004488 size_t output1_size = 0;
4489 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004490 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004491 size_t output2_size = 0;
4492 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004493 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004494
Gilles Peskine449bd832023-01-11 14:50:10 +01004495 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004496
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4498 psa_set_key_algorithm(&attributes, alg);
4499 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004500
Gilles Peskine449bd832023-01-11 14:50:10 +01004501 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4502 &key));
4503 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4504 ASSERT_ALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004505
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4507 output1, output1_size,
4508 &output1_length));
4509 TEST_LE_U(output1_length,
4510 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4511 TEST_LE_U(output1_length,
4512 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004513
4514 output2_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004515 ASSERT_ALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004516
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4518 output2, output2_size,
4519 &output2_length));
4520 TEST_LE_U(output2_length,
4521 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4522 TEST_LE_U(output2_length,
4523 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004524
Gilles Peskine449bd832023-01-11 14:50:10 +01004525 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004526
4527exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 mbedtls_free(output1);
4529 mbedtls_free(output2);
4530 psa_destroy_key(key);
4531 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004532}
4533/* END_CASE */
4534
4535/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004536void cipher_verify_output_multipart(int alg_arg,
4537 int key_type_arg,
4538 data_t *key_data,
4539 data_t *input,
4540 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004541{
Ronald Cron5425a212020-08-04 14:58:35 +02004542 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004543 psa_key_type_t key_type = key_type_arg;
4544 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004545 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004547 size_t iv_size = 16;
4548 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004549 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004550 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004551 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004552 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004553 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004554 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004555 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004556 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4557 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004558 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004559
Gilles Peskine449bd832023-01-11 14:50:10 +01004560 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004561
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4563 psa_set_key_algorithm(&attributes, alg);
4564 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004565
Gilles Peskine449bd832023-01-11 14:50:10 +01004566 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4567 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004568
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4570 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004571
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 if (alg != PSA_ALG_ECB_NO_PADDING) {
4573 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4574 iv, iv_size,
4575 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004576 }
4577
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4579 TEST_LE_U(output1_buffer_size,
4580 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
4581 ASSERT_ALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004582
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004584
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4586 output1, output1_buffer_size,
4587 &function_output_length));
4588 TEST_LE_U(function_output_length,
4589 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4590 TEST_LE_U(function_output_length,
4591 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004592 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004593
Gilles Peskine449bd832023-01-11 14:50:10 +01004594 PSA_ASSERT(psa_cipher_update(&operation1,
4595 input->x + first_part_size,
4596 input->len - first_part_size,
4597 output1, output1_buffer_size,
4598 &function_output_length));
4599 TEST_LE_U(function_output_length,
4600 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4601 alg,
4602 input->len - first_part_size));
4603 TEST_LE_U(function_output_length,
4604 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004605 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004606
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 PSA_ASSERT(psa_cipher_finish(&operation1,
4608 output1 + output1_length,
4609 output1_buffer_size - output1_length,
4610 &function_output_length));
4611 TEST_LE_U(function_output_length,
4612 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4613 TEST_LE_U(function_output_length,
4614 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004615 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004616
Gilles Peskine449bd832023-01-11 14:50:10 +01004617 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004618
Gilles Peskine048b7f02018-06-08 14:20:49 +02004619 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 TEST_LE_U(output2_buffer_size,
4621 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4622 TEST_LE_U(output2_buffer_size,
4623 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
4624 ASSERT_ALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004625
Gilles Peskine449bd832023-01-11 14:50:10 +01004626 if (iv_length > 0) {
4627 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4628 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004629 }
Moran Pekerded84402018-06-06 16:36:50 +03004630
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4632 output2, output2_buffer_size,
4633 &function_output_length));
4634 TEST_LE_U(function_output_length,
4635 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4636 TEST_LE_U(function_output_length,
4637 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004638 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004639
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 PSA_ASSERT(psa_cipher_update(&operation2,
4641 output1 + first_part_size,
4642 output1_length - first_part_size,
4643 output2, output2_buffer_size,
4644 &function_output_length));
4645 TEST_LE_U(function_output_length,
4646 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4647 alg,
4648 output1_length - first_part_size));
4649 TEST_LE_U(function_output_length,
4650 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004651 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004652
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 PSA_ASSERT(psa_cipher_finish(&operation2,
4654 output2 + output2_length,
4655 output2_buffer_size - output2_length,
4656 &function_output_length));
4657 TEST_LE_U(function_output_length,
4658 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4659 TEST_LE_U(function_output_length,
4660 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004661 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004662
Gilles Peskine449bd832023-01-11 14:50:10 +01004663 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004664
Gilles Peskine449bd832023-01-11 14:50:10 +01004665 ASSERT_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004666
4667exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 psa_cipher_abort(&operation1);
4669 psa_cipher_abort(&operation2);
4670 mbedtls_free(output1);
4671 mbedtls_free(output2);
4672 psa_destroy_key(key);
4673 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004674}
4675/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004676
Gilles Peskine20035e32018-02-03 22:44:14 +01004677/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004678void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4679 int alg_arg,
4680 data_t *nonce,
4681 data_t *additional_data,
4682 data_t *input_data,
4683 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004684{
Ronald Cron5425a212020-08-04 14:58:35 +02004685 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004686 psa_key_type_t key_type = key_type_arg;
4687 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004688 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004689 unsigned char *output_data = NULL;
4690 size_t output_size = 0;
4691 size_t output_length = 0;
4692 unsigned char *output_data2 = NULL;
4693 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004694 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004695 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004696 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004697
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004699
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4701 psa_set_key_algorithm(&attributes, alg);
4702 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004703
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4705 &key));
4706 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4707 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004708
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4710 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004711 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4712 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004713 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4714 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4715 TEST_EQUAL(output_size,
4716 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4717 TEST_LE_U(output_size,
4718 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004719 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004721
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 status = psa_aead_encrypt(key, alg,
4723 nonce->x, nonce->len,
4724 additional_data->x,
4725 additional_data->len,
4726 input_data->x, input_data->len,
4727 output_data, output_size,
4728 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004729
4730 /* If the operation is not supported, just skip and not fail in case the
4731 * encryption involves a common limitation of cryptography hardwares and
4732 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004733 if (status == PSA_ERROR_NOT_SUPPORTED) {
4734 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4735 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004736 }
4737
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004739
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 if (PSA_SUCCESS == expected_result) {
4741 ASSERT_ALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004742
Gilles Peskine003a4a92019-05-14 16:09:40 +02004743 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4744 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 TEST_EQUAL(input_data->len,
4746 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004747
Gilles Peskine449bd832023-01-11 14:50:10 +01004748 TEST_LE_U(input_data->len,
4749 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004750
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 TEST_EQUAL(psa_aead_decrypt(key, alg,
4752 nonce->x, nonce->len,
4753 additional_data->x,
4754 additional_data->len,
4755 output_data, output_length,
4756 output_data2, output_length,
4757 &output_length2),
4758 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004759
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 ASSERT_COMPARE(input_data->x, input_data->len,
4761 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004762 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004763
Gilles Peskinea1cac842018-06-11 19:33:02 +02004764exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 psa_destroy_key(key);
4766 mbedtls_free(output_data);
4767 mbedtls_free(output_data2);
4768 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004769}
4770/* END_CASE */
4771
4772/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004773void aead_encrypt(int key_type_arg, data_t *key_data,
4774 int alg_arg,
4775 data_t *nonce,
4776 data_t *additional_data,
4777 data_t *input_data,
4778 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004779{
Ronald Cron5425a212020-08-04 14:58:35 +02004780 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004781 psa_key_type_t key_type = key_type_arg;
4782 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004783 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004784 unsigned char *output_data = NULL;
4785 size_t output_size = 0;
4786 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004787 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004788 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004789
Gilles Peskine449bd832023-01-11 14:50:10 +01004790 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004791
Gilles Peskine449bd832023-01-11 14:50:10 +01004792 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4793 psa_set_key_algorithm(&attributes, alg);
4794 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004795
Gilles Peskine449bd832023-01-11 14:50:10 +01004796 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4797 &key));
4798 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4799 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004800
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4802 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004803 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4804 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 TEST_EQUAL(output_size,
4806 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4807 TEST_LE_U(output_size,
4808 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
4809 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004810
Gilles Peskine449bd832023-01-11 14:50:10 +01004811 status = psa_aead_encrypt(key, alg,
4812 nonce->x, nonce->len,
4813 additional_data->x, additional_data->len,
4814 input_data->x, input_data->len,
4815 output_data, output_size,
4816 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004817
Ronald Cron28a45ed2021-02-09 20:35:42 +01004818 /* If the operation is not supported, just skip and not fail in case the
4819 * encryption involves a common limitation of cryptography hardwares and
4820 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 if (status == PSA_ERROR_NOT_SUPPORTED) {
4822 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4823 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004824 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004825
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 PSA_ASSERT(status);
4827 ASSERT_COMPARE(expected_result->x, expected_result->len,
4828 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004829
Gilles Peskinea1cac842018-06-11 19:33:02 +02004830exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 psa_destroy_key(key);
4832 mbedtls_free(output_data);
4833 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004834}
4835/* END_CASE */
4836
4837/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004838void aead_decrypt(int key_type_arg, data_t *key_data,
4839 int alg_arg,
4840 data_t *nonce,
4841 data_t *additional_data,
4842 data_t *input_data,
4843 data_t *expected_data,
4844 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004845{
Ronald Cron5425a212020-08-04 14:58:35 +02004846 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004847 psa_key_type_t key_type = key_type_arg;
4848 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004849 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004850 unsigned char *output_data = NULL;
4851 size_t output_size = 0;
4852 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004853 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004854 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004855 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004856
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004858
Gilles Peskine449bd832023-01-11 14:50:10 +01004859 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4860 psa_set_key_algorithm(&attributes, alg);
4861 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004862
Gilles Peskine449bd832023-01-11 14:50:10 +01004863 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4864 &key));
4865 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4866 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4869 alg);
4870 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4871 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004872 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4873 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 TEST_EQUAL(output_size,
4875 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4876 TEST_LE_U(output_size,
4877 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004878 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004879 ASSERT_ALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004880
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 status = psa_aead_decrypt(key, alg,
4882 nonce->x, nonce->len,
4883 additional_data->x,
4884 additional_data->len,
4885 input_data->x, input_data->len,
4886 output_data, output_size,
4887 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004888
Ronald Cron28a45ed2021-02-09 20:35:42 +01004889 /* If the operation is not supported, just skip and not fail in case the
4890 * decryption involves a common limitation of cryptography hardwares and
4891 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 if (status == PSA_ERROR_NOT_SUPPORTED) {
4893 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4894 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004895 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004896
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004898
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 if (expected_result == PSA_SUCCESS) {
4900 ASSERT_COMPARE(expected_data->x, expected_data->len,
4901 output_data, output_length);
4902 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004903
Gilles Peskinea1cac842018-06-11 19:33:02 +02004904exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 psa_destroy_key(key);
4906 mbedtls_free(output_data);
4907 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004908}
4909/* END_CASE */
4910
4911/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004912void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4913 int alg_arg,
4914 data_t *nonce,
4915 data_t *additional_data,
4916 data_t *input_data,
4917 int do_set_lengths,
4918 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004919{
Paul Elliottd3f82412021-06-16 16:52:21 +01004920 size_t ad_part_len = 0;
4921 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004922 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004923
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4925 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004926
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 if (do_set_lengths) {
4928 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004929 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004930 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004931 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004933 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004934
4935 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004936 if (!aead_multipart_internal_func(key_type_arg, key_data,
4937 alg_arg, nonce,
4938 additional_data,
4939 ad_part_len,
4940 input_data, -1,
4941 set_lengths_method,
4942 expected_output,
4943 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004944 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004945 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004946
Gilles Peskine449bd832023-01-11 14:50:10 +01004947 /* length(0) part, length(ad_part_len) part, length(0) part... */
4948 mbedtls_test_set_step(1000 + ad_part_len);
4949
4950 if (!aead_multipart_internal_func(key_type_arg, key_data,
4951 alg_arg, nonce,
4952 additional_data,
4953 ad_part_len,
4954 input_data, -1,
4955 set_lengths_method,
4956 expected_output,
4957 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004958 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 }
4960 }
4961
4962 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4963 /* Split data into length(data_part_len) parts. */
4964 mbedtls_test_set_step(2000 + data_part_len);
4965
4966 if (do_set_lengths) {
4967 if (data_part_len & 0x01) {
4968 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4969 } else {
4970 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4971 }
4972 }
4973
4974 if (!aead_multipart_internal_func(key_type_arg, key_data,
4975 alg_arg, nonce,
4976 additional_data, -1,
4977 input_data, data_part_len,
4978 set_lengths_method,
4979 expected_output,
4980 1, 0)) {
4981 break;
4982 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004983
4984 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01004985 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004986
Gilles Peskine449bd832023-01-11 14:50:10 +01004987 if (!aead_multipart_internal_func(key_type_arg, key_data,
4988 alg_arg, nonce,
4989 additional_data, -1,
4990 input_data, data_part_len,
4991 set_lengths_method,
4992 expected_output,
4993 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004994 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004995 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004996 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004997
Paul Elliott8fc45162021-06-23 16:06:01 +01004998 /* Goto is required to silence warnings about unused labels, as we
4999 * don't actually do any test assertions in this function. */
5000 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005001}
5002/* END_CASE */
5003
5004/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005005void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5006 int alg_arg,
5007 data_t *nonce,
5008 data_t *additional_data,
5009 data_t *input_data,
5010 int do_set_lengths,
5011 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005012{
Paul Elliottd3f82412021-06-16 16:52:21 +01005013 size_t ad_part_len = 0;
5014 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005015 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005016
Gilles Peskine449bd832023-01-11 14:50:10 +01005017 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005018 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005020
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 if (do_set_lengths) {
5022 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005023 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005024 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005025 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005026 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005027 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005028
Gilles Peskine449bd832023-01-11 14:50:10 +01005029 if (!aead_multipart_internal_func(key_type_arg, key_data,
5030 alg_arg, nonce,
5031 additional_data,
5032 ad_part_len,
5033 input_data, -1,
5034 set_lengths_method,
5035 expected_output,
5036 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005037 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005038 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005039
5040 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005041 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005042
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 if (!aead_multipart_internal_func(key_type_arg, key_data,
5044 alg_arg, nonce,
5045 additional_data,
5046 ad_part_len,
5047 input_data, -1,
5048 set_lengths_method,
5049 expected_output,
5050 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005051 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005052 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005053 }
5054
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005056 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005057 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005058
Gilles Peskine449bd832023-01-11 14:50:10 +01005059 if (do_set_lengths) {
5060 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005061 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005062 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005063 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005065 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005066
Gilles Peskine449bd832023-01-11 14:50:10 +01005067 if (!aead_multipart_internal_func(key_type_arg, key_data,
5068 alg_arg, nonce,
5069 additional_data, -1,
5070 input_data, data_part_len,
5071 set_lengths_method,
5072 expected_output,
5073 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005074 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005076
5077 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005079
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 if (!aead_multipart_internal_func(key_type_arg, key_data,
5081 alg_arg, nonce,
5082 additional_data, -1,
5083 input_data, data_part_len,
5084 set_lengths_method,
5085 expected_output,
5086 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005087 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005089 }
5090
Paul Elliott8fc45162021-06-23 16:06:01 +01005091 /* Goto is required to silence warnings about unused labels, as we
5092 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005093 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005094}
5095/* END_CASE */
5096
5097/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005098void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5099 int alg_arg,
5100 int nonce_length,
5101 int expected_nonce_length_arg,
5102 data_t *additional_data,
5103 data_t *input_data,
5104 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005105{
5106
5107 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5108 psa_key_type_t key_type = key_type_arg;
5109 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005110 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005111 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5112 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5113 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005114 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005115 size_t actual_nonce_length = 0;
5116 size_t expected_nonce_length = expected_nonce_length_arg;
5117 unsigned char *output = NULL;
5118 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005119 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005120 size_t ciphertext_size = 0;
5121 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005122 size_t tag_length = 0;
5123 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005124
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005126
Gilles Peskine449bd832023-01-11 14:50:10 +01005127 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5128 psa_set_key_algorithm(&attributes, alg);
5129 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005130
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5132 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005133
Gilles Peskine449bd832023-01-11 14:50:10 +01005134 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005135
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005137
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 ASSERT_ALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005139
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005141
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005143
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005145
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005147
5148 /* If the operation is not supported, just skip and not fail in case the
5149 * encryption involves a common limitation of cryptography hardwares and
5150 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 if (status == PSA_ERROR_NOT_SUPPORTED) {
5152 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5153 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005154 }
5155
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005157
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5159 nonce_length,
5160 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005161
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005163
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005165
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 if (expected_status == PSA_SUCCESS) {
5167 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5168 alg));
5169 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005170
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005172
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005174 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005175 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5176 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005177
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5179 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005180
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5182 output, output_size,
5183 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005184
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5186 &ciphertext_length, tag_buffer,
5187 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005188 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005189
5190exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 psa_destroy_key(key);
5192 mbedtls_free(output);
5193 mbedtls_free(ciphertext);
5194 psa_aead_abort(&operation);
5195 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005196}
5197/* END_CASE */
5198
5199/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005200void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5201 int alg_arg,
5202 int nonce_length_arg,
5203 int set_lengths_method_arg,
5204 data_t *additional_data,
5205 data_t *input_data,
5206 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005207{
5208
5209 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5210 psa_key_type_t key_type = key_type_arg;
5211 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005212 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005213 uint8_t *nonce_buffer = NULL;
5214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5215 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5216 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005217 unsigned char *output = NULL;
5218 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005219 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005220 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005221 size_t ciphertext_size = 0;
5222 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005223 size_t tag_length = 0;
5224 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005225 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005226 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005227
Gilles Peskine449bd832023-01-11 14:50:10 +01005228 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005229
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5231 psa_set_key_algorithm(&attributes, alg);
5232 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005233
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5235 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005236
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005238
Gilles Peskine449bd832023-01-11 14:50:10 +01005239 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005240
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 ASSERT_ALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005242
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005244
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005246
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005248
Gilles Peskine449bd832023-01-11 14:50:10 +01005249 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005250
5251 /* If the operation is not supported, just skip and not fail in case the
5252 * encryption involves a common limitation of cryptography hardwares and
5253 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 if (status == PSA_ERROR_NOT_SUPPORTED) {
5255 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5256 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005257 }
5258
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005260
Paul Elliott4023ffd2021-09-10 16:21:22 +01005261 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 if (nonce_length_arg == -1) {
5263 /* Arbitrary size buffer, to test zero length valid buffer. */
5264 ASSERT_ALLOC(nonce_buffer, 4);
5265 nonce_length = 0;
5266 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005267 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 nonce_length = (size_t) nonce_length_arg;
5269 ASSERT_ALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005270
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 if (nonce_buffer) {
5272 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005273 nonce_buffer[index] = 'a' + index;
5274 }
Paul Elliott66696b52021-08-16 18:42:41 +01005275 }
Paul Elliott863864a2021-07-23 17:28:31 +01005276 }
5277
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5279 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5280 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005281 }
5282
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005284
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005286
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 if (expected_status == PSA_SUCCESS) {
5288 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5289 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5290 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005291 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005293 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 }
Paul Elliott863864a2021-07-23 17:28:31 +01005295
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005296 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5298 additional_data->len),
5299 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005300
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5302 output, output_size,
5303 &ciphertext_length),
5304 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005305
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5307 &ciphertext_length, tag_buffer,
5308 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5309 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005310 }
5311
5312exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 psa_destroy_key(key);
5314 mbedtls_free(output);
5315 mbedtls_free(ciphertext);
5316 mbedtls_free(nonce_buffer);
5317 psa_aead_abort(&operation);
5318 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005319}
5320/* END_CASE */
5321
5322/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005323void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005324 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005325 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005326 data_t *nonce,
5327 data_t *additional_data,
5328 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005330{
5331
5332 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5333 psa_key_type_t key_type = key_type_arg;
5334 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005335 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005336 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5337 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5338 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005339 unsigned char *output = NULL;
5340 unsigned char *ciphertext = NULL;
5341 size_t output_size = output_size_arg;
5342 size_t ciphertext_size = 0;
5343 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005344 size_t tag_length = 0;
5345 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5346
Gilles Peskine449bd832023-01-11 14:50:10 +01005347 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005348
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5350 psa_set_key_algorithm(&attributes, alg);
5351 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005352
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5354 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005355
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005357
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 ASSERT_ALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005359
Gilles Peskine449bd832023-01-11 14:50:10 +01005360 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005361
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005363
Gilles Peskine449bd832023-01-11 14:50:10 +01005364 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005365
5366 /* If the operation is not supported, just skip and not fail in case the
5367 * encryption involves a common limitation of cryptography hardwares and
5368 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 if (status == PSA_ERROR_NOT_SUPPORTED) {
5370 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5371 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005372 }
5373
Gilles Peskine449bd832023-01-11 14:50:10 +01005374 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005375
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5377 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005378
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005380
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5382 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005383
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 status = psa_aead_update(&operation, input_data->x, input_data->len,
5385 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005386
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005388
Gilles Peskine449bd832023-01-11 14:50:10 +01005389 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005390 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5392 &ciphertext_length, tag_buffer,
5393 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005394 }
5395
5396exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 psa_destroy_key(key);
5398 mbedtls_free(output);
5399 mbedtls_free(ciphertext);
5400 psa_aead_abort(&operation);
5401 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005402}
5403/* END_CASE */
5404
Paul Elliott91b021e2021-07-23 18:52:31 +01005405/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005406void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5407 int alg_arg,
5408 int finish_ciphertext_size_arg,
5409 int tag_size_arg,
5410 data_t *nonce,
5411 data_t *additional_data,
5412 data_t *input_data,
5413 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005414{
5415
5416 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5417 psa_key_type_t key_type = key_type_arg;
5418 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005419 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005420 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5421 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5422 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005423 unsigned char *ciphertext = NULL;
5424 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005425 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005426 size_t ciphertext_size = 0;
5427 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005428 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5429 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005430 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005431
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005433
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5435 psa_set_key_algorithm(&attributes, alg);
5436 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005437
Gilles Peskine449bd832023-01-11 14:50:10 +01005438 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5439 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005440
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005442
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005444
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 ASSERT_ALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005446
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 ASSERT_ALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005448
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 ASSERT_ALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005450
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005452
5453 /* If the operation is not supported, just skip and not fail in case the
5454 * encryption involves a common limitation of cryptography hardwares and
5455 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 if (status == PSA_ERROR_NOT_SUPPORTED) {
5457 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5458 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005459 }
5460
Gilles Peskine449bd832023-01-11 14:50:10 +01005461 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005462
Gilles Peskine449bd832023-01-11 14:50:10 +01005463 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005464
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5466 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005467
Gilles Peskine449bd832023-01-11 14:50:10 +01005468 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5469 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005470
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5472 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005473
5474 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 status = psa_aead_finish(&operation, finish_ciphertext,
5476 finish_ciphertext_size,
5477 &ciphertext_length, tag_buffer,
5478 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005479
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005481
5482exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 psa_destroy_key(key);
5484 mbedtls_free(ciphertext);
5485 mbedtls_free(finish_ciphertext);
5486 mbedtls_free(tag_buffer);
5487 psa_aead_abort(&operation);
5488 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005489}
5490/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005491
5492/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005493void aead_multipart_verify(int key_type_arg, data_t *key_data,
5494 int alg_arg,
5495 data_t *nonce,
5496 data_t *additional_data,
5497 data_t *input_data,
5498 data_t *tag,
5499 int tag_usage_arg,
5500 int expected_setup_status_arg,
5501 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005502{
5503 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5504 psa_key_type_t key_type = key_type_arg;
5505 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005506 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005507 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5508 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5509 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005510 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005511 unsigned char *plaintext = NULL;
5512 unsigned char *finish_plaintext = NULL;
5513 size_t plaintext_size = 0;
5514 size_t plaintext_length = 0;
5515 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005516 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005517 unsigned char *tag_buffer = NULL;
5518 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005519
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005521
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5523 psa_set_key_algorithm(&attributes, alg);
5524 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005525
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5527 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005528
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005530
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5532 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005533
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 ASSERT_ALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005535
Gilles Peskine449bd832023-01-11 14:50:10 +01005536 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005537
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 ASSERT_ALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005539
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005541
5542 /* If the operation is not supported, just skip and not fail in case the
5543 * encryption involves a common limitation of cryptography hardwares and
5544 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 if (status == PSA_ERROR_NOT_SUPPORTED) {
5546 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5547 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005548 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005550
Gilles Peskine449bd832023-01-11 14:50:10 +01005551 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005552 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005553 }
Paul Elliott9961a662021-09-17 19:19:02 +01005554
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005556
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005558
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 status = psa_aead_set_lengths(&operation, additional_data->len,
5560 input_data->len);
5561 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005562
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5564 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005565
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5567 input_data->len,
5568 plaintext, plaintext_size,
5569 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005570
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005572 tag_buffer = tag->x;
5573 tag_size = tag->len;
5574 }
5575
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 status = psa_aead_verify(&operation, finish_plaintext,
5577 verify_plaintext_size,
5578 &plaintext_length,
5579 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005580
Gilles Peskine449bd832023-01-11 14:50:10 +01005581 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005582
5583exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 psa_destroy_key(key);
5585 mbedtls_free(plaintext);
5586 mbedtls_free(finish_plaintext);
5587 psa_aead_abort(&operation);
5588 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005589}
5590/* END_CASE */
5591
Paul Elliott9961a662021-09-17 19:19:02 +01005592/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005593void aead_multipart_setup(int key_type_arg, data_t *key_data,
5594 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005595{
5596 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5597 psa_key_type_t key_type = key_type_arg;
5598 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005599 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005600 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5601 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5602 psa_status_t expected_status = expected_status_arg;
5603
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005605
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 psa_set_key_usage_flags(&attributes,
5607 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5608 psa_set_key_algorithm(&attributes, alg);
5609 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005610
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5612 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005613
Gilles Peskine449bd832023-01-11 14:50:10 +01005614 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005615
Gilles Peskine449bd832023-01-11 14:50:10 +01005616 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005617
Gilles Peskine449bd832023-01-11 14:50:10 +01005618 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005619
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005621
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005623
5624exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 psa_destroy_key(key);
5626 psa_aead_abort(&operation);
5627 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005628}
5629/* END_CASE */
5630
5631/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005632void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5633 int alg_arg,
5634 data_t *nonce,
5635 data_t *additional_data,
5636 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005637{
5638 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5639 psa_key_type_t key_type = key_type_arg;
5640 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005641 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005642 unsigned char *output_data = NULL;
5643 unsigned char *final_data = NULL;
5644 size_t output_size = 0;
5645 size_t finish_output_size = 0;
5646 size_t output_length = 0;
5647 size_t key_bits = 0;
5648 size_t tag_length = 0;
5649 size_t tag_size = 0;
5650 size_t nonce_length = 0;
5651 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5652 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5653 size_t output_part_length = 0;
5654 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5655
Gilles Peskine449bd832023-01-11 14:50:10 +01005656 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005657
Gilles Peskine449bd832023-01-11 14:50:10 +01005658 psa_set_key_usage_flags(&attributes,
5659 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5660 psa_set_key_algorithm(&attributes, alg);
5661 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005662
Gilles Peskine449bd832023-01-11 14:50:10 +01005663 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5664 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005665
Gilles Peskine449bd832023-01-11 14:50:10 +01005666 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5667 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005668
Gilles Peskine449bd832023-01-11 14:50:10 +01005669 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005670
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005672
Gilles Peskine449bd832023-01-11 14:50:10 +01005673 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005674
Gilles Peskine449bd832023-01-11 14:50:10 +01005675 ASSERT_ALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005676
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005678
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005680
Gilles Peskine449bd832023-01-11 14:50:10 +01005681 ASSERT_ALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005682
5683 /* Test all operations error without calling setup first. */
5684
Gilles Peskine449bd832023-01-11 14:50:10 +01005685 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5686 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005687
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005689
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5691 PSA_AEAD_NONCE_MAX_SIZE,
5692 &nonce_length),
5693 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005694
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005696
Paul Elliott481be342021-07-16 17:38:47 +01005697 /* ------------------------------------------------------- */
5698
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5700 input_data->len),
5701 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005702
Gilles Peskine449bd832023-01-11 14:50:10 +01005703 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005704
Paul Elliott481be342021-07-16 17:38:47 +01005705 /* ------------------------------------------------------- */
5706
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5708 additional_data->len),
5709 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005710
Gilles Peskine449bd832023-01-11 14:50:10 +01005711 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005712
Paul Elliott481be342021-07-16 17:38:47 +01005713 /* ------------------------------------------------------- */
5714
Gilles Peskine449bd832023-01-11 14:50:10 +01005715 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5716 input_data->len, output_data,
5717 output_size, &output_length),
5718 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005719
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005721
Paul Elliott481be342021-07-16 17:38:47 +01005722 /* ------------------------------------------------------- */
5723
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5725 finish_output_size,
5726 &output_part_length,
5727 tag_buffer, tag_length,
5728 &tag_size),
5729 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005730
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005732
Paul Elliott481be342021-07-16 17:38:47 +01005733 /* ------------------------------------------------------- */
5734
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5736 finish_output_size,
5737 &output_part_length,
5738 tag_buffer,
5739 tag_length),
5740 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005741
Gilles Peskine449bd832023-01-11 14:50:10 +01005742 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005743
5744 /* Test for double setups. */
5745
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005747
Gilles Peskine449bd832023-01-11 14:50:10 +01005748 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5749 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005750
Gilles Peskine449bd832023-01-11 14:50:10 +01005751 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005752
Paul Elliott481be342021-07-16 17:38:47 +01005753 /* ------------------------------------------------------- */
5754
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005756
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5758 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005759
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005761
Paul Elliott374a2be2021-07-16 17:53:40 +01005762 /* ------------------------------------------------------- */
5763
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005765
Gilles Peskine449bd832023-01-11 14:50:10 +01005766 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5767 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005768
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005770
5771 /* ------------------------------------------------------- */
5772
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005774
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5776 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005777
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005779
Paul Elliottc23a9a02021-06-21 18:32:46 +01005780 /* Test for not setting a nonce. */
5781
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005783
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5785 additional_data->len),
5786 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005787
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005789
Paul Elliott7f628422021-09-01 12:08:29 +01005790 /* ------------------------------------------------------- */
5791
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005793
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5795 input_data->len, output_data,
5796 output_size, &output_length),
5797 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005798
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005800
Paul Elliottbdc2c682021-09-21 18:37:10 +01005801 /* ------------------------------------------------------- */
5802
Gilles Peskine449bd832023-01-11 14:50:10 +01005803 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005804
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5806 finish_output_size,
5807 &output_part_length,
5808 tag_buffer, tag_length,
5809 &tag_size),
5810 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005811
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005813
5814 /* ------------------------------------------------------- */
5815
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005817
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5819 finish_output_size,
5820 &output_part_length,
5821 tag_buffer,
5822 tag_length),
5823 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005824
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005826
Paul Elliottc23a9a02021-06-21 18:32:46 +01005827 /* Test for double setting nonce. */
5828
Gilles Peskine449bd832023-01-11 14:50:10 +01005829 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005830
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005832
Gilles Peskine449bd832023-01-11 14:50:10 +01005833 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5834 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005835
Gilles Peskine449bd832023-01-11 14:50:10 +01005836 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005837
Paul Elliott374a2be2021-07-16 17:53:40 +01005838 /* Test for double generating nonce. */
5839
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005841
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5843 PSA_AEAD_NONCE_MAX_SIZE,
5844 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005845
Gilles Peskine449bd832023-01-11 14:50:10 +01005846 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5847 PSA_AEAD_NONCE_MAX_SIZE,
5848 &nonce_length),
5849 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005850
5851
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005853
5854 /* Test for generate nonce then set and vice versa */
5855
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005857
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5859 PSA_AEAD_NONCE_MAX_SIZE,
5860 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005861
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5863 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005864
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005866
Andrzej Kurekad837522021-12-15 15:28:49 +01005867 /* Test for generating nonce after calling set lengths */
5868
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005870
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5872 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005873
Gilles Peskine449bd832023-01-11 14:50:10 +01005874 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5875 PSA_AEAD_NONCE_MAX_SIZE,
5876 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005877
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005879
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005880 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005881
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005883
Gilles Peskine449bd832023-01-11 14:50:10 +01005884 if (operation.alg == PSA_ALG_CCM) {
5885 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5886 input_data->len),
5887 PSA_ERROR_INVALID_ARGUMENT);
5888 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5889 PSA_AEAD_NONCE_MAX_SIZE,
5890 &nonce_length),
5891 PSA_ERROR_BAD_STATE);
5892 } else {
5893 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5894 input_data->len));
5895 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5896 PSA_AEAD_NONCE_MAX_SIZE,
5897 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005898 }
5899
Gilles Peskine449bd832023-01-11 14:50:10 +01005900 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005901
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005902 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005903#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005904 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005905
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5907 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5908 input_data->len),
5909 PSA_ERROR_INVALID_ARGUMENT);
5910 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5911 PSA_AEAD_NONCE_MAX_SIZE,
5912 &nonce_length),
5913 PSA_ERROR_BAD_STATE);
5914 } else {
5915 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5916 input_data->len));
5917 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5918 PSA_AEAD_NONCE_MAX_SIZE,
5919 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005920 }
5921
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005923#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005924
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005925 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005926
Gilles Peskine449bd832023-01-11 14:50:10 +01005927 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005928
Gilles Peskine449bd832023-01-11 14:50:10 +01005929 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5930 PSA_AEAD_NONCE_MAX_SIZE,
5931 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005932
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 if (operation.alg == PSA_ALG_CCM) {
5934 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5935 input_data->len),
5936 PSA_ERROR_INVALID_ARGUMENT);
5937 } else {
5938 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5939 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005940 }
5941
Gilles Peskine449bd832023-01-11 14:50:10 +01005942 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005943
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005944 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005945 /* Test for setting nonce after calling set lengths */
5946
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005948
Gilles Peskine449bd832023-01-11 14:50:10 +01005949 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5950 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005951
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005953
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005955
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005956 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005957
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005959
Gilles Peskine449bd832023-01-11 14:50:10 +01005960 if (operation.alg == PSA_ALG_CCM) {
5961 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5962 input_data->len),
5963 PSA_ERROR_INVALID_ARGUMENT);
5964 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5965 PSA_ERROR_BAD_STATE);
5966 } else {
5967 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5968 input_data->len));
5969 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005970 }
5971
Gilles Peskine449bd832023-01-11 14:50:10 +01005972 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005973
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005974 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005975#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005976 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005977
Gilles Peskine449bd832023-01-11 14:50:10 +01005978 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5979 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5980 input_data->len),
5981 PSA_ERROR_INVALID_ARGUMENT);
5982 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5983 PSA_ERROR_BAD_STATE);
5984 } else {
5985 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5986 input_data->len));
5987 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005988 }
5989
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00005991#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005992
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005993 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005994
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005996
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005998
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 if (operation.alg == PSA_ALG_CCM) {
6000 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6001 input_data->len),
6002 PSA_ERROR_INVALID_ARGUMENT);
6003 } else {
6004 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6005 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006006 }
6007
Gilles Peskine449bd832023-01-11 14:50:10 +01006008 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006009
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006010 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006011#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006013
Gilles Peskine449bd832023-01-11 14:50:10 +01006014 if (operation.alg == PSA_ALG_GCM) {
6015 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6016 SIZE_MAX),
6017 PSA_ERROR_INVALID_ARGUMENT);
6018 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6019 PSA_ERROR_BAD_STATE);
6020 } else if (operation.alg != PSA_ALG_CCM) {
6021 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6022 SIZE_MAX));
6023 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006024 }
6025
Gilles Peskine449bd832023-01-11 14:50:10 +01006026 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006027#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006028
Tom Cosgrove1797b052022-12-04 17:19:59 +00006029 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006030#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006031 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006032
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006034
Gilles Peskine449bd832023-01-11 14:50:10 +01006035 if (operation.alg == PSA_ALG_GCM) {
6036 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6037 SIZE_MAX),
6038 PSA_ERROR_INVALID_ARGUMENT);
6039 } else if (operation.alg != PSA_ALG_CCM) {
6040 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6041 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006042 }
6043
Gilles Peskine449bd832023-01-11 14:50:10 +01006044 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006045#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006046
6047 /* ------------------------------------------------------- */
6048
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006050
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006052
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6054 PSA_AEAD_NONCE_MAX_SIZE,
6055 &nonce_length),
6056 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006057
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006059
Paul Elliott7220cae2021-06-22 17:25:57 +01006060 /* Test for generating nonce in decrypt setup. */
6061
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006063
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6065 PSA_AEAD_NONCE_MAX_SIZE,
6066 &nonce_length),
6067 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006068
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006070
Paul Elliottc23a9a02021-06-21 18:32:46 +01006071 /* Test for setting lengths twice. */
6072
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006074
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006076
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6078 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006079
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6081 input_data->len),
6082 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006083
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006085
Andrzej Kurekad837522021-12-15 15:28:49 +01006086 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006087
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006089
Gilles Peskine449bd832023-01-11 14:50:10 +01006090 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006091
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006093
Gilles Peskine449bd832023-01-11 14:50:10 +01006094 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6095 additional_data->len),
6096 PSA_ERROR_BAD_STATE);
6097 } else {
6098 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6099 additional_data->len));
6100
6101 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6102 input_data->len),
6103 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006104 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006106
6107 /* ------------------------------------------------------- */
6108
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006110
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006112
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 if (operation.alg == PSA_ALG_CCM) {
6114 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6115 input_data->len, output_data,
6116 output_size, &output_length),
6117 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006118
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 } else {
6120 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6121 input_data->len, output_data,
6122 output_size, &output_length));
6123
6124 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6125 input_data->len),
6126 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006127 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006128 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006129
6130 /* ------------------------------------------------------- */
6131
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006133
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006135
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 if (operation.alg == PSA_ALG_CCM) {
6137 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6138 finish_output_size,
6139 &output_part_length,
6140 tag_buffer, tag_length,
6141 &tag_size));
6142 } else {
6143 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6144 finish_output_size,
6145 &output_part_length,
6146 tag_buffer, tag_length,
6147 &tag_size));
6148
6149 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6150 input_data->len),
6151 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006152 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006154
6155 /* Test for setting lengths after generating nonce + already starting data. */
6156
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006158
Gilles Peskine449bd832023-01-11 14:50:10 +01006159 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6160 PSA_AEAD_NONCE_MAX_SIZE,
6161 &nonce_length));
6162 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006163
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6165 additional_data->len),
6166 PSA_ERROR_BAD_STATE);
6167 } else {
6168 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6169 additional_data->len));
6170
6171 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6172 input_data->len),
6173 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006174 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006175 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006176
6177 /* ------------------------------------------------------- */
6178
Gilles Peskine449bd832023-01-11 14:50:10 +01006179 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006180
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6182 PSA_AEAD_NONCE_MAX_SIZE,
6183 &nonce_length));
6184 if (operation.alg == PSA_ALG_CCM) {
6185 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6186 input_data->len, output_data,
6187 output_size, &output_length),
6188 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006189
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 } else {
6191 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6192 input_data->len, output_data,
6193 output_size, &output_length));
6194
6195 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6196 input_data->len),
6197 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006198 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006199 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006200
6201 /* ------------------------------------------------------- */
6202
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006204
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6206 PSA_AEAD_NONCE_MAX_SIZE,
6207 &nonce_length));
6208 if (operation.alg == PSA_ALG_CCM) {
6209 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6210 finish_output_size,
6211 &output_part_length,
6212 tag_buffer, tag_length,
6213 &tag_size));
6214 } else {
6215 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6216 finish_output_size,
6217 &output_part_length,
6218 tag_buffer, tag_length,
6219 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006220
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6222 input_data->len),
6223 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006224 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006226
Paul Elliott243080c2021-07-21 19:01:17 +01006227 /* Test for not sending any additional data or data after setting non zero
6228 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006229
Gilles Peskine449bd832023-01-11 14:50:10 +01006230 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006231
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006233
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6235 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006236
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6238 finish_output_size,
6239 &output_part_length,
6240 tag_buffer, tag_length,
6241 &tag_size),
6242 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006243
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006245
Paul Elliott243080c2021-07-21 19:01:17 +01006246 /* Test for not sending any additional data or data after setting non-zero
6247 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006248
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006250
Gilles Peskine449bd832023-01-11 14:50:10 +01006251 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006252
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6254 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6257 finish_output_size,
6258 &output_part_length,
6259 tag_buffer,
6260 tag_length),
6261 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006262
Gilles Peskine449bd832023-01-11 14:50:10 +01006263 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006264
Paul Elliott243080c2021-07-21 19:01:17 +01006265 /* Test for not sending any additional data after setting a non-zero length
6266 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006267
Gilles Peskine449bd832023-01-11 14:50:10 +01006268 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006269
Gilles Peskine449bd832023-01-11 14:50:10 +01006270 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006271
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6273 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006274
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6276 input_data->len, output_data,
6277 output_size, &output_length),
6278 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006279
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006281
Paul Elliottf94bd992021-09-19 18:15:59 +01006282 /* Test for not sending any data after setting a non-zero length for it.*/
6283
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006285
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006287
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6289 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006290
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6292 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006293
Gilles Peskine449bd832023-01-11 14:50:10 +01006294 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6295 finish_output_size,
6296 &output_part_length,
6297 tag_buffer, tag_length,
6298 &tag_size),
6299 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006300
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006302
Paul Elliottb0450fe2021-09-01 15:06:26 +01006303 /* Test for sending too much additional data after setting lengths. */
6304
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006306
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006308
Gilles Peskine449bd832023-01-11 14:50:10 +01006309 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006310
6311
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6313 additional_data->len),
6314 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006315
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006317
Paul Elliotta2a09b02021-09-22 14:56:40 +01006318 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006319
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006321
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006323
Gilles Peskine449bd832023-01-11 14:50:10 +01006324 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6325 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006326
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6328 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006329
Gilles Peskine449bd832023-01-11 14:50:10 +01006330 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6331 1),
6332 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006333
Gilles Peskine449bd832023-01-11 14:50:10 +01006334 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006335
Paul Elliottb0450fe2021-09-01 15:06:26 +01006336 /* Test for sending too much data after setting lengths. */
6337
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006339
Gilles Peskine449bd832023-01-11 14:50:10 +01006340 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006341
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006343
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6345 input_data->len, output_data,
6346 output_size, &output_length),
6347 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006348
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006350
Paul Elliotta2a09b02021-09-22 14:56:40 +01006351 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006352
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006354
Gilles Peskine449bd832023-01-11 14:50:10 +01006355 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006356
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6358 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006359
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6361 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006362
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6364 input_data->len, output_data,
6365 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006366
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6368 1, output_data,
6369 output_size, &output_length),
6370 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006371
Gilles Peskine449bd832023-01-11 14:50:10 +01006372 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006373
Paul Elliottc23a9a02021-06-21 18:32:46 +01006374 /* Test sending additional data after data. */
6375
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006377
Gilles Peskine449bd832023-01-11 14:50:10 +01006378 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006379
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 if (operation.alg != PSA_ALG_CCM) {
6381 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6382 input_data->len, output_data,
6383 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006384
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6386 additional_data->len),
6387 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006388 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006390
Paul Elliott534d0b42021-06-22 19:15:20 +01006391 /* Test calling finish on decryption. */
6392
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006394
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006396
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6398 finish_output_size,
6399 &output_part_length,
6400 tag_buffer, tag_length,
6401 &tag_size),
6402 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006403
Gilles Peskine449bd832023-01-11 14:50:10 +01006404 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006405
6406 /* Test calling verify on encryption. */
6407
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006409
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006411
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6413 finish_output_size,
6414 &output_part_length,
6415 tag_buffer,
6416 tag_length),
6417 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006418
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006420
6421
Paul Elliottc23a9a02021-06-21 18:32:46 +01006422exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006423 psa_destroy_key(key);
6424 psa_aead_abort(&operation);
6425 mbedtls_free(output_data);
6426 mbedtls_free(final_data);
6427 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006428}
6429/* END_CASE */
6430
6431/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006432void signature_size(int type_arg,
6433 int bits,
6434 int alg_arg,
6435 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006436{
6437 psa_key_type_t type = type_arg;
6438 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006439 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006440
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006442
Gilles Peskinee59236f2018-01-27 23:32:46 +01006443exit:
6444 ;
6445}
6446/* END_CASE */
6447
6448/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006449void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6450 int alg_arg, data_t *input_data,
6451 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006452{
Ronald Cron5425a212020-08-04 14:58:35 +02006453 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006454 psa_key_type_t key_type = key_type_arg;
6455 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006456 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006457 unsigned char *signature = NULL;
6458 size_t signature_size;
6459 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006461
Gilles Peskine449bd832023-01-11 14:50:10 +01006462 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006463
Gilles Peskine449bd832023-01-11 14:50:10 +01006464 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6465 psa_set_key_algorithm(&attributes, alg);
6466 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006467
Gilles Peskine449bd832023-01-11 14:50:10 +01006468 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6469 &key));
6470 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6471 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006472
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006473 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006474 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6476 key_bits, alg);
6477 TEST_ASSERT(signature_size != 0);
6478 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6479 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006480
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006481 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006482 PSA_ASSERT(psa_sign_hash(key, alg,
6483 input_data->x, input_data->len,
6484 signature, signature_size,
6485 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006486 /* Verify that the signature is what is expected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 ASSERT_COMPARE(output_data->x, output_data->len,
6488 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006489
6490exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006491 /*
6492 * Key attributes may have been returned by psa_get_key_attributes()
6493 * thus reset them as required.
6494 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006495 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006496
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 psa_destroy_key(key);
6498 mbedtls_free(signature);
6499 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006500}
6501/* END_CASE */
6502
Paul Elliott712d5122022-12-07 14:03:10 +00006503/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006504/**
6505 * sign_hash_interruptible() test intentions:
6506 *
6507 * Note: This test can currently only handle ECDSA.
6508 *
6509 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006510 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006511 *
6512 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6513 * expected for different max_ops values.
6514 *
6515 * 3. Test that the number of ops done prior to start and after abort is zero
6516 * and that each successful stage completes some ops (this is not mandated by
6517 * the PSA specification, but is currently the case).
6518 *
6519 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6520 * complete() calls does not alter the number of ops returned.
6521 */
Paul Elliott712d5122022-12-07 14:03:10 +00006522void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6523 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006524 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006525{
6526 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6527 psa_key_type_t key_type = key_type_arg;
6528 psa_algorithm_t alg = alg_arg;
6529 size_t key_bits;
6530 unsigned char *signature = NULL;
6531 size_t signature_size;
6532 size_t signature_length = 0xdeadbeef;
6533 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6534 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006535 uint32_t num_ops = 0;
6536 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006537 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006538 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006539 size_t min_completes = 0;
6540 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006541
Paul Elliott712d5122022-12-07 14:03:10 +00006542 psa_sign_hash_interruptible_operation_t operation =
6543 psa_sign_hash_interruptible_operation_init();
6544
6545 PSA_ASSERT(psa_crypto_init());
6546
6547 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6548 psa_set_key_algorithm(&attributes, alg);
6549 psa_set_key_type(&attributes, key_type);
6550
6551 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6552 &key));
6553 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6554 key_bits = psa_get_key_bits(&attributes);
6555
6556 /* Allocate a buffer which has the size advertised by the
6557 * library. */
6558 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6559 key_bits, alg);
6560 TEST_ASSERT(signature_size != 0);
6561 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6562 ASSERT_ALLOC(signature, signature_size);
6563
Paul Elliott0c683352022-12-16 19:16:56 +00006564 psa_interruptible_set_max_ops(max_ops);
6565
Paul Elliott6f600372023-02-06 18:41:05 +00006566 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6567 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006568
Paul Elliott712d5122022-12-07 14:03:10 +00006569 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6570 TEST_ASSERT(num_ops_prior == 0);
6571
6572 /* Start performing the signature. */
6573 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6574 input_data->x, input_data->len));
6575
6576 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6577 TEST_ASSERT(num_ops_prior == 0);
6578
6579 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006580 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006581 status = psa_sign_hash_complete(&operation, signature, signature_size,
6582 &signature_length);
6583
Paul Elliott0c683352022-12-16 19:16:56 +00006584 num_completes++;
6585
Paul Elliott712d5122022-12-07 14:03:10 +00006586 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6587 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006588 /* We are asserting here that every complete makes progress
6589 * (completes some ops), which is true of the internal
6590 * implementation and probably any implementation, however this is
6591 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006592 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006593
Paul Elliott712d5122022-12-07 14:03:10 +00006594 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006595
6596 /* Ensure calling get_num_ops() twice still returns the same
6597 * number of ops as previously reported. */
6598 num_ops = psa_sign_hash_get_num_ops(&operation);
6599
6600 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006601 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006602 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006603
6604 TEST_ASSERT(status == PSA_SUCCESS);
6605
Paul Elliott0c683352022-12-16 19:16:56 +00006606 TEST_LE_U(min_completes, num_completes);
6607 TEST_LE_U(num_completes, max_completes);
6608
Paul Elliott712d5122022-12-07 14:03:10 +00006609 /* Verify that the signature is what is expected. */
6610 ASSERT_COMPARE(output_data->x, output_data->len,
6611 signature, signature_length);
6612
6613 PSA_ASSERT(psa_sign_hash_abort(&operation));
6614
Paul Elliott59ad9452022-12-18 15:09:02 +00006615 num_ops = psa_sign_hash_get_num_ops(&operation);
6616 TEST_ASSERT(num_ops == 0);
6617
Paul Elliott712d5122022-12-07 14:03:10 +00006618exit:
6619
6620 /*
6621 * Key attributes may have been returned by psa_get_key_attributes()
6622 * thus reset them as required.
6623 */
6624 psa_reset_key_attributes(&attributes);
6625
6626 psa_destroy_key(key);
6627 mbedtls_free(signature);
6628 PSA_DONE();
6629}
6630/* END_CASE */
6631
Gilles Peskine20035e32018-02-03 22:44:14 +01006632/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006633void sign_hash_fail(int key_type_arg, data_t *key_data,
6634 int alg_arg, data_t *input_data,
6635 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006636{
Ronald Cron5425a212020-08-04 14:58:35 +02006637 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006638 psa_key_type_t key_type = key_type_arg;
6639 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006640 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006641 psa_status_t actual_status;
6642 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006643 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006644 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006645 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006646
Gilles Peskine449bd832023-01-11 14:50:10 +01006647 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006648
Gilles Peskine449bd832023-01-11 14:50:10 +01006649 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006650
Gilles Peskine449bd832023-01-11 14:50:10 +01006651 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6652 psa_set_key_algorithm(&attributes, alg);
6653 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006654
Gilles Peskine449bd832023-01-11 14:50:10 +01006655 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6656 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006657
Gilles Peskine449bd832023-01-11 14:50:10 +01006658 actual_status = psa_sign_hash(key, alg,
6659 input_data->x, input_data->len,
6660 signature, signature_size,
6661 &signature_length);
6662 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006663 /* The value of *signature_length is unspecified on error, but
6664 * whatever it is, it should be less than signature_size, so that
6665 * if the caller tries to read *signature_length bytes without
6666 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006667 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006668
6669exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006670 psa_reset_key_attributes(&attributes);
6671 psa_destroy_key(key);
6672 mbedtls_free(signature);
6673 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006674}
6675/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006676
Paul Elliott91007972022-12-16 12:21:24 +00006677/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006678/**
6679 * sign_hash_fail_interruptible() test intentions:
6680 *
6681 * Note: This test can currently only handle ECDSA.
6682 *
6683 * 1. Test that various failure cases for interruptible sign hash fail with the
6684 * correct error codes, and at the correct point (at start or during
6685 * complete).
6686 *
6687 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6688 * expected for different max_ops values.
6689 *
6690 * 3. Test that the number of ops done prior to start and after abort is zero
6691 * and that each successful stage completes some ops (this is not mandated by
6692 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006693 *
6694 * 4. Check that calling complete() when start() fails and complete()
6695 * after completion results in a BAD_STATE error.
6696 *
6697 * 5. Check that calling start() again after start fails results in a BAD_STATE
6698 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006699 */
Paul Elliott91007972022-12-16 12:21:24 +00006700void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6701 int alg_arg, data_t *input_data,
6702 int signature_size_arg,
6703 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006704 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006705 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006706{
6707 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6708 psa_key_type_t key_type = key_type_arg;
6709 psa_algorithm_t alg = alg_arg;
6710 size_t signature_size = signature_size_arg;
6711 psa_status_t actual_status;
6712 psa_status_t expected_start_status = expected_start_status_arg;
6713 psa_status_t expected_complete_status = expected_complete_status_arg;
6714 unsigned char *signature = NULL;
6715 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006716 uint32_t num_ops = 0;
6717 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006718 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006719 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006720 size_t min_completes = 0;
6721 size_t max_completes = 0;
6722
Paul Elliott91007972022-12-16 12:21:24 +00006723 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6724 psa_sign_hash_interruptible_operation_t operation =
6725 psa_sign_hash_interruptible_operation_init();
6726
6727 ASSERT_ALLOC(signature, signature_size);
6728
6729 PSA_ASSERT(psa_crypto_init());
6730
6731 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6732 psa_set_key_algorithm(&attributes, alg);
6733 psa_set_key_type(&attributes, key_type);
6734
6735 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6736 &key));
6737
Paul Elliott0c683352022-12-16 19:16:56 +00006738 psa_interruptible_set_max_ops(max_ops);
6739
Paul Elliott6f600372023-02-06 18:41:05 +00006740 interruptible_signverify_get_minmax_completes(max_ops,
6741 expected_complete_status,
6742 &min_completes,
6743 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006744
Paul Elliott91007972022-12-16 12:21:24 +00006745 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6746 TEST_ASSERT(num_ops_prior == 0);
6747
6748 /* Start performing the signature. */
6749 actual_status = psa_sign_hash_start(&operation, key, alg,
6750 input_data->x, input_data->len);
6751
6752 TEST_EQUAL(actual_status, expected_start_status);
6753
Paul Elliottc9774412023-02-06 15:14:07 +00006754 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006755 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006756 * start failed. */
6757 actual_status = psa_sign_hash_complete(&operation, signature,
6758 signature_size,
6759 &signature_length);
6760
6761 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6762
6763 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006764 actual_status = psa_sign_hash_start(&operation, key, alg,
6765 input_data->x, input_data->len);
6766
6767 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6768 }
6769
Paul Elliott91007972022-12-16 12:21:24 +00006770 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6771 TEST_ASSERT(num_ops_prior == 0);
6772
Paul Elliott91007972022-12-16 12:21:24 +00006773 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006774 do {
Paul Elliott91007972022-12-16 12:21:24 +00006775 actual_status = psa_sign_hash_complete(&operation, signature,
6776 signature_size,
6777 &signature_length);
6778
Paul Elliott0c683352022-12-16 19:16:56 +00006779 num_completes++;
6780
Paul Elliott334d7262023-01-20 17:29:41 +00006781 if (actual_status == PSA_SUCCESS ||
6782 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006783 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006784 /* We are asserting here that every complete makes progress
6785 * (completes some ops), which is true of the internal
6786 * implementation and probably any implementation, however this is
6787 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006788 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006789
Paul Elliott91007972022-12-16 12:21:24 +00006790 num_ops_prior = num_ops;
6791 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006792 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006793
Paul Elliottc9774412023-02-06 15:14:07 +00006794 TEST_EQUAL(actual_status, expected_complete_status);
6795
Paul Elliottefebad02023-02-15 16:56:45 +00006796 /* Check that another complete returns BAD_STATE. */
6797 actual_status = psa_sign_hash_complete(&operation, signature,
6798 signature_size,
6799 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006800
Paul Elliottefebad02023-02-15 16:56:45 +00006801 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006802
Paul Elliott91007972022-12-16 12:21:24 +00006803 PSA_ASSERT(psa_sign_hash_abort(&operation));
6804
Paul Elliott59ad9452022-12-18 15:09:02 +00006805 num_ops = psa_sign_hash_get_num_ops(&operation);
6806 TEST_ASSERT(num_ops == 0);
6807
Paul Elliott91007972022-12-16 12:21:24 +00006808 /* The value of *signature_length is unspecified on error, but
6809 * whatever it is, it should be less than signature_size, so that
6810 * if the caller tries to read *signature_length bytes without
6811 * checking the error code then they don't overflow a buffer. */
6812 TEST_LE_U(signature_length, signature_size);
6813
Paul Elliott0c683352022-12-16 19:16:56 +00006814 TEST_LE_U(min_completes, num_completes);
6815 TEST_LE_U(num_completes, max_completes);
6816
Paul Elliott91007972022-12-16 12:21:24 +00006817exit:
6818 psa_reset_key_attributes(&attributes);
6819 psa_destroy_key(key);
6820 mbedtls_free(signature);
6821 PSA_DONE();
6822}
6823/* END_CASE */
6824
mohammad16038cc1cee2018-03-28 01:21:33 +03006825/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006826void sign_verify_hash(int key_type_arg, data_t *key_data,
6827 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006828{
Ronald Cron5425a212020-08-04 14:58:35 +02006829 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006830 psa_key_type_t key_type = key_type_arg;
6831 psa_algorithm_t alg = alg_arg;
6832 size_t key_bits;
6833 unsigned char *signature = NULL;
6834 size_t signature_size;
6835 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006836 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006837
Gilles Peskine449bd832023-01-11 14:50:10 +01006838 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006839
Gilles Peskine449bd832023-01-11 14:50:10 +01006840 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6841 psa_set_key_algorithm(&attributes, alg);
6842 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006843
Gilles Peskine449bd832023-01-11 14:50:10 +01006844 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6845 &key));
6846 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6847 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006848
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006849 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006850 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6852 key_bits, alg);
6853 TEST_ASSERT(signature_size != 0);
6854 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6855 ASSERT_ALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006856
6857 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006858 PSA_ASSERT(psa_sign_hash(key, alg,
6859 input_data->x, input_data->len,
6860 signature, signature_size,
6861 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006862 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006863 TEST_LE_U(signature_length, signature_size);
6864 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006865
6866 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006867 PSA_ASSERT(psa_verify_hash(key, alg,
6868 input_data->x, input_data->len,
6869 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006870
Gilles Peskine449bd832023-01-11 14:50:10 +01006871 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006872 /* Flip a bit in the input and verify that the signature is now
6873 * detected as invalid. Flip a bit at the beginning, not at the end,
6874 * because ECDSA may ignore the last few bits of the input. */
6875 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006876 TEST_EQUAL(psa_verify_hash(key, alg,
6877 input_data->x, input_data->len,
6878 signature, signature_length),
6879 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006880 }
6881
6882exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006883 /*
6884 * Key attributes may have been returned by psa_get_key_attributes()
6885 * thus reset them as required.
6886 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006887 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006888
Gilles Peskine449bd832023-01-11 14:50:10 +01006889 psa_destroy_key(key);
6890 mbedtls_free(signature);
6891 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006892}
6893/* END_CASE */
6894
Paul Elliott712d5122022-12-07 14:03:10 +00006895/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006896/**
6897 * sign_verify_hash_interruptible() test intentions:
6898 *
6899 * Note: This test can currently only handle ECDSA.
6900 *
Paul Elliott8c092052023-03-06 17:49:14 +00006901 * 1. Test that we can sign an input hash with the given keypair and then
6902 * afterwards verify that signature. This is currently the only way to test
6903 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00006904 *
6905 * 2. Test that after corrupting the hash, the verification detects an invalid
6906 * signature.
6907 *
6908 * 3. Test the number of calls to psa_sign_hash_complete() required are as
6909 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00006910 *
6911 * 4. Test that the number of ops done prior to starting signing and after abort
6912 * is zero and that each successful signing stage completes some ops (this is
6913 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00006914 */
Paul Elliott712d5122022-12-07 14:03:10 +00006915void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006916 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006917 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006918{
6919 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6920 psa_key_type_t key_type = key_type_arg;
6921 psa_algorithm_t alg = alg_arg;
6922 size_t key_bits;
6923 unsigned char *signature = NULL;
6924 size_t signature_size;
6925 size_t signature_length = 0xdeadbeef;
6926 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6927 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006928 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00006929 uint32_t num_ops = 0;
6930 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006931 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006932 size_t min_completes = 0;
6933 size_t max_completes = 0;
6934
Paul Elliott712d5122022-12-07 14:03:10 +00006935 psa_sign_hash_interruptible_operation_t sign_operation =
6936 psa_sign_hash_interruptible_operation_init();
6937 psa_verify_hash_interruptible_operation_t verify_operation =
6938 psa_verify_hash_interruptible_operation_init();
6939
6940 PSA_ASSERT(psa_crypto_init());
6941
Paul Elliott0c683352022-12-16 19:16:56 +00006942 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6943 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006944 psa_set_key_algorithm(&attributes, alg);
6945 psa_set_key_type(&attributes, key_type);
6946
6947 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6948 &key));
6949 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6950 key_bits = psa_get_key_bits(&attributes);
6951
6952 /* Allocate a buffer which has the size advertised by the
6953 * library. */
6954 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6955 key_bits, alg);
6956 TEST_ASSERT(signature_size != 0);
6957 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
6958 ASSERT_ALLOC(signature, signature_size);
6959
Paul Elliott0c683352022-12-16 19:16:56 +00006960 psa_interruptible_set_max_ops(max_ops);
6961
Paul Elliott6f600372023-02-06 18:41:05 +00006962 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6963 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006964
Paul Elliott7c173082023-02-26 18:44:45 +00006965 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6966 TEST_ASSERT(num_ops_prior == 0);
6967
Paul Elliott712d5122022-12-07 14:03:10 +00006968 /* Start performing the signature. */
6969 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6970 input_data->x, input_data->len));
6971
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 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006976 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006977
Paul Elliott0c683352022-12-16 19:16:56 +00006978 status = psa_sign_hash_complete(&sign_operation, signature,
6979 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00006980 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00006981
6982 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00006983
6984 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6985 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
6986 /* We are asserting here that every complete makes progress
6987 * (completes some ops), which is true of the internal
6988 * implementation and probably any implementation, however this is
6989 * not mandated by the PSA specification. */
6990 TEST_ASSERT(num_ops > num_ops_prior);
6991
6992 num_ops_prior = num_ops;
6993 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006994 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006995
6996 TEST_ASSERT(status == PSA_SUCCESS);
6997
Paul Elliott0c683352022-12-16 19:16:56 +00006998 TEST_LE_U(min_completes, num_completes);
6999 TEST_LE_U(num_completes, max_completes);
7000
Paul Elliott712d5122022-12-07 14:03:10 +00007001 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7002
Paul Elliott7c173082023-02-26 18:44:45 +00007003 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7004 TEST_ASSERT(num_ops == 0);
7005
Paul Elliott712d5122022-12-07 14:03:10 +00007006 /* Check that the signature length looks sensible. */
7007 TEST_LE_U(signature_length, signature_size);
7008 TEST_ASSERT(signature_length > 0);
7009
Paul Elliott0c683352022-12-16 19:16:56 +00007010 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007011
7012 /* Start verification. */
7013 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7014 input_data->x, input_data->len,
7015 signature, signature_length));
7016
7017 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007018 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007019 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007020
7021 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007022 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007023
7024 TEST_ASSERT(status == PSA_SUCCESS);
7025
Paul Elliott0c683352022-12-16 19:16:56 +00007026 TEST_LE_U(min_completes, num_completes);
7027 TEST_LE_U(num_completes, max_completes);
7028
Paul Elliott712d5122022-12-07 14:03:10 +00007029 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7030
7031 verify_operation = psa_verify_hash_interruptible_operation_init();
7032
7033 if (input_data->len != 0) {
7034 /* Flip a bit in the input and verify that the signature is now
7035 * detected as invalid. Flip a bit at the beginning, not at the end,
7036 * because ECDSA may ignore the last few bits of the input. */
7037 input_data->x[0] ^= 1;
7038
Paul Elliott712d5122022-12-07 14:03:10 +00007039 /* Start verification. */
7040 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7041 input_data->x, input_data->len,
7042 signature, signature_length));
7043
7044 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007045 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007046 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007047 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007048
7049 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7050 }
7051
7052 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7053
7054exit:
7055 /*
7056 * Key attributes may have been returned by psa_get_key_attributes()
7057 * thus reset them as required.
7058 */
7059 psa_reset_key_attributes(&attributes);
7060
7061 psa_destroy_key(key);
7062 mbedtls_free(signature);
7063 PSA_DONE();
7064}
7065/* END_CASE */
7066
Gilles Peskine9911b022018-06-29 17:30:48 +02007067/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007068void verify_hash(int key_type_arg, data_t *key_data,
7069 int alg_arg, data_t *hash_data,
7070 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007071{
Ronald Cron5425a212020-08-04 14:58:35 +02007072 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007073 psa_key_type_t key_type = key_type_arg;
7074 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007075 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007076
Gilles Peskine449bd832023-01-11 14:50:10 +01007077 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007078
Gilles Peskine449bd832023-01-11 14:50:10 +01007079 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007080
Gilles Peskine449bd832023-01-11 14:50:10 +01007081 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7082 psa_set_key_algorithm(&attributes, alg);
7083 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007084
Gilles Peskine449bd832023-01-11 14:50:10 +01007085 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7086 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007087
Gilles Peskine449bd832023-01-11 14:50:10 +01007088 PSA_ASSERT(psa_verify_hash(key, alg,
7089 hash_data->x, hash_data->len,
7090 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007091
itayzafrir5c753392018-05-08 11:18:38 +03007092exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007093 psa_reset_key_attributes(&attributes);
7094 psa_destroy_key(key);
7095 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007096}
7097/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007098
Paul Elliott712d5122022-12-07 14:03:10 +00007099/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007100/**
7101 * verify_hash_interruptible() test intentions:
7102 *
7103 * Note: This test can currently only handle ECDSA.
7104 *
7105 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007106 * only). Given this test only does verification it can accept public keys as
7107 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007108 *
7109 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7110 * expected for different max_ops values.
7111 *
7112 * 3. Test that the number of ops done prior to start and after abort is zero
7113 * and that each successful stage completes some ops (this is not mandated by
7114 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007115 *
7116 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7117 * complete() calls does not alter the number of ops returned.
7118 *
7119 * 5. Test that after corrupting the hash, the verification detects an invalid
7120 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007121 */
Paul Elliott712d5122022-12-07 14:03:10 +00007122void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7123 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007124 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007125{
7126 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7127 psa_key_type_t key_type = key_type_arg;
7128 psa_algorithm_t alg = alg_arg;
7129 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7130 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007131 uint32_t num_ops = 0;
7132 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007133 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007134 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007135 size_t min_completes = 0;
7136 size_t max_completes = 0;
7137
Paul Elliott712d5122022-12-07 14:03:10 +00007138 psa_verify_hash_interruptible_operation_t operation =
7139 psa_verify_hash_interruptible_operation_init();
7140
7141 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7142
7143 PSA_ASSERT(psa_crypto_init());
7144
7145 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7146 psa_set_key_algorithm(&attributes, alg);
7147 psa_set_key_type(&attributes, key_type);
7148
7149 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7150 &key));
7151
Paul Elliott0c683352022-12-16 19:16:56 +00007152 psa_interruptible_set_max_ops(max_ops);
7153
Paul Elliott6f600372023-02-06 18:41:05 +00007154 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7155 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007156
Paul Elliott712d5122022-12-07 14:03:10 +00007157 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7158
7159 TEST_ASSERT(num_ops_prior == 0);
7160
7161 /* Start verification. */
7162 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7163 hash_data->x, hash_data->len,
7164 signature_data->x, signature_data->len)
7165 );
7166
7167 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7168
7169 TEST_ASSERT(num_ops_prior == 0);
7170
7171 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007172 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007173 status = psa_verify_hash_complete(&operation);
7174
Paul Elliott0c683352022-12-16 19:16:56 +00007175 num_completes++;
7176
Paul Elliott712d5122022-12-07 14:03:10 +00007177 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7178 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007179 /* We are asserting here that every complete makes progress
7180 * (completes some ops), which is true of the internal
7181 * implementation and probably any implementation, however this is
7182 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007183 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007184
Paul Elliott712d5122022-12-07 14:03:10 +00007185 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007186
7187 /* Ensure calling get_num_ops() twice still returns the same
7188 * number of ops as previously reported. */
7189 num_ops = psa_verify_hash_get_num_ops(&operation);
7190
7191 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007192 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007193 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007194
7195 TEST_ASSERT(status == PSA_SUCCESS);
7196
Paul Elliott0c683352022-12-16 19:16:56 +00007197 TEST_LE_U(min_completes, num_completes);
7198 TEST_LE_U(num_completes, max_completes);
7199
Paul Elliott712d5122022-12-07 14:03:10 +00007200 PSA_ASSERT(psa_verify_hash_abort(&operation));
7201
Paul Elliott59ad9452022-12-18 15:09:02 +00007202 num_ops = psa_verify_hash_get_num_ops(&operation);
7203 TEST_ASSERT(num_ops == 0);
7204
Paul Elliott8359c142023-02-24 18:40:10 +00007205 if (hash_data->len != 0) {
7206 /* Flip a bit in the hash and verify that the signature is now detected
7207 * as invalid. Flip a bit at the beginning, not at the end, because
7208 * ECDSA may ignore the last few bits of the input. */
7209 hash_data->x[0] ^= 1;
7210
7211 /* Start verification. */
7212 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7213 hash_data->x, hash_data->len,
7214 signature_data->x, signature_data->len));
7215
7216 /* Continue performing the signature until complete. */
7217 do {
7218 status = psa_verify_hash_complete(&operation);
7219 } while (status == PSA_OPERATION_INCOMPLETE);
7220
7221 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7222 }
7223
Paul Elliott712d5122022-12-07 14:03:10 +00007224exit:
7225 psa_reset_key_attributes(&attributes);
7226 psa_destroy_key(key);
7227 PSA_DONE();
7228}
7229/* END_CASE */
7230
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007231/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007232void verify_hash_fail(int key_type_arg, data_t *key_data,
7233 int alg_arg, data_t *hash_data,
7234 data_t *signature_data,
7235 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007236{
Ronald Cron5425a212020-08-04 14:58:35 +02007237 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007238 psa_key_type_t key_type = key_type_arg;
7239 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007240 psa_status_t actual_status;
7241 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007242 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007243
Gilles Peskine449bd832023-01-11 14:50:10 +01007244 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007245
Gilles Peskine449bd832023-01-11 14:50:10 +01007246 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7247 psa_set_key_algorithm(&attributes, alg);
7248 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007249
Gilles Peskine449bd832023-01-11 14:50:10 +01007250 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7251 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007252
Gilles Peskine449bd832023-01-11 14:50:10 +01007253 actual_status = psa_verify_hash(key, alg,
7254 hash_data->x, hash_data->len,
7255 signature_data->x, signature_data->len);
7256 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007257
7258exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007259 psa_reset_key_attributes(&attributes);
7260 psa_destroy_key(key);
7261 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007262}
7263/* END_CASE */
7264
Paul Elliott91007972022-12-16 12:21:24 +00007265/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007266/**
7267 * verify_hash_fail_interruptible() test intentions:
7268 *
7269 * Note: This test can currently only handle ECDSA.
7270 *
7271 * 1. Test that various failure cases for interruptible verify hash fail with
7272 * the correct error codes, and at the correct point (at start or during
7273 * complete).
7274 *
7275 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7276 * expected for different max_ops values.
7277 *
7278 * 3. Test that the number of ops done prior to start and after abort is zero
7279 * and that each successful stage completes some ops (this is not mandated by
7280 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007281 *
7282 * 4. Check that calling complete() when start() fails and complete()
7283 * after completion results in a BAD_STATE error.
7284 *
7285 * 5. Check that calling start() again after start fails results in a BAD_STATE
7286 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007287 */
Paul Elliott91007972022-12-16 12:21:24 +00007288void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7289 int alg_arg, data_t *hash_data,
7290 data_t *signature_data,
7291 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007292 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007293 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007294{
7295 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7296 psa_key_type_t key_type = key_type_arg;
7297 psa_algorithm_t alg = alg_arg;
7298 psa_status_t actual_status;
7299 psa_status_t expected_start_status = expected_start_status_arg;
7300 psa_status_t expected_complete_status = expected_complete_status_arg;
7301 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007302 uint32_t num_ops = 0;
7303 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007304 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007305 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007306 size_t min_completes = 0;
7307 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007308 psa_verify_hash_interruptible_operation_t operation =
7309 psa_verify_hash_interruptible_operation_init();
7310
7311 PSA_ASSERT(psa_crypto_init());
7312
7313 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7314 psa_set_key_algorithm(&attributes, alg);
7315 psa_set_key_type(&attributes, key_type);
7316
7317 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7318 &key));
7319
Paul Elliott0c683352022-12-16 19:16:56 +00007320 psa_interruptible_set_max_ops(max_ops);
7321
Paul Elliott6f600372023-02-06 18:41:05 +00007322 interruptible_signverify_get_minmax_completes(max_ops,
7323 expected_complete_status,
7324 &min_completes,
7325 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007326
Paul Elliott91007972022-12-16 12:21:24 +00007327 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7328 TEST_ASSERT(num_ops_prior == 0);
7329
7330 /* Start verification. */
7331 actual_status = psa_verify_hash_start(&operation, key, alg,
7332 hash_data->x, hash_data->len,
7333 signature_data->x,
7334 signature_data->len);
7335
7336 TEST_EQUAL(actual_status, expected_start_status);
7337
Paul Elliottc9774412023-02-06 15:14:07 +00007338 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007339 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007340 * start failed. */
7341 actual_status = psa_verify_hash_complete(&operation);
7342
7343 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7344
7345 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007346 actual_status = psa_verify_hash_start(&operation, key, alg,
7347 hash_data->x, hash_data->len,
7348 signature_data->x,
7349 signature_data->len);
7350
7351 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7352 }
7353
Paul Elliott91007972022-12-16 12:21:24 +00007354 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7355 TEST_ASSERT(num_ops_prior == 0);
7356
Paul Elliott91007972022-12-16 12:21:24 +00007357 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007358 do {
Paul Elliott91007972022-12-16 12:21:24 +00007359 actual_status = psa_verify_hash_complete(&operation);
7360
Paul Elliott0c683352022-12-16 19:16:56 +00007361 num_completes++;
7362
Paul Elliott334d7262023-01-20 17:29:41 +00007363 if (actual_status == PSA_SUCCESS ||
7364 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007365 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007366 /* We are asserting here that every complete makes progress
7367 * (completes some ops), which is true of the internal
7368 * implementation and probably any implementation, however this is
7369 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007370 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007371
Paul Elliott91007972022-12-16 12:21:24 +00007372 num_ops_prior = num_ops;
7373 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007374 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007375
Paul Elliottc9774412023-02-06 15:14:07 +00007376 TEST_EQUAL(actual_status, expected_complete_status);
7377
Paul Elliottefebad02023-02-15 16:56:45 +00007378 /* Check that another complete returns BAD_STATE. */
7379 actual_status = psa_verify_hash_complete(&operation);
7380 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007381
Paul Elliott0c683352022-12-16 19:16:56 +00007382 TEST_LE_U(min_completes, num_completes);
7383 TEST_LE_U(num_completes, max_completes);
7384
Paul Elliott91007972022-12-16 12:21:24 +00007385 PSA_ASSERT(psa_verify_hash_abort(&operation));
7386
Paul Elliott59ad9452022-12-18 15:09:02 +00007387 num_ops = psa_verify_hash_get_num_ops(&operation);
7388 TEST_ASSERT(num_ops == 0);
7389
Paul Elliott91007972022-12-16 12:21:24 +00007390exit:
7391 psa_reset_key_attributes(&attributes);
7392 psa_destroy_key(key);
7393 PSA_DONE();
7394}
7395/* END_CASE */
7396
Paul Elliott20a36062022-12-18 13:21:25 +00007397/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007398/**
7399 * interruptible_signverify_hash_state_test() test intentions:
7400 *
7401 * Note: This test can currently only handle ECDSA.
7402 *
7403 * 1. Test that calling the various interruptible sign and verify hash functions
7404 * in incorrect orders returns BAD_STATE errors.
7405 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007406void interruptible_signverify_hash_state_test(int key_type_arg,
7407 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007408{
7409 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7410 psa_key_type_t key_type = key_type_arg;
7411 psa_algorithm_t alg = alg_arg;
7412 size_t key_bits;
7413 unsigned char *signature = NULL;
7414 size_t signature_size;
7415 size_t signature_length = 0xdeadbeef;
7416 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7417 psa_sign_hash_interruptible_operation_t sign_operation =
7418 psa_sign_hash_interruptible_operation_init();
7419 psa_verify_hash_interruptible_operation_t verify_operation =
7420 psa_verify_hash_interruptible_operation_init();
7421
7422 PSA_ASSERT(psa_crypto_init());
7423
7424 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7425 PSA_KEY_USAGE_VERIFY_HASH);
7426 psa_set_key_algorithm(&attributes, alg);
7427 psa_set_key_type(&attributes, key_type);
7428
7429 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7430 &key));
7431 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7432 key_bits = psa_get_key_bits(&attributes);
7433
7434 /* Allocate a buffer which has the size advertised by the
7435 * library. */
7436 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7437 key_bits, alg);
7438 TEST_ASSERT(signature_size != 0);
7439 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7440 ASSERT_ALLOC(signature, signature_size);
7441
7442 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7443
7444 /* --- Attempt completes prior to starts --- */
7445 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7446 signature_size,
7447 &signature_length),
7448 PSA_ERROR_BAD_STATE);
7449
7450 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7451
7452 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7453 PSA_ERROR_BAD_STATE);
7454
7455 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7456
7457 /* --- Aborts in all other places. --- */
7458 psa_sign_hash_abort(&sign_operation);
7459
7460 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7461 input_data->x, input_data->len));
7462
7463 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7464
7465 psa_interruptible_set_max_ops(1);
7466
7467 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7468 input_data->x, input_data->len));
7469
7470 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7471 signature_size,
7472 &signature_length),
7473 PSA_OPERATION_INCOMPLETE);
7474
7475 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7476
7477 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7478
7479 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7480 input_data->x, input_data->len));
7481
7482 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7483 signature_size,
7484 &signature_length));
7485
7486 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7487
7488 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7489
7490 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7491 input_data->x, input_data->len,
7492 signature, signature_length));
7493
7494 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7495
7496 psa_interruptible_set_max_ops(1);
7497
7498 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7499 input_data->x, input_data->len,
7500 signature, signature_length));
7501
7502 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7503 PSA_OPERATION_INCOMPLETE);
7504
7505 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7506
7507 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7508
7509 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7510 input_data->x, input_data->len,
7511 signature, signature_length));
7512
7513 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7514
7515 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7516
7517 /* --- Attempt double starts. --- */
7518
7519 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7520 input_data->x, input_data->len));
7521
7522 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7523 input_data->x, input_data->len),
7524 PSA_ERROR_BAD_STATE);
7525
7526 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7527
7528 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7529 input_data->x, input_data->len,
7530 signature, signature_length));
7531
7532 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7533 input_data->x, input_data->len,
7534 signature, signature_length),
7535 PSA_ERROR_BAD_STATE);
7536
7537 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7538
Paul Elliott76d671a2023-02-07 17:45:18 +00007539exit:
7540 /*
7541 * Key attributes may have been returned by psa_get_key_attributes()
7542 * thus reset them as required.
7543 */
7544 psa_reset_key_attributes(&attributes);
7545
7546 psa_destroy_key(key);
7547 mbedtls_free(signature);
7548 PSA_DONE();
7549}
7550/* END_CASE */
7551
7552/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007553/**
Paul Elliottc2033502023-02-26 17:09:14 +00007554 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007555 *
7556 * Note: This test can currently only handle ECDSA.
7557 *
7558 * 1. Test various edge cases in the interruptible sign and verify hash
7559 * interfaces.
7560 */
Paul Elliottc2033502023-02-26 17:09:14 +00007561void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007562 data_t *key_data, int alg_arg, data_t *input_data)
7563{
7564 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7565 psa_key_type_t key_type = key_type_arg;
7566 psa_algorithm_t alg = alg_arg;
7567 size_t key_bits;
7568 unsigned char *signature = NULL;
7569 size_t signature_size;
7570 size_t signature_length = 0xdeadbeef;
7571 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7572 uint8_t *input_buffer = NULL;
7573 psa_sign_hash_interruptible_operation_t sign_operation =
7574 psa_sign_hash_interruptible_operation_init();
7575 psa_verify_hash_interruptible_operation_t verify_operation =
7576 psa_verify_hash_interruptible_operation_init();
7577
7578 PSA_ASSERT(psa_crypto_init());
7579
7580 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7581 PSA_KEY_USAGE_VERIFY_HASH);
7582 psa_set_key_algorithm(&attributes, alg);
7583 psa_set_key_type(&attributes, key_type);
7584
7585 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7586 &key));
7587 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7588 key_bits = psa_get_key_bits(&attributes);
7589
7590 /* Allocate a buffer which has the size advertised by the
7591 * library. */
7592 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7593 key_bits, alg);
7594 TEST_ASSERT(signature_size != 0);
7595 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7596 ASSERT_ALLOC(signature, signature_size);
7597
Paul Elliott20a36062022-12-18 13:21:25 +00007598 /* --- Change function inputs mid run, to cause an error (sign only,
7599 * verify passes all inputs to start. --- */
7600
7601 psa_interruptible_set_max_ops(1);
7602
7603 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7604 input_data->x, input_data->len));
7605
7606 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7607 signature_size,
7608 &signature_length),
7609 PSA_OPERATION_INCOMPLETE);
7610
7611 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7612 0,
7613 &signature_length),
7614 PSA_ERROR_BUFFER_TOO_SMALL);
7615
Paul Elliottc9774412023-02-06 15:14:07 +00007616 /* And test that this invalidates the operation. */
7617 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7618 0,
7619 &signature_length),
7620 PSA_ERROR_BAD_STATE);
7621
Paul Elliott20a36062022-12-18 13:21:25 +00007622 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7623
Paul Elliottf9c91a72023-02-05 18:06:38 +00007624 /* Trash the hash buffer in between start and complete, to ensure
7625 * no reliance on external buffers. */
7626 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7627
7628 input_buffer = mbedtls_calloc(1, input_data->len);
7629 TEST_ASSERT(input_buffer != NULL);
7630
7631 memcpy(input_buffer, input_data->x, input_data->len);
7632
7633 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7634 input_buffer, input_data->len));
7635
7636 memset(input_buffer, '!', input_data->len);
7637 mbedtls_free(input_buffer);
7638 input_buffer = NULL;
7639
7640 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7641 signature_size,
7642 &signature_length));
7643
7644 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7645
7646 input_buffer = mbedtls_calloc(1, input_data->len);
7647 TEST_ASSERT(input_buffer != NULL);
7648
7649 memcpy(input_buffer, input_data->x, input_data->len);
7650
7651 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7652 input_buffer, input_data->len,
7653 signature, signature_length));
7654
7655 memset(input_buffer, '!', input_data->len);
7656 mbedtls_free(input_buffer);
7657 input_buffer = NULL;
7658
7659 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7660
7661 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7662
Paul Elliott20a36062022-12-18 13:21:25 +00007663exit:
7664 /*
7665 * Key attributes may have been returned by psa_get_key_attributes()
7666 * thus reset them as required.
7667 */
7668 psa_reset_key_attributes(&attributes);
7669
7670 psa_destroy_key(key);
7671 mbedtls_free(signature);
7672 PSA_DONE();
7673}
7674/* END_CASE */
7675
Paul Elliotta4cb9092023-02-07 18:01:55 +00007676/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007677/**
Paul Elliott57702242023-02-26 20:36:10 +00007678 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007679 *
7680 * Note: This test can currently only handle ECDSA.
7681 *
7682 * 1. Test that setting max ops is reflected in both interruptible sign and
7683 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007684 * 2. Test that changing the value of max_ops to unlimited during an operation
7685 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007686 *
7687 * 3. Test that calling get_num_ops() between complete calls gives the same
7688 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007689 */
Paul Elliott57702242023-02-26 20:36:10 +00007690void interruptible_signverify_hash_ops_tests(int key_type_arg,
7691 data_t *key_data, int alg_arg,
7692 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007693{
7694 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7695 psa_key_type_t key_type = key_type_arg;
7696 psa_algorithm_t alg = alg_arg;
7697 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007698 size_t key_bits;
7699 unsigned char *signature = NULL;
7700 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007701 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007702 uint32_t num_ops = 0;
7703 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7704
Paul Elliotta4cb9092023-02-07 18:01:55 +00007705 psa_sign_hash_interruptible_operation_t sign_operation =
7706 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007707 psa_verify_hash_interruptible_operation_t verify_operation =
7708 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007709
7710 PSA_ASSERT(psa_crypto_init());
7711
7712 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7713 PSA_KEY_USAGE_VERIFY_HASH);
7714 psa_set_key_algorithm(&attributes, alg);
7715 psa_set_key_type(&attributes, key_type);
7716
Paul Elliottf1743e22023-02-15 18:44:16 +00007717 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7718 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7719 key_bits = psa_get_key_bits(&attributes);
7720
7721 /* Allocate a buffer which has the size advertised by the
7722 * library. */
7723 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7724
7725 TEST_ASSERT(signature_size != 0);
7726 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7727 ASSERT_ALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007728
7729 /* Check that default max ops gets set if we don't set it. */
7730 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7731 input_data->x, input_data->len));
7732
7733 TEST_EQUAL(psa_interruptible_get_max_ops(),
7734 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7735
7736 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7737
Paul Elliottf1743e22023-02-15 18:44:16 +00007738 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7739 input_data->x, input_data->len,
7740 signature, signature_size));
7741
7742 TEST_EQUAL(psa_interruptible_get_max_ops(),
7743 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7744
7745 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7746
Paul Elliotta4cb9092023-02-07 18:01:55 +00007747 /* Check that max ops gets set properly. */
7748
7749 psa_interruptible_set_max_ops(0xbeef);
7750
Paul Elliottf1743e22023-02-15 18:44:16 +00007751 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007752
Paul Elliott9e8819f2023-02-26 19:01:35 +00007753 /* --- Ensure changing the max ops mid operation works (operation should
7754 * complete successfully after setting max ops to unlimited --- */
7755 psa_interruptible_set_max_ops(1);
7756
7757 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7758 input_data->x, input_data->len));
7759
7760 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7761 signature_size,
7762 &signature_length),
7763 PSA_OPERATION_INCOMPLETE);
7764
7765 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7766
7767 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7768 signature_size,
7769 &signature_length));
7770
7771 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7772
7773 psa_interruptible_set_max_ops(1);
7774
7775 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7776 input_data->x, input_data->len,
7777 signature, signature_length));
7778
7779 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7780 PSA_OPERATION_INCOMPLETE);
7781
7782 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7783
7784 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7785
7786 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7787
Paul Elliottc1e04002023-02-26 20:27:23 +00007788 /* --- Test that not calling get_num_ops inbetween complete calls does not
7789 * result in lost ops. ---*/
7790
7791 psa_interruptible_set_max_ops(1);
7792
7793 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7794 input_data->x, input_data->len));
7795
7796 /* Continue performing the signature until complete. */
7797 do {
7798 status = psa_sign_hash_complete(&sign_operation, signature,
7799 signature_size,
7800 &signature_length);
7801
7802 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7803
7804 } while (status == PSA_OPERATION_INCOMPLETE);
7805
7806 PSA_ASSERT(status);
7807
7808 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7809
7810 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7811 input_data->x, input_data->len));
7812
7813 /* Continue performing the signature until complete. */
7814 do {
7815 status = psa_sign_hash_complete(&sign_operation, signature,
7816 signature_size,
7817 &signature_length);
7818 } while (status == PSA_OPERATION_INCOMPLETE);
7819
7820 PSA_ASSERT(status);
7821
7822 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7823
7824 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7825
7826 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7827 input_data->x, input_data->len,
7828 signature, signature_length));
7829
7830 /* Continue performing the verification until complete. */
7831 do {
7832 status = psa_verify_hash_complete(&verify_operation);
7833
7834 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7835
7836 } while (status == PSA_OPERATION_INCOMPLETE);
7837
7838 PSA_ASSERT(status);
7839
7840 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7841
7842 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7843 input_data->x, input_data->len,
7844 signature, signature_length));
7845
7846 /* Continue performing the verification until complete. */
7847 do {
7848 status = psa_verify_hash_complete(&verify_operation);
7849
7850 } while (status == PSA_OPERATION_INCOMPLETE);
7851
7852 PSA_ASSERT(status);
7853
7854 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7855
7856 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7857
Paul Elliotta4cb9092023-02-07 18:01:55 +00007858exit:
7859 /*
7860 * Key attributes may have been returned by psa_get_key_attributes()
7861 * thus reset them as required.
7862 */
7863 psa_reset_key_attributes(&attributes);
7864
7865 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007866 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007867 PSA_DONE();
7868}
7869/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007870
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007871/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007872void sign_message_deterministic(int key_type_arg,
7873 data_t *key_data,
7874 int alg_arg,
7875 data_t *input_data,
7876 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007877{
7878 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7879 psa_key_type_t key_type = key_type_arg;
7880 psa_algorithm_t alg = alg_arg;
7881 size_t key_bits;
7882 unsigned char *signature = NULL;
7883 size_t signature_size;
7884 size_t signature_length = 0xdeadbeef;
7885 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7886
Gilles Peskine449bd832023-01-11 14:50:10 +01007887 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007888
Gilles Peskine449bd832023-01-11 14:50:10 +01007889 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7890 psa_set_key_algorithm(&attributes, alg);
7891 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007892
Gilles Peskine449bd832023-01-11 14:50:10 +01007893 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7894 &key));
7895 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7896 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007897
Gilles Peskine449bd832023-01-11 14:50:10 +01007898 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7899 TEST_ASSERT(signature_size != 0);
7900 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7901 ASSERT_ALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007902
Gilles Peskine449bd832023-01-11 14:50:10 +01007903 PSA_ASSERT(psa_sign_message(key, alg,
7904 input_data->x, input_data->len,
7905 signature, signature_size,
7906 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007907
Gilles Peskine449bd832023-01-11 14:50:10 +01007908 ASSERT_COMPARE(output_data->x, output_data->len,
7909 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007910
7911exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007912 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007913
Gilles Peskine449bd832023-01-11 14:50:10 +01007914 psa_destroy_key(key);
7915 mbedtls_free(signature);
7916 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007917
7918}
7919/* END_CASE */
7920
7921/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007922void sign_message_fail(int key_type_arg,
7923 data_t *key_data,
7924 int alg_arg,
7925 data_t *input_data,
7926 int signature_size_arg,
7927 int expected_status_arg)
7928{
7929 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7930 psa_key_type_t key_type = key_type_arg;
7931 psa_algorithm_t alg = alg_arg;
7932 size_t signature_size = signature_size_arg;
7933 psa_status_t actual_status;
7934 psa_status_t expected_status = expected_status_arg;
7935 unsigned char *signature = NULL;
7936 size_t signature_length = 0xdeadbeef;
7937 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7938
7939 ASSERT_ALLOC(signature, signature_size);
7940
7941 PSA_ASSERT(psa_crypto_init());
7942
7943 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7944 psa_set_key_algorithm(&attributes, alg);
7945 psa_set_key_type(&attributes, key_type);
7946
7947 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7948 &key));
7949
7950 actual_status = psa_sign_message(key, alg,
7951 input_data->x, input_data->len,
7952 signature, signature_size,
7953 &signature_length);
7954 TEST_EQUAL(actual_status, expected_status);
7955 /* The value of *signature_length is unspecified on error, but
7956 * whatever it is, it should be less than signature_size, so that
7957 * if the caller tries to read *signature_length bytes without
7958 * checking the error code then they don't overflow a buffer. */
7959 TEST_LE_U(signature_length, signature_size);
7960
7961exit:
7962 psa_reset_key_attributes(&attributes);
7963 psa_destroy_key(key);
7964 mbedtls_free(signature);
7965 PSA_DONE();
7966}
7967/* END_CASE */
7968
7969/* BEGIN_CASE */
7970void sign_verify_message(int key_type_arg,
7971 data_t *key_data,
7972 int alg_arg,
7973 data_t *input_data)
7974{
7975 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7976 psa_key_type_t key_type = key_type_arg;
7977 psa_algorithm_t alg = alg_arg;
7978 size_t key_bits;
7979 unsigned char *signature = NULL;
7980 size_t signature_size;
7981 size_t signature_length = 0xdeadbeef;
7982 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7983
7984 PSA_ASSERT(psa_crypto_init());
7985
7986 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
7987 PSA_KEY_USAGE_VERIFY_MESSAGE);
7988 psa_set_key_algorithm(&attributes, alg);
7989 psa_set_key_type(&attributes, key_type);
7990
7991 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7992 &key));
7993 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7994 key_bits = psa_get_key_bits(&attributes);
7995
7996 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7997 TEST_ASSERT(signature_size != 0);
7998 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
7999 ASSERT_ALLOC(signature, signature_size);
8000
8001 PSA_ASSERT(psa_sign_message(key, alg,
8002 input_data->x, input_data->len,
8003 signature, signature_size,
8004 &signature_length));
8005 TEST_LE_U(signature_length, signature_size);
8006 TEST_ASSERT(signature_length > 0);
8007
8008 PSA_ASSERT(psa_verify_message(key, alg,
8009 input_data->x, input_data->len,
8010 signature, signature_length));
8011
8012 if (input_data->len != 0) {
8013 /* Flip a bit in the input and verify that the signature is now
8014 * detected as invalid. Flip a bit at the beginning, not at the end,
8015 * because ECDSA may ignore the last few bits of the input. */
8016 input_data->x[0] ^= 1;
8017 TEST_EQUAL(psa_verify_message(key, alg,
8018 input_data->x, input_data->len,
8019 signature, signature_length),
8020 PSA_ERROR_INVALID_SIGNATURE);
8021 }
8022
8023exit:
8024 psa_reset_key_attributes(&attributes);
8025
8026 psa_destroy_key(key);
8027 mbedtls_free(signature);
8028 PSA_DONE();
8029}
8030/* END_CASE */
8031
8032/* BEGIN_CASE */
8033void verify_message(int key_type_arg,
8034 data_t *key_data,
8035 int alg_arg,
8036 data_t *input_data,
8037 data_t *signature_data)
8038{
8039 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8040 psa_key_type_t key_type = key_type_arg;
8041 psa_algorithm_t alg = alg_arg;
8042 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8043
8044 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8045
8046 PSA_ASSERT(psa_crypto_init());
8047
8048 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8049 psa_set_key_algorithm(&attributes, alg);
8050 psa_set_key_type(&attributes, key_type);
8051
8052 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8053 &key));
8054
8055 PSA_ASSERT(psa_verify_message(key, alg,
8056 input_data->x, input_data->len,
8057 signature_data->x, signature_data->len));
8058
8059exit:
8060 psa_reset_key_attributes(&attributes);
8061 psa_destroy_key(key);
8062 PSA_DONE();
8063}
8064/* END_CASE */
8065
8066/* BEGIN_CASE */
8067void verify_message_fail(int key_type_arg,
8068 data_t *key_data,
8069 int alg_arg,
8070 data_t *hash_data,
8071 data_t *signature_data,
8072 int expected_status_arg)
8073{
8074 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8075 psa_key_type_t key_type = key_type_arg;
8076 psa_algorithm_t alg = alg_arg;
8077 psa_status_t actual_status;
8078 psa_status_t expected_status = expected_status_arg;
8079 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8080
8081 PSA_ASSERT(psa_crypto_init());
8082
8083 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8084 psa_set_key_algorithm(&attributes, alg);
8085 psa_set_key_type(&attributes, key_type);
8086
8087 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8088 &key));
8089
8090 actual_status = psa_verify_message(key, alg,
8091 hash_data->x, hash_data->len,
8092 signature_data->x,
8093 signature_data->len);
8094 TEST_EQUAL(actual_status, expected_status);
8095
8096exit:
8097 psa_reset_key_attributes(&attributes);
8098 psa_destroy_key(key);
8099 PSA_DONE();
8100}
8101/* END_CASE */
8102
8103/* BEGIN_CASE */
8104void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008105 data_t *key_data,
8106 int alg_arg,
8107 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008108 data_t *label,
8109 int expected_output_length_arg,
8110 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008111{
Ronald Cron5425a212020-08-04 14:58:35 +02008112 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008113 psa_key_type_t key_type = key_type_arg;
8114 psa_algorithm_t alg = alg_arg;
8115 size_t expected_output_length = expected_output_length_arg;
8116 size_t key_bits;
8117 unsigned char *output = NULL;
8118 size_t output_size;
8119 size_t output_length = ~0;
8120 psa_status_t actual_status;
8121 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008122 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008123
Gilles Peskine449bd832023-01-11 14:50:10 +01008124 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008125
Gilles Peskine656896e2018-06-29 19:12:28 +02008126 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008127 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8128 psa_set_key_algorithm(&attributes, alg);
8129 psa_set_key_type(&attributes, key_type);
8130 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8131 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008132
8133 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008134 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8135 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008136
Gilles Peskine449bd832023-01-11 14:50:10 +01008137 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8138 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
8139 ASSERT_ALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008140
8141 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008142 actual_status = psa_asymmetric_encrypt(key, alg,
8143 input_data->x, input_data->len,
8144 label->x, label->len,
8145 output, output_size,
8146 &output_length);
8147 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008148 if (actual_status == PSA_SUCCESS) {
8149 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008150 } else {
8151 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008152 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008153
Gilles Peskine68428122018-06-30 18:42:41 +02008154 /* If the label is empty, the test framework puts a non-null pointer
8155 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008156 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008157 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008158 if (output_size != 0) {
8159 memset(output, 0, output_size);
8160 }
8161 actual_status = psa_asymmetric_encrypt(key, alg,
8162 input_data->x, input_data->len,
8163 NULL, label->len,
8164 output, output_size,
8165 &output_length);
8166 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008167 if (actual_status == PSA_SUCCESS) {
8168 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008169 } else {
8170 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008171 }
Gilles Peskine68428122018-06-30 18:42:41 +02008172 }
8173
Gilles Peskine656896e2018-06-29 19:12:28 +02008174exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008175 /*
8176 * Key attributes may have been returned by psa_get_key_attributes()
8177 * thus reset them as required.
8178 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008179 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008180
Gilles Peskine449bd832023-01-11 14:50:10 +01008181 psa_destroy_key(key);
8182 mbedtls_free(output);
8183 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008184}
8185/* END_CASE */
8186
8187/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008188void asymmetric_encrypt_decrypt(int key_type_arg,
8189 data_t *key_data,
8190 int alg_arg,
8191 data_t *input_data,
8192 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008193{
Ronald Cron5425a212020-08-04 14:58:35 +02008194 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008195 psa_key_type_t key_type = key_type_arg;
8196 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008197 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008198 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008199 size_t output_size;
8200 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008201 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008202 size_t output2_size;
8203 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008204 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008205
Gilles Peskine449bd832023-01-11 14:50:10 +01008206 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008207
Gilles Peskine449bd832023-01-11 14:50:10 +01008208 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8209 psa_set_key_algorithm(&attributes, alg);
8210 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008211
Gilles Peskine449bd832023-01-11 14:50:10 +01008212 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8213 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008214
8215 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008216 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8217 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008218
Gilles Peskine449bd832023-01-11 14:50:10 +01008219 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8220 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
8221 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008222
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008223 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008224 TEST_LE_U(output2_size,
8225 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8226 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
8227 ASSERT_ALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008228
Gilles Peskineeebd7382018-06-08 18:11:54 +02008229 /* We test encryption by checking that encrypt-then-decrypt gives back
8230 * the original plaintext because of the non-optional random
8231 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008232 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8233 input_data->x, input_data->len,
8234 label->x, label->len,
8235 output, output_size,
8236 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008237 /* We don't know what ciphertext length to expect, but check that
8238 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008239 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008240
Gilles Peskine449bd832023-01-11 14:50:10 +01008241 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8242 output, output_length,
8243 label->x, label->len,
8244 output2, output2_size,
8245 &output2_length));
8246 ASSERT_COMPARE(input_data->x, input_data->len,
8247 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008248
8249exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008250 /*
8251 * Key attributes may have been returned by psa_get_key_attributes()
8252 * thus reset them as required.
8253 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008254 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008255
Gilles Peskine449bd832023-01-11 14:50:10 +01008256 psa_destroy_key(key);
8257 mbedtls_free(output);
8258 mbedtls_free(output2);
8259 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008260}
8261/* END_CASE */
8262
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008263/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008264void asymmetric_decrypt(int key_type_arg,
8265 data_t *key_data,
8266 int alg_arg,
8267 data_t *input_data,
8268 data_t *label,
8269 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008270{
Ronald Cron5425a212020-08-04 14:58:35 +02008271 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008272 psa_key_type_t key_type = key_type_arg;
8273 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008274 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008275 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008276 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008277 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008278 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008279
Gilles Peskine449bd832023-01-11 14:50:10 +01008280 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008281
Gilles Peskine449bd832023-01-11 14:50:10 +01008282 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8283 psa_set_key_algorithm(&attributes, alg);
8284 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008285
Gilles Peskine449bd832023-01-11 14:50:10 +01008286 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8287 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008288
Gilles Peskine449bd832023-01-11 14:50:10 +01008289 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8290 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008291
8292 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008293 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8294 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
8295 ASSERT_ALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008296
Gilles Peskine449bd832023-01-11 14:50:10 +01008297 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8298 input_data->x, input_data->len,
8299 label->x, label->len,
8300 output,
8301 output_size,
8302 &output_length));
8303 ASSERT_COMPARE(expected_data->x, expected_data->len,
8304 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008305
Gilles Peskine68428122018-06-30 18:42:41 +02008306 /* If the label is empty, the test framework puts a non-null pointer
8307 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008308 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008309 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008310 if (output_size != 0) {
8311 memset(output, 0, output_size);
8312 }
8313 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8314 input_data->x, input_data->len,
8315 NULL, label->len,
8316 output,
8317 output_size,
8318 &output_length));
8319 ASSERT_COMPARE(expected_data->x, expected_data->len,
8320 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008321 }
8322
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008323exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008324 psa_reset_key_attributes(&attributes);
8325 psa_destroy_key(key);
8326 mbedtls_free(output);
8327 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008328}
8329/* END_CASE */
8330
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008331/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008332void asymmetric_decrypt_fail(int key_type_arg,
8333 data_t *key_data,
8334 int alg_arg,
8335 data_t *input_data,
8336 data_t *label,
8337 int output_size_arg,
8338 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008339{
Ronald Cron5425a212020-08-04 14:58:35 +02008340 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008341 psa_key_type_t key_type = key_type_arg;
8342 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008343 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008344 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008345 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008346 psa_status_t actual_status;
8347 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008348 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008349
Gilles Peskine449bd832023-01-11 14:50:10 +01008350 ASSERT_ALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008351
Gilles Peskine449bd832023-01-11 14:50:10 +01008352 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008353
Gilles Peskine449bd832023-01-11 14:50:10 +01008354 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8355 psa_set_key_algorithm(&attributes, alg);
8356 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008357
Gilles Peskine449bd832023-01-11 14:50:10 +01008358 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8359 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008360
Gilles Peskine449bd832023-01-11 14:50:10 +01008361 actual_status = psa_asymmetric_decrypt(key, alg,
8362 input_data->x, input_data->len,
8363 label->x, label->len,
8364 output, output_size,
8365 &output_length);
8366 TEST_EQUAL(actual_status, expected_status);
8367 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008368
Gilles Peskine68428122018-06-30 18:42:41 +02008369 /* If the label is empty, the test framework puts a non-null pointer
8370 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008371 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008372 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008373 if (output_size != 0) {
8374 memset(output, 0, output_size);
8375 }
8376 actual_status = psa_asymmetric_decrypt(key, alg,
8377 input_data->x, input_data->len,
8378 NULL, label->len,
8379 output, output_size,
8380 &output_length);
8381 TEST_EQUAL(actual_status, expected_status);
8382 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008383 }
8384
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008385exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008386 psa_reset_key_attributes(&attributes);
8387 psa_destroy_key(key);
8388 mbedtls_free(output);
8389 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008390}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008391/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008392
8393/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008394void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008395{
8396 /* Test each valid way of initializing the object, except for `= {0}`, as
8397 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8398 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008399 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008400 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008401 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008402 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8403 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008404
Gilles Peskine449bd832023-01-11 14:50:10 +01008405 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008406
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008407 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008408 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8409 PSA_ERROR_BAD_STATE);
8410 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8411 PSA_ERROR_BAD_STATE);
8412 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8413 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008414
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008415 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008416 PSA_ASSERT(psa_key_derivation_abort(&func));
8417 PSA_ASSERT(psa_key_derivation_abort(&init));
8418 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008419}
8420/* END_CASE */
8421
Janos Follath16de4a42019-06-13 16:32:24 +01008422/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008423void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008424{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008425 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008426 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008427 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008428
Gilles Peskine449bd832023-01-11 14:50:10 +01008429 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008430
Gilles Peskine449bd832023-01-11 14:50:10 +01008431 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8432 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008433
8434exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008435 psa_key_derivation_abort(&operation);
8436 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008437}
8438/* END_CASE */
8439
Janos Follathaf3c2a02019-06-12 12:34:34 +01008440/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008441void derive_set_capacity(int alg_arg, int capacity_arg,
8442 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008443{
8444 psa_algorithm_t alg = alg_arg;
8445 size_t capacity = capacity_arg;
8446 psa_status_t expected_status = expected_status_arg;
8447 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8448
Gilles Peskine449bd832023-01-11 14:50:10 +01008449 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008450
Gilles Peskine449bd832023-01-11 14:50:10 +01008451 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008452
Gilles Peskine449bd832023-01-11 14:50:10 +01008453 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8454 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008455
8456exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008457 psa_key_derivation_abort(&operation);
8458 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008459}
8460/* END_CASE */
8461
8462/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008463void derive_input(int alg_arg,
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308464 int step_arg1, int key_type_arg1, data_t *input1,
8465 int expected_status_arg1,
8466 int step_arg2, int key_type_arg2, data_t *input2,
8467 int expected_status_arg2,
8468 int step_arg3, int key_type_arg3, data_t *input3,
8469 int expected_status_arg3,
Gilles Peskine449bd832023-01-11 14:50:10 +01008470 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008471{
8472 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008473 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308474 uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
Gilles Peskine449bd832023-01-11 14:50:10 +01008475 psa_status_t expected_statuses[] = { expected_status_arg1,
8476 expected_status_arg2,
8477 expected_status_arg3 };
8478 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008479 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8480 MBEDTLS_SVC_KEY_ID_INIT,
8481 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008482 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8483 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8484 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008485 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008486 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008487 psa_status_t expected_output_status = expected_output_status_arg;
8488 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008489
Gilles Peskine449bd832023-01-11 14:50:10 +01008490 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008491
Gilles Peskine449bd832023-01-11 14:50:10 +01008492 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8493 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008494
Gilles Peskine449bd832023-01-11 14:50:10 +01008495 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008496
Gilles Peskine449bd832023-01-11 14:50:10 +01008497 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8498 mbedtls_test_set_step(i);
8499 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008500 /* Skip this step */
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308501 } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE &&
8502 key_types[i] != INPUT_INTEGER) {
8503 psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i]));
Gilles Peskine449bd832023-01-11 14:50:10 +01008504 PSA_ASSERT(psa_import_key(&attributes,
8505 inputs[i]->x, inputs[i]->len,
8506 &keys[i]));
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308507 if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008508 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008509 // When taking a private key as secret input, use key agreement
8510 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008511 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8512 &operation, keys[i]),
8513 expected_statuses[i]);
8514 } else {
8515 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8516 keys[i]),
8517 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008518 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008519 } else {
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308520 if (key_types[i] == INPUT_INTEGER) {
8521 TEST_EQUAL(psa_key_derivation_input_integer(
8522 &operation, steps[i],
8523 parse_binary_string(inputs[i])),
8524 expected_statuses[i]);
8525 } else {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308526 TEST_EQUAL(psa_key_derivation_input_bytes(
8527 &operation, steps[i],
8528 inputs[i]->x, inputs[i]->len),
8529 expected_statuses[i]);
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308530 }
Janos Follathaf3c2a02019-06-12 12:34:34 +01008531 }
8532 }
8533
Gilles Peskine449bd832023-01-11 14:50:10 +01008534 if (output_key_type != PSA_KEY_TYPE_NONE) {
8535 psa_reset_key_attributes(&attributes);
8536 psa_set_key_type(&attributes, output_key_type);
8537 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008538 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008539 psa_key_derivation_output_key(&attributes, &operation,
8540 &output_key);
8541 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008542 uint8_t buffer[1];
8543 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008544 psa_key_derivation_output_bytes(&operation,
8545 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008546 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008547 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008548
Janos Follathaf3c2a02019-06-12 12:34:34 +01008549exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008550 psa_key_derivation_abort(&operation);
8551 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8552 psa_destroy_key(keys[i]);
8553 }
8554 psa_destroy_key(output_key);
8555 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008556}
8557/* END_CASE */
8558
Janos Follathd958bb72019-07-03 15:02:16 +01008559/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008560void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008561{
Janos Follathd958bb72019-07-03 15:02:16 +01008562 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008563 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008564 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008565 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008566 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008567 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008568 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008569 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008570 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008571 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008572 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8573 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008574 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008575 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008576
Gilles Peskine449bd832023-01-11 14:50:10 +01008577 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008578
Gilles Peskine449bd832023-01-11 14:50:10 +01008579 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8580 psa_set_key_algorithm(&attributes, alg);
8581 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008582
Gilles Peskine449bd832023-01-11 14:50:10 +01008583 PSA_ASSERT(psa_import_key(&attributes,
8584 key_data, sizeof(key_data),
8585 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008586
8587 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008588 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8589 input1, input1_length,
8590 input2, input2_length,
8591 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008592 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008593 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008594
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008595 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008596 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8597 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008598
Gilles Peskine449bd832023-01-11 14:50:10 +01008599 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008600
Gilles Peskine449bd832023-01-11 14:50:10 +01008601 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8602 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008603
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008604exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008605 psa_key_derivation_abort(&operation);
8606 psa_destroy_key(key);
8607 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008608}
8609/* END_CASE */
8610
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008611/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008612void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008613{
8614 uint8_t output_buffer[16];
8615 size_t buffer_size = 16;
8616 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008617 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008618
Gilles Peskine449bd832023-01-11 14:50:10 +01008619 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8620 output_buffer, buffer_size)
8621 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008622
Gilles Peskine449bd832023-01-11 14:50:10 +01008623 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8624 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008625
Gilles Peskine449bd832023-01-11 14:50:10 +01008626 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008627
Gilles Peskine449bd832023-01-11 14:50:10 +01008628 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8629 output_buffer, buffer_size)
8630 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008631
Gilles Peskine449bd832023-01-11 14:50:10 +01008632 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8633 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008634
8635exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008636 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008637}
8638/* END_CASE */
8639
8640/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008641void derive_output(int alg_arg,
8642 int step1_arg, data_t *input1, int expected_status_arg1,
8643 int step2_arg, data_t *input2, int expected_status_arg2,
8644 int step3_arg, data_t *input3, int expected_status_arg3,
8645 int step4_arg, data_t *input4, int expected_status_arg4,
8646 data_t *key_agreement_peer_key,
8647 int requested_capacity_arg,
8648 data_t *expected_output1,
8649 data_t *expected_output2,
8650 int other_key_input_type,
8651 int key_input_type,
8652 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008653{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008654 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008655 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8656 data_t *inputs[] = { input1, input2, input3, input4 };
8657 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8658 MBEDTLS_SVC_KEY_ID_INIT,
8659 MBEDTLS_SVC_KEY_ID_INIT,
8660 MBEDTLS_SVC_KEY_ID_INIT };
8661 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8662 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008663 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008664 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008665 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008666 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008667 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008668 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008669 size_t output_buffer_size = 0;
8670 uint8_t *output_buffer = NULL;
8671 size_t expected_capacity;
8672 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008673 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8674 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8675 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8676 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008677 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008678 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008679 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008680
Gilles Peskine449bd832023-01-11 14:50:10 +01008681 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8682 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008683 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008684 }
8685 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008686 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008687 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008688 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008689 ASSERT_ALLOC(output_buffer, output_buffer_size);
8690 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008691
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008692 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008693 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8694 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8695 requested_capacity));
8696 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8697 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008698 case 0:
8699 break;
8700 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008701 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008702 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008703 TEST_EQUAL(psa_key_derivation_input_bytes(
8704 &operation, steps[i],
8705 inputs[i]->x, inputs[i]->len),
8706 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008707
Gilles Peskine449bd832023-01-11 14:50:10 +01008708 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008709 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008710 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008711 break;
8712 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008713 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8714 psa_set_key_algorithm(&attributes1, alg);
8715 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008716
Gilles Peskine449bd832023-01-11 14:50:10 +01008717 PSA_ASSERT(psa_import_key(&attributes1,
8718 inputs[i]->x, inputs[i]->len,
8719 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008720
Gilles Peskine449bd832023-01-11 14:50:10 +01008721 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8722 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8723 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8724 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008725 }
8726
Gilles Peskine449bd832023-01-11 14:50:10 +01008727 PSA_ASSERT(psa_key_derivation_input_key(&operation,
8728 steps[i],
8729 keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008730 break;
8731 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008732 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008733 break;
8734 }
8735 break;
8736 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008737 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008738 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008739 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8740 steps[i],
8741 inputs[i]->x,
8742 inputs[i]->len),
8743 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008744 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008745 case 1: // input key, type DERIVE
8746 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008747 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8748 psa_set_key_algorithm(&attributes2, alg);
8749 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008750
8751 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008752 if (other_key_input_type == 11) {
8753 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8754 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008755
Gilles Peskine449bd832023-01-11 14:50:10 +01008756 PSA_ASSERT(psa_import_key(&attributes2,
8757 inputs[i]->x, inputs[i]->len,
8758 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008759
Gilles Peskine449bd832023-01-11 14:50:10 +01008760 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8761 steps[i],
8762 keys[i]),
8763 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008764 break;
8765 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008766 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8767 psa_set_key_algorithm(&attributes3, alg);
8768 psa_set_key_type(&attributes3,
8769 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008770
Gilles Peskine449bd832023-01-11 14:50:10 +01008771 PSA_ASSERT(psa_import_key(&attributes3,
8772 inputs[i]->x, inputs[i]->len,
8773 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008774
Gilles Peskine449bd832023-01-11 14:50:10 +01008775 TEST_EQUAL(psa_key_derivation_key_agreement(
8776 &operation,
8777 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8778 keys[i], key_agreement_peer_key->x,
8779 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008780 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008781 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008782 TEST_ASSERT(!"default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008783 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008784 }
8785
Gilles Peskine449bd832023-01-11 14:50:10 +01008786 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008787 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008788 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008789 break;
8790 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008791 TEST_EQUAL(psa_key_derivation_input_bytes(
8792 &operation, steps[i],
8793 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008794
Gilles Peskine449bd832023-01-11 14:50:10 +01008795 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008796 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008797 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008798 break;
8799 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008800 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008801
Gilles Peskine449bd832023-01-11 14:50:10 +01008802 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8803 &current_capacity));
8804 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008805 expected_capacity = requested_capacity;
8806
Gilles Peskine449bd832023-01-11 14:50:10 +01008807 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008808 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8809
8810 /* For output key derivation secret must be provided using
8811 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008812 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008813 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008814 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008815
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8817 psa_set_key_algorithm(&attributes4, alg);
8818 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8819 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008820
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8822 &derived_key), expected_status);
8823 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008824 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008825 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008826 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008827 status = psa_key_derivation_output_bytes(&operation,
8828 output_buffer, output_sizes[i]);
8829 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008830 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008831 TEST_ASSERT(status == PSA_SUCCESS ||
8832 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008833 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008834 } else if (expected_capacity == 0 ||
8835 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008836 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008837 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008838 expected_capacity = 0;
8839 continue;
8840 }
8841 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008842 PSA_ASSERT(status);
8843 if (output_sizes[i] != 0) {
8844 ASSERT_COMPARE(output_buffer, output_sizes[i],
8845 expected_outputs[i], output_sizes[i]);
8846 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008847 /* Check the operation status. */
8848 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008849 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8850 &current_capacity));
8851 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008852 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008853 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008854 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008855
8856exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008857 mbedtls_free(output_buffer);
8858 psa_key_derivation_abort(&operation);
8859 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8860 psa_destroy_key(keys[i]);
8861 }
8862 psa_destroy_key(derived_key);
8863 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008864}
8865/* END_CASE */
8866
8867/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008868void derive_full(int alg_arg,
8869 data_t *key_data,
8870 data_t *input1,
8871 data_t *input2,
8872 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008873{
Ronald Cron5425a212020-08-04 14:58:35 +02008874 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008875 psa_algorithm_t alg = alg_arg;
8876 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008877 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008878 unsigned char output_buffer[16];
8879 size_t expected_capacity = requested_capacity;
8880 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008881 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008882
Gilles Peskine449bd832023-01-11 14:50:10 +01008883 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008884
Gilles Peskine449bd832023-01-11 14:50:10 +01008885 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8886 psa_set_key_algorithm(&attributes, alg);
8887 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008888
Gilles Peskine449bd832023-01-11 14:50:10 +01008889 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8890 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008891
Gilles Peskine449bd832023-01-11 14:50:10 +01008892 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8893 input1->x, input1->len,
8894 input2->x, input2->len,
8895 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008896 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008897 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008898
Gilles Peskine449bd832023-01-11 14:50:10 +01008899 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8900 &current_capacity));
8901 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008902
8903 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008904 while (current_capacity > 0) {
8905 size_t read_size = sizeof(output_buffer);
8906 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008907 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008908 }
8909 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8910 output_buffer,
8911 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008912 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008913 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8914 &current_capacity));
8915 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008916 }
8917
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008918 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008919 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8920 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008921
Gilles Peskine449bd832023-01-11 14:50:10 +01008922 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008923
8924exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008925 psa_key_derivation_abort(&operation);
8926 psa_destroy_key(key);
8927 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008928}
8929/* END_CASE */
8930
Przemek Stekiel8258ea72022-10-19 12:17:19 +02008931/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008932void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8933 int derivation_step,
8934 int capacity, int expected_capacity_status_arg,
8935 data_t *expected_output,
8936 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008937{
8938 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
8939 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04008940 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008941 uint8_t *output_buffer = NULL;
8942 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04008943 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
8944 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
8945 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008946
Gilles Peskine449bd832023-01-11 14:50:10 +01008947 ASSERT_ALLOC(output_buffer, expected_output->len);
8948 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008949
Gilles Peskine449bd832023-01-11 14:50:10 +01008950 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8951 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8952 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008953
Gilles Peskine449bd832023-01-11 14:50:10 +01008954 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8955 step, input->x, input->len),
8956 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008957
Gilles Peskine449bd832023-01-11 14:50:10 +01008958 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008959 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008960 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008961
Gilles Peskine449bd832023-01-11 14:50:10 +01008962 status = psa_key_derivation_output_bytes(&operation, output_buffer,
8963 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008964
Gilles Peskine449bd832023-01-11 14:50:10 +01008965 TEST_EQUAL(status, expected_output_status);
8966 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
8967 ASSERT_COMPARE(output_buffer, expected_output->len, expected_output->x,
8968 expected_output->len);
8969 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008970
8971exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008972 mbedtls_free(output_buffer);
8973 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008974 PSA_DONE();
8975}
8976/* END_CASE */
8977
Janos Follathe60c9052019-07-03 13:51:30 +01008978/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008979void derive_key_exercise(int alg_arg,
8980 data_t *key_data,
8981 data_t *input1,
8982 data_t *input2,
8983 int derived_type_arg,
8984 int derived_bits_arg,
8985 int derived_usage_arg,
8986 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02008987{
Ronald Cron5425a212020-08-04 14:58:35 +02008988 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8989 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008990 psa_algorithm_t alg = alg_arg;
8991 psa_key_type_t derived_type = derived_type_arg;
8992 size_t derived_bits = derived_bits_arg;
8993 psa_key_usage_t derived_usage = derived_usage_arg;
8994 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008995 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008996 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008997 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008998 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008999
Gilles Peskine449bd832023-01-11 14:50:10 +01009000 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009001
Gilles Peskine449bd832023-01-11 14:50:10 +01009002 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9003 psa_set_key_algorithm(&attributes, alg);
9004 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9005 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9006 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009007
9008 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009009 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9010 input1->x, input1->len,
9011 input2->x, input2->len,
9012 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009013 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009014 }
Janos Follathe60c9052019-07-03 13:51:30 +01009015
Gilles Peskine449bd832023-01-11 14:50:10 +01009016 psa_set_key_usage_flags(&attributes, derived_usage);
9017 psa_set_key_algorithm(&attributes, derived_alg);
9018 psa_set_key_type(&attributes, derived_type);
9019 psa_set_key_bits(&attributes, derived_bits);
9020 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9021 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009022
9023 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009024 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9025 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9026 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009027
9028 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009029 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009030 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009031 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009032
9033exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009034 /*
9035 * Key attributes may have been returned by psa_get_key_attributes()
9036 * thus reset them as required.
9037 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009038 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009039
Gilles Peskine449bd832023-01-11 14:50:10 +01009040 psa_key_derivation_abort(&operation);
9041 psa_destroy_key(base_key);
9042 psa_destroy_key(derived_key);
9043 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009044}
9045/* END_CASE */
9046
Janos Follath42fd8882019-07-03 14:17:09 +01009047/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009048void derive_key_export(int alg_arg,
9049 data_t *key_data,
9050 data_t *input1,
9051 data_t *input2,
9052 int bytes1_arg,
9053 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009054{
Ronald Cron5425a212020-08-04 14:58:35 +02009055 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9056 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009057 psa_algorithm_t alg = alg_arg;
9058 size_t bytes1 = bytes1_arg;
9059 size_t bytes2 = bytes2_arg;
9060 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009061 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009062 uint8_t *output_buffer = NULL;
9063 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009064 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9065 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009066 size_t length;
9067
Gilles Peskine449bd832023-01-11 14:50:10 +01009068 ASSERT_ALLOC(output_buffer, capacity);
9069 ASSERT_ALLOC(export_buffer, capacity);
9070 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009071
Gilles Peskine449bd832023-01-11 14:50:10 +01009072 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9073 psa_set_key_algorithm(&base_attributes, alg);
9074 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9075 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9076 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009077
9078 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009079 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9080 input1->x, input1->len,
9081 input2->x, input2->len,
9082 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009083 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009084 }
Janos Follath42fd8882019-07-03 14:17:09 +01009085
Gilles Peskine449bd832023-01-11 14:50:10 +01009086 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9087 output_buffer,
9088 capacity));
9089 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009090
9091 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009092 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9093 input1->x, input1->len,
9094 input2->x, input2->len,
9095 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009096 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009097 }
Janos Follath42fd8882019-07-03 14:17:09 +01009098
Gilles Peskine449bd832023-01-11 14:50:10 +01009099 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9100 psa_set_key_algorithm(&derived_attributes, 0);
9101 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9102 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9103 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9104 &derived_key));
9105 PSA_ASSERT(psa_export_key(derived_key,
9106 export_buffer, bytes1,
9107 &length));
9108 TEST_EQUAL(length, bytes1);
9109 PSA_ASSERT(psa_destroy_key(derived_key));
9110 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9111 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9112 &derived_key));
9113 PSA_ASSERT(psa_export_key(derived_key,
9114 export_buffer + bytes1, bytes2,
9115 &length));
9116 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009117
9118 /* Compare the outputs from the two runs. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009119 ASSERT_COMPARE(output_buffer, bytes1 + bytes2,
9120 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009121
9122exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009123 mbedtls_free(output_buffer);
9124 mbedtls_free(export_buffer);
9125 psa_key_derivation_abort(&operation);
9126 psa_destroy_key(base_key);
9127 psa_destroy_key(derived_key);
9128 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009129}
9130/* END_CASE */
9131
9132/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009133void derive_key_type(int alg_arg,
9134 data_t *key_data,
9135 data_t *input1,
9136 data_t *input2,
9137 int key_type_arg, int bits_arg,
9138 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009139{
9140 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9141 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9142 const psa_algorithm_t alg = alg_arg;
9143 const psa_key_type_t key_type = key_type_arg;
9144 const size_t bits = bits_arg;
9145 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9146 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009147 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009148 uint8_t *export_buffer = NULL;
9149 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9150 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9151 size_t export_length;
9152
Gilles Peskine449bd832023-01-11 14:50:10 +01009153 ASSERT_ALLOC(export_buffer, export_buffer_size);
9154 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009155
Gilles Peskine449bd832023-01-11 14:50:10 +01009156 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9157 psa_set_key_algorithm(&base_attributes, alg);
9158 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9159 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9160 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009161
Gilles Peskine449bd832023-01-11 14:50:10 +01009162 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009163 &operation, base_key, alg,
9164 input1->x, input1->len,
9165 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009166 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009167 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009168 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009169
Gilles Peskine449bd832023-01-11 14:50:10 +01009170 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9171 psa_set_key_algorithm(&derived_attributes, 0);
9172 psa_set_key_type(&derived_attributes, key_type);
9173 psa_set_key_bits(&derived_attributes, bits);
9174 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9175 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009176
Gilles Peskine449bd832023-01-11 14:50:10 +01009177 PSA_ASSERT(psa_export_key(derived_key,
9178 export_buffer, export_buffer_size,
9179 &export_length));
9180 ASSERT_COMPARE(export_buffer, export_length,
9181 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009182
9183exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009184 mbedtls_free(export_buffer);
9185 psa_key_derivation_abort(&operation);
9186 psa_destroy_key(base_key);
9187 psa_destroy_key(derived_key);
9188 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009189}
9190/* END_CASE */
9191
9192/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009193void derive_key(int alg_arg,
9194 data_t *key_data, data_t *input1, data_t *input2,
9195 int type_arg, int bits_arg,
9196 int expected_status_arg,
9197 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009198{
Ronald Cron5425a212020-08-04 14:58:35 +02009199 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9200 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009201 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009202 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009203 size_t bits = bits_arg;
9204 psa_status_t expected_status = expected_status_arg;
9205 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9206 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9207 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9208
Gilles Peskine449bd832023-01-11 14:50:10 +01009209 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009210
Gilles Peskine449bd832023-01-11 14:50:10 +01009211 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9212 psa_set_key_algorithm(&base_attributes, alg);
9213 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9214 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9215 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009216
Gilles Peskine449bd832023-01-11 14:50:10 +01009217 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9218 input1->x, input1->len,
9219 input2->x, input2->len,
9220 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009221 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009222 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009223
Gilles Peskine449bd832023-01-11 14:50:10 +01009224 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9225 psa_set_key_algorithm(&derived_attributes, 0);
9226 psa_set_key_type(&derived_attributes, type);
9227 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009228
9229 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009230 psa_key_derivation_output_key(&derived_attributes,
9231 &operation,
9232 &derived_key);
9233 if (is_large_output > 0) {
9234 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9235 }
9236 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009237
9238exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009239 psa_key_derivation_abort(&operation);
9240 psa_destroy_key(base_key);
9241 psa_destroy_key(derived_key);
9242 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009243}
9244/* END_CASE */
9245
9246/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009247void key_agreement_setup(int alg_arg,
9248 int our_key_type_arg, int our_key_alg_arg,
9249 data_t *our_key_data, data_t *peer_key_data,
9250 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009251{
Ronald Cron5425a212020-08-04 14:58:35 +02009252 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009253 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009254 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009255 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009256 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009257 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009258 psa_status_t expected_status = expected_status_arg;
9259 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009260
Gilles Peskine449bd832023-01-11 14:50:10 +01009261 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009262
Gilles Peskine449bd832023-01-11 14:50:10 +01009263 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9264 psa_set_key_algorithm(&attributes, our_key_alg);
9265 psa_set_key_type(&attributes, our_key_type);
9266 PSA_ASSERT(psa_import_key(&attributes,
9267 our_key_data->x, our_key_data->len,
9268 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009269
Gilles Peskine77f40d82019-04-11 21:27:06 +02009270 /* The tests currently include inputs that should fail at either step.
9271 * Test cases that fail at the setup step should be changed to call
9272 * key_derivation_setup instead, and this function should be renamed
9273 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009274 status = psa_key_derivation_setup(&operation, alg);
9275 if (status == PSA_SUCCESS) {
9276 TEST_EQUAL(psa_key_derivation_key_agreement(
9277 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9278 our_key,
9279 peer_key_data->x, peer_key_data->len),
9280 expected_status);
9281 } else {
9282 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009283 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009284
9285exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009286 psa_key_derivation_abort(&operation);
9287 psa_destroy_key(our_key);
9288 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009289}
9290/* END_CASE */
9291
9292/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009293void raw_key_agreement(int alg_arg,
9294 int our_key_type_arg, data_t *our_key_data,
9295 data_t *peer_key_data,
9296 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009297{
Ronald Cron5425a212020-08-04 14:58:35 +02009298 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009299 psa_algorithm_t alg = alg_arg;
9300 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009301 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009302 unsigned char *output = NULL;
9303 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009304 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009305
Gilles Peskine449bd832023-01-11 14:50:10 +01009306 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009307
Gilles Peskine449bd832023-01-11 14:50:10 +01009308 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9309 psa_set_key_algorithm(&attributes, alg);
9310 psa_set_key_type(&attributes, our_key_type);
9311 PSA_ASSERT(psa_import_key(&attributes,
9312 our_key_data->x, our_key_data->len,
9313 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009314
Gilles Peskine449bd832023-01-11 14:50:10 +01009315 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9316 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009317
Gilles Peskine992bee82022-04-13 23:25:52 +02009318 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009319 TEST_LE_U(expected_output->len,
9320 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9321 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9322 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009323
9324 /* Good case with exact output size */
Gilles Peskine449bd832023-01-11 14:50:10 +01009325 ASSERT_ALLOC(output, expected_output->len);
9326 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9327 peer_key_data->x, peer_key_data->len,
9328 output, expected_output->len,
9329 &output_length));
9330 ASSERT_COMPARE(output, output_length,
9331 expected_output->x, expected_output->len);
9332 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009333 output = NULL;
9334 output_length = ~0;
9335
9336 /* Larger buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009337 ASSERT_ALLOC(output, expected_output->len + 1);
9338 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9339 peer_key_data->x, peer_key_data->len,
9340 output, expected_output->len + 1,
9341 &output_length));
9342 ASSERT_COMPARE(output, output_length,
9343 expected_output->x, expected_output->len);
9344 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009345 output = NULL;
9346 output_length = ~0;
9347
9348 /* Buffer too small */
Gilles Peskine449bd832023-01-11 14:50:10 +01009349 ASSERT_ALLOC(output, expected_output->len - 1);
9350 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9351 peer_key_data->x, peer_key_data->len,
9352 output, expected_output->len - 1,
9353 &output_length),
9354 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009355 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009356 TEST_LE_U(output_length, expected_output->len - 1);
9357 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009358 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009359
9360exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009361 mbedtls_free(output);
9362 psa_destroy_key(our_key);
9363 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009364}
9365/* END_CASE */
9366
9367/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009368void key_agreement_capacity(int alg_arg,
9369 int our_key_type_arg, data_t *our_key_data,
9370 data_t *peer_key_data,
9371 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009372{
Ronald Cron5425a212020-08-04 14:58:35 +02009373 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009374 psa_algorithm_t alg = alg_arg;
9375 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009376 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009377 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009378 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009379 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009380
Gilles Peskine449bd832023-01-11 14:50:10 +01009381 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009382
Gilles Peskine449bd832023-01-11 14:50:10 +01009383 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9384 psa_set_key_algorithm(&attributes, alg);
9385 psa_set_key_type(&attributes, our_key_type);
9386 PSA_ASSERT(psa_import_key(&attributes,
9387 our_key_data->x, our_key_data->len,
9388 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009389
Gilles Peskine449bd832023-01-11 14:50:10 +01009390 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9391 PSA_ASSERT(psa_key_derivation_key_agreement(
9392 &operation,
9393 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9394 peer_key_data->x, peer_key_data->len));
9395 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009396 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009397 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9398 PSA_KEY_DERIVATION_INPUT_INFO,
9399 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009400 }
Gilles Peskine59685592018-09-18 12:11:34 +02009401
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009402 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009403 PSA_ASSERT(psa_key_derivation_get_capacity(
9404 &operation, &actual_capacity));
9405 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009406
Gilles Peskinebf491972018-10-25 22:36:12 +02009407 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009408 while (actual_capacity > sizeof(output)) {
9409 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9410 output, sizeof(output)));
9411 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009412 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009413 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9414 output, actual_capacity));
9415 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9416 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009417
Gilles Peskine59685592018-09-18 12:11:34 +02009418exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009419 psa_key_derivation_abort(&operation);
9420 psa_destroy_key(our_key);
9421 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009422}
9423/* END_CASE */
9424
9425/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009426void key_agreement_output(int alg_arg,
9427 int our_key_type_arg, data_t *our_key_data,
9428 data_t *peer_key_data,
9429 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009430{
Ronald Cron5425a212020-08-04 14:58:35 +02009431 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009432 psa_algorithm_t alg = alg_arg;
9433 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009434 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009435 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009436 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009437
Gilles Peskine449bd832023-01-11 14:50:10 +01009438 ASSERT_ALLOC(actual_output, MAX(expected_output1->len,
9439 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009440
Gilles Peskine449bd832023-01-11 14:50:10 +01009441 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009442
Gilles Peskine449bd832023-01-11 14:50:10 +01009443 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9444 psa_set_key_algorithm(&attributes, alg);
9445 psa_set_key_type(&attributes, our_key_type);
9446 PSA_ASSERT(psa_import_key(&attributes,
9447 our_key_data->x, our_key_data->len,
9448 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009449
Gilles Peskine449bd832023-01-11 14:50:10 +01009450 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9451 PSA_ASSERT(psa_key_derivation_key_agreement(
9452 &operation,
9453 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9454 peer_key_data->x, peer_key_data->len));
9455 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009456 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009457 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9458 PSA_KEY_DERIVATION_INPUT_INFO,
9459 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009460 }
Gilles Peskine59685592018-09-18 12:11:34 +02009461
Gilles Peskine449bd832023-01-11 14:50:10 +01009462 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9463 actual_output,
9464 expected_output1->len));
9465 ASSERT_COMPARE(actual_output, expected_output1->len,
9466 expected_output1->x, expected_output1->len);
9467 if (expected_output2->len != 0) {
9468 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9469 actual_output,
9470 expected_output2->len));
9471 ASSERT_COMPARE(actual_output, expected_output2->len,
9472 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009473 }
Gilles Peskine59685592018-09-18 12:11:34 +02009474
9475exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009476 psa_key_derivation_abort(&operation);
9477 psa_destroy_key(our_key);
9478 PSA_DONE();
9479 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009480}
9481/* END_CASE */
9482
9483/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009484void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009485{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009486 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009487 unsigned char *output = NULL;
9488 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009489 size_t i;
9490 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009491
Gilles Peskine449bd832023-01-11 14:50:10 +01009492 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009493
Gilles Peskine449bd832023-01-11 14:50:10 +01009494 ASSERT_ALLOC(output, bytes);
9495 ASSERT_ALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009496
Gilles Peskine449bd832023-01-11 14:50:10 +01009497 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009498
Gilles Peskinea50d7392018-06-21 10:22:13 +02009499 /* Run several times, to ensure that every output byte will be
9500 * nonzero at least once with overwhelming probability
9501 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009502 for (run = 0; run < 10; run++) {
9503 if (bytes != 0) {
9504 memset(output, 0, bytes);
9505 }
9506 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009507
Gilles Peskine449bd832023-01-11 14:50:10 +01009508 for (i = 0; i < bytes; i++) {
9509 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009510 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009511 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009512 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009513 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009514
9515 /* Check that every byte was changed to nonzero at least once. This
9516 * validates that psa_generate_random is overwriting every byte of
9517 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009518 for (i = 0; i < bytes; i++) {
9519 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009520 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009521
9522exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009523 PSA_DONE();
9524 mbedtls_free(output);
9525 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009526}
9527/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009528
9529/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009530void generate_key(int type_arg,
9531 int bits_arg,
9532 int usage_arg,
9533 int alg_arg,
9534 int expected_status_arg,
9535 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009536{
Ronald Cron5425a212020-08-04 14:58:35 +02009537 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009538 psa_key_type_t type = type_arg;
9539 psa_key_usage_t usage = usage_arg;
9540 size_t bits = bits_arg;
9541 psa_algorithm_t alg = alg_arg;
9542 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009543 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009544 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009545
Gilles Peskine449bd832023-01-11 14:50:10 +01009546 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009547
Gilles Peskine449bd832023-01-11 14:50:10 +01009548 psa_set_key_usage_flags(&attributes, usage);
9549 psa_set_key_algorithm(&attributes, alg);
9550 psa_set_key_type(&attributes, type);
9551 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009552
9553 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009554 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009555
Gilles Peskine449bd832023-01-11 14:50:10 +01009556 if (is_large_key > 0) {
9557 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9558 }
9559 TEST_EQUAL(status, expected_status);
9560 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009561 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009562 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009563
9564 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009565 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9566 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9567 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009568
Gilles Peskine818ca122018-06-20 18:16:48 +02009569 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009570 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009571 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009572 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009573
9574exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009575 /*
9576 * Key attributes may have been returned by psa_get_key_attributes()
9577 * thus reset them as required.
9578 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009579 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009580
Gilles Peskine449bd832023-01-11 14:50:10 +01009581 psa_destroy_key(key);
9582 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009583}
9584/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009585
Ronald Cronee414c72021-03-18 18:50:08 +01009586/* 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 +01009587void generate_key_rsa(int bits_arg,
9588 data_t *e_arg,
9589 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009590{
Ronald Cron5425a212020-08-04 14:58:35 +02009591 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009592 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009593 size_t bits = bits_arg;
9594 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9595 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9596 psa_status_t expected_status = expected_status_arg;
9597 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9598 uint8_t *exported = NULL;
9599 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009600 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009601 size_t exported_length = SIZE_MAX;
9602 uint8_t *e_read_buffer = NULL;
9603 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009604 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009605 size_t e_read_length = SIZE_MAX;
9606
Gilles Peskine449bd832023-01-11 14:50:10 +01009607 if (e_arg->len == 0 ||
9608 (e_arg->len == 3 &&
9609 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009610 is_default_public_exponent = 1;
9611 e_read_size = 0;
9612 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009613 ASSERT_ALLOC(e_read_buffer, e_read_size);
9614 ASSERT_ALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009615
Gilles Peskine449bd832023-01-11 14:50:10 +01009616 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009617
Gilles Peskine449bd832023-01-11 14:50:10 +01009618 psa_set_key_usage_flags(&attributes, usage);
9619 psa_set_key_algorithm(&attributes, alg);
9620 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9621 e_arg->x, e_arg->len));
9622 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009623
9624 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009625 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9626 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009627 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009628 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009629
9630 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009631 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9632 TEST_EQUAL(psa_get_key_type(&attributes), type);
9633 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9634 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
9635 e_read_buffer, e_read_size,
9636 &e_read_length));
9637 if (is_default_public_exponent) {
9638 TEST_EQUAL(e_read_length, 0);
9639 } else {
9640 ASSERT_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
9641 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009642
9643 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009644 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009645 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009646 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009647
9648 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009649 PSA_ASSERT(psa_export_public_key(key,
9650 exported, exported_size,
9651 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009652 {
9653 uint8_t *p = exported;
9654 uint8_t *end = exported + exported_length;
9655 size_t len;
9656 /* RSAPublicKey ::= SEQUENCE {
9657 * modulus INTEGER, -- n
9658 * publicExponent INTEGER } -- e
9659 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009660 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9661 MBEDTLS_ASN1_SEQUENCE |
9662 MBEDTLS_ASN1_CONSTRUCTED));
9663 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9664 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9665 MBEDTLS_ASN1_INTEGER));
9666 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009667 ++p;
9668 --len;
9669 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009670 if (e_arg->len == 0) {
9671 TEST_EQUAL(len, 3);
9672 TEST_EQUAL(p[0], 1);
9673 TEST_EQUAL(p[1], 0);
9674 TEST_EQUAL(p[2], 1);
9675 } else {
9676 ASSERT_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009677 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009678 }
9679
9680exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009681 /*
9682 * Key attributes may have been returned by psa_get_key_attributes() or
9683 * set by psa_set_key_domain_parameters() thus reset them as required.
9684 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009685 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009686
Gilles Peskine449bd832023-01-11 14:50:10 +01009687 psa_destroy_key(key);
9688 PSA_DONE();
9689 mbedtls_free(e_read_buffer);
9690 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009691}
9692/* END_CASE */
9693
Darryl Greend49a4992018-06-18 17:27:26 +01009694/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009695void persistent_key_load_key_from_storage(data_t *data,
9696 int type_arg, int bits_arg,
9697 int usage_flags_arg, int alg_arg,
9698 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009699{
Gilles Peskine449bd832023-01-11 14:50:10 +01009700 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009701 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009702 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9703 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009704 psa_key_type_t type = type_arg;
9705 size_t bits = bits_arg;
9706 psa_key_usage_t usage_flags = usage_flags_arg;
9707 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009708 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009709 unsigned char *first_export = NULL;
9710 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009711 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009712 size_t first_exported_length;
9713 size_t second_exported_length;
9714
Gilles Peskine449bd832023-01-11 14:50:10 +01009715 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9716 ASSERT_ALLOC(first_export, export_size);
9717 ASSERT_ALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009718 }
Darryl Greend49a4992018-06-18 17:27:26 +01009719
Gilles Peskine449bd832023-01-11 14:50:10 +01009720 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009721
Gilles Peskine449bd832023-01-11 14:50:10 +01009722 psa_set_key_id(&attributes, key_id);
9723 psa_set_key_usage_flags(&attributes, usage_flags);
9724 psa_set_key_algorithm(&attributes, alg);
9725 psa_set_key_type(&attributes, type);
9726 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009727
Gilles Peskine449bd832023-01-11 14:50:10 +01009728 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009729 case IMPORT_KEY:
9730 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009731 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9732 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009733 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009734
Darryl Green0c6575a2018-11-07 16:05:30 +00009735 case GENERATE_KEY:
9736 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009737 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009738 break;
9739
9740 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009741#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009742 {
9743 /* Create base key */
9744 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9745 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9746 psa_set_key_usage_flags(&base_attributes,
9747 PSA_KEY_USAGE_DERIVE);
9748 psa_set_key_algorithm(&base_attributes, derive_alg);
9749 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9750 PSA_ASSERT(psa_import_key(&base_attributes,
9751 data->x, data->len,
9752 &base_key));
9753 /* Derive a key. */
9754 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9755 PSA_ASSERT(psa_key_derivation_input_key(
9756 &operation,
9757 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9758 PSA_ASSERT(psa_key_derivation_input_bytes(
9759 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9760 NULL, 0));
9761 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9762 &operation,
9763 &key));
9764 PSA_ASSERT(psa_key_derivation_abort(&operation));
9765 PSA_ASSERT(psa_destroy_key(base_key));
9766 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9767 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009768#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009769 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009770#endif
9771 break;
9772
9773 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009774 TEST_ASSERT(!"generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009775 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009776 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009777 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009778
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009779 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009780 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9781 PSA_ASSERT(psa_export_key(key,
9782 first_export, export_size,
9783 &first_exported_length));
9784 if (generation_method == IMPORT_KEY) {
9785 ASSERT_COMPARE(data->x, data->len,
9786 first_export, first_exported_length);
9787 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009788 }
Darryl Greend49a4992018-06-18 17:27:26 +01009789
9790 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009791 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009792 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009793 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009794
Darryl Greend49a4992018-06-18 17:27:26 +01009795 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009796 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9797 TEST_ASSERT(mbedtls_svc_key_id_equal(
9798 psa_get_key_id(&attributes), key_id));
9799 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9800 PSA_KEY_LIFETIME_PERSISTENT);
9801 TEST_EQUAL(psa_get_key_type(&attributes), type);
9802 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9803 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9804 mbedtls_test_update_key_usage_flags(usage_flags));
9805 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009806
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009807 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009808 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9809 PSA_ASSERT(psa_export_key(key,
9810 second_export, export_size,
9811 &second_exported_length));
9812 ASSERT_COMPARE(first_export, first_exported_length,
9813 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009814 }
9815
9816 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009817 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009818 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009819 }
Darryl Greend49a4992018-06-18 17:27:26 +01009820
9821exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009822 /*
9823 * Key attributes may have been returned by psa_get_key_attributes()
9824 * thus reset them as required.
9825 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009826 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009827
Gilles Peskine449bd832023-01-11 14:50:10 +01009828 mbedtls_free(first_export);
9829 mbedtls_free(second_export);
9830 psa_key_derivation_abort(&operation);
9831 psa_destroy_key(base_key);
9832 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009833 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009834}
9835/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009836
Neil Armstronga557cb82022-06-10 08:58:32 +02009837/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009838void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9839 int primitive_arg, int hash_arg, int role_arg,
9840 int test_input, data_t *pw_data,
9841 int inj_err_type_arg,
9842 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009843{
9844 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9845 psa_pake_operation_t operation = psa_pake_operation_init();
9846 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009847 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009848 psa_key_type_t key_type_pw = key_type_pw_arg;
9849 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009850 psa_algorithm_t hash_alg = hash_arg;
9851 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009852 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9853 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009854 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9855 psa_status_t expected_error = expected_error_arg;
9856 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009857 unsigned char *output_buffer = NULL;
9858 size_t output_len = 0;
9859
Gilles Peskine449bd832023-01-11 14:50:10 +01009860 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009861
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009862 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009863 PSA_PAKE_STEP_KEY_SHARE);
9864 ASSERT_ALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009865
Gilles Peskine449bd832023-01-11 14:50:10 +01009866 if (pw_data->len > 0) {
9867 psa_set_key_usage_flags(&attributes, key_usage_pw);
9868 psa_set_key_algorithm(&attributes, alg);
9869 psa_set_key_type(&attributes, key_type_pw);
9870 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9871 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009872 }
9873
Gilles Peskine449bd832023-01-11 14:50:10 +01009874 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9875 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9876 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009877
Gilles Peskine449bd832023-01-11 14:50:10 +01009878 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009879
Gilles Peskine449bd832023-01-11 14:50:10 +01009880 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9881 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9882 expected_error);
9883 PSA_ASSERT(psa_pake_abort(&operation));
9884 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9885 expected_error);
9886 PSA_ASSERT(psa_pake_abort(&operation));
9887 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9888 expected_error);
9889 PSA_ASSERT(psa_pake_abort(&operation));
9890 TEST_EQUAL(psa_pake_set_role(&operation, role),
9891 expected_error);
9892 PSA_ASSERT(psa_pake_abort(&operation));
9893 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9894 NULL, 0, NULL),
9895 expected_error);
9896 PSA_ASSERT(psa_pake_abort(&operation));
9897 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9898 expected_error);
9899 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009900 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009901 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009902
Gilles Peskine449bd832023-01-11 14:50:10 +01009903 status = psa_pake_setup(&operation, &cipher_suite);
9904 if (status != PSA_SUCCESS) {
9905 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009906 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009907 }
9908
Gilles Peskine449bd832023-01-11 14:50:10 +01009909 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9910 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9911 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009912 goto exit;
9913 }
9914
Gilles Peskine449bd832023-01-11 14:50:10 +01009915 status = psa_pake_set_role(&operation, role);
9916 if (status != PSA_SUCCESS) {
9917 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009918 goto exit;
9919 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009920
Gilles Peskine449bd832023-01-11 14:50:10 +01009921 if (pw_data->len > 0) {
9922 status = psa_pake_set_password_key(&operation, key);
9923 if (status != PSA_SUCCESS) {
9924 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009925 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009926 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009927 }
9928
Gilles Peskine449bd832023-01-11 14:50:10 +01009929 if (inj_err_type == INJECT_ERR_INVALID_USER) {
9930 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9931 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009932 goto exit;
9933 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009934
Gilles Peskine449bd832023-01-11 14:50:10 +01009935 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
9936 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9937 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009938 goto exit;
9939 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009940
Gilles Peskine449bd832023-01-11 14:50:10 +01009941 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009942 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009943 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
9944 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009945 goto exit;
9946 }
9947
Gilles Peskine449bd832023-01-11 14:50:10 +01009948 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009949 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +01009950 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
9951 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +01009952 goto exit;
9953 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009954
Gilles Peskine449bd832023-01-11 14:50:10 +01009955 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
9956 PSA_PAKE_STEP_KEY_SHARE);
9957 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
9958 PSA_PAKE_STEP_ZK_PUBLIC);
9959 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
9960 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009961
Gilles Peskine449bd832023-01-11 14:50:10 +01009962 if (test_input) {
9963 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
9964 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
9965 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009966 goto exit;
9967 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009968
Gilles Peskine449bd832023-01-11 14:50:10 +01009969 if (inj_err_type == INJECT_UNKNOWN_STEP) {
9970 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
9971 output_buffer, size_zk_proof),
9972 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009973 goto exit;
9974 }
9975
Gilles Peskine449bd832023-01-11 14:50:10 +01009976 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
9977 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
9978 output_buffer, size_zk_proof),
9979 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +01009980 goto exit;
9981 }
9982
Gilles Peskine449bd832023-01-11 14:50:10 +01009983 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
9984 output_buffer, size_key_share);
9985 if (status != PSA_SUCCESS) {
9986 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009987 goto exit;
9988 }
9989
Gilles Peskine449bd832023-01-11 14:50:10 +01009990 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
9991 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
9992 output_buffer, size_zk_public + 1),
9993 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009994 goto exit;
9995 }
9996
Gilles Peskine449bd832023-01-11 14:50:10 +01009997 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009998 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +01009999 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10000 output_buffer, size_zk_public + 1);
10001 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10002 output_buffer, size_zk_public),
10003 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010004 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010005 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010006 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010007 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10008 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10009 NULL, 0, NULL),
10010 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010011 goto exit;
10012 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010013
Gilles Peskine449bd832023-01-11 14:50:10 +010010014 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10015 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10016 output_buffer, buf_size, &output_len),
10017 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010018 goto exit;
10019 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010020
Gilles Peskine449bd832023-01-11 14:50:10 +010010021 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10022 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10023 output_buffer, buf_size, &output_len),
10024 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010025 goto exit;
10026 }
10027
Gilles Peskine449bd832023-01-11 14:50:10 +010010028 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10029 output_buffer, buf_size, &output_len);
10030 if (status != PSA_SUCCESS) {
10031 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010032 goto exit;
10033 }
10034
Gilles Peskine449bd832023-01-11 14:50:10 +010010035 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010036
Gilles Peskine449bd832023-01-11 14:50:10 +010010037 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10038 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10039 output_buffer, size_zk_public - 1, &output_len),
10040 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010041 goto exit;
10042 }
10043
Gilles Peskine449bd832023-01-11 14:50:10 +010010044 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010045 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010046 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10047 output_buffer, size_zk_public - 1, &output_len);
10048 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10049 output_buffer, buf_size, &output_len),
10050 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010051 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010052 }
10053 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010054
10055exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010056 PSA_ASSERT(psa_destroy_key(key));
10057 PSA_ASSERT(psa_pake_abort(&operation));
10058 mbedtls_free(output_buffer);
10059 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010060}
10061/* END_CASE */
10062
Neil Armstronga557cb82022-06-10 08:58:32 +020010063/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010064void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10065 int client_input_first, int inject_error,
10066 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010067{
10068 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10069 psa_pake_operation_t server = psa_pake_operation_init();
10070 psa_pake_operation_t client = psa_pake_operation_init();
10071 psa_algorithm_t alg = alg_arg;
10072 psa_algorithm_t hash_alg = hash_arg;
10073 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10074 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10075
Gilles Peskine449bd832023-01-11 14:50:10 +010010076 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010077
Gilles Peskine449bd832023-01-11 14:50:10 +010010078 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10079 psa_set_key_algorithm(&attributes, alg);
10080 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10081 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10082 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010083
Gilles Peskine449bd832023-01-11 14:50:10 +010010084 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10085 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10086 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010087
10088
Gilles Peskine449bd832023-01-11 14:50:10 +010010089 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10090 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010091
Gilles Peskine449bd832023-01-11 14:50:10 +010010092 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10093 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010094
Gilles Peskine449bd832023-01-11 14:50:10 +010010095 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10096 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010097
Gilles Peskine449bd832023-01-11 14:50:10 +010010098 ecjpake_do_round(alg, primitive_arg, &server, &client,
10099 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010100
Gilles Peskine449bd832023-01-11 14:50:10 +010010101 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010102 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010103 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010104
Gilles Peskine449bd832023-01-11 14:50:10 +010010105 ecjpake_do_round(alg, primitive_arg, &server, &client,
10106 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010107
10108exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010109 psa_destroy_key(key);
10110 psa_pake_abort(&server);
10111 psa_pake_abort(&client);
10112 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010113}
10114/* END_CASE */
10115
10116/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010117void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10118 int derive_alg_arg, data_t *pw_data,
10119 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010120{
10121 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10122 psa_pake_operation_t server = psa_pake_operation_init();
10123 psa_pake_operation_t client = psa_pake_operation_init();
10124 psa_algorithm_t alg = alg_arg;
10125 psa_algorithm_t hash_alg = hash_arg;
10126 psa_algorithm_t derive_alg = derive_alg_arg;
10127 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10128 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10129 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010130 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010131 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010132 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010133 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010134
Gilles Peskine449bd832023-01-11 14:50:10 +010010135 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010136
Gilles Peskine449bd832023-01-11 14:50:10 +010010137 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10138 psa_set_key_algorithm(&attributes, alg);
10139 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10140 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10141 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010142
Gilles Peskine449bd832023-01-11 14:50:10 +010010143 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10144 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10145 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010146
Neil Armstrong1e855602022-06-15 11:32:11 +020010147 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010148 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10149 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010150
Gilles Peskine449bd832023-01-11 14:50:10 +010010151 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10152 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10153 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10154 PSA_KEY_DERIVATION_INPUT_SEED,
10155 (const uint8_t *) "", 0));
10156 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10157 PSA_KEY_DERIVATION_INPUT_SEED,
10158 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010159 }
10160
Gilles Peskine449bd832023-01-11 14:50:10 +010010161 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10162 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010163
Gilles Peskine449bd832023-01-11 14:50:10 +010010164 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10165 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010166
Gilles Peskine449bd832023-01-11 14:50:10 +010010167 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10168 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010169
Gilles Peskine449bd832023-01-11 14:50:10 +010010170 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10171 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10172 PSA_ERROR_BAD_STATE);
10173 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10174 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010175 goto exit;
10176 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010177
Neil Armstrongf983caf2022-06-15 15:27:48 +020010178 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010179 ecjpake_do_round(alg, primitive_arg, &server, &client,
10180 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010181
Gilles Peskine449bd832023-01-11 14:50:10 +010010182 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10183 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10184 PSA_ERROR_BAD_STATE);
10185 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10186 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010187 goto exit;
10188 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010189
Neil Armstrongf983caf2022-06-15 15:27:48 +020010190 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010191 ecjpake_do_round(alg, primitive_arg, &server, &client,
10192 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010193
Gilles Peskine449bd832023-01-11 14:50:10 +010010194 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10195 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010196
10197exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010198 psa_key_derivation_abort(&server_derive);
10199 psa_key_derivation_abort(&client_derive);
10200 psa_destroy_key(key);
10201 psa_pake_abort(&server);
10202 psa_pake_abort(&client);
10203 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010204}
10205/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010206
10207/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010208void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010209{
10210 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10211 const size_t bits = 256;
10212 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010213 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010214 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010215 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010216
10217 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10218 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010219 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10220 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10221 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10222 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010223 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010224 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10225 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010226
10227 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010228 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10229 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10230 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10231 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10232 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10233 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010234
10235 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010236 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10237 PSA_PAKE_OUTPUT_MAX_SIZE);
10238 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10239 PSA_PAKE_OUTPUT_MAX_SIZE);
10240 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10241 PSA_PAKE_OUTPUT_MAX_SIZE);
10242 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10243 PSA_PAKE_INPUT_MAX_SIZE);
10244 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10245 PSA_PAKE_INPUT_MAX_SIZE);
10246 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10247 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010248}
10249/* END_CASE */