blob: 2dfc7a4bfc27185435c30809a62fc94a4071acaf [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
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100299/* An overapproximation of the amount of storage needed for a key of the
300 * given type and with the given content. The API doesn't make it easy
301 * to find a good value for the size. The current implementation doesn't
302 * care about the value anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100303#define KEY_BITS_FROM_DATA(type, data) \
304 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100305
Darryl Green0c6575a2018-11-07 16:05:30 +0000306typedef enum {
307 IMPORT_KEY = 0,
308 GENERATE_KEY = 1,
309 DERIVE_KEY = 2
310} generate_method;
311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312typedef enum {
Paul Elliott33746aa2021-09-15 16:40:40 +0100313 DO_NOT_SET_LENGTHS = 0,
314 SET_LENGTHS_BEFORE_NONCE = 1,
315 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100316} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318typedef enum {
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100319 USE_NULL_TAG = 0,
320 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100321} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100322
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +0530323
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100324/*!
325 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100326 * \param key_type_arg Type of key passed in
327 * \param key_data The encryption / decryption key data
328 * \param alg_arg The type of algorithm used
329 * \param nonce Nonce data
330 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100331 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100332 * feed additional data in to be encrypted /
333 * decrypted. If -1, no chunking.
334 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100335 * \param data_part_len_arg If not -1, the length of chunks to feed
336 * the data in to be encrypted / decrypted. If
337 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100338 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100339 * expected here, this controls whether or not
340 * to set lengths, and in what order with
341 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100342 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100343 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100344 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100345 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100346 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100347 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348static int aead_multipart_internal_func(int key_type_arg, data_t *key_data,
349 int alg_arg,
350 data_t *nonce,
351 data_t *additional_data,
352 int ad_part_len_arg,
353 data_t *input_data,
354 int data_part_len_arg,
355 set_lengths_method_t set_lengths_method,
356 data_t *expected_output,
357 int is_encrypt,
358 int do_zero_parts)
Paul Elliottd3f82412021-06-16 16:52:21 +0100359{
360 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
361 psa_key_type_t key_type = key_type_arg;
362 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100363 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100364 unsigned char *output_data = NULL;
365 unsigned char *part_data = NULL;
366 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100367 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100368 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100369 size_t output_size = 0;
370 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100371 size_t output_length = 0;
372 size_t key_bits = 0;
373 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100374 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100375 size_t part_length = 0;
376 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100377 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100378 size_t ad_part_len = 0;
379 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100380 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100381 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
382 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
383
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100384 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100385 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 PSA_ASSERT(psa_crypto_init());
Paul Elliottd3f82412021-06-16 16:52:21 +0100388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 if (is_encrypt) {
390 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
391 } else {
392 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100393 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100394
395 psa_set_key_algorithm(&attributes, alg);
396 psa_set_key_type(&attributes, key_type);
397
398 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
399 &key));
400
401 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
402 key_bits = psa_get_key_bits(&attributes);
403
404 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
405
406 if (is_encrypt) {
407 /* Tag gets written at end of buffer. */
408 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
409 (input_data->len +
410 tag_length));
411 data_true_size = input_data->len;
412 } else {
413 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
414 (input_data->len -
415 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100416
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100417 /* Do not want to attempt to decrypt tag. */
418 data_true_size = input_data->len - tag_length;
419 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100420
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100421 TEST_CALLOC(output_data, output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 if (is_encrypt) {
424 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
425 TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
426 } else {
427 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
428 TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100429 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100430
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100431 TEST_CALLOC(final_data, final_output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (is_encrypt) {
434 status = psa_aead_encrypt_setup(&operation, key, alg);
435 } else {
436 status = psa_aead_decrypt_setup(&operation, key, alg);
437 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100438
439 /* If the operation is not supported, just skip and not fail in case the
440 * encryption involves a common limitation of cryptography hardwares and
441 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if (status == PSA_ERROR_NOT_SUPPORTED) {
443 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
444 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100445 }
446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 PSA_ASSERT(status);
Paul Elliottd3f82412021-06-16 16:52:21 +0100448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 if (set_lengths_method == DO_NOT_SET_LENGTHS) {
450 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
451 } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
452 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
453 data_true_size));
454 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
455 } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
456 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottebf91632021-07-22 17:54:42 +0100457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
459 data_true_size));
Paul Elliottd3f82412021-06-16 16:52:21 +0100460 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 if (ad_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100463 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100464 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100467 part_offset < additional_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 part_offset += part_length, part_count++) {
469 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100470 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 } else if (additional_data->len - part_offset < ad_part_len) {
Paul Elliotte49fe452021-09-15 16:52:11 +0100472 part_length = additional_data->len - part_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100474 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100475 }
476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 PSA_ASSERT(psa_aead_update_ad(&operation,
478 additional_data->x + part_offset,
479 part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100480
Paul Elliottd3f82412021-06-16 16:52:21 +0100481 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 } else {
Paul Elliottd3f82412021-06-16 16:52:21 +0100483 /* Pass additional data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
485 additional_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100486 }
487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 if (data_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100489 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 data_part_len = (size_t) data_part_len_arg;
491 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
492 (size_t) data_part_len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100493
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100494 TEST_CALLOC(part_data, part_data_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100497 part_offset < data_true_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 part_offset += part_length, part_count++) {
499 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100500 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 } else if ((data_true_size - part_offset) < data_part_len) {
502 part_length = (data_true_size - part_offset);
503 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100504 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100505 }
506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 PSA_ASSERT(psa_aead_update(&operation,
508 (input_data->x + part_offset),
509 part_length, part_data,
510 part_data_size,
511 &output_part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100512
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 if (output_data && output_part_length) {
514 memcpy((output_data + output_length), part_data,
515 output_part_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100516 }
517
Paul Elliottd3f82412021-06-16 16:52:21 +0100518 output_length += output_part_length;
519 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 } else {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100521 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
523 data_true_size, output_data,
524 output_size, &output_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100525 }
526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 if (is_encrypt) {
528 PSA_ASSERT(psa_aead_finish(&operation, final_data,
529 final_output_size,
530 &output_part_length,
531 tag_buffer, tag_length,
532 &tag_size));
533 } else {
534 PSA_ASSERT(psa_aead_verify(&operation, final_data,
535 final_output_size,
536 &output_part_length,
537 (input_data->x + data_true_size),
538 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100539 }
540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 if (output_data && output_part_length) {
542 memcpy((output_data + output_length), final_data,
543 output_part_length);
544 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100545
546 output_length += output_part_length;
547
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100548
549 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
550 * should be exact.*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if (is_encrypt) {
552 TEST_EQUAL(tag_length, tag_size);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if (output_data && tag_length) {
555 memcpy((output_data + output_length), tag_buffer,
556 tag_length);
557 }
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100558
559 output_length += tag_length;
560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 TEST_EQUAL(output_length,
562 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
563 input_data->len));
564 TEST_LE_U(output_length,
565 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
566 } else {
567 TEST_EQUAL(output_length,
568 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
569 input_data->len));
570 TEST_LE_U(output_length,
571 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100572 }
573
Paul Elliottd3f82412021-06-16 16:52:21 +0100574
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100575 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100576 output_data, output_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100577
Paul Elliottd3f82412021-06-16 16:52:21 +0100578
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100579 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
581exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 psa_destroy_key(key);
583 psa_aead_abort(&operation);
584 mbedtls_free(output_data);
585 mbedtls_free(part_data);
586 mbedtls_free(final_data);
587 PSA_DONE();
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 return test_ok;
Paul Elliottd3f82412021-06-16 16:52:21 +0100590}
591
Neil Armstrong4766f992022-02-28 16:23:59 +0100592/*!
593 * \brief Internal Function for MAC multipart tests.
594 * \param key_type_arg Type of key passed in
595 * \param key_data The encryption / decryption key data
596 * \param alg_arg The type of algorithm used
597 * \param input_data Data to encrypt / decrypt
598 * \param data_part_len_arg If not -1, the length of chunks to feed
599 * the data in to be encrypted / decrypted. If
600 * -1, no chunking
601 * \param expected_output Expected output
Tom Cosgrove1797b052022-12-04 17:19:59 +0000602 * \param is_verify If non-zero this is a verify operation.
Neil Armstrong4766f992022-02-28 16:23:59 +0100603 * \param do_zero_parts If non-zero, interleave zero length chunks
604 * with normal length chunks.
605 * \return int Zero on failure, non-zero on success.
606 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100607static int mac_multipart_internal_func(int key_type_arg, data_t *key_data,
608 int alg_arg,
609 data_t *input_data,
610 int data_part_len_arg,
611 data_t *expected_output,
612 int is_verify,
613 int do_zero_parts)
Neil Armstrong4766f992022-02-28 16:23:59 +0100614{
615 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
616 psa_key_type_t key_type = key_type_arg;
617 psa_algorithm_t alg = alg_arg;
618 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
619 unsigned char mac[PSA_MAC_MAX_SIZE];
620 size_t part_offset = 0;
621 size_t part_length = 0;
622 size_t data_part_len = 0;
623 size_t mac_len = 0;
624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
625 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
626
627 int test_ok = 0;
628 size_t part_count = 0;
629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 PSA_INIT();
Neil Armstrong4766f992022-02-28 16:23:59 +0100631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 if (is_verify) {
633 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
634 } else {
635 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
636 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 psa_set_key_algorithm(&attributes, alg);
639 psa_set_key_type(&attributes, key_type);
Neil Armstrong4766f992022-02-28 16:23:59 +0100640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
642 &key));
Neil Armstrong4766f992022-02-28 16:23:59 +0100643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if (is_verify) {
645 status = psa_mac_verify_setup(&operation, key, alg);
646 } else {
647 status = psa_mac_sign_setup(&operation, key, alg);
648 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 PSA_ASSERT(status);
Neil Armstrong4766f992022-02-28 16:23:59 +0100651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (data_part_len_arg != -1) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100653 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 data_part_len = (size_t) data_part_len_arg;
Neil Armstrong4766f992022-02-28 16:23:59 +0100655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 for (part_offset = 0, part_count = 0;
Neil Armstrong4766f992022-02-28 16:23:59 +0100657 part_offset < input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 part_offset += part_length, part_count++) {
659 if (do_zero_parts && (part_count & 0x01)) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100660 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 } else if ((input_data->len - part_offset) < data_part_len) {
662 part_length = (input_data->len - part_offset);
663 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100664 part_length = data_part_len;
665 }
666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 PSA_ASSERT(psa_mac_update(&operation,
668 (input_data->x + part_offset),
669 part_length));
Neil Armstrong4766f992022-02-28 16:23:59 +0100670 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100672 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 PSA_ASSERT(psa_mac_update(&operation, input_data->x,
674 input_data->len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100675 }
676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 if (is_verify) {
678 PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x,
679 expected_output->len));
680 } else {
681 PSA_ASSERT(psa_mac_sign_finish(&operation, mac,
682 PSA_MAC_MAX_SIZE, &mac_len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100683
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100684 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100685 mac, mac_len);
Neil Armstrong4766f992022-02-28 16:23:59 +0100686 }
687
688 test_ok = 1;
689
690exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 psa_destroy_key(key);
692 psa_mac_abort(&operation);
693 PSA_DONE();
Neil Armstrong4766f992022-02-28 16:23:59 +0100694
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 return test_ok;
Neil Armstrong4766f992022-02-28 16:23:59 +0100696}
697
Neil Armstrong75673ab2022-06-15 17:39:01 +0200698#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100699static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
700 psa_pake_operation_t *server,
701 psa_pake_operation_t *client,
702 int client_input_first,
703 int round, int inject_error)
Neil Armstrongf983caf2022-06-15 15:27:48 +0200704{
705 unsigned char *buffer0 = NULL, *buffer1 = NULL;
706 size_t buffer_length = (
707 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
708 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
709 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200710 /* The output should be exactly this size according to the spec */
711 const size_t expected_size_key_share =
712 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
713 /* The output should be exactly this size according to the spec */
714 const size_t expected_size_zk_public =
715 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
716 /* The output can be smaller: the spec allows stripping leading zeroes */
717 const size_t max_expected_size_zk_proof =
718 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200719 size_t buffer0_off = 0;
720 size_t buffer1_off = 0;
721 size_t s_g1_len, s_g2_len, s_a_len;
722 size_t s_g1_off, s_g2_off, s_a_off;
723 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
724 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
725 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
726 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
727 size_t c_g1_len, c_g2_len, c_a_len;
728 size_t c_g1_off, c_g2_off, c_a_off;
729 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
730 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
731 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
732 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
733 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200734 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200735
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100736 TEST_CALLOC(buffer0, buffer_length);
737 TEST_CALLOC(buffer1, buffer_length);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 switch (round) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200740 case 1:
741 /* Server first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
743 buffer0 + buffer0_off,
744 512 - buffer0_off, &s_g1_len));
745 TEST_EQUAL(s_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200746 s_g1_off = buffer0_off;
747 buffer0_off += s_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
749 buffer0 + buffer0_off,
750 512 - buffer0_off, &s_x1_pk_len));
751 TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200752 s_x1_pk_off = buffer0_off;
753 buffer0_off += s_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
755 buffer0 + buffer0_off,
756 512 - buffer0_off, &s_x1_pr_len));
757 TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200758 s_x1_pr_off = buffer0_off;
759 buffer0_off += s_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
761 buffer0 + buffer0_off,
762 512 - buffer0_off, &s_g2_len));
763 TEST_EQUAL(s_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200764 s_g2_off = buffer0_off;
765 buffer0_off += s_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
767 buffer0 + buffer0_off,
768 512 - buffer0_off, &s_x2_pk_len));
769 TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200770 s_x2_pk_off = buffer0_off;
771 buffer0_off += s_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
773 buffer0 + buffer0_off,
774 512 - buffer0_off, &s_x2_pr_len));
775 TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200776 s_x2_pr_off = buffer0_off;
777 buffer0_off += s_x2_pr_len;
778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 if (inject_error == 1) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500780 buffer0[s_x1_pr_off + 8] ^= 1;
781 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200782 expected_status = PSA_ERROR_DATA_INVALID;
783 }
784
Neil Armstrong51009d72022-09-05 17:59:54 +0200785 /*
786 * When injecting errors in inputs, the implementation is
787 * free to detect it right away of with a delay.
788 * This permits delaying the error until the end of the input
789 * sequence, if no error appears then, this will be treated
790 * as an error.
791 */
792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200794 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
796 buffer0 + s_g1_off, s_g1_len);
797 if (inject_error == 1 && status != PSA_SUCCESS) {
798 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200799 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 } else {
801 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200802 }
803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
805 buffer0 + s_x1_pk_off,
806 s_x1_pk_len);
807 if (inject_error == 1 && status != PSA_SUCCESS) {
808 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200809 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 } else {
811 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200812 }
813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
815 buffer0 + s_x1_pr_off,
816 s_x1_pr_len);
817 if (inject_error == 1 && status != PSA_SUCCESS) {
818 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200819 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 } else {
821 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200822 }
823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
825 buffer0 + s_g2_off,
826 s_g2_len);
827 if (inject_error == 1 && status != PSA_SUCCESS) {
828 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200829 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 } else {
831 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200832 }
833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
835 buffer0 + s_x2_pk_off,
836 s_x2_pk_len);
837 if (inject_error == 1 && status != PSA_SUCCESS) {
838 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200839 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 } else {
841 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200842 }
843
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
845 buffer0 + s_x2_pr_off,
846 s_x2_pr_len);
847 if (inject_error == 1 && status != PSA_SUCCESS) {
848 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200849 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 } else {
851 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200852 }
853
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200854 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if (inject_error == 1) {
856 TEST_ASSERT(
857 !"One of the last psa_pake_input() calls should have returned the expected error.");
858 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200859 }
860
861 /* Client first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
863 buffer1 + buffer1_off,
864 512 - buffer1_off, &c_g1_len));
865 TEST_EQUAL(c_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200866 c_g1_off = buffer1_off;
867 buffer1_off += c_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
869 buffer1 + buffer1_off,
870 512 - buffer1_off, &c_x1_pk_len));
871 TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200872 c_x1_pk_off = buffer1_off;
873 buffer1_off += c_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
875 buffer1 + buffer1_off,
876 512 - buffer1_off, &c_x1_pr_len));
877 TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200878 c_x1_pr_off = buffer1_off;
879 buffer1_off += c_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
881 buffer1 + buffer1_off,
882 512 - buffer1_off, &c_g2_len));
883 TEST_EQUAL(c_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200884 c_g2_off = buffer1_off;
885 buffer1_off += c_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
887 buffer1 + buffer1_off,
888 512 - buffer1_off, &c_x2_pk_len));
889 TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200890 c_x2_pk_off = buffer1_off;
891 buffer1_off += c_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
893 buffer1 + buffer1_off,
894 512 - buffer1_off, &c_x2_pr_len));
895 TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200896 c_x2_pr_off = buffer1_off;
897 buffer1_off += c_x2_pr_len;
898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200900 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
902 buffer0 + s_g1_off, s_g1_len);
903 if (inject_error == 1 && status != PSA_SUCCESS) {
904 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200905 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 } else {
907 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200908 }
909
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
911 buffer0 + s_x1_pk_off,
912 s_x1_pk_len);
913 if (inject_error == 1 && status != PSA_SUCCESS) {
914 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200915 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 } else {
917 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200918 }
919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
921 buffer0 + s_x1_pr_off,
922 s_x1_pr_len);
923 if (inject_error == 1 && status != PSA_SUCCESS) {
924 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200925 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 } else {
927 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200928 }
929
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
931 buffer0 + s_g2_off,
932 s_g2_len);
933 if (inject_error == 1 && status != PSA_SUCCESS) {
934 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200935 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 } else {
937 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200938 }
939
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
941 buffer0 + s_x2_pk_off,
942 s_x2_pk_len);
943 if (inject_error == 1 && status != PSA_SUCCESS) {
944 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200945 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 } else {
947 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200948 }
949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
951 buffer0 + s_x2_pr_off,
952 s_x2_pr_len);
953 if (inject_error == 1 && status != PSA_SUCCESS) {
954 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200955 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 } else {
957 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200958 }
959
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200960 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 if (inject_error == 1) {
962 TEST_ASSERT(
963 !"One of the last psa_pake_input() calls should have returned the expected error.");
964 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200965 }
966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 if (inject_error == 2) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500968 buffer1[c_x1_pr_off + 12] ^= 1;
969 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200970 expected_status = PSA_ERROR_DATA_INVALID;
971 }
972
973 /* Server first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
975 buffer1 + c_g1_off, c_g1_len);
976 if (inject_error == 2 && status != PSA_SUCCESS) {
977 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200978 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 } else {
980 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200981 }
982
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
984 buffer1 + c_x1_pk_off, c_x1_pk_len);
985 if (inject_error == 2 && status != PSA_SUCCESS) {
986 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200987 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 } else {
989 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200990 }
991
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
993 buffer1 + c_x1_pr_off, c_x1_pr_len);
994 if (inject_error == 2 && status != PSA_SUCCESS) {
995 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200996 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 } else {
998 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200999 }
1000
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1002 buffer1 + c_g2_off, c_g2_len);
1003 if (inject_error == 2 && status != PSA_SUCCESS) {
1004 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001005 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 } else {
1007 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001008 }
1009
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1011 buffer1 + c_x2_pk_off, c_x2_pk_len);
1012 if (inject_error == 2 && status != PSA_SUCCESS) {
1013 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001014 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 } else {
1016 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001017 }
1018
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1020 buffer1 + c_x2_pr_off, c_x2_pr_len);
1021 if (inject_error == 2 && status != PSA_SUCCESS) {
1022 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001023 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 } else {
1025 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001026 }
1027
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001028 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if (inject_error == 2) {
1030 TEST_ASSERT(
1031 !"One of the last psa_pake_input() calls should have returned the expected error.");
1032 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001033
1034 break;
1035
1036 case 2:
1037 /* Server second round Output */
1038 buffer0_off = 0;
1039
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
1041 buffer0 + buffer0_off,
1042 512 - buffer0_off, &s_a_len));
1043 TEST_EQUAL(s_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001044 s_a_off = buffer0_off;
1045 buffer0_off += s_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
1047 buffer0 + buffer0_off,
1048 512 - buffer0_off, &s_x2s_pk_len));
1049 TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001050 s_x2s_pk_off = buffer0_off;
1051 buffer0_off += s_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
1053 buffer0 + buffer0_off,
1054 512 - buffer0_off, &s_x2s_pr_len));
1055 TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001056 s_x2s_pr_off = buffer0_off;
1057 buffer0_off += s_x2s_pr_len;
1058
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if (inject_error == 3) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001060 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001061 expected_status = PSA_ERROR_DATA_INVALID;
1062 }
1063
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001065 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1067 buffer0 + s_a_off, s_a_len);
1068 if (inject_error == 3 && status != PSA_SUCCESS) {
1069 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001070 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 } else {
1072 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001073 }
1074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1076 buffer0 + s_x2s_pk_off,
1077 s_x2s_pk_len);
1078 if (inject_error == 3 && status != PSA_SUCCESS) {
1079 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001080 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 } else {
1082 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001083 }
1084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1086 buffer0 + s_x2s_pr_off,
1087 s_x2s_pr_len);
1088 if (inject_error == 3 && status != PSA_SUCCESS) {
1089 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001090 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 } else {
1092 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001093 }
1094
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001095 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 if (inject_error == 3) {
1097 TEST_ASSERT(
1098 !"One of the last psa_pake_input() calls should have returned the expected error.");
1099 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001100 }
1101
1102 /* Client second round Output */
1103 buffer1_off = 0;
1104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
1106 buffer1 + buffer1_off,
1107 512 - buffer1_off, &c_a_len));
1108 TEST_EQUAL(c_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001109 c_a_off = buffer1_off;
1110 buffer1_off += c_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
1112 buffer1 + buffer1_off,
1113 512 - buffer1_off, &c_x2s_pk_len));
1114 TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001115 c_x2s_pk_off = buffer1_off;
1116 buffer1_off += c_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
1118 buffer1 + buffer1_off,
1119 512 - buffer1_off, &c_x2s_pr_len));
1120 TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001121 c_x2s_pr_off = buffer1_off;
1122 buffer1_off += c_x2s_pr_len;
1123
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001125 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1127 buffer0 + s_a_off, s_a_len);
1128 if (inject_error == 3 && status != PSA_SUCCESS) {
1129 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001130 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 } else {
1132 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001133 }
1134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1136 buffer0 + s_x2s_pk_off,
1137 s_x2s_pk_len);
1138 if (inject_error == 3 && status != PSA_SUCCESS) {
1139 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001140 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 } else {
1142 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001143 }
1144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1146 buffer0 + s_x2s_pr_off,
1147 s_x2s_pr_len);
1148 if (inject_error == 3 && status != PSA_SUCCESS) {
1149 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001150 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 } else {
1152 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001153 }
1154
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001155 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 if (inject_error == 3) {
1157 TEST_ASSERT(
1158 !"One of the last psa_pake_input() calls should have returned the expected error.");
1159 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001160 }
1161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 if (inject_error == 4) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001163 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001164 expected_status = PSA_ERROR_DATA_INVALID;
1165 }
1166
1167 /* Server second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1169 buffer1 + c_a_off, c_a_len);
1170 if (inject_error == 4 && status != PSA_SUCCESS) {
1171 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001172 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 } else {
1174 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001175 }
1176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1178 buffer1 + c_x2s_pk_off, c_x2s_pk_len);
1179 if (inject_error == 4 && status != PSA_SUCCESS) {
1180 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001181 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 } else {
1183 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001184 }
1185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1187 buffer1 + c_x2s_pr_off, c_x2s_pr_len);
1188 if (inject_error == 4 && status != PSA_SUCCESS) {
1189 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001190 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 } else {
1192 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001193 }
1194
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001195 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 if (inject_error == 4) {
1197 TEST_ASSERT(
1198 !"One of the last psa_pake_input() calls should have returned the expected error.");
1199 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001200
1201 break;
1202
1203 }
1204
Neil Armstrongf983caf2022-06-15 15:27:48 +02001205exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 mbedtls_free(buffer0);
1207 mbedtls_free(buffer1);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001208}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001209#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211typedef enum {
Valerio Setti1070aed2022-11-11 19:37:31 +01001212 INJECT_ERR_NONE = 0,
1213 INJECT_ERR_UNINITIALIZED_ACCESS,
1214 INJECT_ERR_DUPLICATE_SETUP,
1215 INJECT_ERR_INVALID_USER,
1216 INJECT_ERR_INVALID_PEER,
1217 INJECT_ERR_SET_USER,
1218 INJECT_ERR_SET_PEER,
1219 INJECT_EMPTY_IO_BUFFER,
1220 INJECT_UNKNOWN_STEP,
1221 INJECT_INVALID_FIRST_STEP,
1222 INJECT_WRONG_BUFFER_SIZE,
1223 INJECT_VALID_OPERATION_AFTER_FAILURE,
1224 INJECT_ANTICIPATE_KEY_DERIVATION_1,
1225 INJECT_ANTICIPATE_KEY_DERIVATION_2,
1226} ecjpake_injected_failure_t;
1227
Paul Elliott01885fa2023-02-09 12:07:30 +00001228#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott1243f932023-02-07 11:21:10 +00001229
Paul Elliott6f600372023-02-06 18:41:05 +00001230static void interruptible_signverify_get_minmax_completes(uint32_t max_ops,
1231 psa_status_t expected_status,
1232 size_t *min_completes,
1233 size_t *max_completes)
1234{
1235
1236 /* This is slightly contrived, but we only really know that with a minimum
1237 value of max_ops that a successful operation should take more than one op
1238 to complete, and likewise that with a max_ops of
1239 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */
1240 if (max_ops == 0 || max_ops == 1) {
Paul Elliottc86d45e2023-02-15 17:38:05 +00001241
Paul Elliott6f600372023-02-06 18:41:05 +00001242 if (expected_status == PSA_SUCCESS) {
1243 *min_completes = 2;
1244 } else {
1245 *min_completes = 1;
1246 }
1247
1248 *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
1249 } else {
1250 *min_completes = 1;
1251 *max_completes = 1;
1252 }
1253}
Paul Elliott01885fa2023-02-09 12:07:30 +00001254#endif /* MBEDTLS_ECP_RESTARTABLE */
Paul Elliott6f600372023-02-06 18:41:05 +00001255
Gilles Peskinee59236f2018-01-27 23:32:46 +01001256/* END_HEADER */
1257
1258/* BEGIN_DEPENDENCIES
1259 * depends_on:MBEDTLS_PSA_CRYPTO_C
1260 * END_DEPENDENCIES
1261 */
1262
1263/* BEGIN_CASE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01001264void psa_can_do_hash()
1265{
1266 /* We can't test that this is specific to drivers until partial init has
1267 * been implemented, but we can at least test before/after full init. */
1268 TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE));
1269 PSA_INIT();
1270 TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE));
1271 PSA_DONE();
1272}
1273/* END_CASE */
1274
1275/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001276void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001277{
1278 size_t max_truncated_mac_size =
1279 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1280
1281 /* Check that the length for a truncated MAC always fits in the algorithm
1282 * encoding. The shifted mask is the maximum truncated value. The
1283 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001285}
1286/* END_CASE */
1287
1288/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001289void import_with_policy(int type_arg,
1290 int usage_arg, int alg_arg,
1291 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001292{
1293 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1294 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001295 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001296 psa_key_type_t type = type_arg;
1297 psa_key_usage_t usage = usage_arg;
1298 psa_algorithm_t alg = alg_arg;
1299 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001301 psa_status_t status;
1302
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 psa_set_key_type(&attributes, type);
1306 psa_set_key_usage_flags(&attributes, usage);
1307 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 status = psa_import_key(&attributes,
1310 key_material, sizeof(key_material),
1311 &key);
1312 TEST_EQUAL(status, expected_status);
1313 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001314 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1318 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1319 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1320 mbedtls_test_update_key_usage_flags(usage));
1321 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1322 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001323
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 PSA_ASSERT(psa_destroy_key(key));
1325 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001326
1327exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001328 /*
1329 * Key attributes may have been returned by psa_get_key_attributes()
1330 * thus reset them as required.
1331 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001333
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 psa_destroy_key(key);
1335 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001336}
1337/* END_CASE */
1338
1339/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001340void import_with_data(data_t *data, int type_arg,
1341 int attr_bits_arg,
1342 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001343{
1344 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1345 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001346 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001347 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001348 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001349 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001350 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001351
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 psa_set_key_type(&attributes, type);
1355 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 status = psa_import_key(&attributes, data->x, data->len, &key);
Manuel Pégourié-Gonnard0509b582023-08-08 12:47:56 +02001358 /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED.
1359 *
1360 * This can happen with a type supported only by a driver:
1361 * - the driver sees the invalid data (for example wrong size) and thinks
1362 * "well perhaps this is a key size I don't support" so it returns
1363 * NOT_SUPPORTED which is correct at this point;
1364 * - we fallback to built-ins, which don't support this type, so return
1365 * NOT_SUPPORTED which again is correct at this point.
1366 */
1367 if (expected_status == PSA_ERROR_INVALID_ARGUMENT &&
1368 status == PSA_ERROR_NOT_SUPPORTED) {
1369 ; // OK
1370 } else {
1371 TEST_EQUAL(status, expected_status);
1372 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001374 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1378 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1379 if (attr_bits != 0) {
1380 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1381 }
1382 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 PSA_ASSERT(psa_destroy_key(key));
1385 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001386
1387exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001388 /*
1389 * Key attributes may have been returned by psa_get_key_attributes()
1390 * thus reset them as required.
1391 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001393
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 psa_destroy_key(key);
1395 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001396}
1397/* END_CASE */
1398
1399/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001400/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001401void import_large_key(int type_arg, int byte_size_arg,
1402 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001403{
1404 psa_key_type_t type = type_arg;
1405 size_t byte_size = byte_size_arg;
1406 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1407 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001408 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001409 psa_status_t status;
1410 uint8_t *buffer = NULL;
1411 size_t buffer_size = byte_size + 1;
1412 size_t n;
1413
Steven Cooreman69967ce2021-01-18 18:01:08 +01001414 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001415 * accommodate large keys due to heap size constraints */
Tom Cosgrove412a8132023-07-20 16:55:14 +01001416 TEST_CALLOC_OR_SKIP(buffer, buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001418
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001420
1421 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1423 psa_set_key_type(&attributes, type);
1424 status = psa_import_key(&attributes, buffer, byte_size, &key);
1425 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1426 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001427
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 if (status == PSA_SUCCESS) {
1429 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1430 TEST_EQUAL(psa_get_key_type(&attributes), type);
1431 TEST_EQUAL(psa_get_key_bits(&attributes),
1432 PSA_BYTES_TO_BITS(byte_size));
1433 ASSERT_NO_SLOT_NUMBER(&attributes);
1434 memset(buffer, 0, byte_size + 1);
1435 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1436 for (n = 0; n < byte_size; n++) {
1437 TEST_EQUAL(buffer[n], 'K');
1438 }
1439 for (n = byte_size; n < buffer_size; n++) {
1440 TEST_EQUAL(buffer[n], 0);
1441 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001442 }
1443
1444exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001445 /*
1446 * Key attributes may have been returned by psa_get_key_attributes()
1447 * thus reset them as required.
1448 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001450
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 psa_destroy_key(key);
1452 PSA_DONE();
1453 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001454}
1455/* END_CASE */
1456
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001457/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001458/* Import an RSA key with a valid structure (but not valid numbers
1459 * inside, beyond having sensible size and parity). This is expected to
1460 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001461void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001462{
Ronald Cron5425a212020-08-04 14:58:35 +02001463 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001464 size_t bits = bits_arg;
1465 psa_status_t expected_status = expected_status_arg;
1466 psa_status_t status;
1467 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001468 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001469 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001471 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001472 unsigned char *p;
1473 int ret;
1474 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001475 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 PSA_ASSERT(psa_crypto_init());
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001478 TEST_CALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1481 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001482 length = ret;
1483
1484 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 psa_set_key_type(&attributes, type);
1486 status = psa_import_key(&attributes, p, length, &key);
1487 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 if (status == PSA_SUCCESS) {
1490 PSA_ASSERT(psa_destroy_key(key));
1491 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001492
1493exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 mbedtls_free(buffer);
1495 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001496}
1497/* END_CASE */
1498
1499/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001500void import_export(data_t *data,
1501 int type_arg,
1502 int usage_arg, int alg_arg,
1503 int lifetime_arg,
1504 int expected_bits,
1505 int export_size_delta,
1506 int expected_export_status_arg,
1507 /*whether reexport must give the original input exactly*/
1508 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001509{
Ronald Cron5425a212020-08-04 14:58:35 +02001510 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001511 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001512 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001513 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001514 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301515 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001516 unsigned char *exported = NULL;
1517 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001518 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001519 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001520 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001521 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001522 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001523
Moran Pekercb088e72018-07-17 17:36:59 +03001524 export_size = (ptrdiff_t) data->len + export_size_delta;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001525 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 if (!canonical_input) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001527 TEST_CALLOC(reexported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 }
1529 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 psa_set_key_lifetime(&attributes, lifetime);
1532 psa_set_key_usage_flags(&attributes, usage_arg);
1533 psa_set_key_algorithm(&attributes, alg);
1534 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001535
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001536 if (PSA_KEY_TYPE_IS_DH(type) &&
1537 expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) {
Przemek Stekiel654bef02022-12-15 13:28:02 +01001538 /* Simulate that buffer is too small, by decreasing its size by 1 byte. */
1539 export_size -= 1;
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001540 }
1541
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001542 /* Import the key */
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001543 TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key),
Przemek Stekiel2e7c33d2023-04-27 12:29:45 +02001544 PSA_SUCCESS);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001545
1546 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1548 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1549 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1550 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001551
1552 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 status = psa_export_key(key, exported, export_size, &exported_length);
1554 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001555
1556 /* The exported length must be set by psa_export_key() to a value between 0
1557 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1559 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1560 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1563 export_size - exported_length));
1564 if (status != PSA_SUCCESS) {
1565 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001566 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001567 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001568
Gilles Peskineea38a922021-02-13 00:05:16 +01001569 /* Run sanity checks on the exported key. For non-canonical inputs,
1570 * this validates the canonical representations. For canonical inputs,
1571 * this doesn't directly validate the implementation, but it still helps
1572 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 if (!psa_key_lifetime_is_external(lifetime)) {
1574 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301575 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 }
Archana4d7ae1d2021-07-07 02:50:22 +05301577 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001578
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 if (canonical_input) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001580 TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001582 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1584 &key2));
1585 PSA_ASSERT(psa_export_key(key2,
1586 reexported,
1587 export_size,
1588 &reexported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001589 TEST_MEMORY_COMPARE(exported, exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001590 reexported, reexported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001592 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 TEST_LE_U(exported_length,
1594 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1595 psa_get_key_bits(&got_attributes)));
Valerio Setti1eacae82023-07-28 16:07:03 +02001596 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
1597 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
1598 } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
1599 TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1600 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001601
1602destroy:
1603 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 PSA_ASSERT(psa_destroy_key(key));
1605 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001606
1607exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001608 /*
1609 * Key attributes may have been returned by psa_get_key_attributes()
1610 * thus reset them as required.
1611 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 psa_reset_key_attributes(&got_attributes);
1613 psa_destroy_key(key);
1614 mbedtls_free(exported);
1615 mbedtls_free(reexported);
1616 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001617}
1618/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001619
Moran Pekerf709f4a2018-06-06 17:26:04 +03001620/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001621void import_export_public_key(data_t *data,
1622 int type_arg, // key pair or public key
1623 int alg_arg,
1624 int lifetime_arg,
1625 int export_size_delta,
1626 int expected_export_status_arg,
1627 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001628{
Ronald Cron5425a212020-08-04 14:58:35 +02001629 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001630 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001631 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001632 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001633 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301634 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001635 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001636 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001637 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001638 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001639
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 psa_set_key_lifetime(&attributes, lifetime);
1643 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1644 psa_set_key_algorithm(&attributes, alg);
1645 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001646
1647 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001649
Gilles Peskine49c25912018-10-29 15:15:31 +01001650 /* Export the public key */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001651 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 status = psa_export_public_key(key,
1653 exported, export_size,
1654 &exported_length);
1655 TEST_EQUAL(status, expected_export_status);
1656 if (status == PSA_SUCCESS) {
1657 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001658 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1660 bits = psa_get_key_bits(&attributes);
1661 TEST_LE_U(expected_public_key->len,
1662 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1663 TEST_LE_U(expected_public_key->len,
1664 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1665 TEST_LE_U(expected_public_key->len,
1666 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001667 TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001668 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001669 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001670exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001671 /*
1672 * Key attributes may have been returned by psa_get_key_attributes()
1673 * thus reset them as required.
1674 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001676
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 mbedtls_free(exported);
1678 psa_destroy_key(key);
1679 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001680}
1681/* END_CASE */
1682
Gilles Peskine20035e32018-02-03 22:44:14 +01001683/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001684void import_and_exercise_key(data_t *data,
1685 int type_arg,
1686 int bits_arg,
1687 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001688{
Ronald Cron5425a212020-08-04 14:58:35 +02001689 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001690 psa_key_type_t type = type_arg;
1691 size_t bits = bits_arg;
1692 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001694 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001695 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001696
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 psa_set_key_usage_flags(&attributes, usage);
1700 psa_set_key_algorithm(&attributes, alg);
1701 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001702
1703 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001705
1706 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1708 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1709 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001710
1711 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001713 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001715
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 PSA_ASSERT(psa_destroy_key(key));
1717 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001718
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001719exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001720 /*
1721 * Key attributes may have been returned by psa_get_key_attributes()
1722 * thus reset them as required.
1723 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001725
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 psa_reset_key_attributes(&attributes);
1727 psa_destroy_key(key);
1728 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001729}
1730/* END_CASE */
1731
1732/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001733void effective_key_attributes(int type_arg, int expected_type_arg,
1734 int bits_arg, int expected_bits_arg,
1735 int usage_arg, int expected_usage_arg,
1736 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001737{
Ronald Cron5425a212020-08-04 14:58:35 +02001738 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001739 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001740 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001741 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001742 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001743 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001744 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001745 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001746 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001747 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001748
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001750
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 psa_set_key_usage_flags(&attributes, usage);
1752 psa_set_key_algorithm(&attributes, alg);
1753 psa_set_key_type(&attributes, key_type);
1754 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001755
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 PSA_ASSERT(psa_generate_key(&attributes, &key));
1757 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001758
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1760 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1761 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1762 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1763 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001764
1765exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001766 /*
1767 * Key attributes may have been returned by psa_get_key_attributes()
1768 * thus reset them as required.
1769 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001771
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 psa_destroy_key(key);
1773 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001774}
1775/* END_CASE */
1776
1777/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001778void check_key_policy(int type_arg, int bits_arg,
1779 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001780{
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1782 usage_arg,
1783 mbedtls_test_update_key_usage_flags(usage_arg),
1784 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001785 goto exit;
1786}
1787/* END_CASE */
1788
1789/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001790void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001791{
1792 /* Test each valid way of initializing the object, except for `= {0}`, as
1793 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1794 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001795 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001797 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1798 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001799
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1803 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1804 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001805
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 TEST_EQUAL(psa_get_key_type(&func), 0);
1807 TEST_EQUAL(psa_get_key_type(&init), 0);
1808 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001809
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 TEST_EQUAL(psa_get_key_bits(&func), 0);
1811 TEST_EQUAL(psa_get_key_bits(&init), 0);
1812 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001813
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1815 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1816 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001817
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1819 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1820 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001821}
1822/* END_CASE */
1823
1824/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001825void mac_key_policy(int policy_usage_arg,
1826 int policy_alg_arg,
1827 int key_type_arg,
1828 data_t *key_data,
1829 int exercise_alg_arg,
1830 int expected_status_sign_arg,
1831 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001832{
Ronald Cron5425a212020-08-04 14:58:35 +02001833 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001834 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001835 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001836 psa_key_type_t key_type = key_type_arg;
1837 psa_algorithm_t policy_alg = policy_alg_arg;
1838 psa_algorithm_t exercise_alg = exercise_alg_arg;
1839 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001840 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001841 psa_status_t expected_status_sign = expected_status_sign_arg;
1842 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001843 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001844
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 psa_set_key_usage_flags(&attributes, policy_usage);
1848 psa_set_key_algorithm(&attributes, policy_alg);
1849 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001850
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1852 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001853
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1855 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001856
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1858 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001859
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001860 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001862 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1864 input, 128,
1865 mac, PSA_MAC_MAX_SIZE, &mac_len),
1866 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001867
Neil Armstrong3af9b972022-02-07 12:20:21 +01001868 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 PSA_ASSERT(psa_mac_abort(&operation));
1870 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1871 if (status == PSA_SUCCESS) {
1872 status = psa_mac_update(&operation, input, 128);
1873 if (status == PSA_SUCCESS) {
1874 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1875 &mac_len),
1876 expected_status_sign);
1877 } else {
1878 TEST_EQUAL(status, expected_status_sign);
1879 }
1880 } else {
1881 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001882 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001884
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001885 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 status = psa_mac_verify(key, exercise_alg, input, 128,
1887 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001888
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1890 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1891 } else {
1892 TEST_EQUAL(status, expected_status_verify);
1893 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001894
Neil Armstrong3af9b972022-02-07 12:20:21 +01001895 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001896 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1897 if (status == PSA_SUCCESS) {
1898 status = psa_mac_update(&operation, input, 128);
1899 if (status == PSA_SUCCESS) {
1900 status = psa_mac_verify_finish(&operation, mac, mac_len);
1901 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1902 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1903 } else {
1904 TEST_EQUAL(status, expected_status_verify);
1905 }
1906 } else {
1907 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001908 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 } else {
1910 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001911 }
1912
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 memset(mac, 0, sizeof(mac));
1916 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1917 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001918
1919exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 psa_mac_abort(&operation);
1921 psa_destroy_key(key);
1922 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001923}
1924/* END_CASE */
1925
1926/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001927void cipher_key_policy(int policy_usage_arg,
1928 int policy_alg,
1929 int key_type,
1930 data_t *key_data,
1931 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001932{
Ronald Cron5425a212020-08-04 14:58:35 +02001933 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001934 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001935 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001936 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001937 size_t output_buffer_size = 0;
1938 size_t input_buffer_size = 0;
1939 size_t output_length = 0;
1940 uint8_t *output = NULL;
1941 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001942 psa_status_t status;
1943
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1945 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1946 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001947
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001948 TEST_CALLOC(input, input_buffer_size);
1949 TEST_CALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001950
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001952
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 psa_set_key_usage_flags(&attributes, policy_usage);
1954 psa_set_key_algorithm(&attributes, policy_alg);
1955 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001956
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1958 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001959
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001960 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 TEST_EQUAL(policy_usage,
1962 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001963
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001964 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1966 output, output_buffer_size,
1967 &output_length);
1968 if (policy_alg == exercise_alg &&
1969 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1970 PSA_ASSERT(status);
1971 } else {
1972 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1973 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001974
1975 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1977 if (policy_alg == exercise_alg &&
1978 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1979 PSA_ASSERT(status);
1980 } else {
1981 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1982 }
1983 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001984
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001985 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1987 input, input_buffer_size,
1988 &output_length);
1989 if (policy_alg == exercise_alg &&
1990 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1991 PSA_ASSERT(status);
1992 } else {
1993 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1994 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001995
1996 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
1998 if (policy_alg == exercise_alg &&
1999 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2000 PSA_ASSERT(status);
2001 } else {
2002 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2003 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002004
2005exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 psa_cipher_abort(&operation);
2007 mbedtls_free(input);
2008 mbedtls_free(output);
2009 psa_destroy_key(key);
2010 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002011}
2012/* END_CASE */
2013
2014/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002015void aead_key_policy(int policy_usage_arg,
2016 int policy_alg,
2017 int key_type,
2018 data_t *key_data,
2019 int nonce_length_arg,
2020 int tag_length_arg,
2021 int exercise_alg,
2022 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002023{
Ronald Cron5425a212020-08-04 14:58:35 +02002024 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002025 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002026 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002027 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002028 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002029 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002031 size_t nonce_length = nonce_length_arg;
2032 unsigned char tag[16];
2033 size_t tag_length = tag_length_arg;
2034 size_t output_length;
2035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 TEST_LE_U(nonce_length, sizeof(nonce));
2037 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002040
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 psa_set_key_usage_flags(&attributes, policy_usage);
2042 psa_set_key_algorithm(&attributes, policy_alg);
2043 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002044
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2046 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002047
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002048 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 TEST_EQUAL(policy_usage,
2050 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002051
Neil Armstrong752d8112022-02-07 14:51:11 +01002052 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 status = psa_aead_encrypt(key, exercise_alg,
2054 nonce, nonce_length,
2055 NULL, 0,
2056 NULL, 0,
2057 tag, tag_length,
2058 &output_length);
2059 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2060 TEST_EQUAL(status, expected_status);
2061 } else {
2062 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2063 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002064
Neil Armstrong752d8112022-02-07 14:51:11 +01002065 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2067 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2068 TEST_EQUAL(status, expected_status);
2069 } else {
2070 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2071 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002072
2073 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 memset(tag, 0, sizeof(tag));
2075 status = psa_aead_decrypt(key, exercise_alg,
2076 nonce, nonce_length,
2077 NULL, 0,
2078 tag, tag_length,
2079 NULL, 0,
2080 &output_length);
2081 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2082 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2083 } else if (expected_status == PSA_SUCCESS) {
2084 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2085 } else {
2086 TEST_EQUAL(status, expected_status);
2087 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002088
Neil Armstrong752d8112022-02-07 14:51:11 +01002089 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 PSA_ASSERT(psa_aead_abort(&operation));
2091 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2092 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2093 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2094 } else {
2095 TEST_EQUAL(status, expected_status);
2096 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002097
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002098exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 PSA_ASSERT(psa_aead_abort(&operation));
2100 psa_destroy_key(key);
2101 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002102}
2103/* END_CASE */
2104
2105/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002106void asymmetric_encryption_key_policy(int policy_usage_arg,
2107 int policy_alg,
2108 int key_type,
2109 data_t *key_data,
2110 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002111{
Ronald Cron5425a212020-08-04 14:58:35 +02002112 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002113 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002114 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002115 psa_status_t status;
2116 size_t key_bits;
2117 size_t buffer_length;
2118 unsigned char *buffer = NULL;
2119 size_t output_length;
2120
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 psa_set_key_usage_flags(&attributes, policy_usage);
2124 psa_set_key_algorithm(&attributes, policy_alg);
2125 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2128 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002129
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002130 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 TEST_EQUAL(policy_usage,
2132 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002133
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2135 key_bits = psa_get_key_bits(&attributes);
2136 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2137 exercise_alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002138 TEST_CALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002139
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 status = psa_asymmetric_encrypt(key, exercise_alg,
2141 NULL, 0,
2142 NULL, 0,
2143 buffer, buffer_length,
2144 &output_length);
2145 if (policy_alg == exercise_alg &&
2146 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2147 PSA_ASSERT(status);
2148 } else {
2149 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2150 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002151
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 if (buffer_length != 0) {
2153 memset(buffer, 0, buffer_length);
2154 }
2155 status = psa_asymmetric_decrypt(key, exercise_alg,
2156 buffer, buffer_length,
2157 NULL, 0,
2158 buffer, buffer_length,
2159 &output_length);
2160 if (policy_alg == exercise_alg &&
2161 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2162 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2163 } else {
2164 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2165 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002166
2167exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002168 /*
2169 * Key attributes may have been returned by psa_get_key_attributes()
2170 * thus reset them as required.
2171 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002173
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 psa_destroy_key(key);
2175 PSA_DONE();
2176 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002177}
2178/* END_CASE */
2179
2180/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002181void asymmetric_signature_key_policy(int policy_usage_arg,
2182 int policy_alg,
2183 int key_type,
2184 data_t *key_data,
2185 int exercise_alg,
2186 int payload_length_arg,
2187 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002188{
Ronald Cron5425a212020-08-04 14:58:35 +02002189 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002190 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002191 psa_key_usage_t policy_usage = policy_usage_arg;
2192 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002193 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002195 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2196 * compatible with the policy and `payload_length_arg` is supposed to be
2197 * a valid input length to sign. If `payload_length_arg <= 0`,
2198 * `exercise_alg` is supposed to be forbidden by the policy. */
2199 int compatible_alg = payload_length_arg > 0;
2200 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002202 size_t signature_length;
2203
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002204 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002205 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 TEST_EQUAL(expected_usage,
2207 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 psa_set_key_usage_flags(&attributes, policy_usage);
2212 psa_set_key_algorithm(&attributes, policy_alg);
2213 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002214
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2216 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002217
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 status = psa_sign_hash(key, exercise_alg,
2221 payload, payload_length,
2222 signature, sizeof(signature),
2223 &signature_length);
2224 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2225 PSA_ASSERT(status);
2226 } else {
2227 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2228 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 memset(signature, 0, sizeof(signature));
2231 status = psa_verify_hash(key, exercise_alg,
2232 payload, payload_length,
2233 signature, sizeof(signature));
2234 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2235 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2236 } else {
2237 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2238 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002239
Gilles Peskine449bd832023-01-11 14:50:10 +01002240 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2241 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2242 status = psa_sign_message(key, exercise_alg,
2243 payload, payload_length,
2244 signature, sizeof(signature),
2245 &signature_length);
2246 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2247 PSA_ASSERT(status);
2248 } else {
2249 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2250 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002251
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 memset(signature, 0, sizeof(signature));
2253 status = psa_verify_message(key, exercise_alg,
2254 payload, payload_length,
2255 signature, sizeof(signature));
2256 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2257 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2258 } else {
2259 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2260 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002261 }
2262
Gilles Peskined5b33222018-06-18 22:20:03 +02002263exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 psa_destroy_key(key);
2265 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002266}
2267/* END_CASE */
2268
Janos Follathba3fab92019-06-11 14:50:16 +01002269/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002270void derive_key_policy(int policy_usage,
2271 int policy_alg,
2272 int key_type,
2273 data_t *key_data,
2274 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002275{
Ronald Cron5425a212020-08-04 14:58:35 +02002276 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002277 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002278 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002279 psa_status_t status;
2280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 psa_set_key_usage_flags(&attributes, policy_usage);
2284 psa_set_key_algorithm(&attributes, policy_alg);
2285 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002286
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2288 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2293 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2294 PSA_ASSERT(psa_key_derivation_input_bytes(
2295 &operation,
2296 PSA_KEY_DERIVATION_INPUT_SEED,
2297 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002298 }
Janos Follathba3fab92019-06-11 14:50:16 +01002299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 status = psa_key_derivation_input_key(&operation,
2301 PSA_KEY_DERIVATION_INPUT_SECRET,
2302 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 if (policy_alg == exercise_alg &&
2305 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2306 PSA_ASSERT(status);
2307 } else {
2308 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2309 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002310
2311exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 psa_key_derivation_abort(&operation);
2313 psa_destroy_key(key);
2314 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002315}
2316/* END_CASE */
2317
2318/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002319void agreement_key_policy(int policy_usage,
2320 int policy_alg,
2321 int key_type_arg,
2322 data_t *key_data,
2323 int exercise_alg,
2324 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002325{
Ronald Cron5425a212020-08-04 14:58:35 +02002326 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002327 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002328 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002329 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002330 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002331 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 psa_set_key_usage_flags(&attributes, policy_usage);
2336 psa_set_key_algorithm(&attributes, policy_alg);
2337 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2340 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002341
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2343 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002344
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002346
2347exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 psa_key_derivation_abort(&operation);
2349 psa_destroy_key(key);
2350 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002351}
2352/* END_CASE */
2353
2354/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002355void key_policy_alg2(int key_type_arg, data_t *key_data,
2356 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002357{
Ronald Cron5425a212020-08-04 14:58:35 +02002358 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002359 psa_key_type_t key_type = key_type_arg;
2360 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2361 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2362 psa_key_usage_t usage = usage_arg;
2363 psa_algorithm_t alg = alg_arg;
2364 psa_algorithm_t alg2 = alg2_arg;
2365
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 psa_set_key_usage_flags(&attributes, usage);
2369 psa_set_key_algorithm(&attributes, alg);
2370 psa_set_key_enrollment_algorithm(&attributes, alg2);
2371 psa_set_key_type(&attributes, key_type);
2372 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2373 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002374
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002375 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 usage = mbedtls_test_update_key_usage_flags(usage);
2377 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2378 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2379 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2380 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002381
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002383 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 }
2385 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002386 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002388
2389exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002390 /*
2391 * Key attributes may have been returned by psa_get_key_attributes()
2392 * thus reset them as required.
2393 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002395
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 psa_destroy_key(key);
2397 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002398}
2399/* END_CASE */
2400
2401/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002402void raw_agreement_key_policy(int policy_usage,
2403 int policy_alg,
2404 int key_type_arg,
2405 data_t *key_data,
2406 int exercise_alg,
2407 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002408{
Ronald Cron5425a212020-08-04 14:58:35 +02002409 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002410 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002411 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002412 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002413 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002414 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002415
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 psa_set_key_usage_flags(&attributes, policy_usage);
2419 psa_set_key_algorithm(&attributes, policy_alg);
2420 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002421
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2423 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002426
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002428
2429exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 psa_key_derivation_abort(&operation);
2431 psa_destroy_key(key);
2432 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002433}
2434/* END_CASE */
2435
2436/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002437void copy_success(int source_usage_arg,
2438 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002439 int source_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 int type_arg, data_t *material,
2441 int copy_attributes,
2442 int target_usage_arg,
2443 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002444 int target_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 int expected_usage_arg,
2446 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002447{
Gilles Peskineca25db92019-04-19 11:43:08 +02002448 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2449 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002450 psa_key_usage_t expected_usage = expected_usage_arg;
2451 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002452 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302453 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2454 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002455 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2456 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002457 uint8_t *export_buffer = NULL;
2458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002460
Gilles Peskineca25db92019-04-19 11:43:08 +02002461 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2463 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2464 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2465 psa_set_key_type(&source_attributes, type_arg);
2466 psa_set_key_lifetime(&source_attributes, source_lifetime);
2467 PSA_ASSERT(psa_import_key(&source_attributes,
2468 material->x, material->len,
2469 &source_key));
2470 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002471
Gilles Peskineca25db92019-04-19 11:43:08 +02002472 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002474 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002475 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 if (target_usage_arg != -1) {
2479 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2480 }
2481 if (target_alg_arg != -1) {
2482 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2483 }
2484 if (target_alg2_arg != -1) {
2485 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2486 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002487
Archana8a180362021-07-05 02:18:48 +05302488
Gilles Peskine57ab7212019-01-28 13:03:09 +01002489 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 PSA_ASSERT(psa_copy_key(source_key,
2491 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002492
2493 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002495
2496 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2498 TEST_EQUAL(psa_get_key_type(&source_attributes),
2499 psa_get_key_type(&target_attributes));
2500 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2501 psa_get_key_bits(&target_attributes));
2502 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2503 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2504 TEST_EQUAL(expected_alg2,
2505 psa_get_key_enrollment_algorithm(&target_attributes));
2506 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002507 size_t length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002508 TEST_CALLOC(export_buffer, material->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2510 material->len, &length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002511 TEST_MEMORY_COMPARE(material->x, material->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002512 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002513 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 if (!psa_key_lifetime_is_external(target_lifetime)) {
2516 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302517 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 }
2519 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302520 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 }
Archana8a180362021-07-05 02:18:48 +05302522 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002525
2526exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002527 /*
2528 * Source and target key attributes may have been returned by
2529 * psa_get_key_attributes() thus reset them as required.
2530 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 psa_reset_key_attributes(&source_attributes);
2532 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002533
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 PSA_DONE();
2535 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002536}
2537/* END_CASE */
2538
2539/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002540void copy_fail(int source_usage_arg,
2541 int source_alg_arg, int source_alg2_arg,
2542 int source_lifetime_arg,
2543 int type_arg, data_t *material,
2544 int target_type_arg, int target_bits_arg,
2545 int target_usage_arg,
2546 int target_alg_arg, int target_alg2_arg,
2547 int target_id_arg, int target_lifetime_arg,
2548 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002549{
2550 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2551 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002552 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2553 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002555
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002557
2558 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2560 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2561 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2562 psa_set_key_type(&source_attributes, type_arg);
2563 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2564 PSA_ASSERT(psa_import_key(&source_attributes,
2565 material->x, material->len,
2566 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002567
2568 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 psa_set_key_id(&target_attributes, key_id);
2570 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2571 psa_set_key_type(&target_attributes, target_type_arg);
2572 psa_set_key_bits(&target_attributes, target_bits_arg);
2573 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2574 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2575 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002576
2577 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 TEST_EQUAL(psa_copy_key(source_key,
2579 &target_attributes, &target_key),
2580 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002583
Gilles Peskine4a644642019-05-03 17:14:08 +02002584exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 psa_reset_key_attributes(&source_attributes);
2586 psa_reset_key_attributes(&target_attributes);
2587 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002588}
2589/* END_CASE */
2590
2591/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002592void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002593{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002594 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002595 /* Test each valid way of initializing the object, except for `= {0}`, as
2596 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2597 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002598 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002600 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2601 psa_hash_operation_t zero;
2602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002604
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002605 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2607 PSA_ERROR_BAD_STATE);
2608 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2609 PSA_ERROR_BAD_STATE);
2610 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2611 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002612
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002613 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 PSA_ASSERT(psa_hash_abort(&func));
2615 PSA_ASSERT(psa_hash_abort(&init));
2616 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002617}
2618/* END_CASE */
2619
2620/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002621void hash_setup(int alg_arg,
2622 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002623{
2624 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002625 uint8_t *output = NULL;
2626 size_t output_size = 0;
2627 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002628 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002629 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002630 psa_status_t status;
2631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002633
Neil Armstrongedb20862022-02-07 15:47:44 +01002634 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 output_size = PSA_HASH_LENGTH(alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002636 TEST_CALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002637
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 status = psa_hash_compute(alg, NULL, 0,
2639 output, output_size, &output_length);
2640 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002641
2642 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 status = psa_hash_setup(&operation, alg);
2644 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002645
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002646 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002648
2649 /* If setup failed, reproduce the failure, so as to
2650 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 if (status != PSA_SUCCESS) {
2652 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2653 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002654
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002655 /* Now the operation object should be reusable. */
2656#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2658 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002659#endif
2660
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002661exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 mbedtls_free(output);
2663 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002664}
2665/* END_CASE */
2666
2667/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002668void hash_compute_fail(int alg_arg, data_t *input,
2669 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002670{
2671 psa_algorithm_t alg = alg_arg;
2672 uint8_t *output = NULL;
2673 size_t output_size = output_size_arg;
2674 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002675 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002676 psa_status_t expected_status = expected_status_arg;
2677 psa_status_t status;
2678
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002679 TEST_CALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002680
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002682
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002683 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 status = psa_hash_compute(alg, input->x, input->len,
2685 output, output_size, &output_length);
2686 TEST_EQUAL(status, expected_status);
2687 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002688
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002689 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 status = psa_hash_setup(&operation, alg);
2691 if (status == PSA_SUCCESS) {
2692 status = psa_hash_update(&operation, input->x, input->len);
2693 if (status == PSA_SUCCESS) {
2694 status = psa_hash_finish(&operation, output, output_size,
2695 &output_length);
2696 if (status == PSA_SUCCESS) {
2697 TEST_LE_U(output_length, output_size);
2698 } else {
2699 TEST_EQUAL(status, expected_status);
2700 }
2701 } else {
2702 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002703 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 } else {
2705 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002706 }
2707
Gilles Peskine0a749c82019-11-28 19:33:58 +01002708exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 PSA_ASSERT(psa_hash_abort(&operation));
2710 mbedtls_free(output);
2711 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002712}
2713/* END_CASE */
2714
2715/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002716void hash_compare_fail(int alg_arg, data_t *input,
2717 data_t *reference_hash,
2718 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002719{
2720 psa_algorithm_t alg = alg_arg;
2721 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002722 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002723 psa_status_t status;
2724
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002726
Neil Armstrong55a1be12022-02-07 11:23:20 +01002727 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 status = psa_hash_compare(alg, input->x, input->len,
2729 reference_hash->x, reference_hash->len);
2730 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002731
Neil Armstrong55a1be12022-02-07 11:23:20 +01002732 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 status = psa_hash_setup(&operation, alg);
2734 if (status == PSA_SUCCESS) {
2735 status = psa_hash_update(&operation, input->x, input->len);
2736 if (status == PSA_SUCCESS) {
2737 status = psa_hash_verify(&operation, reference_hash->x,
2738 reference_hash->len);
2739 TEST_EQUAL(status, expected_status);
2740 } else {
2741 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002742 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 } else {
2744 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002745 }
2746
Gilles Peskine88e08462020-01-28 20:43:00 +01002747exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 PSA_ASSERT(psa_hash_abort(&operation));
2749 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002750}
2751/* END_CASE */
2752
2753/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002754void hash_compute_compare(int alg_arg, data_t *input,
2755 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002756{
2757 psa_algorithm_t alg = alg_arg;
2758 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2759 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002760 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002761 size_t i;
2762
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002764
Neil Armstrongca30a002022-02-07 11:40:23 +01002765 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2767 output, PSA_HASH_LENGTH(alg),
2768 &output_length));
2769 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002770 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002771 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002772
Neil Armstrongca30a002022-02-07 11:40:23 +01002773 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 PSA_ASSERT(psa_hash_setup(&operation, alg));
2775 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2776 PSA_ASSERT(psa_hash_finish(&operation, output,
2777 PSA_HASH_LENGTH(alg),
2778 &output_length));
2779 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002780 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002781 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002782
2783 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2785 output, sizeof(output),
2786 &output_length));
2787 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002788 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002789 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002790
Neil Armstrongca30a002022-02-07 11:40:23 +01002791 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002792 PSA_ASSERT(psa_hash_setup(&operation, alg));
2793 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2794 PSA_ASSERT(psa_hash_finish(&operation, output,
2795 sizeof(output), &output_length));
2796 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002797 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002798 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002799
2800 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2802 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002803
Neil Armstrongca30a002022-02-07 11:40:23 +01002804 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 PSA_ASSERT(psa_hash_setup(&operation, alg));
2806 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2807 PSA_ASSERT(psa_hash_verify(&operation, output,
2808 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002809
2810 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2812 output, output_length + 1),
2813 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002814
Neil Armstrongca30a002022-02-07 11:40:23 +01002815 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 PSA_ASSERT(psa_hash_setup(&operation, alg));
2817 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2818 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2819 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002820
2821 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2823 output, output_length - 1),
2824 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002825
Neil Armstrongca30a002022-02-07 11:40:23 +01002826 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 PSA_ASSERT(psa_hash_setup(&operation, alg));
2828 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2829 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2830 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002831
Gilles Peskine0a749c82019-11-28 19:33:58 +01002832 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 for (i = 0; i < output_length; i++) {
2834 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002835 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002836
2837 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2839 output, output_length),
2840 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002841
2842 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 PSA_ASSERT(psa_hash_setup(&operation, alg));
2844 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2845 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2846 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002847
Gilles Peskine0a749c82019-11-28 19:33:58 +01002848 output[i] ^= 1;
2849 }
2850
2851exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 PSA_ASSERT(psa_hash_abort(&operation));
2853 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002854}
2855/* END_CASE */
2856
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002857/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002858void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002859{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002860 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002861 unsigned char input[] = "";
2862 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002863 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002864 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2865 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2867 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002868 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002869 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002870 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002871
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002873
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002874 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 PSA_ASSERT(psa_hash_setup(&operation, alg));
2876 ASSERT_OPERATION_IS_ACTIVE(operation);
2877 TEST_EQUAL(psa_hash_setup(&operation, alg),
2878 PSA_ERROR_BAD_STATE);
2879 ASSERT_OPERATION_IS_INACTIVE(operation);
2880 PSA_ASSERT(psa_hash_abort(&operation));
2881 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002882
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002883 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2885 PSA_ERROR_BAD_STATE);
2886 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002887
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002888 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002889 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002890 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 ASSERT_OPERATION_IS_ACTIVE(operation);
2892 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2893 PSA_ERROR_BAD_STATE);
2894 ASSERT_OPERATION_IS_INACTIVE(operation);
2895 PSA_ASSERT(psa_hash_abort(&operation));
2896 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002897
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002898 /* Call update 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_update(&operation, input, sizeof(input)),
2903 PSA_ERROR_BAD_STATE);
2904 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002905
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002906 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 TEST_EQUAL(psa_hash_verify(&operation,
2908 valid_hash, sizeof(valid_hash)),
2909 PSA_ERROR_BAD_STATE);
2910 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002911
2912 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 PSA_ASSERT(psa_hash_setup(&operation, alg));
2914 PSA_ASSERT(psa_hash_finish(&operation,
2915 hash, sizeof(hash), &hash_len));
2916 TEST_EQUAL(psa_hash_verify(&operation,
2917 valid_hash, sizeof(valid_hash)),
2918 PSA_ERROR_BAD_STATE);
2919 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002920
2921 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 PSA_ASSERT(psa_hash_setup(&operation, alg));
2923 ASSERT_OPERATION_IS_ACTIVE(operation);
2924 PSA_ASSERT(psa_hash_verify(&operation,
2925 valid_hash, sizeof(valid_hash)));
2926 ASSERT_OPERATION_IS_INACTIVE(operation);
2927 TEST_EQUAL(psa_hash_verify(&operation,
2928 valid_hash, sizeof(valid_hash)),
2929 PSA_ERROR_BAD_STATE);
2930 ASSERT_OPERATION_IS_INACTIVE(operation);
2931 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002932
2933 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 TEST_EQUAL(psa_hash_finish(&operation,
2935 hash, sizeof(hash), &hash_len),
2936 PSA_ERROR_BAD_STATE);
2937 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002938
2939 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 PSA_ASSERT(psa_hash_setup(&operation, alg));
2941 PSA_ASSERT(psa_hash_finish(&operation,
2942 hash, sizeof(hash), &hash_len));
2943 TEST_EQUAL(psa_hash_finish(&operation,
2944 hash, sizeof(hash), &hash_len),
2945 PSA_ERROR_BAD_STATE);
2946 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002947
2948 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 PSA_ASSERT(psa_hash_setup(&operation, alg));
2950 PSA_ASSERT(psa_hash_verify(&operation,
2951 valid_hash, sizeof(valid_hash)));
2952 TEST_EQUAL(psa_hash_finish(&operation,
2953 hash, sizeof(hash), &hash_len),
2954 PSA_ERROR_BAD_STATE);
2955 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002956
2957exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002959}
2960/* END_CASE */
2961
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002962/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002963void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002964{
2965 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002966 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2967 * appended to it */
2968 unsigned char hash[] = {
2969 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2970 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2972 };
2973 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002974 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002975
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002977
itayzafrir27e69452018-11-01 14:26:34 +02002978 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 PSA_ASSERT(psa_hash_setup(&operation, alg));
2980 ASSERT_OPERATION_IS_ACTIVE(operation);
2981 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2982 PSA_ERROR_INVALID_SIGNATURE);
2983 ASSERT_OPERATION_IS_INACTIVE(operation);
2984 PSA_ASSERT(psa_hash_abort(&operation));
2985 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002986
itayzafrir27e69452018-11-01 14:26:34 +02002987 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 PSA_ASSERT(psa_hash_setup(&operation, alg));
2989 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
2990 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03002991
itayzafrir27e69452018-11-01 14:26:34 +02002992 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 PSA_ASSERT(psa_hash_setup(&operation, alg));
2994 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
2995 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03002996
itayzafrirec93d302018-10-18 18:01:10 +03002997exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03002999}
3000/* END_CASE */
3001
Ronald Cronee414c72021-03-18 18:50:08 +01003002/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003003void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03003004{
3005 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003006 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003008 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003009 size_t hash_len;
3010
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03003012
itayzafrir58028322018-10-25 10:22:01 +03003013 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 PSA_ASSERT(psa_hash_setup(&operation, alg));
3015 TEST_EQUAL(psa_hash_finish(&operation,
3016 hash, expected_size - 1, &hash_len),
3017 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003018
3019exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003021}
3022/* END_CASE */
3023
Ronald Cronee414c72021-03-18 18:50:08 +01003024/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003025void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003026{
3027 psa_algorithm_t alg = PSA_ALG_SHA_256;
3028 unsigned char hash[PSA_HASH_MAX_SIZE];
3029 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3030 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3031 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3032 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3033 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3034 size_t hash_len;
3035
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 PSA_ASSERT(psa_crypto_init());
3037 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003038
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3040 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3041 PSA_ASSERT(psa_hash_finish(&op_finished,
3042 hash, sizeof(hash), &hash_len));
3043 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3044 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003045
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3047 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003048
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3050 PSA_ASSERT(psa_hash_finish(&op_init,
3051 hash, sizeof(hash), &hash_len));
3052 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3053 PSA_ASSERT(psa_hash_finish(&op_finished,
3054 hash, sizeof(hash), &hash_len));
3055 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3056 PSA_ASSERT(psa_hash_finish(&op_aborted,
3057 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003058
3059exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 psa_hash_abort(&op_source);
3061 psa_hash_abort(&op_init);
3062 psa_hash_abort(&op_setup);
3063 psa_hash_abort(&op_finished);
3064 psa_hash_abort(&op_aborted);
3065 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003066}
3067/* END_CASE */
3068
Ronald Cronee414c72021-03-18 18:50:08 +01003069/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003070void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003071{
3072 psa_algorithm_t alg = PSA_ALG_SHA_256;
3073 unsigned char hash[PSA_HASH_MAX_SIZE];
3074 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3075 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3076 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3077 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3078 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3079 size_t hash_len;
3080
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3084 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3085 PSA_ASSERT(psa_hash_finish(&op_finished,
3086 hash, sizeof(hash), &hash_len));
3087 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3088 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003089
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3091 PSA_ASSERT(psa_hash_finish(&op_target,
3092 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003093
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3095 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3096 PSA_ERROR_BAD_STATE);
3097 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3098 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003099
3100exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 psa_hash_abort(&op_target);
3102 psa_hash_abort(&op_init);
3103 psa_hash_abort(&op_setup);
3104 psa_hash_abort(&op_finished);
3105 psa_hash_abort(&op_aborted);
3106 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003107}
3108/* END_CASE */
3109
itayzafrir58028322018-10-25 10:22:01 +03003110/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003111void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003112{
Jaeden Amero252ef282019-02-15 14:05:35 +00003113 const uint8_t input[1] = { 0 };
3114
Jaeden Amero769ce272019-01-04 11:48:03 +00003115 /* Test each valid way of initializing the object, except for `= {0}`, as
3116 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3117 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003118 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003120 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3121 psa_mac_operation_t zero;
3122
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003124
Jaeden Amero252ef282019-02-15 14:05:35 +00003125 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 TEST_EQUAL(psa_mac_update(&func,
3127 input, sizeof(input)),
3128 PSA_ERROR_BAD_STATE);
3129 TEST_EQUAL(psa_mac_update(&init,
3130 input, sizeof(input)),
3131 PSA_ERROR_BAD_STATE);
3132 TEST_EQUAL(psa_mac_update(&zero,
3133 input, sizeof(input)),
3134 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003135
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003136 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 PSA_ASSERT(psa_mac_abort(&func));
3138 PSA_ASSERT(psa_mac_abort(&init));
3139 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003140}
3141/* END_CASE */
3142
3143/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003144void mac_setup(int key_type_arg,
3145 data_t *key,
3146 int alg_arg,
3147 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003148{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003149 psa_key_type_t key_type = key_type_arg;
3150 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003151 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003152 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003153 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3154#if defined(KNOWN_SUPPORTED_MAC_ALG)
3155 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3156#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003157
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003159
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3161 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003162 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 }
3164 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003165
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003166 /* The operation object should be reusable. */
3167#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3169 smoke_test_key_data,
3170 sizeof(smoke_test_key_data),
3171 KNOWN_SUPPORTED_MAC_ALG,
3172 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003173 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003174 }
3175 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003176#endif
3177
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003178exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003180}
3181/* END_CASE */
3182
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003183/* 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 +01003184void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003185{
Ronald Cron5425a212020-08-04 14:58:35 +02003186 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003187 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3188 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003189 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003190 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3191 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003192 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3193 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003194 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003195 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3196 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3197 size_t sign_mac_length = 0;
3198 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3199 const uint8_t verify_mac[] = {
3200 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3201 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3203 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003204
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 PSA_ASSERT(psa_crypto_init());
3206 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3207 psa_set_key_algorithm(&attributes, alg);
3208 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003209
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3211 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003212
Jaeden Amero252ef282019-02-15 14:05:35 +00003213 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3215 PSA_ERROR_BAD_STATE);
3216 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003217
3218 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003219 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3220 &sign_mac_length),
3221 PSA_ERROR_BAD_STATE);
3222 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003223
3224 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003225 TEST_EQUAL(psa_mac_verify_finish(&operation,
3226 verify_mac, sizeof(verify_mac)),
3227 PSA_ERROR_BAD_STATE);
3228 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003229
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003230 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003231 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3232 ASSERT_OPERATION_IS_ACTIVE(operation);
3233 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3234 PSA_ERROR_BAD_STATE);
3235 ASSERT_OPERATION_IS_INACTIVE(operation);
3236 PSA_ASSERT(psa_mac_abort(&operation));
3237 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003238
Jaeden Amero252ef282019-02-15 14:05:35 +00003239 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003240 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3241 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3242 PSA_ASSERT(psa_mac_sign_finish(&operation,
3243 sign_mac, sizeof(sign_mac),
3244 &sign_mac_length));
3245 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3246 PSA_ERROR_BAD_STATE);
3247 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003248
3249 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3251 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3252 PSA_ASSERT(psa_mac_verify_finish(&operation,
3253 verify_mac, sizeof(verify_mac)));
3254 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3255 PSA_ERROR_BAD_STATE);
3256 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003257
3258 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003259 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3260 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3261 PSA_ASSERT(psa_mac_sign_finish(&operation,
3262 sign_mac, sizeof(sign_mac),
3263 &sign_mac_length));
3264 TEST_EQUAL(psa_mac_sign_finish(&operation,
3265 sign_mac, sizeof(sign_mac),
3266 &sign_mac_length),
3267 PSA_ERROR_BAD_STATE);
3268 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003269
3270 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3272 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3273 PSA_ASSERT(psa_mac_verify_finish(&operation,
3274 verify_mac, sizeof(verify_mac)));
3275 TEST_EQUAL(psa_mac_verify_finish(&operation,
3276 verify_mac, sizeof(verify_mac)),
3277 PSA_ERROR_BAD_STATE);
3278 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003279
3280 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003281 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3282 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3283 ASSERT_OPERATION_IS_ACTIVE(operation);
3284 TEST_EQUAL(psa_mac_verify_finish(&operation,
3285 verify_mac, sizeof(verify_mac)),
3286 PSA_ERROR_BAD_STATE);
3287 ASSERT_OPERATION_IS_INACTIVE(operation);
3288 PSA_ASSERT(psa_mac_abort(&operation));
3289 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003290
3291 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3293 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3294 ASSERT_OPERATION_IS_ACTIVE(operation);
3295 TEST_EQUAL(psa_mac_sign_finish(&operation,
3296 sign_mac, sizeof(sign_mac),
3297 &sign_mac_length),
3298 PSA_ERROR_BAD_STATE);
3299 ASSERT_OPERATION_IS_INACTIVE(operation);
3300 PSA_ASSERT(psa_mac_abort(&operation));
3301 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003302
Gilles Peskine449bd832023-01-11 14:50:10 +01003303 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003304
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003305exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003307}
3308/* END_CASE */
3309
3310/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003311void mac_sign_verify_multi(int key_type_arg,
3312 data_t *key_data,
3313 int alg_arg,
3314 data_t *input,
3315 int is_verify,
3316 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003317{
3318 size_t data_part_len = 0;
3319
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003321 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003323
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 if (mac_multipart_internal_func(key_type_arg, key_data,
3325 alg_arg,
3326 input, data_part_len,
3327 expected_mac,
3328 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003329 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003331
3332 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003333 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003334
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 if (mac_multipart_internal_func(key_type_arg, key_data,
3336 alg_arg,
3337 input, data_part_len,
3338 expected_mac,
3339 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003340 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003342 }
3343
3344 /* Goto is required to silence warnings about unused labels, as we
3345 * don't actually do any test assertions in this function. */
3346 goto exit;
3347}
3348/* END_CASE */
3349
3350/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003351void mac_sign(int key_type_arg,
3352 data_t *key_data,
3353 int alg_arg,
3354 data_t *input,
3355 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003356{
Ronald Cron5425a212020-08-04 14:58:35 +02003357 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003358 psa_key_type_t key_type = key_type_arg;
3359 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003360 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003361 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003362 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003363 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003364 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003365 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003366 const size_t output_sizes_to_test[] = {
3367 0,
3368 1,
3369 expected_mac->len - 1,
3370 expected_mac->len,
3371 expected_mac->len + 1,
3372 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003373
Gilles Peskine449bd832023-01-11 14:50:10 +01003374 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003375 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003376 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003377
Gilles Peskine449bd832023-01-11 14:50:10 +01003378 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003379
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3381 psa_set_key_algorithm(&attributes, alg);
3382 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003383
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3385 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003386
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003388 const size_t output_size = output_sizes_to_test[i];
3389 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003390 (output_size >= expected_mac->len ? PSA_SUCCESS :
3391 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003392
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 mbedtls_test_set_step(output_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003394 TEST_CALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003395
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003396 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003397 TEST_EQUAL(psa_mac_compute(key, alg,
3398 input->x, input->len,
3399 actual_mac, output_size, &mac_length),
3400 expected_status);
3401 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003402 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003403 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003404 }
3405
Gilles Peskine449bd832023-01-11 14:50:10 +01003406 if (output_size > 0) {
3407 memset(actual_mac, 0, output_size);
3408 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003409
3410 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003411 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3412 PSA_ASSERT(psa_mac_update(&operation,
3413 input->x, input->len));
3414 TEST_EQUAL(psa_mac_sign_finish(&operation,
3415 actual_mac, output_size,
3416 &mac_length),
3417 expected_status);
3418 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003419
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003421 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003422 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003423 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003425 actual_mac = NULL;
3426 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003427
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003428exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003429 psa_mac_abort(&operation);
3430 psa_destroy_key(key);
3431 PSA_DONE();
3432 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003433}
3434/* END_CASE */
3435
3436/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003437void mac_verify(int key_type_arg,
3438 data_t *key_data,
3439 int alg_arg,
3440 data_t *input,
3441 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003442{
Ronald Cron5425a212020-08-04 14:58:35 +02003443 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003444 psa_key_type_t key_type = key_type_arg;
3445 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003446 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003447 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003448 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003449
Gilles Peskine449bd832023-01-11 14:50:10 +01003450 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003451
Gilles Peskine449bd832023-01-11 14:50:10 +01003452 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003453
Gilles Peskine449bd832023-01-11 14:50:10 +01003454 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3455 psa_set_key_algorithm(&attributes, alg);
3456 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003457
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3459 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003460
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003461 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3463 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003464
3465 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003466 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3467 PSA_ASSERT(psa_mac_update(&operation,
3468 input->x, input->len));
3469 PSA_ASSERT(psa_mac_verify_finish(&operation,
3470 expected_mac->x,
3471 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003472
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003473 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003474 TEST_EQUAL(psa_mac_verify(key, alg,
3475 input->x, input->len,
3476 expected_mac->x,
3477 expected_mac->len - 1),
3478 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003479
3480 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3482 PSA_ASSERT(psa_mac_update(&operation,
3483 input->x, input->len));
3484 TEST_EQUAL(psa_mac_verify_finish(&operation,
3485 expected_mac->x,
3486 expected_mac->len - 1),
3487 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003488
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003489 /* Test a MAC that's too long, one-shot case. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003490 TEST_CALLOC(perturbed_mac, expected_mac->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01003491 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3492 TEST_EQUAL(psa_mac_verify(key, alg,
3493 input->x, input->len,
3494 perturbed_mac, expected_mac->len + 1),
3495 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003496
3497 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3499 PSA_ASSERT(psa_mac_update(&operation,
3500 input->x, input->len));
3501 TEST_EQUAL(psa_mac_verify_finish(&operation,
3502 perturbed_mac,
3503 expected_mac->len + 1),
3504 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003505
3506 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003507 for (size_t i = 0; i < expected_mac->len; i++) {
3508 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003509 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003510
Gilles Peskine449bd832023-01-11 14:50:10 +01003511 TEST_EQUAL(psa_mac_verify(key, alg,
3512 input->x, input->len,
3513 perturbed_mac, expected_mac->len),
3514 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003515
Gilles Peskine449bd832023-01-11 14:50:10 +01003516 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3517 PSA_ASSERT(psa_mac_update(&operation,
3518 input->x, input->len));
3519 TEST_EQUAL(psa_mac_verify_finish(&operation,
3520 perturbed_mac,
3521 expected_mac->len),
3522 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003523 perturbed_mac[i] ^= 1;
3524 }
3525
Gilles Peskine8c9def32018-02-08 10:02:12 +01003526exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003527 psa_mac_abort(&operation);
3528 psa_destroy_key(key);
3529 PSA_DONE();
3530 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003531}
3532/* END_CASE */
3533
3534/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003535void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003536{
Jaeden Ameroab439972019-02-15 14:12:05 +00003537 const uint8_t input[1] = { 0 };
3538 unsigned char output[1] = { 0 };
3539 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003540 /* Test each valid way of initializing the object, except for `= {0}`, as
3541 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3542 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003543 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003544 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003545 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3546 psa_cipher_operation_t zero;
3547
Gilles Peskine449bd832023-01-11 14:50:10 +01003548 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003549
Jaeden Ameroab439972019-02-15 14:12:05 +00003550 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003551 TEST_EQUAL(psa_cipher_update(&func,
3552 input, sizeof(input),
3553 output, sizeof(output),
3554 &output_length),
3555 PSA_ERROR_BAD_STATE);
3556 TEST_EQUAL(psa_cipher_update(&init,
3557 input, sizeof(input),
3558 output, sizeof(output),
3559 &output_length),
3560 PSA_ERROR_BAD_STATE);
3561 TEST_EQUAL(psa_cipher_update(&zero,
3562 input, sizeof(input),
3563 output, sizeof(output),
3564 &output_length),
3565 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003566
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003567 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003568 PSA_ASSERT(psa_cipher_abort(&func));
3569 PSA_ASSERT(psa_cipher_abort(&init));
3570 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003571}
3572/* END_CASE */
3573
3574/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003575void cipher_setup(int key_type_arg,
3576 data_t *key,
3577 int alg_arg,
3578 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003579{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003580 psa_key_type_t key_type = key_type_arg;
3581 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003582 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003583 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003584 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003585#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003586 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3587#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003588
Gilles Peskine449bd832023-01-11 14:50:10 +01003589 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003590
Gilles Peskine449bd832023-01-11 14:50:10 +01003591 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3592 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003593 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003594 }
3595 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003596
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003597 /* The operation object should be reusable. */
3598#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003599 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3600 smoke_test_key_data,
3601 sizeof(smoke_test_key_data),
3602 KNOWN_SUPPORTED_CIPHER_ALG,
3603 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003604 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003605 }
3606 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003607#endif
3608
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003609exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003610 psa_cipher_abort(&operation);
3611 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003612}
3613/* END_CASE */
3614
Ronald Cronee414c72021-03-18 18:50:08 +01003615/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003616void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003617{
Ronald Cron5425a212020-08-04 14:58:35 +02003618 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003619 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3620 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003621 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003622 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003623 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003624 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003625 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003626 0xaa, 0xaa, 0xaa, 0xaa
3627 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003628 const uint8_t text[] = {
3629 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003630 0xbb, 0xbb, 0xbb, 0xbb
3631 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003632 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003633 size_t length = 0;
3634
Gilles Peskine449bd832023-01-11 14:50:10 +01003635 PSA_ASSERT(psa_crypto_init());
3636 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3637 psa_set_key_algorithm(&attributes, alg);
3638 psa_set_key_type(&attributes, key_type);
3639 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3640 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003641
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003642 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003643 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3644 ASSERT_OPERATION_IS_ACTIVE(operation);
3645 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3646 PSA_ERROR_BAD_STATE);
3647 ASSERT_OPERATION_IS_INACTIVE(operation);
3648 PSA_ASSERT(psa_cipher_abort(&operation));
3649 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003650
3651 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003652 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3653 ASSERT_OPERATION_IS_ACTIVE(operation);
3654 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3655 PSA_ERROR_BAD_STATE);
3656 ASSERT_OPERATION_IS_INACTIVE(operation);
3657 PSA_ASSERT(psa_cipher_abort(&operation));
3658 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003659
Jaeden Ameroab439972019-02-15 14:12:05 +00003660 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003661 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3662 buffer, sizeof(buffer),
3663 &length),
3664 PSA_ERROR_BAD_STATE);
3665 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003666
3667 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003668 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3669 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3670 buffer, sizeof(buffer),
3671 &length));
3672 ASSERT_OPERATION_IS_ACTIVE(operation);
3673 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3674 buffer, sizeof(buffer),
3675 &length),
3676 PSA_ERROR_BAD_STATE);
3677 ASSERT_OPERATION_IS_INACTIVE(operation);
3678 PSA_ASSERT(psa_cipher_abort(&operation));
3679 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003680
3681 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003682 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3683 PSA_ASSERT(psa_cipher_set_iv(&operation,
3684 iv, sizeof(iv)));
3685 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3686 buffer, sizeof(buffer),
3687 &length),
3688 PSA_ERROR_BAD_STATE);
3689 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003690
3691 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003692 TEST_EQUAL(psa_cipher_set_iv(&operation,
3693 iv, sizeof(iv)),
3694 PSA_ERROR_BAD_STATE);
3695 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003696
3697 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003698 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3699 PSA_ASSERT(psa_cipher_set_iv(&operation,
3700 iv, sizeof(iv)));
3701 ASSERT_OPERATION_IS_ACTIVE(operation);
3702 TEST_EQUAL(psa_cipher_set_iv(&operation,
3703 iv, sizeof(iv)),
3704 PSA_ERROR_BAD_STATE);
3705 ASSERT_OPERATION_IS_INACTIVE(operation);
3706 PSA_ASSERT(psa_cipher_abort(&operation));
3707 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003708
3709 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003710 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3711 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3712 buffer, sizeof(buffer),
3713 &length));
3714 TEST_EQUAL(psa_cipher_set_iv(&operation,
3715 iv, sizeof(iv)),
3716 PSA_ERROR_BAD_STATE);
3717 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003718
3719 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003720 TEST_EQUAL(psa_cipher_update(&operation,
3721 text, sizeof(text),
3722 buffer, sizeof(buffer),
3723 &length),
3724 PSA_ERROR_BAD_STATE);
3725 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003726
3727 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003728 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3729 ASSERT_OPERATION_IS_ACTIVE(operation);
3730 TEST_EQUAL(psa_cipher_update(&operation,
3731 text, sizeof(text),
3732 buffer, sizeof(buffer),
3733 &length),
3734 PSA_ERROR_BAD_STATE);
3735 ASSERT_OPERATION_IS_INACTIVE(operation);
3736 PSA_ASSERT(psa_cipher_abort(&operation));
3737 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003738
3739 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003740 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3741 PSA_ASSERT(psa_cipher_set_iv(&operation,
3742 iv, sizeof(iv)));
3743 PSA_ASSERT(psa_cipher_finish(&operation,
3744 buffer, sizeof(buffer), &length));
3745 TEST_EQUAL(psa_cipher_update(&operation,
3746 text, sizeof(text),
3747 buffer, sizeof(buffer),
3748 &length),
3749 PSA_ERROR_BAD_STATE);
3750 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003751
3752 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003753 TEST_EQUAL(psa_cipher_finish(&operation,
3754 buffer, sizeof(buffer), &length),
3755 PSA_ERROR_BAD_STATE);
3756 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003757
3758 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003759 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003760 /* Not calling update means we are encrypting an empty buffer, which is OK
3761 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003762 ASSERT_OPERATION_IS_ACTIVE(operation);
3763 TEST_EQUAL(psa_cipher_finish(&operation,
3764 buffer, sizeof(buffer), &length),
3765 PSA_ERROR_BAD_STATE);
3766 ASSERT_OPERATION_IS_INACTIVE(operation);
3767 PSA_ASSERT(psa_cipher_abort(&operation));
3768 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003769
3770 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003771 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3772 PSA_ASSERT(psa_cipher_set_iv(&operation,
3773 iv, sizeof(iv)));
3774 PSA_ASSERT(psa_cipher_finish(&operation,
3775 buffer, sizeof(buffer), &length));
3776 TEST_EQUAL(psa_cipher_finish(&operation,
3777 buffer, sizeof(buffer), &length),
3778 PSA_ERROR_BAD_STATE);
3779 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003780
Gilles Peskine449bd832023-01-11 14:50:10 +01003781 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003782
Jaeden Ameroab439972019-02-15 14:12:05 +00003783exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003784 psa_cipher_abort(&operation);
3785 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003786}
3787/* END_CASE */
3788
3789/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003790void cipher_encrypt_fail(int alg_arg,
3791 int key_type_arg,
3792 data_t *key_data,
3793 data_t *input,
3794 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003795{
Ronald Cron5425a212020-08-04 14:58:35 +02003796 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003797 psa_status_t status;
3798 psa_key_type_t key_type = key_type_arg;
3799 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003800 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003801 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003802 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3803 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003804 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003805 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003806 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003807 size_t function_output_length;
3808 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003809 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3810
Gilles Peskine449bd832023-01-11 14:50:10 +01003811 if (PSA_ERROR_BAD_STATE != expected_status) {
3812 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003813
Gilles Peskine449bd832023-01-11 14:50:10 +01003814 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3815 psa_set_key_algorithm(&attributes, alg);
3816 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003817
Gilles Peskine449bd832023-01-11 14:50:10 +01003818 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3819 input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003820 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003821
Gilles Peskine449bd832023-01-11 14:50:10 +01003822 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3823 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003824 }
3825
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003826 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003827 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3828 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003829
Gilles Peskine449bd832023-01-11 14:50:10 +01003830 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003831
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003832 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003833 status = psa_cipher_encrypt_setup(&operation, key, alg);
3834 if (status == PSA_SUCCESS) {
3835 if (alg != PSA_ALG_ECB_NO_PADDING) {
3836 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3837 iv, iv_size,
3838 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003839 }
3840
Gilles Peskine449bd832023-01-11 14:50:10 +01003841 status = psa_cipher_update(&operation, input->x, input->len,
3842 output, output_buffer_size,
3843 &function_output_length);
3844 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003845 output_length += function_output_length;
3846
Gilles Peskine449bd832023-01-11 14:50:10 +01003847 status = psa_cipher_finish(&operation, output + output_length,
3848 output_buffer_size - output_length,
3849 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003850
Gilles Peskine449bd832023-01-11 14:50:10 +01003851 TEST_EQUAL(status, expected_status);
3852 } else {
3853 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003854 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003855 } else {
3856 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003857 }
3858
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003859exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003860 psa_cipher_abort(&operation);
3861 mbedtls_free(output);
3862 psa_destroy_key(key);
3863 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003864}
3865/* END_CASE */
3866
3867/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003868void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3869 data_t *input, int iv_length,
3870 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003871{
3872 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3873 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3874 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3875 size_t output_buffer_size = 0;
3876 unsigned char *output = NULL;
3877
Gilles Peskine449bd832023-01-11 14:50:10 +01003878 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003879 TEST_CALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003880
Gilles Peskine449bd832023-01-11 14:50:10 +01003881 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003882
Gilles Peskine449bd832023-01-11 14:50:10 +01003883 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3884 psa_set_key_algorithm(&attributes, alg);
3885 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003886
Gilles Peskine449bd832023-01-11 14:50:10 +01003887 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3888 &key));
3889 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3890 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3891 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003892
3893exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003894 psa_cipher_abort(&operation);
3895 mbedtls_free(output);
3896 psa_destroy_key(key);
3897 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003898}
3899/* END_CASE */
3900
3901/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003902void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3903 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003904{
3905 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3906 psa_key_type_t key_type = key_type_arg;
3907 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003908 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3909 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003910 unsigned char *output = NULL;
3911 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003912 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003913 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3914
Gilles Peskine449bd832023-01-11 14:50:10 +01003915 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003916
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003917 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003918 TEST_LE_U(ciphertext->len,
3919 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3920 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3921 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3922 TEST_LE_U(plaintext->len,
3923 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3924 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3925 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003926
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003927
3928 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003929 psa_set_key_usage_flags(&attributes,
3930 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3931 psa_set_key_algorithm(&attributes, alg);
3932 psa_set_key_type(&attributes, key_type);
3933 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3934 &key));
3935 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3936 plaintext->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003937 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003938
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003939 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003940 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3941 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3942 PSA_ERROR_BAD_STATE);
3943 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3944 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3945 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003946
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003947 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003948 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3949 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3950 &length),
3951 PSA_ERROR_BAD_STATE);
3952 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3953 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3954 &length),
3955 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003956
Gilles Peskine286c3142022-04-20 17:09:38 +02003957 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003959 output_length = 0;
3960 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003961 PSA_ASSERT(psa_cipher_update(&operation,
3962 plaintext->x, plaintext->len,
3963 output, output_buffer_size,
3964 &length));
3965 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003966 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003967 PSA_ASSERT(psa_cipher_finish(&operation,
3968 mbedtls_buffer_offset(output, output_length),
3969 output_buffer_size - output_length,
3970 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003971 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003972 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003973 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003974
Gilles Peskine286c3142022-04-20 17:09:38 +02003975 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003976 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003977 output_length = 0;
3978 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003979 PSA_ASSERT(psa_cipher_update(&operation,
3980 ciphertext->x, ciphertext->len,
3981 output, output_buffer_size,
3982 &length));
3983 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003984 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003985 PSA_ASSERT(psa_cipher_finish(&operation,
3986 mbedtls_buffer_offset(output, output_length),
3987 output_buffer_size - output_length,
3988 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003989 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003990 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003991 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003992
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003993 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003994 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003995 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
3996 output, output_buffer_size,
3997 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003998 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003999 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004000
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004001 /* One-shot decryption */
4002 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004003 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
4004 output, output_buffer_size,
4005 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004006 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004007 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004008
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004009exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004010 PSA_ASSERT(psa_cipher_abort(&operation));
4011 mbedtls_free(output);
4012 psa_cipher_abort(&operation);
4013 psa_destroy_key(key);
4014 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004015}
4016/* END_CASE */
4017
4018/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004019void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004020{
4021 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4022 psa_algorithm_t alg = alg_arg;
4023 psa_key_type_t key_type = key_type_arg;
4024 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4025 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4026 psa_status_t status;
4027
Gilles Peskine449bd832023-01-11 14:50:10 +01004028 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004029
Gilles Peskine449bd832023-01-11 14:50:10 +01004030 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4031 psa_set_key_algorithm(&attributes, alg);
4032 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004033
4034 /* Usage of either of these two size macros would cause divide by zero
4035 * with incorrect key types previously. Input length should be irrelevant
4036 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004037 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4038 0);
4039 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004040
4041
Gilles Peskine449bd832023-01-11 14:50:10 +01004042 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4043 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004044
4045 /* Should fail due to invalid alg type (to support invalid key type).
4046 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004047 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004048
Gilles Peskine449bd832023-01-11 14:50:10 +01004049 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004050
4051exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004052 psa_cipher_abort(&operation);
4053 psa_destroy_key(key);
4054 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004055}
4056/* END_CASE */
4057
4058/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004059void cipher_encrypt_validation(int alg_arg,
4060 int key_type_arg,
4061 data_t *key_data,
4062 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004063{
4064 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4065 psa_key_type_t key_type = key_type_arg;
4066 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004067 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004068 unsigned char *output1 = NULL;
4069 size_t output1_buffer_size = 0;
4070 size_t output1_length = 0;
4071 unsigned char *output2 = NULL;
4072 size_t output2_buffer_size = 0;
4073 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004074 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004075 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004076 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004077
Gilles Peskine449bd832023-01-11 14:50:10 +01004078 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004079
Gilles Peskine449bd832023-01-11 14:50:10 +01004080 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4081 psa_set_key_algorithm(&attributes, alg);
4082 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004083
Gilles Peskine449bd832023-01-11 14:50:10 +01004084 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4085 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4086 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004087 TEST_CALLOC(output1, output1_buffer_size);
4088 TEST_CALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004089
Gilles Peskine449bd832023-01-11 14:50:10 +01004090 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4091 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004092
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004093 /* The one-shot cipher encryption uses generated iv so validating
4094 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004095 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4096 output1_buffer_size, &output1_length));
4097 TEST_LE_U(output1_length,
4098 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4099 TEST_LE_U(output1_length,
4100 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004101
Gilles Peskine449bd832023-01-11 14:50:10 +01004102 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4103 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004104
Gilles Peskine449bd832023-01-11 14:50:10 +01004105 PSA_ASSERT(psa_cipher_update(&operation,
4106 input->x, input->len,
4107 output2, output2_buffer_size,
4108 &function_output_length));
4109 TEST_LE_U(function_output_length,
4110 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4111 TEST_LE_U(function_output_length,
4112 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004113 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004114
Gilles Peskine449bd832023-01-11 14:50:10 +01004115 PSA_ASSERT(psa_cipher_finish(&operation,
4116 output2 + output2_length,
4117 output2_buffer_size - output2_length,
4118 &function_output_length));
4119 TEST_LE_U(function_output_length,
4120 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4121 TEST_LE_U(function_output_length,
4122 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004123 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004124
Gilles Peskine449bd832023-01-11 14:50:10 +01004125 PSA_ASSERT(psa_cipher_abort(&operation));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004126 TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004127 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004128
Gilles Peskine50e586b2018-06-08 14:28:46 +02004129exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004130 psa_cipher_abort(&operation);
4131 mbedtls_free(output1);
4132 mbedtls_free(output2);
4133 psa_destroy_key(key);
4134 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004135}
4136/* END_CASE */
4137
4138/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004139void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4140 data_t *key_data, data_t *iv,
4141 data_t *input,
4142 int first_part_size_arg,
4143 int output1_length_arg, int output2_length_arg,
4144 data_t *expected_output,
4145 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004146{
Ronald Cron5425a212020-08-04 14:58:35 +02004147 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004148 psa_key_type_t key_type = key_type_arg;
4149 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004150 psa_status_t status;
4151 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004152 size_t first_part_size = first_part_size_arg;
4153 size_t output1_length = output1_length_arg;
4154 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004155 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004156 size_t output_buffer_size = 0;
4157 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004158 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004159 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004160 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004161
Gilles Peskine449bd832023-01-11 14:50:10 +01004162 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004163
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4165 psa_set_key_algorithm(&attributes, alg);
4166 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004167
Gilles Peskine449bd832023-01-11 14:50:10 +01004168 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4169 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004170
Gilles Peskine449bd832023-01-11 14:50:10 +01004171 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004172
Gilles Peskine449bd832023-01-11 14:50:10 +01004173 if (iv->len > 0) {
4174 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004175 }
4176
Gilles Peskine449bd832023-01-11 14:50:10 +01004177 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4178 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004179 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004180
Gilles Peskine449bd832023-01-11 14:50:10 +01004181 TEST_LE_U(first_part_size, input->len);
4182 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4183 output, output_buffer_size,
4184 &function_output_length));
4185 TEST_ASSERT(function_output_length == output1_length);
4186 TEST_LE_U(function_output_length,
4187 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4188 TEST_LE_U(function_output_length,
4189 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004190 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004191
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 if (first_part_size < input->len) {
4193 PSA_ASSERT(psa_cipher_update(&operation,
4194 input->x + first_part_size,
4195 input->len - first_part_size,
4196 (output_buffer_size == 0 ? NULL :
4197 output + total_output_length),
4198 output_buffer_size - total_output_length,
4199 &function_output_length));
4200 TEST_ASSERT(function_output_length == output2_length);
4201 TEST_LE_U(function_output_length,
4202 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4203 alg,
4204 input->len - first_part_size));
4205 TEST_LE_U(function_output_length,
4206 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004207 total_output_length += function_output_length;
4208 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004209
Gilles Peskine449bd832023-01-11 14:50:10 +01004210 status = psa_cipher_finish(&operation,
4211 (output_buffer_size == 0 ? NULL :
4212 output + total_output_length),
4213 output_buffer_size - total_output_length,
4214 &function_output_length);
4215 TEST_LE_U(function_output_length,
4216 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4217 TEST_LE_U(function_output_length,
4218 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004219 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004220 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004221
Gilles Peskine449bd832023-01-11 14:50:10 +01004222 if (expected_status == PSA_SUCCESS) {
4223 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004224
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004225 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004226 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004227 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004228
4229exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004230 psa_cipher_abort(&operation);
4231 mbedtls_free(output);
4232 psa_destroy_key(key);
4233 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004234}
4235/* END_CASE */
4236
4237/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004238void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4239 data_t *key_data, data_t *iv,
4240 data_t *input,
4241 int first_part_size_arg,
4242 int output1_length_arg, int output2_length_arg,
4243 data_t *expected_output,
4244 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004245{
Ronald Cron5425a212020-08-04 14:58:35 +02004246 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004247 psa_key_type_t key_type = key_type_arg;
4248 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004249 psa_status_t status;
4250 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004251 size_t first_part_size = first_part_size_arg;
4252 size_t output1_length = output1_length_arg;
4253 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004254 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004255 size_t output_buffer_size = 0;
4256 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004257 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004258 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004259 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004260
Gilles Peskine449bd832023-01-11 14:50:10 +01004261 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004262
Gilles Peskine449bd832023-01-11 14:50:10 +01004263 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4264 psa_set_key_algorithm(&attributes, alg);
4265 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004266
Gilles Peskine449bd832023-01-11 14:50:10 +01004267 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4268 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004269
Gilles Peskine449bd832023-01-11 14:50:10 +01004270 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004271
Gilles Peskine449bd832023-01-11 14:50:10 +01004272 if (iv->len > 0) {
4273 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004274 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004275
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4277 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004278 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004279
Gilles Peskine449bd832023-01-11 14:50:10 +01004280 TEST_LE_U(first_part_size, input->len);
4281 PSA_ASSERT(psa_cipher_update(&operation,
4282 input->x, first_part_size,
4283 output, output_buffer_size,
4284 &function_output_length));
4285 TEST_ASSERT(function_output_length == output1_length);
4286 TEST_LE_U(function_output_length,
4287 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4288 TEST_LE_U(function_output_length,
4289 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004290 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004291
Gilles Peskine449bd832023-01-11 14:50:10 +01004292 if (first_part_size < input->len) {
4293 PSA_ASSERT(psa_cipher_update(&operation,
4294 input->x + first_part_size,
4295 input->len - first_part_size,
4296 (output_buffer_size == 0 ? NULL :
4297 output + total_output_length),
4298 output_buffer_size - total_output_length,
4299 &function_output_length));
4300 TEST_ASSERT(function_output_length == output2_length);
4301 TEST_LE_U(function_output_length,
4302 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4303 alg,
4304 input->len - first_part_size));
4305 TEST_LE_U(function_output_length,
4306 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004307 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004308 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004309
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 status = psa_cipher_finish(&operation,
4311 (output_buffer_size == 0 ? NULL :
4312 output + total_output_length),
4313 output_buffer_size - total_output_length,
4314 &function_output_length);
4315 TEST_LE_U(function_output_length,
4316 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4317 TEST_LE_U(function_output_length,
4318 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004319 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004321
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 if (expected_status == PSA_SUCCESS) {
4323 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004324
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004325 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004326 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004327 }
4328
Gilles Peskine50e586b2018-06-08 14:28:46 +02004329exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 psa_cipher_abort(&operation);
4331 mbedtls_free(output);
4332 psa_destroy_key(key);
4333 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004334}
4335/* END_CASE */
4336
Gilles Peskine50e586b2018-06-08 14:28:46 +02004337/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004338void cipher_decrypt_fail(int alg_arg,
4339 int key_type_arg,
4340 data_t *key_data,
4341 data_t *iv,
4342 data_t *input_arg,
4343 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004344{
4345 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4346 psa_status_t status;
4347 psa_key_type_t key_type = key_type_arg;
4348 psa_algorithm_t alg = alg_arg;
4349 psa_status_t expected_status = expected_status_arg;
4350 unsigned char *input = NULL;
4351 size_t input_buffer_size = 0;
4352 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004353 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004354 size_t output_buffer_size = 0;
4355 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004356 size_t function_output_length;
4357 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004358 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4359
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 if (PSA_ERROR_BAD_STATE != expected_status) {
4361 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004362
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4364 psa_set_key_algorithm(&attributes, alg);
4365 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004366
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4368 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004369 }
4370
4371 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004372 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4373 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004374 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 memcpy(input, iv->x, iv->len);
4376 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004377 }
4378
Gilles Peskine449bd832023-01-11 14:50:10 +01004379 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004380 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004381
Neil Armstrong66a479f2022-02-07 15:41:19 +01004382 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004383 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4384 output_buffer_size, &output_length);
4385 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004386
Neil Armstrong66a479f2022-02-07 15:41:19 +01004387 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 status = psa_cipher_decrypt_setup(&operation, key, alg);
4389 if (status == PSA_SUCCESS) {
4390 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4391 input_arg->len) +
4392 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004393 TEST_CALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004394
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 if (iv->len > 0) {
4396 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004397
Gilles Peskine449bd832023-01-11 14:50:10 +01004398 if (status != PSA_SUCCESS) {
4399 TEST_EQUAL(status, expected_status);
4400 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004401 }
4402
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 if (status == PSA_SUCCESS) {
4404 status = psa_cipher_update(&operation,
4405 input_arg->x, input_arg->len,
4406 output_multi, output_buffer_size,
4407 &function_output_length);
4408 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004409 output_length = function_output_length;
4410
Gilles Peskine449bd832023-01-11 14:50:10 +01004411 status = psa_cipher_finish(&operation,
4412 output_multi + output_length,
4413 output_buffer_size - output_length,
4414 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004415
Gilles Peskine449bd832023-01-11 14:50:10 +01004416 TEST_EQUAL(status, expected_status);
4417 } else {
4418 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004419 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 } else {
4421 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004422 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004423 } else {
4424 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004425 }
4426
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004427exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004428 psa_cipher_abort(&operation);
4429 mbedtls_free(input);
4430 mbedtls_free(output);
4431 mbedtls_free(output_multi);
4432 psa_destroy_key(key);
4433 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004434}
4435/* END_CASE */
4436
4437/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004438void cipher_decrypt(int alg_arg,
4439 int key_type_arg,
4440 data_t *key_data,
4441 data_t *iv,
4442 data_t *input_arg,
4443 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004444{
4445 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4446 psa_key_type_t key_type = key_type_arg;
4447 psa_algorithm_t alg = alg_arg;
4448 unsigned char *input = NULL;
4449 size_t input_buffer_size = 0;
4450 unsigned char *output = NULL;
4451 size_t output_buffer_size = 0;
4452 size_t output_length = 0;
4453 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4454
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004456
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4458 psa_set_key_algorithm(&attributes, alg);
4459 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004460
4461 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4463 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004464 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 memcpy(input, iv->x, iv->len);
4466 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004467 }
4468
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004470 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004471
Gilles Peskine449bd832023-01-11 14:50:10 +01004472 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4473 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004474
Gilles Peskine449bd832023-01-11 14:50:10 +01004475 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4476 output_buffer_size, &output_length));
4477 TEST_LE_U(output_length,
4478 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4479 TEST_LE_U(output_length,
4480 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004481
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004482 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004483 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004484exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 mbedtls_free(input);
4486 mbedtls_free(output);
4487 psa_destroy_key(key);
4488 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004489}
4490/* END_CASE */
4491
4492/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004493void cipher_verify_output(int alg_arg,
4494 int key_type_arg,
4495 data_t *key_data,
4496 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004497{
Ronald Cron5425a212020-08-04 14:58:35 +02004498 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004499 psa_key_type_t key_type = key_type_arg;
4500 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004501 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004502 size_t output1_size = 0;
4503 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004504 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004505 size_t output2_size = 0;
4506 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004507 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004508
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004510
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4512 psa_set_key_algorithm(&attributes, alg);
4513 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004514
Gilles Peskine449bd832023-01-11 14:50:10 +01004515 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4516 &key));
4517 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004518 TEST_CALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004519
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4521 output1, output1_size,
4522 &output1_length));
4523 TEST_LE_U(output1_length,
4524 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4525 TEST_LE_U(output1_length,
4526 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004527
4528 output2_size = output1_length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004529 TEST_CALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004530
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4532 output2, output2_size,
4533 &output2_length));
4534 TEST_LE_U(output2_length,
4535 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4536 TEST_LE_U(output2_length,
4537 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004538
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004539 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004540
4541exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 mbedtls_free(output1);
4543 mbedtls_free(output2);
4544 psa_destroy_key(key);
4545 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004546}
4547/* END_CASE */
4548
4549/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004550void cipher_verify_output_multipart(int alg_arg,
4551 int key_type_arg,
4552 data_t *key_data,
4553 data_t *input,
4554 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004555{
Ronald Cron5425a212020-08-04 14:58:35 +02004556 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004557 psa_key_type_t key_type = key_type_arg;
4558 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004559 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004560 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004561 size_t iv_size = 16;
4562 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004563 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004564 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004565 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004566 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004567 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004568 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004569 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004570 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4571 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004572 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004573
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004575
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4577 psa_set_key_algorithm(&attributes, alg);
4578 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004579
Gilles Peskine449bd832023-01-11 14:50:10 +01004580 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4581 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004582
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4584 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004585
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 if (alg != PSA_ALG_ECB_NO_PADDING) {
4587 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4588 iv, iv_size,
4589 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004590 }
4591
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4593 TEST_LE_U(output1_buffer_size,
4594 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004595 TEST_CALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004596
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004598
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4600 output1, output1_buffer_size,
4601 &function_output_length));
4602 TEST_LE_U(function_output_length,
4603 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4604 TEST_LE_U(function_output_length,
4605 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004606 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004607
Gilles Peskine449bd832023-01-11 14:50:10 +01004608 PSA_ASSERT(psa_cipher_update(&operation1,
4609 input->x + first_part_size,
4610 input->len - first_part_size,
4611 output1, output1_buffer_size,
4612 &function_output_length));
4613 TEST_LE_U(function_output_length,
4614 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4615 alg,
4616 input->len - first_part_size));
4617 TEST_LE_U(function_output_length,
4618 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004619 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004620
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 PSA_ASSERT(psa_cipher_finish(&operation1,
4622 output1 + output1_length,
4623 output1_buffer_size - output1_length,
4624 &function_output_length));
4625 TEST_LE_U(function_output_length,
4626 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4627 TEST_LE_U(function_output_length,
4628 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004629 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004630
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004632
Gilles Peskine048b7f02018-06-08 14:20:49 +02004633 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 TEST_LE_U(output2_buffer_size,
4635 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4636 TEST_LE_U(output2_buffer_size,
4637 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004638 TEST_CALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004639
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 if (iv_length > 0) {
4641 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4642 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004643 }
Moran Pekerded84402018-06-06 16:36:50 +03004644
Gilles Peskine449bd832023-01-11 14:50:10 +01004645 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4646 output2, output2_buffer_size,
4647 &function_output_length));
4648 TEST_LE_U(function_output_length,
4649 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4650 TEST_LE_U(function_output_length,
4651 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004652 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004653
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 PSA_ASSERT(psa_cipher_update(&operation2,
4655 output1 + first_part_size,
4656 output1_length - first_part_size,
4657 output2, output2_buffer_size,
4658 &function_output_length));
4659 TEST_LE_U(function_output_length,
4660 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4661 alg,
4662 output1_length - first_part_size));
4663 TEST_LE_U(function_output_length,
4664 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004665 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004666
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 PSA_ASSERT(psa_cipher_finish(&operation2,
4668 output2 + output2_length,
4669 output2_buffer_size - output2_length,
4670 &function_output_length));
4671 TEST_LE_U(function_output_length,
4672 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4673 TEST_LE_U(function_output_length,
4674 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004675 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004676
Gilles Peskine449bd832023-01-11 14:50:10 +01004677 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004678
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004679 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004680
4681exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 psa_cipher_abort(&operation1);
4683 psa_cipher_abort(&operation2);
4684 mbedtls_free(output1);
4685 mbedtls_free(output2);
4686 psa_destroy_key(key);
4687 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004688}
4689/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004690
Gilles Peskine20035e32018-02-03 22:44:14 +01004691/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004692void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4693 int alg_arg,
4694 data_t *nonce,
4695 data_t *additional_data,
4696 data_t *input_data,
4697 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004698{
Ronald Cron5425a212020-08-04 14:58:35 +02004699 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004700 psa_key_type_t key_type = key_type_arg;
4701 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004702 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004703 unsigned char *output_data = NULL;
4704 size_t output_size = 0;
4705 size_t output_length = 0;
4706 unsigned char *output_data2 = NULL;
4707 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004708 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004709 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004710 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004711
Gilles Peskine449bd832023-01-11 14:50:10 +01004712 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004713
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4715 psa_set_key_algorithm(&attributes, alg);
4716 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004717
Gilles Peskine449bd832023-01-11 14:50:10 +01004718 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4719 &key));
4720 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4721 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004722
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4724 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004725 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4726 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4728 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4729 TEST_EQUAL(output_size,
4730 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4731 TEST_LE_U(output_size,
4732 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004733 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004734 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004735
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 status = psa_aead_encrypt(key, alg,
4737 nonce->x, nonce->len,
4738 additional_data->x,
4739 additional_data->len,
4740 input_data->x, input_data->len,
4741 output_data, output_size,
4742 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004743
4744 /* If the operation is not supported, just skip and not fail in case the
4745 * encryption involves a common limitation of cryptography hardwares and
4746 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004747 if (status == PSA_ERROR_NOT_SUPPORTED) {
4748 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4749 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004750 }
4751
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004753
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 if (PSA_SUCCESS == expected_result) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004755 TEST_CALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004756
Gilles Peskine003a4a92019-05-14 16:09:40 +02004757 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4758 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 TEST_EQUAL(input_data->len,
4760 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004761
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 TEST_LE_U(input_data->len,
4763 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004764
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 TEST_EQUAL(psa_aead_decrypt(key, alg,
4766 nonce->x, nonce->len,
4767 additional_data->x,
4768 additional_data->len,
4769 output_data, output_length,
4770 output_data2, output_length,
4771 &output_length2),
4772 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004773
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004774 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004775 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004776 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004777
Gilles Peskinea1cac842018-06-11 19:33:02 +02004778exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004779 psa_destroy_key(key);
4780 mbedtls_free(output_data);
4781 mbedtls_free(output_data2);
4782 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004783}
4784/* END_CASE */
4785
4786/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004787void aead_encrypt(int key_type_arg, data_t *key_data,
4788 int alg_arg,
4789 data_t *nonce,
4790 data_t *additional_data,
4791 data_t *input_data,
4792 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004793{
Ronald Cron5425a212020-08-04 14:58:35 +02004794 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004795 psa_key_type_t key_type = key_type_arg;
4796 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004797 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004798 unsigned char *output_data = NULL;
4799 size_t output_size = 0;
4800 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004801 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004802 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004803
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004805
Gilles Peskine449bd832023-01-11 14:50:10 +01004806 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4807 psa_set_key_algorithm(&attributes, alg);
4808 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004809
Gilles Peskine449bd832023-01-11 14:50:10 +01004810 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4811 &key));
4812 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4813 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004814
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4816 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004817 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4818 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004819 TEST_EQUAL(output_size,
4820 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4821 TEST_LE_U(output_size,
4822 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004823 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004824
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 status = psa_aead_encrypt(key, alg,
4826 nonce->x, nonce->len,
4827 additional_data->x, additional_data->len,
4828 input_data->x, input_data->len,
4829 output_data, output_size,
4830 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004831
Ronald Cron28a45ed2021-02-09 20:35:42 +01004832 /* If the operation is not supported, just skip and not fail in case the
4833 * encryption involves a common limitation of cryptography hardwares and
4834 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 if (status == PSA_ERROR_NOT_SUPPORTED) {
4836 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4837 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004838 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004839
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 PSA_ASSERT(status);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004841 TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004842 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004843
Gilles Peskinea1cac842018-06-11 19:33:02 +02004844exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004845 psa_destroy_key(key);
4846 mbedtls_free(output_data);
4847 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004848}
4849/* END_CASE */
4850
4851/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004852void aead_decrypt(int key_type_arg, data_t *key_data,
4853 int alg_arg,
4854 data_t *nonce,
4855 data_t *additional_data,
4856 data_t *input_data,
4857 data_t *expected_data,
4858 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004859{
Ronald Cron5425a212020-08-04 14:58:35 +02004860 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004861 psa_key_type_t key_type = key_type_arg;
4862 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004863 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004864 unsigned char *output_data = NULL;
4865 size_t output_size = 0;
4866 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004867 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004868 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004869 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004872
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4874 psa_set_key_algorithm(&attributes, alg);
4875 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004876
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4878 &key));
4879 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4880 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004881
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4883 alg);
4884 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4885 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004886 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4887 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004888 TEST_EQUAL(output_size,
4889 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4890 TEST_LE_U(output_size,
4891 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004892 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004893 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004894
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 status = psa_aead_decrypt(key, alg,
4896 nonce->x, nonce->len,
4897 additional_data->x,
4898 additional_data->len,
4899 input_data->x, input_data->len,
4900 output_data, output_size,
4901 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004902
Ronald Cron28a45ed2021-02-09 20:35:42 +01004903 /* If the operation is not supported, just skip and not fail in case the
4904 * decryption involves a common limitation of cryptography hardwares and
4905 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 if (status == PSA_ERROR_NOT_SUPPORTED) {
4907 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4908 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004909 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004910
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004912
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 if (expected_result == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004914 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004915 output_data, output_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004917
Gilles Peskinea1cac842018-06-11 19:33:02 +02004918exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004919 psa_destroy_key(key);
4920 mbedtls_free(output_data);
4921 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004922}
4923/* END_CASE */
4924
4925/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004926void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4927 int alg_arg,
4928 data_t *nonce,
4929 data_t *additional_data,
4930 data_t *input_data,
4931 int do_set_lengths,
4932 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004933{
Paul Elliottd3f82412021-06-16 16:52:21 +01004934 size_t ad_part_len = 0;
4935 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004936 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004937
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4939 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004940
Gilles Peskine449bd832023-01-11 14:50:10 +01004941 if (do_set_lengths) {
4942 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004943 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004944 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004945 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004947 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004948
4949 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004950 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, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004958 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004959 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004960
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 /* length(0) part, length(ad_part_len) part, length(0) part... */
4962 mbedtls_test_set_step(1000 + ad_part_len);
4963
4964 if (!aead_multipart_internal_func(key_type_arg, key_data,
4965 alg_arg, nonce,
4966 additional_data,
4967 ad_part_len,
4968 input_data, -1,
4969 set_lengths_method,
4970 expected_output,
4971 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004972 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 }
4974 }
4975
4976 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4977 /* Split data into length(data_part_len) parts. */
4978 mbedtls_test_set_step(2000 + data_part_len);
4979
4980 if (do_set_lengths) {
4981 if (data_part_len & 0x01) {
4982 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4983 } else {
4984 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4985 }
4986 }
4987
4988 if (!aead_multipart_internal_func(key_type_arg, key_data,
4989 alg_arg, nonce,
4990 additional_data, -1,
4991 input_data, data_part_len,
4992 set_lengths_method,
4993 expected_output,
4994 1, 0)) {
4995 break;
4996 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004997
4998 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01004999 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005000
Gilles Peskine449bd832023-01-11 14:50:10 +01005001 if (!aead_multipart_internal_func(key_type_arg, key_data,
5002 alg_arg, nonce,
5003 additional_data, -1,
5004 input_data, data_part_len,
5005 set_lengths_method,
5006 expected_output,
5007 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005008 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005009 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005010 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005011
Paul Elliott8fc45162021-06-23 16:06:01 +01005012 /* Goto is required to silence warnings about unused labels, as we
5013 * don't actually do any test assertions in this function. */
5014 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005015}
5016/* END_CASE */
5017
5018/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005019void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5020 int alg_arg,
5021 data_t *nonce,
5022 data_t *additional_data,
5023 data_t *input_data,
5024 int do_set_lengths,
5025 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005026{
Paul Elliottd3f82412021-06-16 16:52:21 +01005027 size_t ad_part_len = 0;
5028 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005029 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005030
Gilles Peskine449bd832023-01-11 14:50:10 +01005031 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005032 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005034
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 if (do_set_lengths) {
5036 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005037 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005038 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005039 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005040 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005041 }
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, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005051 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005052 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005053
5054 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005056
Gilles Peskine449bd832023-01-11 14:50:10 +01005057 if (!aead_multipart_internal_func(key_type_arg, key_data,
5058 alg_arg, nonce,
5059 additional_data,
5060 ad_part_len,
5061 input_data, -1,
5062 set_lengths_method,
5063 expected_output,
5064 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005065 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005067 }
5068
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005070 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005072
Gilles Peskine449bd832023-01-11 14:50:10 +01005073 if (do_set_lengths) {
5074 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005075 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005077 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005079 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005080
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 if (!aead_multipart_internal_func(key_type_arg, key_data,
5082 alg_arg, nonce,
5083 additional_data, -1,
5084 input_data, data_part_len,
5085 set_lengths_method,
5086 expected_output,
5087 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005088 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005089 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005090
5091 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005092 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005093
Gilles Peskine449bd832023-01-11 14:50:10 +01005094 if (!aead_multipart_internal_func(key_type_arg, key_data,
5095 alg_arg, nonce,
5096 additional_data, -1,
5097 input_data, data_part_len,
5098 set_lengths_method,
5099 expected_output,
5100 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005101 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005102 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005103 }
5104
Paul Elliott8fc45162021-06-23 16:06:01 +01005105 /* Goto is required to silence warnings about unused labels, as we
5106 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005107 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005108}
5109/* END_CASE */
5110
5111/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005112void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5113 int alg_arg,
5114 int nonce_length,
5115 int expected_nonce_length_arg,
5116 data_t *additional_data,
5117 data_t *input_data,
5118 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005119{
5120
5121 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5122 psa_key_type_t key_type = key_type_arg;
5123 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005124 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005125 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5126 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5127 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005128 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005129 size_t actual_nonce_length = 0;
5130 size_t expected_nonce_length = expected_nonce_length_arg;
5131 unsigned char *output = NULL;
5132 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005133 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005134 size_t ciphertext_size = 0;
5135 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005136 size_t tag_length = 0;
5137 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005138
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005140
Gilles Peskine449bd832023-01-11 14:50:10 +01005141 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5142 psa_set_key_algorithm(&attributes, alg);
5143 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005144
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5146 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005147
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005151
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005152 TEST_CALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005153
Gilles Peskine449bd832023-01-11 14:50:10 +01005154 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005155
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005157
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005158 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005159
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005161
5162 /* If the operation is not supported, just skip and not fail in case the
5163 * encryption involves a common limitation of cryptography hardwares and
5164 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005165 if (status == PSA_ERROR_NOT_SUPPORTED) {
5166 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5167 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005168 }
5169
Gilles Peskine449bd832023-01-11 14:50:10 +01005170 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005171
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5173 nonce_length,
5174 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005175
Gilles Peskine449bd832023-01-11 14:50:10 +01005176 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005177
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005179
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 if (expected_status == PSA_SUCCESS) {
5181 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5182 alg));
5183 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005184
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005186
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005188 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5190 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005191
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5193 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005194
Gilles Peskine449bd832023-01-11 14:50:10 +01005195 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5196 output, output_size,
5197 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005198
Gilles Peskine449bd832023-01-11 14:50:10 +01005199 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5200 &ciphertext_length, tag_buffer,
5201 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005202 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005203
5204exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005205 psa_destroy_key(key);
5206 mbedtls_free(output);
5207 mbedtls_free(ciphertext);
5208 psa_aead_abort(&operation);
5209 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005210}
5211/* END_CASE */
5212
5213/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005214void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5215 int alg_arg,
5216 int nonce_length_arg,
5217 int set_lengths_method_arg,
5218 data_t *additional_data,
5219 data_t *input_data,
5220 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005221{
5222
5223 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5224 psa_key_type_t key_type = key_type_arg;
5225 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005226 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005227 uint8_t *nonce_buffer = NULL;
5228 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5229 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5230 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005231 unsigned char *output = NULL;
5232 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005233 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005234 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005235 size_t ciphertext_size = 0;
5236 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005237 size_t tag_length = 0;
5238 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005239 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005240 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005241
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005243
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5245 psa_set_key_algorithm(&attributes, alg);
5246 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005247
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5249 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005250
Gilles Peskine449bd832023-01-11 14:50:10 +01005251 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005252
Gilles Peskine449bd832023-01-11 14:50:10 +01005253 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005254
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005255 TEST_CALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005256
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005258
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005260
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005261 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005262
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005264
5265 /* If the operation is not supported, just skip and not fail in case the
5266 * encryption involves a common limitation of cryptography hardwares and
5267 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 if (status == PSA_ERROR_NOT_SUPPORTED) {
5269 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5270 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005271 }
5272
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005274
Paul Elliott4023ffd2021-09-10 16:21:22 +01005275 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 if (nonce_length_arg == -1) {
5277 /* Arbitrary size buffer, to test zero length valid buffer. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005278 TEST_CALLOC(nonce_buffer, 4);
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 nonce_length = 0;
5280 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005281 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 nonce_length = (size_t) nonce_length_arg;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005283 TEST_CALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005284
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 if (nonce_buffer) {
5286 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005287 nonce_buffer[index] = 'a' + index;
5288 }
Paul Elliott66696b52021-08-16 18:42:41 +01005289 }
Paul Elliott863864a2021-07-23 17:28:31 +01005290 }
5291
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5293 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5294 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005295 }
5296
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005298
Gilles Peskine449bd832023-01-11 14:50:10 +01005299 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005300
Gilles Peskine449bd832023-01-11 14:50:10 +01005301 if (expected_status == PSA_SUCCESS) {
5302 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5303 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5304 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005305 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005307 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 }
Paul Elliott863864a2021-07-23 17:28:31 +01005309
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005310 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5312 additional_data->len),
5313 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005314
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5316 output, output_size,
5317 &ciphertext_length),
5318 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005319
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5321 &ciphertext_length, tag_buffer,
5322 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5323 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005324 }
5325
5326exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 psa_destroy_key(key);
5328 mbedtls_free(output);
5329 mbedtls_free(ciphertext);
5330 mbedtls_free(nonce_buffer);
5331 psa_aead_abort(&operation);
5332 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005333}
5334/* END_CASE */
5335
5336/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005337void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005338 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005339 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005340 data_t *nonce,
5341 data_t *additional_data,
5342 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005344{
5345
5346 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5347 psa_key_type_t key_type = key_type_arg;
5348 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005349 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5351 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5352 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005353 unsigned char *output = NULL;
5354 unsigned char *ciphertext = NULL;
5355 size_t output_size = output_size_arg;
5356 size_t ciphertext_size = 0;
5357 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005358 size_t tag_length = 0;
5359 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5360
Gilles Peskine449bd832023-01-11 14:50:10 +01005361 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005362
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5364 psa_set_key_algorithm(&attributes, alg);
5365 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005366
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5368 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005369
Gilles Peskine449bd832023-01-11 14:50:10 +01005370 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005371
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005372 TEST_CALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005373
Gilles Peskine449bd832023-01-11 14:50:10 +01005374 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005375
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005376 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005377
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005379
5380 /* If the operation is not supported, just skip and not fail in case the
5381 * encryption involves a common limitation of cryptography hardwares and
5382 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 if (status == PSA_ERROR_NOT_SUPPORTED) {
5384 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5385 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005386 }
5387
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005389
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5391 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005392
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005394
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5396 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005397
Gilles Peskine449bd832023-01-11 14:50:10 +01005398 status = psa_aead_update(&operation, input_data->x, input_data->len,
5399 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005400
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005402
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005404 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5406 &ciphertext_length, tag_buffer,
5407 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005408 }
5409
5410exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005411 psa_destroy_key(key);
5412 mbedtls_free(output);
5413 mbedtls_free(ciphertext);
5414 psa_aead_abort(&operation);
5415 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005416}
5417/* END_CASE */
5418
Paul Elliott91b021e2021-07-23 18:52:31 +01005419/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005420void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5421 int alg_arg,
5422 int finish_ciphertext_size_arg,
5423 int tag_size_arg,
5424 data_t *nonce,
5425 data_t *additional_data,
5426 data_t *input_data,
5427 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005428{
5429
5430 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5431 psa_key_type_t key_type = key_type_arg;
5432 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005433 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005434 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5435 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5436 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005437 unsigned char *ciphertext = NULL;
5438 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005439 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005440 size_t ciphertext_size = 0;
5441 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5443 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005444 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005445
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005447
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5449 psa_set_key_algorithm(&attributes, alg);
5450 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005451
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5453 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005454
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005456
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005458
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005459 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005460
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005461 TEST_CALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005462
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005463 TEST_CALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005464
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005466
5467 /* If the operation is not supported, just skip and not fail in case the
5468 * encryption involves a common limitation of cryptography hardwares and
5469 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 if (status == PSA_ERROR_NOT_SUPPORTED) {
5471 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5472 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005473 }
5474
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005476
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005478
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5480 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005481
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5483 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005484
Gilles Peskine449bd832023-01-11 14:50:10 +01005485 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5486 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005487
5488 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 status = psa_aead_finish(&operation, finish_ciphertext,
5490 finish_ciphertext_size,
5491 &ciphertext_length, tag_buffer,
5492 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005493
Gilles Peskine449bd832023-01-11 14:50:10 +01005494 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005495
5496exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 psa_destroy_key(key);
5498 mbedtls_free(ciphertext);
5499 mbedtls_free(finish_ciphertext);
5500 mbedtls_free(tag_buffer);
5501 psa_aead_abort(&operation);
5502 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005503}
5504/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005505
5506/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005507void aead_multipart_verify(int key_type_arg, data_t *key_data,
5508 int alg_arg,
5509 data_t *nonce,
5510 data_t *additional_data,
5511 data_t *input_data,
5512 data_t *tag,
5513 int tag_usage_arg,
5514 int expected_setup_status_arg,
5515 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005516{
5517 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5518 psa_key_type_t key_type = key_type_arg;
5519 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005520 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005521 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5522 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5523 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005524 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005525 unsigned char *plaintext = NULL;
5526 unsigned char *finish_plaintext = NULL;
5527 size_t plaintext_size = 0;
5528 size_t plaintext_length = 0;
5529 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005530 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005531 unsigned char *tag_buffer = NULL;
5532 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005533
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005535
Gilles Peskine449bd832023-01-11 14:50:10 +01005536 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5537 psa_set_key_algorithm(&attributes, alg);
5538 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005539
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5541 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005542
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005544
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5546 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005547
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005548 TEST_CALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005549
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005551
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005552 TEST_CALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005553
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005555
5556 /* If the operation is not supported, just skip and not fail in case the
5557 * encryption involves a common limitation of cryptography hardwares and
5558 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 if (status == PSA_ERROR_NOT_SUPPORTED) {
5560 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5561 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005562 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005564
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005566 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 }
Paul Elliott9961a662021-09-17 19:19:02 +01005568
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005570
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005572
Gilles Peskine449bd832023-01-11 14:50:10 +01005573 status = psa_aead_set_lengths(&operation, additional_data->len,
5574 input_data->len);
5575 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005576
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5578 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005579
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5581 input_data->len,
5582 plaintext, plaintext_size,
5583 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005584
Gilles Peskine449bd832023-01-11 14:50:10 +01005585 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005586 tag_buffer = tag->x;
5587 tag_size = tag->len;
5588 }
5589
Gilles Peskine449bd832023-01-11 14:50:10 +01005590 status = psa_aead_verify(&operation, finish_plaintext,
5591 verify_plaintext_size,
5592 &plaintext_length,
5593 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005594
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005596
5597exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 psa_destroy_key(key);
5599 mbedtls_free(plaintext);
5600 mbedtls_free(finish_plaintext);
5601 psa_aead_abort(&operation);
5602 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005603}
5604/* END_CASE */
5605
Paul Elliott9961a662021-09-17 19:19:02 +01005606/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005607void aead_multipart_setup(int key_type_arg, data_t *key_data,
5608 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005609{
5610 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5611 psa_key_type_t key_type = key_type_arg;
5612 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005613 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005614 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5615 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5616 psa_status_t expected_status = expected_status_arg;
5617
Gilles Peskine449bd832023-01-11 14:50:10 +01005618 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005619
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 psa_set_key_usage_flags(&attributes,
5621 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5622 psa_set_key_algorithm(&attributes, alg);
5623 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005624
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5626 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005627
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005629
Gilles Peskine449bd832023-01-11 14:50:10 +01005630 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005631
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005633
Gilles Peskine449bd832023-01-11 14:50:10 +01005634 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005635
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005637
5638exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 psa_destroy_key(key);
5640 psa_aead_abort(&operation);
5641 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005642}
5643/* END_CASE */
5644
5645/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005646void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5647 int alg_arg,
5648 data_t *nonce,
5649 data_t *additional_data,
5650 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005651{
5652 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5653 psa_key_type_t key_type = key_type_arg;
5654 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005655 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005656 unsigned char *output_data = NULL;
5657 unsigned char *final_data = NULL;
5658 size_t output_size = 0;
5659 size_t finish_output_size = 0;
5660 size_t output_length = 0;
5661 size_t key_bits = 0;
5662 size_t tag_length = 0;
5663 size_t tag_size = 0;
5664 size_t nonce_length = 0;
5665 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5666 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5667 size_t output_part_length = 0;
5668 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5669
Gilles Peskine449bd832023-01-11 14:50:10 +01005670 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005671
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 psa_set_key_usage_flags(&attributes,
5673 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5674 psa_set_key_algorithm(&attributes, alg);
5675 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005676
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5678 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005679
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5681 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005682
Gilles Peskine449bd832023-01-11 14:50:10 +01005683 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005684
Gilles Peskine449bd832023-01-11 14:50:10 +01005685 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005686
Gilles Peskine449bd832023-01-11 14:50:10 +01005687 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005688
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005689 TEST_CALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005690
Gilles Peskine449bd832023-01-11 14:50:10 +01005691 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005692
Gilles Peskine449bd832023-01-11 14:50:10 +01005693 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005694
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005695 TEST_CALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005696
5697 /* Test all operations error without calling setup first. */
5698
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5700 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005701
Gilles Peskine449bd832023-01-11 14:50:10 +01005702 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005703
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5705 PSA_AEAD_NONCE_MAX_SIZE,
5706 &nonce_length),
5707 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005708
Gilles Peskine449bd832023-01-11 14:50:10 +01005709 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005710
Paul Elliott481be342021-07-16 17:38:47 +01005711 /* ------------------------------------------------------- */
5712
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5714 input_data->len),
5715 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005716
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005718
Paul Elliott481be342021-07-16 17:38:47 +01005719 /* ------------------------------------------------------- */
5720
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5722 additional_data->len),
5723 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005724
Gilles Peskine449bd832023-01-11 14:50:10 +01005725 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005726
Paul Elliott481be342021-07-16 17:38:47 +01005727 /* ------------------------------------------------------- */
5728
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5730 input_data->len, output_data,
5731 output_size, &output_length),
5732 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005733
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005735
Paul Elliott481be342021-07-16 17:38:47 +01005736 /* ------------------------------------------------------- */
5737
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5739 finish_output_size,
5740 &output_part_length,
5741 tag_buffer, tag_length,
5742 &tag_size),
5743 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005744
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005746
Paul Elliott481be342021-07-16 17:38:47 +01005747 /* ------------------------------------------------------- */
5748
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5750 finish_output_size,
5751 &output_part_length,
5752 tag_buffer,
5753 tag_length),
5754 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005755
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005757
5758 /* Test for double setups. */
5759
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005761
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5763 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005764
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005766
Paul Elliott481be342021-07-16 17:38:47 +01005767 /* ------------------------------------------------------- */
5768
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005770
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5772 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005773
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005775
Paul Elliott374a2be2021-07-16 17:53:40 +01005776 /* ------------------------------------------------------- */
5777
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005779
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5781 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005782
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005784
5785 /* ------------------------------------------------------- */
5786
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005788
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5790 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005791
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005793
Paul Elliottc23a9a02021-06-21 18:32:46 +01005794 /* Test for not setting a nonce. */
5795
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005797
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5799 additional_data->len),
5800 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005801
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005803
Paul Elliott7f628422021-09-01 12:08:29 +01005804 /* ------------------------------------------------------- */
5805
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005807
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5809 input_data->len, output_data,
5810 output_size, &output_length),
5811 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005812
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005814
Paul Elliottbdc2c682021-09-21 18:37:10 +01005815 /* ------------------------------------------------------- */
5816
Gilles Peskine449bd832023-01-11 14:50:10 +01005817 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005818
Gilles Peskine449bd832023-01-11 14:50:10 +01005819 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5820 finish_output_size,
5821 &output_part_length,
5822 tag_buffer, tag_length,
5823 &tag_size),
5824 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005825
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005827
5828 /* ------------------------------------------------------- */
5829
Gilles Peskine449bd832023-01-11 14:50:10 +01005830 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005831
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5833 finish_output_size,
5834 &output_part_length,
5835 tag_buffer,
5836 tag_length),
5837 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005838
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005840
Paul Elliottc23a9a02021-06-21 18:32:46 +01005841 /* Test for double setting nonce. */
5842
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005844
Gilles Peskine449bd832023-01-11 14:50:10 +01005845 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005846
Gilles Peskine449bd832023-01-11 14:50:10 +01005847 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5848 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005849
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005851
Paul Elliott374a2be2021-07-16 17:53:40 +01005852 /* Test for double generating nonce. */
5853
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005855
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5857 PSA_AEAD_NONCE_MAX_SIZE,
5858 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005859
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5861 PSA_AEAD_NONCE_MAX_SIZE,
5862 &nonce_length),
5863 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005864
5865
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005867
5868 /* Test for generate nonce then set and vice versa */
5869
Gilles Peskine449bd832023-01-11 14:50:10 +01005870 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005871
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5873 PSA_AEAD_NONCE_MAX_SIZE,
5874 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005875
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5877 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005878
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005880
Andrzej Kurekad837522021-12-15 15:28:49 +01005881 /* Test for generating nonce after calling set lengths */
5882
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005884
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5886 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005887
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5889 PSA_AEAD_NONCE_MAX_SIZE,
5890 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005891
Gilles Peskine449bd832023-01-11 14:50:10 +01005892 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005893
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005894 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005895
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005897
Gilles Peskine449bd832023-01-11 14:50:10 +01005898 if (operation.alg == PSA_ALG_CCM) {
5899 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5900 input_data->len),
5901 PSA_ERROR_INVALID_ARGUMENT);
5902 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5903 PSA_AEAD_NONCE_MAX_SIZE,
5904 &nonce_length),
5905 PSA_ERROR_BAD_STATE);
5906 } else {
5907 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5908 input_data->len));
5909 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5910 PSA_AEAD_NONCE_MAX_SIZE,
5911 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005912 }
5913
Gilles Peskine449bd832023-01-11 14:50:10 +01005914 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005915
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005916 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005917#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005918 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005919
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5921 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5922 input_data->len),
5923 PSA_ERROR_INVALID_ARGUMENT);
5924 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5925 PSA_AEAD_NONCE_MAX_SIZE,
5926 &nonce_length),
5927 PSA_ERROR_BAD_STATE);
5928 } else {
5929 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5930 input_data->len));
5931 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5932 PSA_AEAD_NONCE_MAX_SIZE,
5933 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005934 }
5935
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005937#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005938
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005939 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005940
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005942
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5944 PSA_AEAD_NONCE_MAX_SIZE,
5945 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005946
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 if (operation.alg == PSA_ALG_CCM) {
5948 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5949 input_data->len),
5950 PSA_ERROR_INVALID_ARGUMENT);
5951 } else {
5952 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5953 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005954 }
5955
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005957
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005958 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005959 /* Test for setting nonce after calling set lengths */
5960
Gilles Peskine449bd832023-01-11 14:50:10 +01005961 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005962
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5964 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005965
Gilles Peskine449bd832023-01-11 14:50:10 +01005966 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005967
Gilles Peskine449bd832023-01-11 14:50:10 +01005968 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005969
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005970 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005971
Gilles Peskine449bd832023-01-11 14:50:10 +01005972 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005973
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 if (operation.alg == PSA_ALG_CCM) {
5975 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5976 input_data->len),
5977 PSA_ERROR_INVALID_ARGUMENT);
5978 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5979 PSA_ERROR_BAD_STATE);
5980 } else {
5981 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5982 input_data->len));
5983 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005984 }
5985
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005987
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005988 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005989#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005991
Gilles Peskine449bd832023-01-11 14:50:10 +01005992 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5993 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5994 input_data->len),
5995 PSA_ERROR_INVALID_ARGUMENT);
5996 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5997 PSA_ERROR_BAD_STATE);
5998 } else {
5999 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6000 input_data->len));
6001 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006002 }
6003
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00006005#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006006
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006007 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006008
Gilles Peskine449bd832023-01-11 14:50:10 +01006009 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006010
Gilles Peskine449bd832023-01-11 14:50:10 +01006011 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006012
Gilles Peskine449bd832023-01-11 14:50:10 +01006013 if (operation.alg == PSA_ALG_CCM) {
6014 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6015 input_data->len),
6016 PSA_ERROR_INVALID_ARGUMENT);
6017 } else {
6018 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6019 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006020 }
6021
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006023
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006024 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006025#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006026 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006027
Gilles Peskine449bd832023-01-11 14:50:10 +01006028 if (operation.alg == PSA_ALG_GCM) {
6029 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6030 SIZE_MAX),
6031 PSA_ERROR_INVALID_ARGUMENT);
6032 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6033 PSA_ERROR_BAD_STATE);
6034 } else if (operation.alg != PSA_ALG_CCM) {
6035 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6036 SIZE_MAX));
6037 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006038 }
6039
Gilles Peskine449bd832023-01-11 14:50:10 +01006040 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006041#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006042
Tom Cosgrove1797b052022-12-04 17:19:59 +00006043 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006044#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006046
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006048
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 if (operation.alg == PSA_ALG_GCM) {
6050 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6051 SIZE_MAX),
6052 PSA_ERROR_INVALID_ARGUMENT);
6053 } else if (operation.alg != PSA_ALG_CCM) {
6054 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6055 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006056 }
6057
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006059#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006060
6061 /* ------------------------------------------------------- */
6062
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006064
Gilles Peskine449bd832023-01-11 14:50:10 +01006065 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006066
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6068 PSA_AEAD_NONCE_MAX_SIZE,
6069 &nonce_length),
6070 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006071
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006073
Paul Elliott7220cae2021-06-22 17:25:57 +01006074 /* Test for generating nonce in decrypt setup. */
6075
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006077
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6079 PSA_AEAD_NONCE_MAX_SIZE,
6080 &nonce_length),
6081 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006082
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006084
Paul Elliottc23a9a02021-06-21 18:32:46 +01006085 /* Test for setting lengths twice. */
6086
Gilles Peskine449bd832023-01-11 14:50:10 +01006087 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006088
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006090
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6092 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006093
Gilles Peskine449bd832023-01-11 14:50:10 +01006094 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6095 input_data->len),
6096 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006097
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006099
Andrzej Kurekad837522021-12-15 15:28:49 +01006100 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006101
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006103
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006105
Gilles Peskine449bd832023-01-11 14:50:10 +01006106 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006107
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6109 additional_data->len),
6110 PSA_ERROR_BAD_STATE);
6111 } else {
6112 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6113 additional_data->len));
6114
6115 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6116 input_data->len),
6117 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006118 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006120
6121 /* ------------------------------------------------------- */
6122
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006124
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006126
Gilles Peskine449bd832023-01-11 14:50:10 +01006127 if (operation.alg == PSA_ALG_CCM) {
6128 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6129 input_data->len, output_data,
6130 output_size, &output_length),
6131 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006132
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 } else {
6134 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6135 input_data->len, output_data,
6136 output_size, &output_length));
6137
6138 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6139 input_data->len),
6140 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006141 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006142 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006143
6144 /* ------------------------------------------------------- */
6145
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006147
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006149
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 if (operation.alg == PSA_ALG_CCM) {
6151 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6152 finish_output_size,
6153 &output_part_length,
6154 tag_buffer, tag_length,
6155 &tag_size));
6156 } else {
6157 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6158 finish_output_size,
6159 &output_part_length,
6160 tag_buffer, tag_length,
6161 &tag_size));
6162
6163 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6164 input_data->len),
6165 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006166 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006167 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006168
6169 /* Test for setting lengths after generating nonce + already starting data. */
6170
Gilles Peskine449bd832023-01-11 14:50:10 +01006171 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006172
Gilles Peskine449bd832023-01-11 14:50:10 +01006173 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6174 PSA_AEAD_NONCE_MAX_SIZE,
6175 &nonce_length));
6176 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006177
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6179 additional_data->len),
6180 PSA_ERROR_BAD_STATE);
6181 } else {
6182 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6183 additional_data->len));
6184
6185 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6186 input_data->len),
6187 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006188 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006190
6191 /* ------------------------------------------------------- */
6192
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006194
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6196 PSA_AEAD_NONCE_MAX_SIZE,
6197 &nonce_length));
6198 if (operation.alg == PSA_ALG_CCM) {
6199 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6200 input_data->len, output_data,
6201 output_size, &output_length),
6202 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006203
Gilles Peskine449bd832023-01-11 14:50:10 +01006204 } else {
6205 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6206 input_data->len, output_data,
6207 output_size, &output_length));
6208
6209 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6210 input_data->len),
6211 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006212 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006214
6215 /* ------------------------------------------------------- */
6216
Gilles Peskine449bd832023-01-11 14:50:10 +01006217 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006218
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6220 PSA_AEAD_NONCE_MAX_SIZE,
6221 &nonce_length));
6222 if (operation.alg == PSA_ALG_CCM) {
6223 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6224 finish_output_size,
6225 &output_part_length,
6226 tag_buffer, tag_length,
6227 &tag_size));
6228 } else {
6229 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6230 finish_output_size,
6231 &output_part_length,
6232 tag_buffer, tag_length,
6233 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006234
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6236 input_data->len),
6237 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006238 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006240
Paul Elliott243080c2021-07-21 19:01:17 +01006241 /* Test for not sending any additional data or data after setting non zero
6242 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006243
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006245
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006247
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6249 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006250
Gilles Peskine449bd832023-01-11 14:50:10 +01006251 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6252 finish_output_size,
6253 &output_part_length,
6254 tag_buffer, tag_length,
6255 &tag_size),
6256 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006257
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006259
Paul Elliott243080c2021-07-21 19:01:17 +01006260 /* Test for not sending any additional data or data after setting non-zero
6261 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006262
Gilles Peskine449bd832023-01-11 14:50:10 +01006263 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006264
Gilles Peskine449bd832023-01-11 14:50:10 +01006265 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006266
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6268 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006269
Gilles Peskine449bd832023-01-11 14:50:10 +01006270 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6271 finish_output_size,
6272 &output_part_length,
6273 tag_buffer,
6274 tag_length),
6275 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006276
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006278
Paul Elliott243080c2021-07-21 19:01:17 +01006279 /* Test for not sending any additional data after setting a non-zero length
6280 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006281
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006283
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006285
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6287 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006288
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6290 input_data->len, output_data,
6291 output_size, &output_length),
6292 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006293
Gilles Peskine449bd832023-01-11 14:50:10 +01006294 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006295
Paul Elliottf94bd992021-09-19 18:15:59 +01006296 /* Test for not sending any data after setting a non-zero length for it.*/
6297
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006299
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006301
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6303 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006304
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6306 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006307
Gilles Peskine449bd832023-01-11 14:50:10 +01006308 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6309 finish_output_size,
6310 &output_part_length,
6311 tag_buffer, tag_length,
6312 &tag_size),
6313 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006314
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006316
Paul Elliottb0450fe2021-09-01 15:06:26 +01006317 /* Test for sending too much additional data after setting lengths. */
6318
Gilles Peskine449bd832023-01-11 14:50:10 +01006319 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006320
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006322
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006324
6325
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6327 additional_data->len),
6328 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006329
Gilles Peskine449bd832023-01-11 14:50:10 +01006330 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006331
Paul Elliotta2a09b02021-09-22 14:56:40 +01006332 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006333
Gilles Peskine449bd832023-01-11 14:50:10 +01006334 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006335
Gilles Peskine449bd832023-01-11 14:50:10 +01006336 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006337
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6339 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006340
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6342 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006343
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6345 1),
6346 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006347
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006349
Paul Elliottb0450fe2021-09-01 15:06:26 +01006350 /* Test for sending too much data after setting lengths. */
6351
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006353
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006355
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006357
Gilles Peskine449bd832023-01-11 14:50:10 +01006358 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6359 input_data->len, output_data,
6360 output_size, &output_length),
6361 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006362
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006364
Paul Elliotta2a09b02021-09-22 14:56:40 +01006365 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006366
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006368
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006370
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6372 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006373
Gilles Peskine449bd832023-01-11 14:50:10 +01006374 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6375 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006376
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6378 input_data->len, output_data,
6379 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006380
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6382 1, output_data,
6383 output_size, &output_length),
6384 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006385
Gilles Peskine449bd832023-01-11 14:50:10 +01006386 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006387
Paul Elliottc23a9a02021-06-21 18:32:46 +01006388 /* Test sending additional data after data. */
6389
Gilles Peskine449bd832023-01-11 14:50:10 +01006390 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006391
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006393
Gilles Peskine449bd832023-01-11 14:50:10 +01006394 if (operation.alg != PSA_ALG_CCM) {
6395 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6396 input_data->len, output_data,
6397 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006398
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6400 additional_data->len),
6401 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006402 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006404
Paul Elliott534d0b42021-06-22 19:15:20 +01006405 /* Test calling finish on decryption. */
6406
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006408
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006410
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6412 finish_output_size,
6413 &output_part_length,
6414 tag_buffer, tag_length,
6415 &tag_size),
6416 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006417
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006419
6420 /* Test calling verify on encryption. */
6421
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006423
Gilles Peskine449bd832023-01-11 14:50:10 +01006424 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006425
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6427 finish_output_size,
6428 &output_part_length,
6429 tag_buffer,
6430 tag_length),
6431 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006432
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006434
6435
Paul Elliottc23a9a02021-06-21 18:32:46 +01006436exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 psa_destroy_key(key);
6438 psa_aead_abort(&operation);
6439 mbedtls_free(output_data);
6440 mbedtls_free(final_data);
6441 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006442}
6443/* END_CASE */
6444
6445/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006446void signature_size(int type_arg,
6447 int bits,
6448 int alg_arg,
6449 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006450{
6451 psa_key_type_t type = type_arg;
6452 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006453 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006454
Gilles Peskine449bd832023-01-11 14:50:10 +01006455 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006456
Gilles Peskinee59236f2018-01-27 23:32:46 +01006457exit:
6458 ;
6459}
6460/* END_CASE */
6461
6462/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006463void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6464 int alg_arg, data_t *input_data,
6465 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006466{
Ronald Cron5425a212020-08-04 14:58:35 +02006467 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006468 psa_key_type_t key_type = key_type_arg;
6469 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006470 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006471 unsigned char *signature = NULL;
6472 size_t signature_size;
6473 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006474 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006475
Gilles Peskine449bd832023-01-11 14:50:10 +01006476 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006477
Gilles Peskine449bd832023-01-11 14:50:10 +01006478 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6479 psa_set_key_algorithm(&attributes, alg);
6480 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006481
Gilles Peskine449bd832023-01-11 14:50:10 +01006482 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6483 &key));
6484 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6485 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006486
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006487 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006488 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6490 key_bits, alg);
6491 TEST_ASSERT(signature_size != 0);
6492 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006493 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006494
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006495 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006496 PSA_ASSERT(psa_sign_hash(key, alg,
6497 input_data->x, input_data->len,
6498 signature, signature_size,
6499 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006500 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006501 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006502 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006503
6504exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006505 /*
6506 * Key attributes may have been returned by psa_get_key_attributes()
6507 * thus reset them as required.
6508 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006510
Gilles Peskine449bd832023-01-11 14:50:10 +01006511 psa_destroy_key(key);
6512 mbedtls_free(signature);
6513 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006514}
6515/* END_CASE */
6516
Paul Elliott712d5122022-12-07 14:03:10 +00006517/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006518/**
6519 * sign_hash_interruptible() test intentions:
6520 *
6521 * Note: This test can currently only handle ECDSA.
6522 *
6523 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006524 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006525 *
6526 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6527 * expected for different max_ops values.
6528 *
6529 * 3. Test that the number of ops done prior to start and after abort is zero
6530 * and that each successful stage completes some ops (this is not mandated by
6531 * the PSA specification, but is currently the case).
6532 *
6533 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6534 * complete() calls does not alter the number of ops returned.
6535 */
Paul Elliott712d5122022-12-07 14:03:10 +00006536void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6537 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006538 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006539{
6540 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6541 psa_key_type_t key_type = key_type_arg;
6542 psa_algorithm_t alg = alg_arg;
6543 size_t key_bits;
6544 unsigned char *signature = NULL;
6545 size_t signature_size;
6546 size_t signature_length = 0xdeadbeef;
6547 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6548 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006549 uint32_t num_ops = 0;
6550 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006551 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006552 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006553 size_t min_completes = 0;
6554 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006555
Paul Elliott712d5122022-12-07 14:03:10 +00006556 psa_sign_hash_interruptible_operation_t operation =
6557 psa_sign_hash_interruptible_operation_init();
6558
6559 PSA_ASSERT(psa_crypto_init());
6560
6561 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6562 psa_set_key_algorithm(&attributes, alg);
6563 psa_set_key_type(&attributes, key_type);
6564
6565 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6566 &key));
6567 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6568 key_bits = psa_get_key_bits(&attributes);
6569
6570 /* Allocate a buffer which has the size advertised by the
6571 * library. */
6572 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6573 key_bits, alg);
6574 TEST_ASSERT(signature_size != 0);
6575 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006576 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006577
Paul Elliott0c683352022-12-16 19:16:56 +00006578 psa_interruptible_set_max_ops(max_ops);
6579
Paul Elliott6f600372023-02-06 18:41:05 +00006580 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6581 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006582
Paul Elliott712d5122022-12-07 14:03:10 +00006583 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6584 TEST_ASSERT(num_ops_prior == 0);
6585
6586 /* Start performing the signature. */
6587 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6588 input_data->x, input_data->len));
6589
6590 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6591 TEST_ASSERT(num_ops_prior == 0);
6592
6593 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006594 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006595 status = psa_sign_hash_complete(&operation, signature, signature_size,
6596 &signature_length);
6597
Paul Elliott0c683352022-12-16 19:16:56 +00006598 num_completes++;
6599
Paul Elliott712d5122022-12-07 14:03:10 +00006600 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6601 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006602 /* We are asserting here that every complete makes progress
6603 * (completes some ops), which is true of the internal
6604 * implementation and probably any implementation, however this is
6605 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006606 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006607
Paul Elliott712d5122022-12-07 14:03:10 +00006608 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006609
6610 /* Ensure calling get_num_ops() twice still returns the same
6611 * number of ops as previously reported. */
6612 num_ops = psa_sign_hash_get_num_ops(&operation);
6613
6614 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006615 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006616 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006617
6618 TEST_ASSERT(status == PSA_SUCCESS);
6619
Paul Elliott0c683352022-12-16 19:16:56 +00006620 TEST_LE_U(min_completes, num_completes);
6621 TEST_LE_U(num_completes, max_completes);
6622
Paul Elliott712d5122022-12-07 14:03:10 +00006623 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006624 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006625 signature, signature_length);
Paul Elliott712d5122022-12-07 14:03:10 +00006626
6627 PSA_ASSERT(psa_sign_hash_abort(&operation));
6628
Paul Elliott59ad9452022-12-18 15:09:02 +00006629 num_ops = psa_sign_hash_get_num_ops(&operation);
6630 TEST_ASSERT(num_ops == 0);
6631
Paul Elliott712d5122022-12-07 14:03:10 +00006632exit:
6633
6634 /*
6635 * Key attributes may have been returned by psa_get_key_attributes()
6636 * thus reset them as required.
6637 */
6638 psa_reset_key_attributes(&attributes);
6639
6640 psa_destroy_key(key);
6641 mbedtls_free(signature);
6642 PSA_DONE();
6643}
6644/* END_CASE */
6645
Gilles Peskine20035e32018-02-03 22:44:14 +01006646/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006647void sign_hash_fail(int key_type_arg, data_t *key_data,
6648 int alg_arg, data_t *input_data,
6649 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006650{
Ronald Cron5425a212020-08-04 14:58:35 +02006651 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006652 psa_key_type_t key_type = key_type_arg;
6653 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006654 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006655 psa_status_t actual_status;
6656 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006657 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006658 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006659 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006660
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006661 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006662
Gilles Peskine449bd832023-01-11 14:50:10 +01006663 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006664
Gilles Peskine449bd832023-01-11 14:50:10 +01006665 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6666 psa_set_key_algorithm(&attributes, alg);
6667 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006668
Gilles Peskine449bd832023-01-11 14:50:10 +01006669 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6670 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006671
Gilles Peskine449bd832023-01-11 14:50:10 +01006672 actual_status = psa_sign_hash(key, alg,
6673 input_data->x, input_data->len,
6674 signature, signature_size,
6675 &signature_length);
6676 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006677 /* The value of *signature_length is unspecified on error, but
6678 * whatever it is, it should be less than signature_size, so that
6679 * if the caller tries to read *signature_length bytes without
6680 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006681 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006682
6683exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006684 psa_reset_key_attributes(&attributes);
6685 psa_destroy_key(key);
6686 mbedtls_free(signature);
6687 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006688}
6689/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006690
Paul Elliott91007972022-12-16 12:21:24 +00006691/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006692/**
6693 * sign_hash_fail_interruptible() test intentions:
6694 *
6695 * Note: This test can currently only handle ECDSA.
6696 *
6697 * 1. Test that various failure cases for interruptible sign hash fail with the
6698 * correct error codes, and at the correct point (at start or during
6699 * complete).
6700 *
6701 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6702 * expected for different max_ops values.
6703 *
6704 * 3. Test that the number of ops done prior to start and after abort is zero
6705 * and that each successful stage completes some ops (this is not mandated by
6706 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006707 *
6708 * 4. Check that calling complete() when start() fails and complete()
6709 * after completion results in a BAD_STATE error.
6710 *
6711 * 5. Check that calling start() again after start fails results in a BAD_STATE
6712 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006713 */
Paul Elliott91007972022-12-16 12:21:24 +00006714void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6715 int alg_arg, data_t *input_data,
6716 int signature_size_arg,
6717 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006718 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006719 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006720{
6721 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6722 psa_key_type_t key_type = key_type_arg;
6723 psa_algorithm_t alg = alg_arg;
6724 size_t signature_size = signature_size_arg;
6725 psa_status_t actual_status;
6726 psa_status_t expected_start_status = expected_start_status_arg;
6727 psa_status_t expected_complete_status = expected_complete_status_arg;
6728 unsigned char *signature = NULL;
6729 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006730 uint32_t num_ops = 0;
6731 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006732 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006733 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006734 size_t min_completes = 0;
6735 size_t max_completes = 0;
6736
Paul Elliott91007972022-12-16 12:21:24 +00006737 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6738 psa_sign_hash_interruptible_operation_t operation =
6739 psa_sign_hash_interruptible_operation_init();
6740
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006741 TEST_CALLOC(signature, signature_size);
Paul Elliott91007972022-12-16 12:21:24 +00006742
6743 PSA_ASSERT(psa_crypto_init());
6744
6745 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6746 psa_set_key_algorithm(&attributes, alg);
6747 psa_set_key_type(&attributes, key_type);
6748
6749 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6750 &key));
6751
Paul Elliott0c683352022-12-16 19:16:56 +00006752 psa_interruptible_set_max_ops(max_ops);
6753
Paul Elliott6f600372023-02-06 18:41:05 +00006754 interruptible_signverify_get_minmax_completes(max_ops,
6755 expected_complete_status,
6756 &min_completes,
6757 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006758
Paul Elliott91007972022-12-16 12:21:24 +00006759 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6760 TEST_ASSERT(num_ops_prior == 0);
6761
6762 /* Start performing the signature. */
6763 actual_status = psa_sign_hash_start(&operation, key, alg,
6764 input_data->x, input_data->len);
6765
6766 TEST_EQUAL(actual_status, expected_start_status);
6767
Paul Elliottc9774412023-02-06 15:14:07 +00006768 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006769 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006770 * start failed. */
6771 actual_status = psa_sign_hash_complete(&operation, signature,
6772 signature_size,
6773 &signature_length);
6774
6775 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6776
6777 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006778 actual_status = psa_sign_hash_start(&operation, key, alg,
6779 input_data->x, input_data->len);
6780
6781 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6782 }
6783
Paul Elliott91007972022-12-16 12:21:24 +00006784 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6785 TEST_ASSERT(num_ops_prior == 0);
6786
Paul Elliott91007972022-12-16 12:21:24 +00006787 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006788 do {
Paul Elliott91007972022-12-16 12:21:24 +00006789 actual_status = psa_sign_hash_complete(&operation, signature,
6790 signature_size,
6791 &signature_length);
6792
Paul Elliott0c683352022-12-16 19:16:56 +00006793 num_completes++;
6794
Paul Elliott334d7262023-01-20 17:29:41 +00006795 if (actual_status == PSA_SUCCESS ||
6796 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006797 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006798 /* We are asserting here that every complete makes progress
6799 * (completes some ops), which is true of the internal
6800 * implementation and probably any implementation, however this is
6801 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006802 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006803
Paul Elliott91007972022-12-16 12:21:24 +00006804 num_ops_prior = num_ops;
6805 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006806 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006807
Paul Elliottc9774412023-02-06 15:14:07 +00006808 TEST_EQUAL(actual_status, expected_complete_status);
6809
Paul Elliottefebad02023-02-15 16:56:45 +00006810 /* Check that another complete returns BAD_STATE. */
6811 actual_status = psa_sign_hash_complete(&operation, signature,
6812 signature_size,
6813 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006814
Paul Elliottefebad02023-02-15 16:56:45 +00006815 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006816
Paul Elliott91007972022-12-16 12:21:24 +00006817 PSA_ASSERT(psa_sign_hash_abort(&operation));
6818
Paul Elliott59ad9452022-12-18 15:09:02 +00006819 num_ops = psa_sign_hash_get_num_ops(&operation);
6820 TEST_ASSERT(num_ops == 0);
6821
Paul Elliott91007972022-12-16 12:21:24 +00006822 /* The value of *signature_length is unspecified on error, but
6823 * whatever it is, it should be less than signature_size, so that
6824 * if the caller tries to read *signature_length bytes without
6825 * checking the error code then they don't overflow a buffer. */
6826 TEST_LE_U(signature_length, signature_size);
6827
Paul Elliott0c683352022-12-16 19:16:56 +00006828 TEST_LE_U(min_completes, num_completes);
6829 TEST_LE_U(num_completes, max_completes);
6830
Paul Elliott91007972022-12-16 12:21:24 +00006831exit:
6832 psa_reset_key_attributes(&attributes);
6833 psa_destroy_key(key);
6834 mbedtls_free(signature);
6835 PSA_DONE();
6836}
6837/* END_CASE */
6838
mohammad16038cc1cee2018-03-28 01:21:33 +03006839/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006840void sign_verify_hash(int key_type_arg, data_t *key_data,
6841 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006842{
Ronald Cron5425a212020-08-04 14:58:35 +02006843 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006844 psa_key_type_t key_type = key_type_arg;
6845 psa_algorithm_t alg = alg_arg;
6846 size_t key_bits;
6847 unsigned char *signature = NULL;
6848 size_t signature_size;
6849 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006850 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006851
Gilles Peskine449bd832023-01-11 14:50:10 +01006852 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006853
Gilles Peskine449bd832023-01-11 14:50:10 +01006854 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6855 psa_set_key_algorithm(&attributes, alg);
6856 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006857
Gilles Peskine449bd832023-01-11 14:50:10 +01006858 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6859 &key));
6860 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6861 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006862
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006863 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006864 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006865 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6866 key_bits, alg);
6867 TEST_ASSERT(signature_size != 0);
6868 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006869 TEST_CALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006870
6871 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006872 PSA_ASSERT(psa_sign_hash(key, alg,
6873 input_data->x, input_data->len,
6874 signature, signature_size,
6875 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006876 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006877 TEST_LE_U(signature_length, signature_size);
6878 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006879
6880 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006881 PSA_ASSERT(psa_verify_hash(key, alg,
6882 input_data->x, input_data->len,
6883 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006884
Gilles Peskine449bd832023-01-11 14:50:10 +01006885 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006886 /* Flip a bit in the input and verify that the signature is now
6887 * detected as invalid. Flip a bit at the beginning, not at the end,
6888 * because ECDSA may ignore the last few bits of the input. */
6889 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006890 TEST_EQUAL(psa_verify_hash(key, alg,
6891 input_data->x, input_data->len,
6892 signature, signature_length),
6893 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006894 }
6895
6896exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006897 /*
6898 * Key attributes may have been returned by psa_get_key_attributes()
6899 * thus reset them as required.
6900 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006901 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006902
Gilles Peskine449bd832023-01-11 14:50:10 +01006903 psa_destroy_key(key);
6904 mbedtls_free(signature);
6905 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006906}
6907/* END_CASE */
6908
Paul Elliott712d5122022-12-07 14:03:10 +00006909/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006910/**
6911 * sign_verify_hash_interruptible() test intentions:
6912 *
6913 * Note: This test can currently only handle ECDSA.
6914 *
Paul Elliott8c092052023-03-06 17:49:14 +00006915 * 1. Test that we can sign an input hash with the given keypair and then
6916 * afterwards verify that signature. This is currently the only way to test
6917 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00006918 *
6919 * 2. Test that after corrupting the hash, the verification detects an invalid
6920 * signature.
6921 *
6922 * 3. Test the number of calls to psa_sign_hash_complete() required are as
6923 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00006924 *
6925 * 4. Test that the number of ops done prior to starting signing and after abort
6926 * is zero and that each successful signing stage completes some ops (this is
6927 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00006928 */
Paul Elliott712d5122022-12-07 14:03:10 +00006929void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006930 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006931 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006932{
6933 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6934 psa_key_type_t key_type = key_type_arg;
6935 psa_algorithm_t alg = alg_arg;
6936 size_t key_bits;
6937 unsigned char *signature = NULL;
6938 size_t signature_size;
6939 size_t signature_length = 0xdeadbeef;
6940 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6941 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006942 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00006943 uint32_t num_ops = 0;
6944 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006945 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006946 size_t min_completes = 0;
6947 size_t max_completes = 0;
6948
Paul Elliott712d5122022-12-07 14:03:10 +00006949 psa_sign_hash_interruptible_operation_t sign_operation =
6950 psa_sign_hash_interruptible_operation_init();
6951 psa_verify_hash_interruptible_operation_t verify_operation =
6952 psa_verify_hash_interruptible_operation_init();
6953
6954 PSA_ASSERT(psa_crypto_init());
6955
Paul Elliott0c683352022-12-16 19:16:56 +00006956 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6957 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006958 psa_set_key_algorithm(&attributes, alg);
6959 psa_set_key_type(&attributes, key_type);
6960
6961 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6962 &key));
6963 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6964 key_bits = psa_get_key_bits(&attributes);
6965
6966 /* Allocate a buffer which has the size advertised by the
6967 * library. */
6968 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6969 key_bits, alg);
6970 TEST_ASSERT(signature_size != 0);
6971 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006972 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006973
Paul Elliott0c683352022-12-16 19:16:56 +00006974 psa_interruptible_set_max_ops(max_ops);
6975
Paul Elliott6f600372023-02-06 18:41:05 +00006976 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6977 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006978
Paul Elliott7c173082023-02-26 18:44:45 +00006979 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6980 TEST_ASSERT(num_ops_prior == 0);
6981
Paul Elliott712d5122022-12-07 14:03:10 +00006982 /* Start performing the signature. */
6983 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6984 input_data->x, input_data->len));
6985
Paul Elliott7c173082023-02-26 18:44:45 +00006986 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6987 TEST_ASSERT(num_ops_prior == 0);
6988
Paul Elliott712d5122022-12-07 14:03:10 +00006989 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006990 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006991
Paul Elliott0c683352022-12-16 19:16:56 +00006992 status = psa_sign_hash_complete(&sign_operation, signature,
6993 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00006994 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00006995
6996 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00006997
6998 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6999 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7000 /* We are asserting here that every complete makes progress
7001 * (completes some ops), which is true of the internal
7002 * implementation and probably any implementation, however this is
7003 * not mandated by the PSA specification. */
7004 TEST_ASSERT(num_ops > num_ops_prior);
7005
7006 num_ops_prior = num_ops;
7007 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007008 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007009
7010 TEST_ASSERT(status == PSA_SUCCESS);
7011
Paul Elliott0c683352022-12-16 19:16:56 +00007012 TEST_LE_U(min_completes, num_completes);
7013 TEST_LE_U(num_completes, max_completes);
7014
Paul Elliott712d5122022-12-07 14:03:10 +00007015 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7016
Paul Elliott7c173082023-02-26 18:44:45 +00007017 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7018 TEST_ASSERT(num_ops == 0);
7019
Paul Elliott712d5122022-12-07 14:03:10 +00007020 /* Check that the signature length looks sensible. */
7021 TEST_LE_U(signature_length, signature_size);
7022 TEST_ASSERT(signature_length > 0);
7023
Paul Elliott0c683352022-12-16 19:16:56 +00007024 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007025
7026 /* Start verification. */
7027 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7028 input_data->x, input_data->len,
7029 signature, signature_length));
7030
7031 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007032 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007033 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007034
7035 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007036 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007037
7038 TEST_ASSERT(status == PSA_SUCCESS);
7039
Paul Elliott0c683352022-12-16 19:16:56 +00007040 TEST_LE_U(min_completes, num_completes);
7041 TEST_LE_U(num_completes, max_completes);
7042
Paul Elliott712d5122022-12-07 14:03:10 +00007043 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7044
7045 verify_operation = psa_verify_hash_interruptible_operation_init();
7046
7047 if (input_data->len != 0) {
7048 /* Flip a bit in the input and verify that the signature is now
7049 * detected as invalid. Flip a bit at the beginning, not at the end,
7050 * because ECDSA may ignore the last few bits of the input. */
7051 input_data->x[0] ^= 1;
7052
Paul Elliott712d5122022-12-07 14:03:10 +00007053 /* Start verification. */
7054 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7055 input_data->x, input_data->len,
7056 signature, signature_length));
7057
7058 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007059 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007060 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007061 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007062
7063 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7064 }
7065
7066 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7067
7068exit:
7069 /*
7070 * Key attributes may have been returned by psa_get_key_attributes()
7071 * thus reset them as required.
7072 */
7073 psa_reset_key_attributes(&attributes);
7074
7075 psa_destroy_key(key);
7076 mbedtls_free(signature);
7077 PSA_DONE();
7078}
7079/* END_CASE */
7080
Gilles Peskine9911b022018-06-29 17:30:48 +02007081/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007082void verify_hash(int key_type_arg, data_t *key_data,
7083 int alg_arg, data_t *hash_data,
7084 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007085{
Ronald Cron5425a212020-08-04 14:58:35 +02007086 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007087 psa_key_type_t key_type = key_type_arg;
7088 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007089 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007090
Gilles Peskine449bd832023-01-11 14:50:10 +01007091 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007092
Gilles Peskine449bd832023-01-11 14:50:10 +01007093 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007094
Gilles Peskine449bd832023-01-11 14:50:10 +01007095 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7096 psa_set_key_algorithm(&attributes, alg);
7097 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007098
Gilles Peskine449bd832023-01-11 14:50:10 +01007099 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7100 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007101
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 PSA_ASSERT(psa_verify_hash(key, alg,
7103 hash_data->x, hash_data->len,
7104 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007105
itayzafrir5c753392018-05-08 11:18:38 +03007106exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007107 psa_reset_key_attributes(&attributes);
7108 psa_destroy_key(key);
7109 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007110}
7111/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007112
Paul Elliott712d5122022-12-07 14:03:10 +00007113/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007114/**
7115 * verify_hash_interruptible() test intentions:
7116 *
7117 * Note: This test can currently only handle ECDSA.
7118 *
7119 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007120 * only). Given this test only does verification it can accept public keys as
7121 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007122 *
7123 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7124 * expected for different max_ops values.
7125 *
7126 * 3. Test that the number of ops done prior to start and after abort is zero
7127 * and that each successful stage completes some ops (this is not mandated by
7128 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007129 *
7130 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7131 * complete() calls does not alter the number of ops returned.
7132 *
7133 * 5. Test that after corrupting the hash, the verification detects an invalid
7134 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007135 */
Paul Elliott712d5122022-12-07 14:03:10 +00007136void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7137 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007138 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007139{
7140 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7141 psa_key_type_t key_type = key_type_arg;
7142 psa_algorithm_t alg = alg_arg;
7143 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7144 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007145 uint32_t num_ops = 0;
7146 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007147 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007148 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007149 size_t min_completes = 0;
7150 size_t max_completes = 0;
7151
Paul Elliott712d5122022-12-07 14:03:10 +00007152 psa_verify_hash_interruptible_operation_t operation =
7153 psa_verify_hash_interruptible_operation_init();
7154
7155 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7156
7157 PSA_ASSERT(psa_crypto_init());
7158
7159 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7160 psa_set_key_algorithm(&attributes, alg);
7161 psa_set_key_type(&attributes, key_type);
7162
7163 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7164 &key));
7165
Paul Elliott0c683352022-12-16 19:16:56 +00007166 psa_interruptible_set_max_ops(max_ops);
7167
Paul Elliott6f600372023-02-06 18:41:05 +00007168 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7169 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007170
Paul Elliott712d5122022-12-07 14:03:10 +00007171 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7172
7173 TEST_ASSERT(num_ops_prior == 0);
7174
7175 /* Start verification. */
7176 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7177 hash_data->x, hash_data->len,
7178 signature_data->x, signature_data->len)
7179 );
7180
7181 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7182
7183 TEST_ASSERT(num_ops_prior == 0);
7184
7185 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007186 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007187 status = psa_verify_hash_complete(&operation);
7188
Paul Elliott0c683352022-12-16 19:16:56 +00007189 num_completes++;
7190
Paul Elliott712d5122022-12-07 14:03:10 +00007191 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7192 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007193 /* We are asserting here that every complete makes progress
7194 * (completes some ops), which is true of the internal
7195 * implementation and probably any implementation, however this is
7196 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007197 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007198
Paul Elliott712d5122022-12-07 14:03:10 +00007199 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007200
7201 /* Ensure calling get_num_ops() twice still returns the same
7202 * number of ops as previously reported. */
7203 num_ops = psa_verify_hash_get_num_ops(&operation);
7204
7205 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007206 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007207 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007208
7209 TEST_ASSERT(status == PSA_SUCCESS);
7210
Paul Elliott0c683352022-12-16 19:16:56 +00007211 TEST_LE_U(min_completes, num_completes);
7212 TEST_LE_U(num_completes, max_completes);
7213
Paul Elliott712d5122022-12-07 14:03:10 +00007214 PSA_ASSERT(psa_verify_hash_abort(&operation));
7215
Paul Elliott59ad9452022-12-18 15:09:02 +00007216 num_ops = psa_verify_hash_get_num_ops(&operation);
7217 TEST_ASSERT(num_ops == 0);
7218
Paul Elliott8359c142023-02-24 18:40:10 +00007219 if (hash_data->len != 0) {
7220 /* Flip a bit in the hash and verify that the signature is now detected
7221 * as invalid. Flip a bit at the beginning, not at the end, because
7222 * ECDSA may ignore the last few bits of the input. */
7223 hash_data->x[0] ^= 1;
7224
7225 /* Start verification. */
7226 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7227 hash_data->x, hash_data->len,
7228 signature_data->x, signature_data->len));
7229
7230 /* Continue performing the signature until complete. */
7231 do {
7232 status = psa_verify_hash_complete(&operation);
7233 } while (status == PSA_OPERATION_INCOMPLETE);
7234
7235 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7236 }
7237
Paul Elliott712d5122022-12-07 14:03:10 +00007238exit:
7239 psa_reset_key_attributes(&attributes);
7240 psa_destroy_key(key);
7241 PSA_DONE();
7242}
7243/* END_CASE */
7244
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007245/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007246void verify_hash_fail(int key_type_arg, data_t *key_data,
7247 int alg_arg, data_t *hash_data,
7248 data_t *signature_data,
7249 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007250{
Ronald Cron5425a212020-08-04 14:58:35 +02007251 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007252 psa_key_type_t key_type = key_type_arg;
7253 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007254 psa_status_t actual_status;
7255 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007256 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007257
Gilles Peskine449bd832023-01-11 14:50:10 +01007258 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007259
Gilles Peskine449bd832023-01-11 14:50:10 +01007260 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7261 psa_set_key_algorithm(&attributes, alg);
7262 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007263
Gilles Peskine449bd832023-01-11 14:50:10 +01007264 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7265 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007266
Gilles Peskine449bd832023-01-11 14:50:10 +01007267 actual_status = psa_verify_hash(key, alg,
7268 hash_data->x, hash_data->len,
7269 signature_data->x, signature_data->len);
7270 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007271
7272exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007273 psa_reset_key_attributes(&attributes);
7274 psa_destroy_key(key);
7275 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007276}
7277/* END_CASE */
7278
Paul Elliott91007972022-12-16 12:21:24 +00007279/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007280/**
7281 * verify_hash_fail_interruptible() test intentions:
7282 *
7283 * Note: This test can currently only handle ECDSA.
7284 *
7285 * 1. Test that various failure cases for interruptible verify hash fail with
7286 * the correct error codes, and at the correct point (at start or during
7287 * complete).
7288 *
7289 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7290 * expected for different max_ops values.
7291 *
7292 * 3. Test that the number of ops done prior to start and after abort is zero
7293 * and that each successful stage completes some ops (this is not mandated by
7294 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007295 *
7296 * 4. Check that calling complete() when start() fails and complete()
7297 * after completion results in a BAD_STATE error.
7298 *
7299 * 5. Check that calling start() again after start fails results in a BAD_STATE
7300 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007301 */
Paul Elliott91007972022-12-16 12:21:24 +00007302void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7303 int alg_arg, data_t *hash_data,
7304 data_t *signature_data,
7305 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007306 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007307 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007308{
7309 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7310 psa_key_type_t key_type = key_type_arg;
7311 psa_algorithm_t alg = alg_arg;
7312 psa_status_t actual_status;
7313 psa_status_t expected_start_status = expected_start_status_arg;
7314 psa_status_t expected_complete_status = expected_complete_status_arg;
7315 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007316 uint32_t num_ops = 0;
7317 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007318 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007319 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007320 size_t min_completes = 0;
7321 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007322 psa_verify_hash_interruptible_operation_t operation =
7323 psa_verify_hash_interruptible_operation_init();
7324
7325 PSA_ASSERT(psa_crypto_init());
7326
7327 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7328 psa_set_key_algorithm(&attributes, alg);
7329 psa_set_key_type(&attributes, key_type);
7330
7331 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7332 &key));
7333
Paul Elliott0c683352022-12-16 19:16:56 +00007334 psa_interruptible_set_max_ops(max_ops);
7335
Paul Elliott6f600372023-02-06 18:41:05 +00007336 interruptible_signverify_get_minmax_completes(max_ops,
7337 expected_complete_status,
7338 &min_completes,
7339 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007340
Paul Elliott91007972022-12-16 12:21:24 +00007341 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7342 TEST_ASSERT(num_ops_prior == 0);
7343
7344 /* Start verification. */
7345 actual_status = psa_verify_hash_start(&operation, key, alg,
7346 hash_data->x, hash_data->len,
7347 signature_data->x,
7348 signature_data->len);
7349
7350 TEST_EQUAL(actual_status, expected_start_status);
7351
Paul Elliottc9774412023-02-06 15:14:07 +00007352 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007353 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007354 * start failed. */
7355 actual_status = psa_verify_hash_complete(&operation);
7356
7357 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7358
7359 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007360 actual_status = psa_verify_hash_start(&operation, key, alg,
7361 hash_data->x, hash_data->len,
7362 signature_data->x,
7363 signature_data->len);
7364
7365 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7366 }
7367
Paul Elliott91007972022-12-16 12:21:24 +00007368 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7369 TEST_ASSERT(num_ops_prior == 0);
7370
Paul Elliott91007972022-12-16 12:21:24 +00007371 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007372 do {
Paul Elliott91007972022-12-16 12:21:24 +00007373 actual_status = psa_verify_hash_complete(&operation);
7374
Paul Elliott0c683352022-12-16 19:16:56 +00007375 num_completes++;
7376
Paul Elliott334d7262023-01-20 17:29:41 +00007377 if (actual_status == PSA_SUCCESS ||
7378 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007379 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007380 /* We are asserting here that every complete makes progress
7381 * (completes some ops), which is true of the internal
7382 * implementation and probably any implementation, however this is
7383 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007384 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007385
Paul Elliott91007972022-12-16 12:21:24 +00007386 num_ops_prior = num_ops;
7387 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007388 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007389
Paul Elliottc9774412023-02-06 15:14:07 +00007390 TEST_EQUAL(actual_status, expected_complete_status);
7391
Paul Elliottefebad02023-02-15 16:56:45 +00007392 /* Check that another complete returns BAD_STATE. */
7393 actual_status = psa_verify_hash_complete(&operation);
7394 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007395
Paul Elliott0c683352022-12-16 19:16:56 +00007396 TEST_LE_U(min_completes, num_completes);
7397 TEST_LE_U(num_completes, max_completes);
7398
Paul Elliott91007972022-12-16 12:21:24 +00007399 PSA_ASSERT(psa_verify_hash_abort(&operation));
7400
Paul Elliott59ad9452022-12-18 15:09:02 +00007401 num_ops = psa_verify_hash_get_num_ops(&operation);
7402 TEST_ASSERT(num_ops == 0);
7403
Paul Elliott91007972022-12-16 12:21:24 +00007404exit:
7405 psa_reset_key_attributes(&attributes);
7406 psa_destroy_key(key);
7407 PSA_DONE();
7408}
7409/* END_CASE */
7410
Paul Elliott20a36062022-12-18 13:21:25 +00007411/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007412/**
7413 * interruptible_signverify_hash_state_test() test intentions:
7414 *
7415 * Note: This test can currently only handle ECDSA.
7416 *
7417 * 1. Test that calling the various interruptible sign and verify hash functions
7418 * in incorrect orders returns BAD_STATE errors.
7419 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007420void interruptible_signverify_hash_state_test(int key_type_arg,
7421 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007422{
7423 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7424 psa_key_type_t key_type = key_type_arg;
7425 psa_algorithm_t alg = alg_arg;
7426 size_t key_bits;
7427 unsigned char *signature = NULL;
7428 size_t signature_size;
7429 size_t signature_length = 0xdeadbeef;
7430 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7431 psa_sign_hash_interruptible_operation_t sign_operation =
7432 psa_sign_hash_interruptible_operation_init();
7433 psa_verify_hash_interruptible_operation_t verify_operation =
7434 psa_verify_hash_interruptible_operation_init();
7435
7436 PSA_ASSERT(psa_crypto_init());
7437
7438 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7439 PSA_KEY_USAGE_VERIFY_HASH);
7440 psa_set_key_algorithm(&attributes, alg);
7441 psa_set_key_type(&attributes, key_type);
7442
7443 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7444 &key));
7445 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7446 key_bits = psa_get_key_bits(&attributes);
7447
7448 /* Allocate a buffer which has the size advertised by the
7449 * library. */
7450 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7451 key_bits, alg);
7452 TEST_ASSERT(signature_size != 0);
7453 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007454 TEST_CALLOC(signature, signature_size);
Paul Elliott20a36062022-12-18 13:21:25 +00007455
7456 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7457
7458 /* --- Attempt completes prior to starts --- */
7459 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7460 signature_size,
7461 &signature_length),
7462 PSA_ERROR_BAD_STATE);
7463
7464 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7465
7466 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7467 PSA_ERROR_BAD_STATE);
7468
7469 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7470
7471 /* --- Aborts in all other places. --- */
7472 psa_sign_hash_abort(&sign_operation);
7473
7474 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7475 input_data->x, input_data->len));
7476
7477 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7478
7479 psa_interruptible_set_max_ops(1);
7480
7481 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7482 input_data->x, input_data->len));
7483
7484 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7485 signature_size,
7486 &signature_length),
7487 PSA_OPERATION_INCOMPLETE);
7488
7489 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7490
7491 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7492
7493 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7494 input_data->x, input_data->len));
7495
7496 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7497 signature_size,
7498 &signature_length));
7499
7500 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7501
7502 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7503
7504 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7505 input_data->x, input_data->len,
7506 signature, signature_length));
7507
7508 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7509
7510 psa_interruptible_set_max_ops(1);
7511
7512 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7513 input_data->x, input_data->len,
7514 signature, signature_length));
7515
7516 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7517 PSA_OPERATION_INCOMPLETE);
7518
7519 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7520
7521 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7522
7523 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7524 input_data->x, input_data->len,
7525 signature, signature_length));
7526
7527 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7528
7529 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7530
7531 /* --- Attempt double starts. --- */
7532
7533 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7534 input_data->x, input_data->len));
7535
7536 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7537 input_data->x, input_data->len),
7538 PSA_ERROR_BAD_STATE);
7539
7540 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7541
7542 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7543 input_data->x, input_data->len,
7544 signature, signature_length));
7545
7546 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7547 input_data->x, input_data->len,
7548 signature, signature_length),
7549 PSA_ERROR_BAD_STATE);
7550
7551 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7552
Paul Elliott76d671a2023-02-07 17:45:18 +00007553exit:
7554 /*
7555 * Key attributes may have been returned by psa_get_key_attributes()
7556 * thus reset them as required.
7557 */
7558 psa_reset_key_attributes(&attributes);
7559
7560 psa_destroy_key(key);
7561 mbedtls_free(signature);
7562 PSA_DONE();
7563}
7564/* END_CASE */
7565
7566/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007567/**
Paul Elliottc2033502023-02-26 17:09:14 +00007568 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007569 *
7570 * Note: This test can currently only handle ECDSA.
7571 *
7572 * 1. Test various edge cases in the interruptible sign and verify hash
7573 * interfaces.
7574 */
Paul Elliottc2033502023-02-26 17:09:14 +00007575void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007576 data_t *key_data, int alg_arg, data_t *input_data)
7577{
7578 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7579 psa_key_type_t key_type = key_type_arg;
7580 psa_algorithm_t alg = alg_arg;
7581 size_t key_bits;
7582 unsigned char *signature = NULL;
7583 size_t signature_size;
7584 size_t signature_length = 0xdeadbeef;
7585 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7586 uint8_t *input_buffer = NULL;
7587 psa_sign_hash_interruptible_operation_t sign_operation =
7588 psa_sign_hash_interruptible_operation_init();
7589 psa_verify_hash_interruptible_operation_t verify_operation =
7590 psa_verify_hash_interruptible_operation_init();
7591
7592 PSA_ASSERT(psa_crypto_init());
7593
7594 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7595 PSA_KEY_USAGE_VERIFY_HASH);
7596 psa_set_key_algorithm(&attributes, alg);
7597 psa_set_key_type(&attributes, key_type);
7598
7599 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7600 &key));
7601 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7602 key_bits = psa_get_key_bits(&attributes);
7603
7604 /* Allocate a buffer which has the size advertised by the
7605 * library. */
7606 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7607 key_bits, alg);
7608 TEST_ASSERT(signature_size != 0);
7609 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007610 TEST_CALLOC(signature, signature_size);
Paul Elliott76d671a2023-02-07 17:45:18 +00007611
Paul Elliott20a36062022-12-18 13:21:25 +00007612 /* --- Change function inputs mid run, to cause an error (sign only,
7613 * verify passes all inputs to start. --- */
7614
7615 psa_interruptible_set_max_ops(1);
7616
7617 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7618 input_data->x, input_data->len));
7619
7620 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7621 signature_size,
7622 &signature_length),
7623 PSA_OPERATION_INCOMPLETE);
7624
7625 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7626 0,
7627 &signature_length),
7628 PSA_ERROR_BUFFER_TOO_SMALL);
7629
Paul Elliottc9774412023-02-06 15:14:07 +00007630 /* And test that this invalidates the operation. */
7631 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7632 0,
7633 &signature_length),
7634 PSA_ERROR_BAD_STATE);
7635
Paul Elliott20a36062022-12-18 13:21:25 +00007636 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7637
Paul Elliottf9c91a72023-02-05 18:06:38 +00007638 /* Trash the hash buffer in between start and complete, to ensure
7639 * no reliance on external buffers. */
7640 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7641
7642 input_buffer = mbedtls_calloc(1, input_data->len);
7643 TEST_ASSERT(input_buffer != NULL);
7644
7645 memcpy(input_buffer, input_data->x, input_data->len);
7646
7647 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7648 input_buffer, input_data->len));
7649
7650 memset(input_buffer, '!', input_data->len);
7651 mbedtls_free(input_buffer);
7652 input_buffer = NULL;
7653
7654 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7655 signature_size,
7656 &signature_length));
7657
7658 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7659
7660 input_buffer = mbedtls_calloc(1, input_data->len);
7661 TEST_ASSERT(input_buffer != NULL);
7662
7663 memcpy(input_buffer, input_data->x, input_data->len);
7664
7665 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7666 input_buffer, input_data->len,
7667 signature, signature_length));
7668
7669 memset(input_buffer, '!', input_data->len);
7670 mbedtls_free(input_buffer);
7671 input_buffer = NULL;
7672
7673 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7674
7675 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7676
Paul Elliott20a36062022-12-18 13:21:25 +00007677exit:
7678 /*
7679 * Key attributes may have been returned by psa_get_key_attributes()
7680 * thus reset them as required.
7681 */
7682 psa_reset_key_attributes(&attributes);
7683
7684 psa_destroy_key(key);
7685 mbedtls_free(signature);
7686 PSA_DONE();
7687}
7688/* END_CASE */
7689
Paul Elliotta4cb9092023-02-07 18:01:55 +00007690/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007691/**
Paul Elliott57702242023-02-26 20:36:10 +00007692 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007693 *
7694 * Note: This test can currently only handle ECDSA.
7695 *
7696 * 1. Test that setting max ops is reflected in both interruptible sign and
7697 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007698 * 2. Test that changing the value of max_ops to unlimited during an operation
7699 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007700 *
7701 * 3. Test that calling get_num_ops() between complete calls gives the same
7702 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007703 */
Paul Elliott57702242023-02-26 20:36:10 +00007704void interruptible_signverify_hash_ops_tests(int key_type_arg,
7705 data_t *key_data, int alg_arg,
7706 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007707{
7708 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7709 psa_key_type_t key_type = key_type_arg;
7710 psa_algorithm_t alg = alg_arg;
7711 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007712 size_t key_bits;
7713 unsigned char *signature = NULL;
7714 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007715 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007716 uint32_t num_ops = 0;
7717 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7718
Paul Elliotta4cb9092023-02-07 18:01:55 +00007719 psa_sign_hash_interruptible_operation_t sign_operation =
7720 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007721 psa_verify_hash_interruptible_operation_t verify_operation =
7722 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007723
7724 PSA_ASSERT(psa_crypto_init());
7725
7726 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7727 PSA_KEY_USAGE_VERIFY_HASH);
7728 psa_set_key_algorithm(&attributes, alg);
7729 psa_set_key_type(&attributes, key_type);
7730
Paul Elliottf1743e22023-02-15 18:44:16 +00007731 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7732 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7733 key_bits = psa_get_key_bits(&attributes);
7734
7735 /* Allocate a buffer which has the size advertised by the
7736 * library. */
7737 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7738
7739 TEST_ASSERT(signature_size != 0);
7740 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007741 TEST_CALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007742
7743 /* Check that default max ops gets set if we don't set it. */
7744 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7745 input_data->x, input_data->len));
7746
7747 TEST_EQUAL(psa_interruptible_get_max_ops(),
7748 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7749
7750 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7751
Paul Elliottf1743e22023-02-15 18:44:16 +00007752 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7753 input_data->x, input_data->len,
7754 signature, signature_size));
7755
7756 TEST_EQUAL(psa_interruptible_get_max_ops(),
7757 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7758
7759 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7760
Paul Elliotta4cb9092023-02-07 18:01:55 +00007761 /* Check that max ops gets set properly. */
7762
7763 psa_interruptible_set_max_ops(0xbeef);
7764
Paul Elliottf1743e22023-02-15 18:44:16 +00007765 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007766
Paul Elliott9e8819f2023-02-26 19:01:35 +00007767 /* --- Ensure changing the max ops mid operation works (operation should
7768 * complete successfully after setting max ops to unlimited --- */
7769 psa_interruptible_set_max_ops(1);
7770
7771 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7772 input_data->x, input_data->len));
7773
7774 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7775 signature_size,
7776 &signature_length),
7777 PSA_OPERATION_INCOMPLETE);
7778
7779 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7780
7781 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7782 signature_size,
7783 &signature_length));
7784
7785 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7786
7787 psa_interruptible_set_max_ops(1);
7788
7789 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7790 input_data->x, input_data->len,
7791 signature, signature_length));
7792
7793 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7794 PSA_OPERATION_INCOMPLETE);
7795
7796 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7797
7798 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7799
7800 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7801
Paul Elliottc1e04002023-02-26 20:27:23 +00007802 /* --- Test that not calling get_num_ops inbetween complete calls does not
7803 * result in lost ops. ---*/
7804
7805 psa_interruptible_set_max_ops(1);
7806
7807 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7808 input_data->x, input_data->len));
7809
7810 /* Continue performing the signature until complete. */
7811 do {
7812 status = psa_sign_hash_complete(&sign_operation, signature,
7813 signature_size,
7814 &signature_length);
7815
7816 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7817
7818 } while (status == PSA_OPERATION_INCOMPLETE);
7819
7820 PSA_ASSERT(status);
7821
7822 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7823
7824 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7825 input_data->x, input_data->len));
7826
7827 /* Continue performing the signature until complete. */
7828 do {
7829 status = psa_sign_hash_complete(&sign_operation, signature,
7830 signature_size,
7831 &signature_length);
7832 } while (status == PSA_OPERATION_INCOMPLETE);
7833
7834 PSA_ASSERT(status);
7835
7836 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7837
7838 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7839
7840 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7841 input_data->x, input_data->len,
7842 signature, signature_length));
7843
7844 /* Continue performing the verification until complete. */
7845 do {
7846 status = psa_verify_hash_complete(&verify_operation);
7847
7848 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7849
7850 } while (status == PSA_OPERATION_INCOMPLETE);
7851
7852 PSA_ASSERT(status);
7853
7854 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7855
7856 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7857 input_data->x, input_data->len,
7858 signature, signature_length));
7859
7860 /* Continue performing the verification until complete. */
7861 do {
7862 status = psa_verify_hash_complete(&verify_operation);
7863
7864 } while (status == PSA_OPERATION_INCOMPLETE);
7865
7866 PSA_ASSERT(status);
7867
7868 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7869
7870 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7871
Paul Elliotta4cb9092023-02-07 18:01:55 +00007872exit:
7873 /*
7874 * Key attributes may have been returned by psa_get_key_attributes()
7875 * thus reset them as required.
7876 */
7877 psa_reset_key_attributes(&attributes);
7878
7879 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007880 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007881 PSA_DONE();
7882}
7883/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007884
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007885/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007886void sign_message_deterministic(int key_type_arg,
7887 data_t *key_data,
7888 int alg_arg,
7889 data_t *input_data,
7890 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007891{
7892 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7893 psa_key_type_t key_type = key_type_arg;
7894 psa_algorithm_t alg = alg_arg;
7895 size_t key_bits;
7896 unsigned char *signature = NULL;
7897 size_t signature_size;
7898 size_t signature_length = 0xdeadbeef;
7899 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7900
Gilles Peskine449bd832023-01-11 14:50:10 +01007901 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007902
Gilles Peskine449bd832023-01-11 14:50:10 +01007903 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7904 psa_set_key_algorithm(&attributes, alg);
7905 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007906
Gilles Peskine449bd832023-01-11 14:50:10 +01007907 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7908 &key));
7909 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7910 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007911
Gilles Peskine449bd832023-01-11 14:50:10 +01007912 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7913 TEST_ASSERT(signature_size != 0);
7914 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007915 TEST_CALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007916
Gilles Peskine449bd832023-01-11 14:50:10 +01007917 PSA_ASSERT(psa_sign_message(key, alg,
7918 input_data->x, input_data->len,
7919 signature, signature_size,
7920 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007921
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01007922 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01007923 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007924
7925exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007926 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007927
Gilles Peskine449bd832023-01-11 14:50:10 +01007928 psa_destroy_key(key);
7929 mbedtls_free(signature);
7930 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007931
7932}
7933/* END_CASE */
7934
7935/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007936void sign_message_fail(int key_type_arg,
7937 data_t *key_data,
7938 int alg_arg,
7939 data_t *input_data,
7940 int signature_size_arg,
7941 int expected_status_arg)
7942{
7943 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7944 psa_key_type_t key_type = key_type_arg;
7945 psa_algorithm_t alg = alg_arg;
7946 size_t signature_size = signature_size_arg;
7947 psa_status_t actual_status;
7948 psa_status_t expected_status = expected_status_arg;
7949 unsigned char *signature = NULL;
7950 size_t signature_length = 0xdeadbeef;
7951 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7952
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007953 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01007954
7955 PSA_ASSERT(psa_crypto_init());
7956
7957 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7958 psa_set_key_algorithm(&attributes, alg);
7959 psa_set_key_type(&attributes, key_type);
7960
7961 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7962 &key));
7963
7964 actual_status = psa_sign_message(key, alg,
7965 input_data->x, input_data->len,
7966 signature, signature_size,
7967 &signature_length);
7968 TEST_EQUAL(actual_status, expected_status);
7969 /* The value of *signature_length is unspecified on error, but
7970 * whatever it is, it should be less than signature_size, so that
7971 * if the caller tries to read *signature_length bytes without
7972 * checking the error code then they don't overflow a buffer. */
7973 TEST_LE_U(signature_length, signature_size);
7974
7975exit:
7976 psa_reset_key_attributes(&attributes);
7977 psa_destroy_key(key);
7978 mbedtls_free(signature);
7979 PSA_DONE();
7980}
7981/* END_CASE */
7982
7983/* BEGIN_CASE */
7984void sign_verify_message(int key_type_arg,
7985 data_t *key_data,
7986 int alg_arg,
7987 data_t *input_data)
7988{
7989 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7990 psa_key_type_t key_type = key_type_arg;
7991 psa_algorithm_t alg = alg_arg;
7992 size_t key_bits;
7993 unsigned char *signature = NULL;
7994 size_t signature_size;
7995 size_t signature_length = 0xdeadbeef;
7996 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7997
7998 PSA_ASSERT(psa_crypto_init());
7999
8000 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
8001 PSA_KEY_USAGE_VERIFY_MESSAGE);
8002 psa_set_key_algorithm(&attributes, alg);
8003 psa_set_key_type(&attributes, key_type);
8004
8005 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8006 &key));
8007 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8008 key_bits = psa_get_key_bits(&attributes);
8009
8010 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8011 TEST_ASSERT(signature_size != 0);
8012 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008013 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008014
8015 PSA_ASSERT(psa_sign_message(key, alg,
8016 input_data->x, input_data->len,
8017 signature, signature_size,
8018 &signature_length));
8019 TEST_LE_U(signature_length, signature_size);
8020 TEST_ASSERT(signature_length > 0);
8021
8022 PSA_ASSERT(psa_verify_message(key, alg,
8023 input_data->x, input_data->len,
8024 signature, signature_length));
8025
8026 if (input_data->len != 0) {
8027 /* Flip a bit in the input and verify that the signature is now
8028 * detected as invalid. Flip a bit at the beginning, not at the end,
8029 * because ECDSA may ignore the last few bits of the input. */
8030 input_data->x[0] ^= 1;
8031 TEST_EQUAL(psa_verify_message(key, alg,
8032 input_data->x, input_data->len,
8033 signature, signature_length),
8034 PSA_ERROR_INVALID_SIGNATURE);
8035 }
8036
8037exit:
8038 psa_reset_key_attributes(&attributes);
8039
8040 psa_destroy_key(key);
8041 mbedtls_free(signature);
8042 PSA_DONE();
8043}
8044/* END_CASE */
8045
8046/* BEGIN_CASE */
8047void verify_message(int key_type_arg,
8048 data_t *key_data,
8049 int alg_arg,
8050 data_t *input_data,
8051 data_t *signature_data)
8052{
8053 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8054 psa_key_type_t key_type = key_type_arg;
8055 psa_algorithm_t alg = alg_arg;
8056 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8057
8058 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8059
8060 PSA_ASSERT(psa_crypto_init());
8061
8062 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8063 psa_set_key_algorithm(&attributes, alg);
8064 psa_set_key_type(&attributes, key_type);
8065
8066 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8067 &key));
8068
8069 PSA_ASSERT(psa_verify_message(key, alg,
8070 input_data->x, input_data->len,
8071 signature_data->x, signature_data->len));
8072
8073exit:
8074 psa_reset_key_attributes(&attributes);
8075 psa_destroy_key(key);
8076 PSA_DONE();
8077}
8078/* END_CASE */
8079
8080/* BEGIN_CASE */
8081void verify_message_fail(int key_type_arg,
8082 data_t *key_data,
8083 int alg_arg,
8084 data_t *hash_data,
8085 data_t *signature_data,
8086 int expected_status_arg)
8087{
8088 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8089 psa_key_type_t key_type = key_type_arg;
8090 psa_algorithm_t alg = alg_arg;
8091 psa_status_t actual_status;
8092 psa_status_t expected_status = expected_status_arg;
8093 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8094
8095 PSA_ASSERT(psa_crypto_init());
8096
8097 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8098 psa_set_key_algorithm(&attributes, alg);
8099 psa_set_key_type(&attributes, key_type);
8100
8101 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8102 &key));
8103
8104 actual_status = psa_verify_message(key, alg,
8105 hash_data->x, hash_data->len,
8106 signature_data->x,
8107 signature_data->len);
8108 TEST_EQUAL(actual_status, expected_status);
8109
8110exit:
8111 psa_reset_key_attributes(&attributes);
8112 psa_destroy_key(key);
8113 PSA_DONE();
8114}
8115/* END_CASE */
8116
8117/* BEGIN_CASE */
8118void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008119 data_t *key_data,
8120 int alg_arg,
8121 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008122 data_t *label,
8123 int expected_output_length_arg,
8124 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008125{
Ronald Cron5425a212020-08-04 14:58:35 +02008126 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008127 psa_key_type_t key_type = key_type_arg;
8128 psa_algorithm_t alg = alg_arg;
8129 size_t expected_output_length = expected_output_length_arg;
8130 size_t key_bits;
8131 unsigned char *output = NULL;
8132 size_t output_size;
8133 size_t output_length = ~0;
8134 psa_status_t actual_status;
8135 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008137
Gilles Peskine449bd832023-01-11 14:50:10 +01008138 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008139
Gilles Peskine656896e2018-06-29 19:12:28 +02008140 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008141 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8142 psa_set_key_algorithm(&attributes, alg);
8143 psa_set_key_type(&attributes, key_type);
8144 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8145 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008146
8147 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008148 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8149 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008150
Gilles Peskine449bd832023-01-11 14:50:10 +01008151 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8152 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008153 TEST_CALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008154
8155 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008156 actual_status = psa_asymmetric_encrypt(key, alg,
8157 input_data->x, input_data->len,
8158 label->x, label->len,
8159 output, output_size,
8160 &output_length);
8161 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008162 if (actual_status == PSA_SUCCESS) {
8163 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008164 } else {
8165 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008166 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008167
Gilles Peskine68428122018-06-30 18:42:41 +02008168 /* If the label is empty, the test framework puts a non-null pointer
8169 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008170 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008171 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008172 if (output_size != 0) {
8173 memset(output, 0, output_size);
8174 }
8175 actual_status = psa_asymmetric_encrypt(key, alg,
8176 input_data->x, input_data->len,
8177 NULL, label->len,
8178 output, output_size,
8179 &output_length);
8180 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008181 if (actual_status == PSA_SUCCESS) {
8182 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008183 } else {
8184 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008185 }
Gilles Peskine68428122018-06-30 18:42:41 +02008186 }
8187
Gilles Peskine656896e2018-06-29 19:12:28 +02008188exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008189 /*
8190 * Key attributes may have been returned by psa_get_key_attributes()
8191 * thus reset them as required.
8192 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008193 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008194
Gilles Peskine449bd832023-01-11 14:50:10 +01008195 psa_destroy_key(key);
8196 mbedtls_free(output);
8197 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008198}
8199/* END_CASE */
8200
8201/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008202void asymmetric_encrypt_decrypt(int key_type_arg,
8203 data_t *key_data,
8204 int alg_arg,
8205 data_t *input_data,
8206 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008207{
Ronald Cron5425a212020-08-04 14:58:35 +02008208 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008209 psa_key_type_t key_type = key_type_arg;
8210 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008211 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008212 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008213 size_t output_size;
8214 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008215 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008216 size_t output2_size;
8217 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008218 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008219
Gilles Peskine449bd832023-01-11 14:50:10 +01008220 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008221
Gilles Peskine449bd832023-01-11 14:50:10 +01008222 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8223 psa_set_key_algorithm(&attributes, alg);
8224 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008225
Gilles Peskine449bd832023-01-11 14:50:10 +01008226 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8227 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008228
8229 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008230 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8231 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008232
Gilles Peskine449bd832023-01-11 14:50:10 +01008233 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8234 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008235 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008236
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008237 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008238 TEST_LE_U(output2_size,
8239 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8240 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008241 TEST_CALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008242
Gilles Peskineeebd7382018-06-08 18:11:54 +02008243 /* We test encryption by checking that encrypt-then-decrypt gives back
8244 * the original plaintext because of the non-optional random
8245 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008246 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8247 input_data->x, input_data->len,
8248 label->x, label->len,
8249 output, output_size,
8250 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008251 /* We don't know what ciphertext length to expect, but check that
8252 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008253 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008254
Gilles Peskine449bd832023-01-11 14:50:10 +01008255 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8256 output, output_length,
8257 label->x, label->len,
8258 output2, output2_size,
8259 &output2_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008260 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008261 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008262
8263exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008264 /*
8265 * Key attributes may have been returned by psa_get_key_attributes()
8266 * thus reset them as required.
8267 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008268 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008269
Gilles Peskine449bd832023-01-11 14:50:10 +01008270 psa_destroy_key(key);
8271 mbedtls_free(output);
8272 mbedtls_free(output2);
8273 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008274}
8275/* END_CASE */
8276
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008277/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008278void asymmetric_decrypt(int key_type_arg,
8279 data_t *key_data,
8280 int alg_arg,
8281 data_t *input_data,
8282 data_t *label,
8283 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008284{
Ronald Cron5425a212020-08-04 14:58:35 +02008285 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008286 psa_key_type_t key_type = key_type_arg;
8287 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008288 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008289 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008290 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008291 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008292 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008293
Gilles Peskine449bd832023-01-11 14:50:10 +01008294 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008295
Gilles Peskine449bd832023-01-11 14:50:10 +01008296 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8297 psa_set_key_algorithm(&attributes, alg);
8298 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008299
Gilles Peskine449bd832023-01-11 14:50:10 +01008300 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8301 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008302
Gilles Peskine449bd832023-01-11 14:50:10 +01008303 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8304 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008305
8306 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008307 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8308 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008309 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008310
Gilles Peskine449bd832023-01-11 14:50:10 +01008311 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8312 input_data->x, input_data->len,
8313 label->x, label->len,
8314 output,
8315 output_size,
8316 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008317 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008318 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008319
Gilles Peskine68428122018-06-30 18:42:41 +02008320 /* If the label is empty, the test framework puts a non-null pointer
8321 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008322 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008323 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008324 if (output_size != 0) {
8325 memset(output, 0, output_size);
8326 }
8327 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8328 input_data->x, input_data->len,
8329 NULL, label->len,
8330 output,
8331 output_size,
8332 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008333 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008334 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008335 }
8336
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008337exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008338 psa_reset_key_attributes(&attributes);
8339 psa_destroy_key(key);
8340 mbedtls_free(output);
8341 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008342}
8343/* END_CASE */
8344
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008345/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008346void asymmetric_decrypt_fail(int key_type_arg,
8347 data_t *key_data,
8348 int alg_arg,
8349 data_t *input_data,
8350 data_t *label,
8351 int output_size_arg,
8352 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008353{
Ronald Cron5425a212020-08-04 14:58:35 +02008354 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008355 psa_key_type_t key_type = key_type_arg;
8356 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008357 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008358 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008359 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008360 psa_status_t actual_status;
8361 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008362 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008363
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008364 TEST_CALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008365
Gilles Peskine449bd832023-01-11 14:50:10 +01008366 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008367
Gilles Peskine449bd832023-01-11 14:50:10 +01008368 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8369 psa_set_key_algorithm(&attributes, alg);
8370 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008371
Gilles Peskine449bd832023-01-11 14:50:10 +01008372 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8373 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008374
Gilles Peskine449bd832023-01-11 14:50:10 +01008375 actual_status = psa_asymmetric_decrypt(key, alg,
8376 input_data->x, input_data->len,
8377 label->x, label->len,
8378 output, output_size,
8379 &output_length);
8380 TEST_EQUAL(actual_status, expected_status);
8381 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008382
Gilles Peskine68428122018-06-30 18:42:41 +02008383 /* If the label is empty, the test framework puts a non-null pointer
8384 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008385 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008386 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008387 if (output_size != 0) {
8388 memset(output, 0, output_size);
8389 }
8390 actual_status = psa_asymmetric_decrypt(key, alg,
8391 input_data->x, input_data->len,
8392 NULL, label->len,
8393 output, output_size,
8394 &output_length);
8395 TEST_EQUAL(actual_status, expected_status);
8396 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008397 }
8398
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008399exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008400 psa_reset_key_attributes(&attributes);
8401 psa_destroy_key(key);
8402 mbedtls_free(output);
8403 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008404}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008405/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008406
8407/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008408void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008409{
8410 /* Test each valid way of initializing the object, except for `= {0}`, as
8411 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8412 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008413 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008414 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008415 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008416 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8417 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008418
Gilles Peskine449bd832023-01-11 14:50:10 +01008419 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008420
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008421 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008422 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8423 PSA_ERROR_BAD_STATE);
8424 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8425 PSA_ERROR_BAD_STATE);
8426 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8427 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008428
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008429 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008430 PSA_ASSERT(psa_key_derivation_abort(&func));
8431 PSA_ASSERT(psa_key_derivation_abort(&init));
8432 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008433}
8434/* END_CASE */
8435
Janos Follath16de4a42019-06-13 16:32:24 +01008436/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008437void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008438{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008439 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008440 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008441 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008442
Gilles Peskine449bd832023-01-11 14:50:10 +01008443 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008444
Gilles Peskine449bd832023-01-11 14:50:10 +01008445 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8446 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008447
8448exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008449 psa_key_derivation_abort(&operation);
8450 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008451}
8452/* END_CASE */
8453
Janos Follathaf3c2a02019-06-12 12:34:34 +01008454/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008455void derive_set_capacity(int alg_arg, int capacity_arg,
8456 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008457{
8458 psa_algorithm_t alg = alg_arg;
8459 size_t capacity = capacity_arg;
8460 psa_status_t expected_status = expected_status_arg;
8461 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8462
Gilles Peskine449bd832023-01-11 14:50:10 +01008463 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008464
Gilles Peskine449bd832023-01-11 14:50:10 +01008465 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008466
Gilles Peskine449bd832023-01-11 14:50:10 +01008467 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8468 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008469
8470exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008471 psa_key_derivation_abort(&operation);
8472 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008473}
8474/* END_CASE */
8475
8476/* BEGIN_CASE */
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308477void parse_binary_string_test(data_t *input, int output)
8478{
8479 uint64_t value;
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308480 value = mbedtls_test_parse_binary_string(input);
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308481 TEST_EQUAL(value, output);
8482}
8483/* END_CASE */
8484
8485/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008486void derive_input(int alg_arg,
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308487 int step_arg1, int key_type_arg1, data_t *input1,
8488 int expected_status_arg1,
8489 int step_arg2, int key_type_arg2, data_t *input2,
8490 int expected_status_arg2,
8491 int step_arg3, int key_type_arg3, data_t *input3,
8492 int expected_status_arg3,
Gilles Peskine449bd832023-01-11 14:50:10 +01008493 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008494{
8495 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008496 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308497 uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
Gilles Peskine449bd832023-01-11 14:50:10 +01008498 psa_status_t expected_statuses[] = { expected_status_arg1,
8499 expected_status_arg2,
8500 expected_status_arg3 };
8501 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008502 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8503 MBEDTLS_SVC_KEY_ID_INIT,
8504 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008505 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8506 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8507 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008508 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008509 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008510 psa_status_t expected_output_status = expected_output_status_arg;
8511 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008512
Gilles Peskine449bd832023-01-11 14:50:10 +01008513 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008514
Gilles Peskine449bd832023-01-11 14:50:10 +01008515 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8516 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008517
Gilles Peskine449bd832023-01-11 14:50:10 +01008518 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008519
Gilles Peskine449bd832023-01-11 14:50:10 +01008520 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8521 mbedtls_test_set_step(i);
8522 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008523 /* Skip this step */
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308524 } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE &&
8525 key_types[i] != INPUT_INTEGER) {
8526 psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i]));
Gilles Peskine449bd832023-01-11 14:50:10 +01008527 PSA_ASSERT(psa_import_key(&attributes,
8528 inputs[i]->x, inputs[i]->len,
8529 &keys[i]));
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308530 if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008531 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008532 // When taking a private key as secret input, use key agreement
8533 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008534 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8535 &operation, keys[i]),
8536 expected_statuses[i]);
8537 } else {
8538 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8539 keys[i]),
8540 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008541 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008542 } else {
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308543 if (key_types[i] == INPUT_INTEGER) {
8544 TEST_EQUAL(psa_key_derivation_input_integer(
8545 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308546 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308547 expected_statuses[i]);
8548 } else {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308549 TEST_EQUAL(psa_key_derivation_input_bytes(
8550 &operation, steps[i],
8551 inputs[i]->x, inputs[i]->len),
8552 expected_statuses[i]);
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308553 }
Janos Follathaf3c2a02019-06-12 12:34:34 +01008554 }
8555 }
8556
Gilles Peskine449bd832023-01-11 14:50:10 +01008557 if (output_key_type != PSA_KEY_TYPE_NONE) {
8558 psa_reset_key_attributes(&attributes);
8559 psa_set_key_type(&attributes, output_key_type);
8560 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008561 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008562 psa_key_derivation_output_key(&attributes, &operation,
8563 &output_key);
8564 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008565 uint8_t buffer[1];
8566 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008567 psa_key_derivation_output_bytes(&operation,
8568 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008569 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008570 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008571
Janos Follathaf3c2a02019-06-12 12:34:34 +01008572exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008573 psa_key_derivation_abort(&operation);
8574 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8575 psa_destroy_key(keys[i]);
8576 }
8577 psa_destroy_key(output_key);
8578 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008579}
8580/* END_CASE */
8581
Kusumit Ghoderao42b02b92023-06-06 16:48:46 +05308582/* BEGIN_CASE*/
8583void derive_input_invalid_cost(int alg_arg, int64_t cost)
8584{
8585 psa_algorithm_t alg = alg_arg;
8586 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8587
8588 PSA_ASSERT(psa_crypto_init());
8589 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8590
8591 TEST_EQUAL(psa_key_derivation_input_integer(&operation,
8592 PSA_KEY_DERIVATION_INPUT_COST,
8593 cost),
8594 PSA_ERROR_NOT_SUPPORTED);
8595
8596exit:
8597 psa_key_derivation_abort(&operation);
8598 PSA_DONE();
8599}
8600/* END_CASE*/
8601
Janos Follathd958bb72019-07-03 15:02:16 +01008602/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008603void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008604{
Janos Follathd958bb72019-07-03 15:02:16 +01008605 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008606 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008607 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008608 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008609 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008610 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008611 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008612 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008613 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008614 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008615 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8616 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008617 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008618 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008619
Gilles Peskine449bd832023-01-11 14:50:10 +01008620 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008621
Gilles Peskine449bd832023-01-11 14:50:10 +01008622 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8623 psa_set_key_algorithm(&attributes, alg);
8624 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008625
Gilles Peskine449bd832023-01-11 14:50:10 +01008626 PSA_ASSERT(psa_import_key(&attributes,
8627 key_data, sizeof(key_data),
8628 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008629
8630 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008631 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8632 input1, input1_length,
8633 input2, input2_length,
8634 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008635 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008636 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008637
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008638 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008639 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8640 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008641
Gilles Peskine449bd832023-01-11 14:50:10 +01008642 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008643
Gilles Peskine449bd832023-01-11 14:50:10 +01008644 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8645 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008646
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008647exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008648 psa_key_derivation_abort(&operation);
8649 psa_destroy_key(key);
8650 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008651}
8652/* END_CASE */
8653
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008654/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008655void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008656{
8657 uint8_t output_buffer[16];
8658 size_t buffer_size = 16;
8659 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008660 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008661
Gilles Peskine449bd832023-01-11 14:50:10 +01008662 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8663 output_buffer, buffer_size)
8664 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008665
Gilles Peskine449bd832023-01-11 14:50:10 +01008666 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8667 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008668
Gilles Peskine449bd832023-01-11 14:50:10 +01008669 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008670
Gilles Peskine449bd832023-01-11 14:50:10 +01008671 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8672 output_buffer, buffer_size)
8673 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008674
Gilles Peskine449bd832023-01-11 14:50:10 +01008675 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8676 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008677
8678exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008679 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008680}
8681/* END_CASE */
8682
8683/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008684void derive_output(int alg_arg,
8685 int step1_arg, data_t *input1, int expected_status_arg1,
8686 int step2_arg, data_t *input2, int expected_status_arg2,
8687 int step3_arg, data_t *input3, int expected_status_arg3,
8688 int step4_arg, data_t *input4, int expected_status_arg4,
8689 data_t *key_agreement_peer_key,
8690 int requested_capacity_arg,
8691 data_t *expected_output1,
8692 data_t *expected_output2,
8693 int other_key_input_type,
8694 int key_input_type,
8695 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008696{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008697 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008698 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8699 data_t *inputs[] = { input1, input2, input3, input4 };
8700 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8701 MBEDTLS_SVC_KEY_ID_INIT,
8702 MBEDTLS_SVC_KEY_ID_INIT,
8703 MBEDTLS_SVC_KEY_ID_INIT };
8704 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8705 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008706 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008707 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008708 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008709 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008710 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008711 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008712 size_t output_buffer_size = 0;
8713 uint8_t *output_buffer = NULL;
8714 size_t expected_capacity;
8715 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008716 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8717 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8718 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8719 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008720 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008721 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008722 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008723
Gilles Peskine449bd832023-01-11 14:50:10 +01008724 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8725 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008726 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008727 }
8728 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008729 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008730 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008731 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008732 TEST_CALLOC(output_buffer, output_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008733 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008734
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008735 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008736 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8737 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8738 requested_capacity));
8739 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8740 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008741 case 0:
8742 break;
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308743 case PSA_KEY_DERIVATION_INPUT_COST:
8744 TEST_EQUAL(psa_key_derivation_input_integer(
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308745 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308746 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308747 statuses[i]);
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308748 if (statuses[i] != PSA_SUCCESS) {
8749 goto exit;
8750 }
8751 break;
8752 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Gilles Peskine1468da72019-05-29 17:35:49 +02008753 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008754 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008755 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008756 TEST_EQUAL(psa_key_derivation_input_bytes(
8757 &operation, steps[i],
8758 inputs[i]->x, inputs[i]->len),
8759 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008760
Gilles Peskine449bd832023-01-11 14:50:10 +01008761 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008762 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008763 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008764 break;
8765 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008766 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8767 psa_set_key_algorithm(&attributes1, alg);
8768 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008769
Gilles Peskine449bd832023-01-11 14:50:10 +01008770 PSA_ASSERT(psa_import_key(&attributes1,
8771 inputs[i]->x, inputs[i]->len,
8772 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008773
Gilles Peskine449bd832023-01-11 14:50:10 +01008774 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8775 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8776 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8777 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008778 }
8779
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308780 TEST_EQUAL(psa_key_derivation_input_key(&operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01008781 steps[i],
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308782 keys[i]),
8783 statuses[i]);
8784
8785 if (statuses[i] != PSA_SUCCESS) {
8786 goto exit;
8787 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008788 break;
8789 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008790 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008791 break;
8792 }
8793 break;
8794 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008795 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008796 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008797 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8798 steps[i],
8799 inputs[i]->x,
8800 inputs[i]->len),
8801 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008802 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008803 case 1: // input key, type DERIVE
8804 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008805 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8806 psa_set_key_algorithm(&attributes2, alg);
8807 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008808
8809 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008810 if (other_key_input_type == 11) {
8811 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8812 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008813
Gilles Peskine449bd832023-01-11 14:50:10 +01008814 PSA_ASSERT(psa_import_key(&attributes2,
8815 inputs[i]->x, inputs[i]->len,
8816 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008817
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8819 steps[i],
8820 keys[i]),
8821 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008822 break;
8823 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008824 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8825 psa_set_key_algorithm(&attributes3, alg);
8826 psa_set_key_type(&attributes3,
8827 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008828
Gilles Peskine449bd832023-01-11 14:50:10 +01008829 PSA_ASSERT(psa_import_key(&attributes3,
8830 inputs[i]->x, inputs[i]->len,
8831 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008832
Gilles Peskine449bd832023-01-11 14:50:10 +01008833 TEST_EQUAL(psa_key_derivation_key_agreement(
8834 &operation,
8835 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8836 keys[i], key_agreement_peer_key->x,
8837 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008838 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008839 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008840 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008841 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008842 }
8843
Gilles Peskine449bd832023-01-11 14:50:10 +01008844 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008845 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008846 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008847 break;
8848 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008849 TEST_EQUAL(psa_key_derivation_input_bytes(
8850 &operation, steps[i],
8851 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008852
Gilles Peskine449bd832023-01-11 14:50:10 +01008853 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008854 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008855 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008856 break;
8857 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008858 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008859
Gilles Peskine449bd832023-01-11 14:50:10 +01008860 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8861 &current_capacity));
8862 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008863 expected_capacity = requested_capacity;
8864
Gilles Peskine449bd832023-01-11 14:50:10 +01008865 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008866 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8867
8868 /* For output key derivation secret must be provided using
8869 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008870 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008871 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008872 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008873
Gilles Peskine449bd832023-01-11 14:50:10 +01008874 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8875 psa_set_key_algorithm(&attributes4, alg);
8876 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8877 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008878
Gilles Peskine449bd832023-01-11 14:50:10 +01008879 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8880 &derived_key), expected_status);
8881 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008882 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008883 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008884 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008885 status = psa_key_derivation_output_bytes(&operation,
8886 output_buffer, output_sizes[i]);
8887 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008888 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008889 TEST_ASSERT(status == PSA_SUCCESS ||
8890 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008891 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008892 } else if (expected_capacity == 0 ||
8893 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008894 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008895 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008896 expected_capacity = 0;
8897 continue;
8898 }
8899 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008900 PSA_ASSERT(status);
8901 if (output_sizes[i] != 0) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008902 TEST_MEMORY_COMPARE(output_buffer, output_sizes[i],
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008903 expected_outputs[i], output_sizes[i]);
Gilles Peskine449bd832023-01-11 14:50:10 +01008904 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008905 /* Check the operation status. */
8906 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008907 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8908 &current_capacity));
8909 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008910 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008911 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008912 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008913
8914exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008915 mbedtls_free(output_buffer);
8916 psa_key_derivation_abort(&operation);
8917 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8918 psa_destroy_key(keys[i]);
8919 }
8920 psa_destroy_key(derived_key);
8921 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008922}
8923/* END_CASE */
8924
8925/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008926void derive_full(int alg_arg,
8927 data_t *key_data,
8928 data_t *input1,
8929 data_t *input2,
8930 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008931{
Ronald Cron5425a212020-08-04 14:58:35 +02008932 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008933 psa_algorithm_t alg = alg_arg;
8934 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008935 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008936 unsigned char output_buffer[16];
8937 size_t expected_capacity = requested_capacity;
8938 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008939 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008940
Gilles Peskine449bd832023-01-11 14:50:10 +01008941 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008942
Gilles Peskine449bd832023-01-11 14:50:10 +01008943 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8944 psa_set_key_algorithm(&attributes, alg);
8945 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008946
Gilles Peskine449bd832023-01-11 14:50:10 +01008947 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8948 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008949
Gilles Peskine449bd832023-01-11 14:50:10 +01008950 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8951 input1->x, input1->len,
8952 input2->x, input2->len,
8953 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008954 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008955 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008956
Gilles Peskine449bd832023-01-11 14:50:10 +01008957 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8958 &current_capacity));
8959 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008960
8961 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008962 while (current_capacity > 0) {
8963 size_t read_size = sizeof(output_buffer);
8964 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008965 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008966 }
8967 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8968 output_buffer,
8969 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008970 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008971 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8972 &current_capacity));
8973 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008974 }
8975
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008976 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008977 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8978 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008979
Gilles Peskine449bd832023-01-11 14:50:10 +01008980 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008981
8982exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008983 psa_key_derivation_abort(&operation);
8984 psa_destroy_key(key);
8985 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008986}
8987/* END_CASE */
8988
Stephan Koch78109f52023-04-12 14:19:36 +02008989/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008990void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8991 int derivation_step,
8992 int capacity, int expected_capacity_status_arg,
8993 data_t *expected_output,
8994 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008995{
8996 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
8997 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04008998 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008999 uint8_t *output_buffer = NULL;
9000 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04009001 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
9002 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
9003 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009004
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009005 TEST_CALLOC(output_buffer, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009006 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009007
Gilles Peskine449bd832023-01-11 14:50:10 +01009008 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9009 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
9010 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009011
Gilles Peskine449bd832023-01-11 14:50:10 +01009012 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
9013 step, input->x, input->len),
9014 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009015
Gilles Peskine449bd832023-01-11 14:50:10 +01009016 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009017 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009018 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009019
Gilles Peskine449bd832023-01-11 14:50:10 +01009020 status = psa_key_derivation_output_bytes(&operation, output_buffer,
9021 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009022
Gilles Peskine449bd832023-01-11 14:50:10 +01009023 TEST_EQUAL(status, expected_output_status);
9024 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009025 TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009026 expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009027 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009028
9029exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009030 mbedtls_free(output_buffer);
9031 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009032 PSA_DONE();
9033}
9034/* END_CASE */
9035
Janos Follathe60c9052019-07-03 13:51:30 +01009036/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009037void derive_key_exercise(int alg_arg,
9038 data_t *key_data,
9039 data_t *input1,
9040 data_t *input2,
9041 int derived_type_arg,
9042 int derived_bits_arg,
9043 int derived_usage_arg,
9044 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009045{
Ronald Cron5425a212020-08-04 14:58:35 +02009046 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9047 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009048 psa_algorithm_t alg = alg_arg;
9049 psa_key_type_t derived_type = derived_type_arg;
9050 size_t derived_bits = derived_bits_arg;
9051 psa_key_usage_t derived_usage = derived_usage_arg;
9052 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009053 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009054 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009055 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009056 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009057
Gilles Peskine449bd832023-01-11 14:50:10 +01009058 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009059
Gilles Peskine449bd832023-01-11 14:50:10 +01009060 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9061 psa_set_key_algorithm(&attributes, alg);
9062 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9063 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9064 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009065
9066 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009067 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9068 input1->x, input1->len,
9069 input2->x, input2->len,
9070 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009071 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009072 }
Janos Follathe60c9052019-07-03 13:51:30 +01009073
Gilles Peskine449bd832023-01-11 14:50:10 +01009074 psa_set_key_usage_flags(&attributes, derived_usage);
9075 psa_set_key_algorithm(&attributes, derived_alg);
9076 psa_set_key_type(&attributes, derived_type);
9077 psa_set_key_bits(&attributes, derived_bits);
9078 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9079 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009080
9081 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009082 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9083 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9084 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009085
9086 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009087 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009088 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009089 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009090
9091exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009092 /*
9093 * Key attributes may have been returned by psa_get_key_attributes()
9094 * thus reset them as required.
9095 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009096 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009097
Gilles Peskine449bd832023-01-11 14:50:10 +01009098 psa_key_derivation_abort(&operation);
9099 psa_destroy_key(base_key);
9100 psa_destroy_key(derived_key);
9101 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009102}
9103/* END_CASE */
9104
Janos Follath42fd8882019-07-03 14:17:09 +01009105/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009106void derive_key_export(int alg_arg,
9107 data_t *key_data,
9108 data_t *input1,
9109 data_t *input2,
9110 int bytes1_arg,
9111 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009112{
Ronald Cron5425a212020-08-04 14:58:35 +02009113 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9114 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009115 psa_algorithm_t alg = alg_arg;
9116 size_t bytes1 = bytes1_arg;
9117 size_t bytes2 = bytes2_arg;
9118 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009119 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009120 uint8_t *output_buffer = NULL;
9121 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009122 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9123 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009124 size_t length;
9125
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009126 TEST_CALLOC(output_buffer, capacity);
9127 TEST_CALLOC(export_buffer, capacity);
Gilles Peskine449bd832023-01-11 14:50:10 +01009128 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009129
Gilles Peskine449bd832023-01-11 14:50:10 +01009130 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9131 psa_set_key_algorithm(&base_attributes, alg);
9132 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9133 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9134 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009135
9136 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009137 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9138 input1->x, input1->len,
9139 input2->x, input2->len,
9140 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009141 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009142 }
Janos Follath42fd8882019-07-03 14:17:09 +01009143
Gilles Peskine449bd832023-01-11 14:50:10 +01009144 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9145 output_buffer,
9146 capacity));
9147 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009148
9149 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009150 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9151 input1->x, input1->len,
9152 input2->x, input2->len,
9153 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009154 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009155 }
Janos Follath42fd8882019-07-03 14:17:09 +01009156
Gilles Peskine449bd832023-01-11 14:50:10 +01009157 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9158 psa_set_key_algorithm(&derived_attributes, 0);
9159 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9160 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9161 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9162 &derived_key));
9163 PSA_ASSERT(psa_export_key(derived_key,
9164 export_buffer, bytes1,
9165 &length));
9166 TEST_EQUAL(length, bytes1);
9167 PSA_ASSERT(psa_destroy_key(derived_key));
9168 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9169 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9170 &derived_key));
9171 PSA_ASSERT(psa_export_key(derived_key,
9172 export_buffer + bytes1, bytes2,
9173 &length));
9174 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009175
9176 /* Compare the outputs from the two runs. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009177 TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009178 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009179
9180exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009181 mbedtls_free(output_buffer);
9182 mbedtls_free(export_buffer);
9183 psa_key_derivation_abort(&operation);
9184 psa_destroy_key(base_key);
9185 psa_destroy_key(derived_key);
9186 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009187}
9188/* END_CASE */
9189
9190/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009191void derive_key_type(int alg_arg,
9192 data_t *key_data,
9193 data_t *input1,
9194 data_t *input2,
9195 int key_type_arg, int bits_arg,
9196 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009197{
9198 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9199 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9200 const psa_algorithm_t alg = alg_arg;
9201 const psa_key_type_t key_type = key_type_arg;
9202 const size_t bits = bits_arg;
9203 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9204 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009205 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009206 uint8_t *export_buffer = NULL;
9207 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9208 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9209 size_t export_length;
9210
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009211 TEST_CALLOC(export_buffer, export_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01009212 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009213
Gilles Peskine449bd832023-01-11 14:50:10 +01009214 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9215 psa_set_key_algorithm(&base_attributes, alg);
9216 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9217 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9218 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009219
Gilles Peskine449bd832023-01-11 14:50:10 +01009220 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009221 &operation, base_key, alg,
9222 input1->x, input1->len,
9223 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009224 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009225 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009226 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009227
Gilles Peskine449bd832023-01-11 14:50:10 +01009228 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9229 psa_set_key_algorithm(&derived_attributes, 0);
9230 psa_set_key_type(&derived_attributes, key_type);
9231 psa_set_key_bits(&derived_attributes, bits);
9232 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9233 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009234
Gilles Peskine449bd832023-01-11 14:50:10 +01009235 PSA_ASSERT(psa_export_key(derived_key,
9236 export_buffer, export_buffer_size,
9237 &export_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009238 TEST_MEMORY_COMPARE(export_buffer, export_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009239 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009240
9241exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009242 mbedtls_free(export_buffer);
9243 psa_key_derivation_abort(&operation);
9244 psa_destroy_key(base_key);
9245 psa_destroy_key(derived_key);
9246 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009247}
9248/* END_CASE */
9249
9250/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009251void derive_key(int alg_arg,
9252 data_t *key_data, data_t *input1, data_t *input2,
9253 int type_arg, int bits_arg,
9254 int expected_status_arg,
9255 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009256{
Ronald Cron5425a212020-08-04 14:58:35 +02009257 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9258 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009259 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009260 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009261 size_t bits = bits_arg;
9262 psa_status_t expected_status = expected_status_arg;
9263 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9264 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9265 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9266
Gilles Peskine449bd832023-01-11 14:50:10 +01009267 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009268
Gilles Peskine449bd832023-01-11 14:50:10 +01009269 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9270 psa_set_key_algorithm(&base_attributes, alg);
9271 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9272 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9273 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009274
Gilles Peskine449bd832023-01-11 14:50:10 +01009275 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9276 input1->x, input1->len,
9277 input2->x, input2->len,
9278 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009279 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009280 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009281
Gilles Peskine449bd832023-01-11 14:50:10 +01009282 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9283 psa_set_key_algorithm(&derived_attributes, 0);
9284 psa_set_key_type(&derived_attributes, type);
9285 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009286
9287 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009288 psa_key_derivation_output_key(&derived_attributes,
9289 &operation,
9290 &derived_key);
9291 if (is_large_output > 0) {
9292 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9293 }
9294 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009295
9296exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009297 psa_key_derivation_abort(&operation);
9298 psa_destroy_key(base_key);
9299 psa_destroy_key(derived_key);
9300 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009301}
9302/* END_CASE */
9303
9304/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009305void key_agreement_setup(int alg_arg,
9306 int our_key_type_arg, int our_key_alg_arg,
9307 data_t *our_key_data, data_t *peer_key_data,
9308 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009309{
Ronald Cron5425a212020-08-04 14:58:35 +02009310 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009311 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009312 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009313 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009314 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009315 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009316 psa_status_t expected_status = expected_status_arg;
9317 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009318
Gilles Peskine449bd832023-01-11 14:50:10 +01009319 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009320
Gilles Peskine449bd832023-01-11 14:50:10 +01009321 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9322 psa_set_key_algorithm(&attributes, our_key_alg);
9323 psa_set_key_type(&attributes, our_key_type);
9324 PSA_ASSERT(psa_import_key(&attributes,
9325 our_key_data->x, our_key_data->len,
9326 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009327
Gilles Peskine77f40d82019-04-11 21:27:06 +02009328 /* The tests currently include inputs that should fail at either step.
9329 * Test cases that fail at the setup step should be changed to call
9330 * key_derivation_setup instead, and this function should be renamed
9331 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009332 status = psa_key_derivation_setup(&operation, alg);
9333 if (status == PSA_SUCCESS) {
9334 TEST_EQUAL(psa_key_derivation_key_agreement(
9335 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9336 our_key,
9337 peer_key_data->x, peer_key_data->len),
9338 expected_status);
9339 } else {
9340 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009341 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009342
9343exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009344 psa_key_derivation_abort(&operation);
9345 psa_destroy_key(our_key);
9346 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009347}
9348/* END_CASE */
9349
9350/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009351void raw_key_agreement(int alg_arg,
9352 int our_key_type_arg, data_t *our_key_data,
9353 data_t *peer_key_data,
9354 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009355{
Ronald Cron5425a212020-08-04 14:58:35 +02009356 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009357 psa_algorithm_t alg = alg_arg;
9358 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009360 unsigned char *output = NULL;
9361 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009362 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009363
Gilles Peskine449bd832023-01-11 14:50:10 +01009364 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009365
Gilles Peskine449bd832023-01-11 14:50:10 +01009366 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9367 psa_set_key_algorithm(&attributes, alg);
9368 psa_set_key_type(&attributes, our_key_type);
9369 PSA_ASSERT(psa_import_key(&attributes,
9370 our_key_data->x, our_key_data->len,
9371 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009372
Gilles Peskine449bd832023-01-11 14:50:10 +01009373 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9374 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009375
Gilles Peskine992bee82022-04-13 23:25:52 +02009376 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009377 TEST_LE_U(expected_output->len,
9378 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9379 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9380 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009381
9382 /* Good case with exact output size */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009383 TEST_CALLOC(output, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009384 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9385 peer_key_data->x, peer_key_data->len,
9386 output, expected_output->len,
9387 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009388 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009389 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009390 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009391 output = NULL;
9392 output_length = ~0;
9393
9394 /* Larger buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009395 TEST_CALLOC(output, expected_output->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009396 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9397 peer_key_data->x, peer_key_data->len,
9398 output, expected_output->len + 1,
9399 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009400 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009401 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009402 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009403 output = NULL;
9404 output_length = ~0;
9405
9406 /* Buffer too small */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009407 TEST_CALLOC(output, expected_output->len - 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009408 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9409 peer_key_data->x, peer_key_data->len,
9410 output, expected_output->len - 1,
9411 &output_length),
9412 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009413 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009414 TEST_LE_U(output_length, expected_output->len - 1);
9415 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009416 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009417
9418exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009419 mbedtls_free(output);
9420 psa_destroy_key(our_key);
9421 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009422}
9423/* END_CASE */
9424
9425/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009426void key_agreement_capacity(int alg_arg,
9427 int our_key_type_arg, data_t *our_key_data,
9428 data_t *peer_key_data,
9429 int expected_capacity_arg)
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 Peskine59685592018-09-18 12:11:34 +02009436 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009437 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009438
Gilles Peskine449bd832023-01-11 14:50:10 +01009439 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009440
Gilles Peskine449bd832023-01-11 14:50:10 +01009441 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9442 psa_set_key_algorithm(&attributes, alg);
9443 psa_set_key_type(&attributes, our_key_type);
9444 PSA_ASSERT(psa_import_key(&attributes,
9445 our_key_data->x, our_key_data->len,
9446 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009447
Gilles Peskine449bd832023-01-11 14:50:10 +01009448 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9449 PSA_ASSERT(psa_key_derivation_key_agreement(
9450 &operation,
9451 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9452 peer_key_data->x, peer_key_data->len));
9453 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009454 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009455 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9456 PSA_KEY_DERIVATION_INPUT_INFO,
9457 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009458 }
Gilles Peskine59685592018-09-18 12:11:34 +02009459
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009460 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009461 PSA_ASSERT(psa_key_derivation_get_capacity(
9462 &operation, &actual_capacity));
9463 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009464
Gilles Peskinebf491972018-10-25 22:36:12 +02009465 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009466 while (actual_capacity > sizeof(output)) {
9467 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9468 output, sizeof(output)));
9469 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009470 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009471 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9472 output, actual_capacity));
9473 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9474 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009475
Gilles Peskine59685592018-09-18 12:11:34 +02009476exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009477 psa_key_derivation_abort(&operation);
9478 psa_destroy_key(our_key);
9479 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009480}
9481/* END_CASE */
9482
9483/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009484void key_agreement_output(int alg_arg,
9485 int our_key_type_arg, data_t *our_key_data,
9486 data_t *peer_key_data,
9487 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009488{
Ronald Cron5425a212020-08-04 14:58:35 +02009489 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009490 psa_algorithm_t alg = alg_arg;
9491 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009492 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009493 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009494 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009495
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009496 TEST_CALLOC(actual_output, MAX(expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009497 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009498
Gilles Peskine449bd832023-01-11 14:50:10 +01009499 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009500
Gilles Peskine449bd832023-01-11 14:50:10 +01009501 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9502 psa_set_key_algorithm(&attributes, alg);
9503 psa_set_key_type(&attributes, our_key_type);
9504 PSA_ASSERT(psa_import_key(&attributes,
9505 our_key_data->x, our_key_data->len,
9506 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009507
Gilles Peskine449bd832023-01-11 14:50:10 +01009508 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9509 PSA_ASSERT(psa_key_derivation_key_agreement(
9510 &operation,
9511 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9512 peer_key_data->x, peer_key_data->len));
9513 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009514 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009515 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9516 PSA_KEY_DERIVATION_INPUT_INFO,
9517 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009518 }
Gilles Peskine59685592018-09-18 12:11:34 +02009519
Gilles Peskine449bd832023-01-11 14:50:10 +01009520 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9521 actual_output,
9522 expected_output1->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009523 TEST_MEMORY_COMPARE(actual_output, expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009524 expected_output1->x, expected_output1->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009525 if (expected_output2->len != 0) {
9526 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9527 actual_output,
9528 expected_output2->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009529 TEST_MEMORY_COMPARE(actual_output, expected_output2->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009530 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009531 }
Gilles Peskine59685592018-09-18 12:11:34 +02009532
9533exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009534 psa_key_derivation_abort(&operation);
9535 psa_destroy_key(our_key);
9536 PSA_DONE();
9537 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009538}
9539/* END_CASE */
9540
9541/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009542void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009543{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009544 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009545 unsigned char *output = NULL;
9546 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009547 size_t i;
9548 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009549
Gilles Peskine449bd832023-01-11 14:50:10 +01009550 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009551
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009552 TEST_CALLOC(output, bytes);
9553 TEST_CALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009554
Gilles Peskine449bd832023-01-11 14:50:10 +01009555 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009556
Gilles Peskinea50d7392018-06-21 10:22:13 +02009557 /* Run several times, to ensure that every output byte will be
9558 * nonzero at least once with overwhelming probability
9559 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009560 for (run = 0; run < 10; run++) {
9561 if (bytes != 0) {
9562 memset(output, 0, bytes);
9563 }
9564 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009565
Gilles Peskine449bd832023-01-11 14:50:10 +01009566 for (i = 0; i < bytes; i++) {
9567 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009568 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009569 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009570 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009571 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009572
9573 /* Check that every byte was changed to nonzero at least once. This
9574 * validates that psa_generate_random is overwriting every byte of
9575 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009576 for (i = 0; i < bytes; i++) {
9577 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009578 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009579
9580exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009581 PSA_DONE();
9582 mbedtls_free(output);
9583 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009584}
9585/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009586
9587/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009588void generate_key(int type_arg,
9589 int bits_arg,
9590 int usage_arg,
9591 int alg_arg,
9592 int expected_status_arg,
9593 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009594{
Ronald Cron5425a212020-08-04 14:58:35 +02009595 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009596 psa_key_type_t type = type_arg;
9597 psa_key_usage_t usage = usage_arg;
9598 size_t bits = bits_arg;
9599 psa_algorithm_t alg = alg_arg;
9600 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009601 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009602 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009603
Gilles Peskine449bd832023-01-11 14:50:10 +01009604 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009605
Gilles Peskine449bd832023-01-11 14:50:10 +01009606 psa_set_key_usage_flags(&attributes, usage);
9607 psa_set_key_algorithm(&attributes, alg);
9608 psa_set_key_type(&attributes, type);
9609 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009610
9611 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009612 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009613
Gilles Peskine449bd832023-01-11 14:50:10 +01009614 if (is_large_key > 0) {
9615 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9616 }
9617 TEST_EQUAL(status, expected_status);
9618 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009619 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009620 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009621
9622 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009623 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9624 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9625 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009626
Gilles Peskine818ca122018-06-20 18:16:48 +02009627 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009628 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009629 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009630 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009631
9632exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009633 /*
9634 * Key attributes may have been returned by psa_get_key_attributes()
9635 * thus reset them as required.
9636 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009637 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009638
Gilles Peskine449bd832023-01-11 14:50:10 +01009639 psa_destroy_key(key);
9640 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009641}
9642/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009643
Valerio Setti19fec542023-07-25 12:31:50 +02009644/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine449bd832023-01-11 14:50:10 +01009645void generate_key_rsa(int bits_arg,
9646 data_t *e_arg,
9647 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009648{
Ronald Cron5425a212020-08-04 14:58:35 +02009649 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009650 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009651 size_t bits = bits_arg;
9652 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9653 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9654 psa_status_t expected_status = expected_status_arg;
9655 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9656 uint8_t *exported = NULL;
9657 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009658 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009659 size_t exported_length = SIZE_MAX;
9660 uint8_t *e_read_buffer = NULL;
9661 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009662 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009663 size_t e_read_length = SIZE_MAX;
9664
Gilles Peskine449bd832023-01-11 14:50:10 +01009665 if (e_arg->len == 0 ||
9666 (e_arg->len == 3 &&
9667 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009668 is_default_public_exponent = 1;
9669 e_read_size = 0;
9670 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009671 TEST_CALLOC(e_read_buffer, e_read_size);
9672 TEST_CALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009673
Gilles Peskine449bd832023-01-11 14:50:10 +01009674 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009675
Gilles Peskine449bd832023-01-11 14:50:10 +01009676 psa_set_key_usage_flags(&attributes, usage);
9677 psa_set_key_algorithm(&attributes, alg);
9678 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9679 e_arg->x, e_arg->len));
9680 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009681
9682 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009683 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9684 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009685 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009686 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009687
9688 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009689 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9690 TEST_EQUAL(psa_get_key_type(&attributes), type);
9691 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9692 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
9693 e_read_buffer, e_read_size,
9694 &e_read_length));
9695 if (is_default_public_exponent) {
9696 TEST_EQUAL(e_read_length, 0);
9697 } else {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009698 TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009699 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009700
9701 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009702 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009703 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009704 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009705
9706 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009707 PSA_ASSERT(psa_export_public_key(key,
9708 exported, exported_size,
9709 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009710 {
9711 uint8_t *p = exported;
9712 uint8_t *end = exported + exported_length;
9713 size_t len;
9714 /* RSAPublicKey ::= SEQUENCE {
9715 * modulus INTEGER, -- n
9716 * publicExponent INTEGER } -- e
9717 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009718 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9719 MBEDTLS_ASN1_SEQUENCE |
9720 MBEDTLS_ASN1_CONSTRUCTED));
9721 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9722 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9723 MBEDTLS_ASN1_INTEGER));
9724 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009725 ++p;
9726 --len;
9727 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009728 if (e_arg->len == 0) {
9729 TEST_EQUAL(len, 3);
9730 TEST_EQUAL(p[0], 1);
9731 TEST_EQUAL(p[1], 0);
9732 TEST_EQUAL(p[2], 1);
9733 } else {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009734 TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009735 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009736 }
9737
9738exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009739 /*
9740 * Key attributes may have been returned by psa_get_key_attributes() or
9741 * set by psa_set_key_domain_parameters() thus reset them as required.
9742 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009743 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009744
Gilles Peskine449bd832023-01-11 14:50:10 +01009745 psa_destroy_key(key);
9746 PSA_DONE();
9747 mbedtls_free(e_read_buffer);
9748 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009749}
9750/* END_CASE */
9751
Darryl Greend49a4992018-06-18 17:27:26 +01009752/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009753void persistent_key_load_key_from_storage(data_t *data,
9754 int type_arg, int bits_arg,
9755 int usage_flags_arg, int alg_arg,
9756 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009757{
Gilles Peskine449bd832023-01-11 14:50:10 +01009758 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009759 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009760 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9761 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009762 psa_key_type_t type = type_arg;
9763 size_t bits = bits_arg;
9764 psa_key_usage_t usage_flags = usage_flags_arg;
9765 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009766 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009767 unsigned char *first_export = NULL;
9768 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009769 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Sergeybef1f632023-03-06 15:25:06 -07009770 size_t first_exported_length = 0;
Darryl Greend49a4992018-06-18 17:27:26 +01009771 size_t second_exported_length;
9772
Gilles Peskine449bd832023-01-11 14:50:10 +01009773 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009774 TEST_CALLOC(first_export, export_size);
9775 TEST_CALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009776 }
Darryl Greend49a4992018-06-18 17:27:26 +01009777
Gilles Peskine449bd832023-01-11 14:50:10 +01009778 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009779
Gilles Peskine449bd832023-01-11 14:50:10 +01009780 psa_set_key_id(&attributes, key_id);
9781 psa_set_key_usage_flags(&attributes, usage_flags);
9782 psa_set_key_algorithm(&attributes, alg);
9783 psa_set_key_type(&attributes, type);
9784 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009785
Gilles Peskine449bd832023-01-11 14:50:10 +01009786 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009787 case IMPORT_KEY:
9788 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009789 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9790 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009791 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009792
Darryl Green0c6575a2018-11-07 16:05:30 +00009793 case GENERATE_KEY:
9794 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009795 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009796 break;
9797
9798 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009799#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009800 {
9801 /* Create base key */
9802 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9803 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9804 psa_set_key_usage_flags(&base_attributes,
9805 PSA_KEY_USAGE_DERIVE);
9806 psa_set_key_algorithm(&base_attributes, derive_alg);
9807 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9808 PSA_ASSERT(psa_import_key(&base_attributes,
9809 data->x, data->len,
9810 &base_key));
9811 /* Derive a key. */
9812 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9813 PSA_ASSERT(psa_key_derivation_input_key(
9814 &operation,
9815 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9816 PSA_ASSERT(psa_key_derivation_input_bytes(
9817 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9818 NULL, 0));
9819 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9820 &operation,
9821 &key));
9822 PSA_ASSERT(psa_key_derivation_abort(&operation));
9823 PSA_ASSERT(psa_destroy_key(base_key));
9824 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9825 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009826#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009827 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009828#endif
9829 break;
9830
9831 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01009832 TEST_FAIL("generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009833 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009834 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009835 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009836
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009837 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009838 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9839 PSA_ASSERT(psa_export_key(key,
9840 first_export, export_size,
9841 &first_exported_length));
9842 if (generation_method == IMPORT_KEY) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009843 TEST_MEMORY_COMPARE(data->x, data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009844 first_export, first_exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01009845 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009846 }
Darryl Greend49a4992018-06-18 17:27:26 +01009847
9848 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009849 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009850 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009851 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009852
Darryl Greend49a4992018-06-18 17:27:26 +01009853 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009854 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9855 TEST_ASSERT(mbedtls_svc_key_id_equal(
9856 psa_get_key_id(&attributes), key_id));
9857 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9858 PSA_KEY_LIFETIME_PERSISTENT);
9859 TEST_EQUAL(psa_get_key_type(&attributes), type);
9860 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9861 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9862 mbedtls_test_update_key_usage_flags(usage_flags));
9863 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009864
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009865 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009866 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9867 PSA_ASSERT(psa_export_key(key,
9868 second_export, export_size,
9869 &second_exported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009870 TEST_MEMORY_COMPARE(first_export, first_exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009871 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009872 }
9873
9874 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009875 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009876 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009877 }
Darryl Greend49a4992018-06-18 17:27:26 +01009878
9879exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009880 /*
9881 * Key attributes may have been returned by psa_get_key_attributes()
9882 * thus reset them as required.
9883 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009884 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009885
Gilles Peskine449bd832023-01-11 14:50:10 +01009886 mbedtls_free(first_export);
9887 mbedtls_free(second_export);
9888 psa_key_derivation_abort(&operation);
9889 psa_destroy_key(base_key);
9890 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009891 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009892}
9893/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009894
Neil Armstronga557cb82022-06-10 08:58:32 +02009895/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009896void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9897 int primitive_arg, int hash_arg, int role_arg,
9898 int test_input, data_t *pw_data,
9899 int inj_err_type_arg,
9900 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009901{
9902 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9903 psa_pake_operation_t operation = psa_pake_operation_init();
9904 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009905 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009906 psa_key_type_t key_type_pw = key_type_pw_arg;
9907 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009908 psa_algorithm_t hash_alg = hash_arg;
9909 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009910 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9911 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009912 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9913 psa_status_t expected_error = expected_error_arg;
9914 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009915 unsigned char *output_buffer = NULL;
9916 size_t output_len = 0;
9917
Gilles Peskine449bd832023-01-11 14:50:10 +01009918 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009919
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009920 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009921 PSA_PAKE_STEP_KEY_SHARE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009922 TEST_CALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009923
Gilles Peskine449bd832023-01-11 14:50:10 +01009924 if (pw_data->len > 0) {
9925 psa_set_key_usage_flags(&attributes, key_usage_pw);
9926 psa_set_key_algorithm(&attributes, alg);
9927 psa_set_key_type(&attributes, key_type_pw);
9928 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9929 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009930 }
9931
Gilles Peskine449bd832023-01-11 14:50:10 +01009932 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9933 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9934 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009935
Gilles Peskine449bd832023-01-11 14:50:10 +01009936 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009937
Gilles Peskine449bd832023-01-11 14:50:10 +01009938 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9939 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9940 expected_error);
9941 PSA_ASSERT(psa_pake_abort(&operation));
9942 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9943 expected_error);
9944 PSA_ASSERT(psa_pake_abort(&operation));
9945 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9946 expected_error);
9947 PSA_ASSERT(psa_pake_abort(&operation));
9948 TEST_EQUAL(psa_pake_set_role(&operation, role),
9949 expected_error);
9950 PSA_ASSERT(psa_pake_abort(&operation));
9951 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9952 NULL, 0, NULL),
9953 expected_error);
9954 PSA_ASSERT(psa_pake_abort(&operation));
9955 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9956 expected_error);
9957 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009958 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009959 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009960
Gilles Peskine449bd832023-01-11 14:50:10 +01009961 status = psa_pake_setup(&operation, &cipher_suite);
9962 if (status != PSA_SUCCESS) {
9963 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009964 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009965 }
9966
Gilles Peskine449bd832023-01-11 14:50:10 +01009967 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9968 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9969 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009970 goto exit;
9971 }
9972
Gilles Peskine449bd832023-01-11 14:50:10 +01009973 status = psa_pake_set_role(&operation, role);
9974 if (status != PSA_SUCCESS) {
9975 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009976 goto exit;
9977 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009978
Gilles Peskine449bd832023-01-11 14:50:10 +01009979 if (pw_data->len > 0) {
9980 status = psa_pake_set_password_key(&operation, key);
9981 if (status != PSA_SUCCESS) {
9982 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009983 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009984 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009985 }
9986
Gilles Peskine449bd832023-01-11 14:50:10 +01009987 if (inj_err_type == INJECT_ERR_INVALID_USER) {
9988 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9989 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009990 goto exit;
9991 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009992
Gilles Peskine449bd832023-01-11 14:50:10 +01009993 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
9994 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9995 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009996 goto exit;
9997 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009998
Gilles Peskine449bd832023-01-11 14:50:10 +01009999 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010000 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010001 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
10002 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010003 goto exit;
10004 }
10005
Gilles Peskine449bd832023-01-11 14:50:10 +010010006 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010007 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010008 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
10009 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010010 goto exit;
10011 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010012
Gilles Peskine449bd832023-01-11 14:50:10 +010010013 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
10014 PSA_PAKE_STEP_KEY_SHARE);
10015 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
10016 PSA_PAKE_STEP_ZK_PUBLIC);
10017 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
10018 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010019
Gilles Peskine449bd832023-01-11 14:50:10 +010010020 if (test_input) {
10021 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10022 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
10023 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010024 goto exit;
10025 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010026
Gilles Peskine449bd832023-01-11 14:50:10 +010010027 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10028 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10029 output_buffer, size_zk_proof),
10030 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010031 goto exit;
10032 }
10033
Gilles Peskine449bd832023-01-11 14:50:10 +010010034 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10035 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
10036 output_buffer, size_zk_proof),
10037 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010038 goto exit;
10039 }
10040
Gilles Peskine449bd832023-01-11 14:50:10 +010010041 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
10042 output_buffer, size_key_share);
10043 if (status != PSA_SUCCESS) {
10044 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010045 goto exit;
10046 }
10047
Gilles Peskine449bd832023-01-11 14:50:10 +010010048 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10049 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10050 output_buffer, size_zk_public + 1),
10051 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010052 goto exit;
10053 }
10054
Gilles Peskine449bd832023-01-11 14:50:10 +010010055 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010056 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010057 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10058 output_buffer, size_zk_public + 1);
10059 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10060 output_buffer, size_zk_public),
10061 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010062 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010063 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010064 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010065 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10066 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10067 NULL, 0, NULL),
10068 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010069 goto exit;
10070 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010071
Gilles Peskine449bd832023-01-11 14:50:10 +010010072 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10073 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10074 output_buffer, buf_size, &output_len),
10075 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010076 goto exit;
10077 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010078
Gilles Peskine449bd832023-01-11 14:50:10 +010010079 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10080 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10081 output_buffer, buf_size, &output_len),
10082 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010083 goto exit;
10084 }
10085
Gilles Peskine449bd832023-01-11 14:50:10 +010010086 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10087 output_buffer, buf_size, &output_len);
10088 if (status != PSA_SUCCESS) {
10089 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010090 goto exit;
10091 }
10092
Gilles Peskine449bd832023-01-11 14:50:10 +010010093 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010094
Gilles Peskine449bd832023-01-11 14:50:10 +010010095 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10096 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10097 output_buffer, size_zk_public - 1, &output_len),
10098 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010099 goto exit;
10100 }
10101
Gilles Peskine449bd832023-01-11 14:50:10 +010010102 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010103 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010104 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10105 output_buffer, size_zk_public - 1, &output_len);
10106 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10107 output_buffer, buf_size, &output_len),
10108 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010109 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010110 }
10111 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010112
10113exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010114 PSA_ASSERT(psa_destroy_key(key));
10115 PSA_ASSERT(psa_pake_abort(&operation));
10116 mbedtls_free(output_buffer);
10117 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010118}
10119/* END_CASE */
10120
Neil Armstronga557cb82022-06-10 08:58:32 +020010121/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010122void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10123 int client_input_first, int inject_error,
10124 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010125{
10126 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10127 psa_pake_operation_t server = psa_pake_operation_init();
10128 psa_pake_operation_t client = psa_pake_operation_init();
10129 psa_algorithm_t alg = alg_arg;
10130 psa_algorithm_t hash_alg = hash_arg;
10131 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10132 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10133
Gilles Peskine449bd832023-01-11 14:50:10 +010010134 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010135
Gilles Peskine449bd832023-01-11 14:50:10 +010010136 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10137 psa_set_key_algorithm(&attributes, alg);
10138 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10139 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10140 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010141
Gilles Peskine449bd832023-01-11 14:50:10 +010010142 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10143 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10144 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010145
10146
Gilles Peskine449bd832023-01-11 14:50:10 +010010147 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10148 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010149
Gilles Peskine449bd832023-01-11 14:50:10 +010010150 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10151 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010152
Gilles Peskine449bd832023-01-11 14:50:10 +010010153 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10154 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010155
Gilles Peskine449bd832023-01-11 14:50:10 +010010156 ecjpake_do_round(alg, primitive_arg, &server, &client,
10157 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010158
Gilles Peskine449bd832023-01-11 14:50:10 +010010159 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010160 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010161 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010162
Gilles Peskine449bd832023-01-11 14:50:10 +010010163 ecjpake_do_round(alg, primitive_arg, &server, &client,
10164 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010165
10166exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010167 psa_destroy_key(key);
10168 psa_pake_abort(&server);
10169 psa_pake_abort(&client);
10170 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010171}
10172/* END_CASE */
10173
10174/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010175void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10176 int derive_alg_arg, data_t *pw_data,
10177 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010178{
10179 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10180 psa_pake_operation_t server = psa_pake_operation_init();
10181 psa_pake_operation_t client = psa_pake_operation_init();
10182 psa_algorithm_t alg = alg_arg;
10183 psa_algorithm_t hash_alg = hash_arg;
10184 psa_algorithm_t derive_alg = derive_alg_arg;
10185 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10186 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10187 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010188 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010189 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010190 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010191 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010192
Gilles Peskine449bd832023-01-11 14:50:10 +010010193 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010194
Gilles Peskine449bd832023-01-11 14:50:10 +010010195 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10196 psa_set_key_algorithm(&attributes, alg);
10197 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10198 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10199 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010200
Gilles Peskine449bd832023-01-11 14:50:10 +010010201 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10202 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10203 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010204
Neil Armstrong1e855602022-06-15 11:32:11 +020010205 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010206 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10207 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010208
Gilles Peskine449bd832023-01-11 14:50:10 +010010209 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10210 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10211 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10212 PSA_KEY_DERIVATION_INPUT_SEED,
10213 (const uint8_t *) "", 0));
10214 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10215 PSA_KEY_DERIVATION_INPUT_SEED,
10216 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010217 }
10218
Gilles Peskine449bd832023-01-11 14:50:10 +010010219 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10220 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010221
Gilles Peskine449bd832023-01-11 14:50:10 +010010222 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10223 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010224
Gilles Peskine449bd832023-01-11 14:50:10 +010010225 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10226 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010227
Gilles Peskine449bd832023-01-11 14:50:10 +010010228 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10229 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10230 PSA_ERROR_BAD_STATE);
10231 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10232 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010233 goto exit;
10234 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010235
Neil Armstrongf983caf2022-06-15 15:27:48 +020010236 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010237 ecjpake_do_round(alg, primitive_arg, &server, &client,
10238 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010239
Gilles Peskine449bd832023-01-11 14:50:10 +010010240 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10241 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10242 PSA_ERROR_BAD_STATE);
10243 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10244 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010245 goto exit;
10246 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010247
Neil Armstrongf983caf2022-06-15 15:27:48 +020010248 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010249 ecjpake_do_round(alg, primitive_arg, &server, &client,
10250 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010251
Gilles Peskine449bd832023-01-11 14:50:10 +010010252 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10253 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010254
10255exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010256 psa_key_derivation_abort(&server_derive);
10257 psa_key_derivation_abort(&client_derive);
10258 psa_destroy_key(key);
10259 psa_pake_abort(&server);
10260 psa_pake_abort(&client);
10261 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010262}
10263/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010264
10265/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010266void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010267{
10268 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10269 const size_t bits = 256;
10270 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010271 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010272 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010273 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010274
10275 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10276 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010277 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10278 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10279 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10280 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010281 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010282 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10283 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010284
10285 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010286 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10287 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10288 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10289 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10290 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10291 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010292
10293 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010294 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10295 PSA_PAKE_OUTPUT_MAX_SIZE);
10296 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10297 PSA_PAKE_OUTPUT_MAX_SIZE);
10298 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10299 PSA_PAKE_OUTPUT_MAX_SIZE);
10300 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10301 PSA_PAKE_INPUT_MAX_SIZE);
10302 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10303 PSA_PAKE_INPUT_MAX_SIZE);
10304 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10305 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010306}
10307/* END_CASE */