blob: f67508c5f275d8c5c18d72090a85d20bdaddca6c [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,
Valerio Settif202c292024-01-15 10:42:37 +01002110 int exercise_alg,
2111 int use_opaque_key)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002112{
Ronald Cron5425a212020-08-04 14:58:35 +02002113 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002114 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002115 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002116 psa_status_t status;
2117 size_t key_bits;
2118 size_t buffer_length;
2119 unsigned char *buffer = NULL;
2120 size_t output_length;
2121
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 psa_set_key_usage_flags(&attributes, policy_usage);
2125 psa_set_key_algorithm(&attributes, policy_alg);
2126 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002127
Valerio Settif202c292024-01-15 10:42:37 +01002128 if (use_opaque_key) {
2129 psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
2130 PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION));
2131 }
2132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2134 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002135
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002136 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 TEST_EQUAL(policy_usage,
2138 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002139
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2141 key_bits = psa_get_key_bits(&attributes);
2142 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2143 exercise_alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002144 TEST_CALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 status = psa_asymmetric_encrypt(key, exercise_alg,
2147 NULL, 0,
2148 NULL, 0,
2149 buffer, buffer_length,
2150 &output_length);
Valerio Settif202c292024-01-15 10:42:37 +01002151 if (use_opaque_key) {
2152 /* Encryption/decryption is opaque keys is currently not supported. */
2153 TEST_EQUAL(status, PSA_ERROR_NOT_SUPPORTED);
2154 } else if (policy_alg == exercise_alg &&
2155 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 PSA_ASSERT(status);
2157 } else {
2158 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2159 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 if (buffer_length != 0) {
2162 memset(buffer, 0, buffer_length);
2163 }
2164 status = psa_asymmetric_decrypt(key, exercise_alg,
2165 buffer, buffer_length,
2166 NULL, 0,
2167 buffer, buffer_length,
2168 &output_length);
Valerio Settif202c292024-01-15 10:42:37 +01002169 if (use_opaque_key) {
2170 /* Encryption/decryption is opaque keys is currently not supported. */
2171 TEST_EQUAL(status, PSA_ERROR_NOT_SUPPORTED);
2172 } else if (policy_alg == exercise_alg &&
2173 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2175 } else {
2176 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2177 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002178
2179exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002180 /*
2181 * Key attributes may have been returned by psa_get_key_attributes()
2182 * thus reset them as required.
2183 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002185
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 psa_destroy_key(key);
2187 PSA_DONE();
2188 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002189}
2190/* END_CASE */
2191
2192/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002193void asymmetric_signature_key_policy(int policy_usage_arg,
2194 int policy_alg,
2195 int key_type,
2196 data_t *key_data,
2197 int exercise_alg,
2198 int payload_length_arg,
2199 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002200{
Ronald Cron5425a212020-08-04 14:58:35 +02002201 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002202 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002203 psa_key_usage_t policy_usage = policy_usage_arg;
2204 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002205 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002207 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2208 * compatible with the policy and `payload_length_arg` is supposed to be
2209 * a valid input length to sign. If `payload_length_arg <= 0`,
2210 * `exercise_alg` is supposed to be forbidden by the policy. */
2211 int compatible_alg = payload_length_arg > 0;
2212 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002214 size_t signature_length;
2215
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002216 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002217 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 TEST_EQUAL(expected_usage,
2219 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002222
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 psa_set_key_usage_flags(&attributes, policy_usage);
2224 psa_set_key_algorithm(&attributes, policy_alg);
2225 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002226
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2228 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002231
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 status = psa_sign_hash(key, exercise_alg,
2233 payload, payload_length,
2234 signature, sizeof(signature),
2235 &signature_length);
2236 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2237 PSA_ASSERT(status);
2238 } else {
2239 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2240 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002241
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 memset(signature, 0, sizeof(signature));
2243 status = psa_verify_hash(key, exercise_alg,
2244 payload, payload_length,
2245 signature, sizeof(signature));
2246 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2247 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2248 } else {
2249 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2250 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002251
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2253 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2254 status = psa_sign_message(key, exercise_alg,
2255 payload, payload_length,
2256 signature, sizeof(signature),
2257 &signature_length);
2258 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2259 PSA_ASSERT(status);
2260 } else {
2261 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2262 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002263
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 memset(signature, 0, sizeof(signature));
2265 status = psa_verify_message(key, exercise_alg,
2266 payload, payload_length,
2267 signature, sizeof(signature));
2268 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2269 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2270 } else {
2271 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2272 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002273 }
2274
Gilles Peskined5b33222018-06-18 22:20:03 +02002275exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 psa_destroy_key(key);
2277 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002278}
2279/* END_CASE */
2280
Janos Follathba3fab92019-06-11 14:50:16 +01002281/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002282void derive_key_policy(int policy_usage,
2283 int policy_alg,
2284 int key_type,
2285 data_t *key_data,
2286 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002287{
Ronald Cron5425a212020-08-04 14:58:35 +02002288 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002289 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002290 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002291 psa_status_t status;
2292
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002294
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 psa_set_key_usage_flags(&attributes, policy_usage);
2296 psa_set_key_algorithm(&attributes, policy_alg);
2297 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2300 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002301
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2305 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2306 PSA_ASSERT(psa_key_derivation_input_bytes(
2307 &operation,
2308 PSA_KEY_DERIVATION_INPUT_SEED,
2309 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002310 }
Janos Follathba3fab92019-06-11 14:50:16 +01002311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 status = psa_key_derivation_input_key(&operation,
2313 PSA_KEY_DERIVATION_INPUT_SECRET,
2314 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 if (policy_alg == exercise_alg &&
2317 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2318 PSA_ASSERT(status);
2319 } else {
2320 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2321 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002322
2323exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 psa_key_derivation_abort(&operation);
2325 psa_destroy_key(key);
2326 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002327}
2328/* END_CASE */
2329
2330/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002331void agreement_key_policy(int policy_usage,
2332 int policy_alg,
2333 int key_type_arg,
2334 data_t *key_data,
2335 int exercise_alg,
2336 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002337{
Ronald Cron5425a212020-08-04 14:58:35 +02002338 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002339 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002340 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002341 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002342 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002343 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002344
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 psa_set_key_usage_flags(&attributes, policy_usage);
2348 psa_set_key_algorithm(&attributes, policy_alg);
2349 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002350
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2352 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2355 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002358
2359exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 psa_key_derivation_abort(&operation);
2361 psa_destroy_key(key);
2362 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002363}
2364/* END_CASE */
2365
2366/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002367void key_policy_alg2(int key_type_arg, data_t *key_data,
2368 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002369{
Ronald Cron5425a212020-08-04 14:58:35 +02002370 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002371 psa_key_type_t key_type = key_type_arg;
2372 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2373 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2374 psa_key_usage_t usage = usage_arg;
2375 psa_algorithm_t alg = alg_arg;
2376 psa_algorithm_t alg2 = alg2_arg;
2377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 psa_set_key_usage_flags(&attributes, usage);
2381 psa_set_key_algorithm(&attributes, alg);
2382 psa_set_key_enrollment_algorithm(&attributes, alg2);
2383 psa_set_key_type(&attributes, key_type);
2384 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2385 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002386
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002387 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 usage = mbedtls_test_update_key_usage_flags(usage);
2389 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2390 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2391 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2392 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002393
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002395 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 }
2397 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002398 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002400
2401exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002402 /*
2403 * Key attributes may have been returned by psa_get_key_attributes()
2404 * thus reset them as required.
2405 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002407
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 psa_destroy_key(key);
2409 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002410}
2411/* END_CASE */
2412
2413/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002414void raw_agreement_key_policy(int policy_usage,
2415 int policy_alg,
2416 int key_type_arg,
2417 data_t *key_data,
2418 int exercise_alg,
2419 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002420{
Ronald Cron5425a212020-08-04 14:58:35 +02002421 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002422 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002423 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002424 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002425 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002426 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002427
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002429
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 psa_set_key_usage_flags(&attributes, policy_usage);
2431 psa_set_key_algorithm(&attributes, policy_alg);
2432 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002433
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2435 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002440
2441exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 psa_key_derivation_abort(&operation);
2443 psa_destroy_key(key);
2444 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002445}
2446/* END_CASE */
2447
2448/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002449void copy_success(int source_usage_arg,
2450 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002451 int source_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 int type_arg, data_t *material,
2453 int copy_attributes,
2454 int target_usage_arg,
2455 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002456 int target_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 int expected_usage_arg,
2458 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002459{
Gilles Peskineca25db92019-04-19 11:43:08 +02002460 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2461 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002462 psa_key_usage_t expected_usage = expected_usage_arg;
2463 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002464 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302465 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2466 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002467 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2468 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002469 uint8_t *export_buffer = NULL;
2470
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002472
Gilles Peskineca25db92019-04-19 11:43:08 +02002473 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2475 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2476 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2477 psa_set_key_type(&source_attributes, type_arg);
2478 psa_set_key_lifetime(&source_attributes, source_lifetime);
2479 PSA_ASSERT(psa_import_key(&source_attributes,
2480 material->x, material->len,
2481 &source_key));
2482 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002483
Gilles Peskineca25db92019-04-19 11:43:08 +02002484 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002486 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002487 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002489
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 if (target_usage_arg != -1) {
2491 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2492 }
2493 if (target_alg_arg != -1) {
2494 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2495 }
2496 if (target_alg2_arg != -1) {
2497 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2498 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002499
Archana8a180362021-07-05 02:18:48 +05302500
Gilles Peskine57ab7212019-01-28 13:03:09 +01002501 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 PSA_ASSERT(psa_copy_key(source_key,
2503 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002504
2505 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002507
2508 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2510 TEST_EQUAL(psa_get_key_type(&source_attributes),
2511 psa_get_key_type(&target_attributes));
2512 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2513 psa_get_key_bits(&target_attributes));
2514 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2515 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2516 TEST_EQUAL(expected_alg2,
2517 psa_get_key_enrollment_algorithm(&target_attributes));
2518 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002519 size_t length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002520 TEST_CALLOC(export_buffer, material->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2522 material->len, &length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002523 TEST_MEMORY_COMPARE(material->x, material->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002524 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002525 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002526
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 if (!psa_key_lifetime_is_external(target_lifetime)) {
2528 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302529 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 }
2531 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302532 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 }
Archana8a180362021-07-05 02:18:48 +05302534 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002537
2538exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002539 /*
2540 * Source and target key attributes may have been returned by
2541 * psa_get_key_attributes() thus reset them as required.
2542 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 psa_reset_key_attributes(&source_attributes);
2544 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 PSA_DONE();
2547 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002548}
2549/* END_CASE */
2550
2551/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002552void copy_fail(int source_usage_arg,
2553 int source_alg_arg, int source_alg2_arg,
2554 int source_lifetime_arg,
2555 int type_arg, data_t *material,
2556 int target_type_arg, int target_bits_arg,
2557 int target_usage_arg,
2558 int target_alg_arg, int target_alg2_arg,
2559 int target_id_arg, int target_lifetime_arg,
2560 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002561{
2562 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2563 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002564 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2565 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002569
2570 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2572 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2573 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2574 psa_set_key_type(&source_attributes, type_arg);
2575 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2576 PSA_ASSERT(psa_import_key(&source_attributes,
2577 material->x, material->len,
2578 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002579
2580 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 psa_set_key_id(&target_attributes, key_id);
2582 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2583 psa_set_key_type(&target_attributes, target_type_arg);
2584 psa_set_key_bits(&target_attributes, target_bits_arg);
2585 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2586 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2587 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002588
2589 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 TEST_EQUAL(psa_copy_key(source_key,
2591 &target_attributes, &target_key),
2592 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002593
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002595
Gilles Peskine4a644642019-05-03 17:14:08 +02002596exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 psa_reset_key_attributes(&source_attributes);
2598 psa_reset_key_attributes(&target_attributes);
2599 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002600}
2601/* END_CASE */
2602
2603/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002604void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002605{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002606 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002607 /* Test each valid way of initializing the object, except for `= {0}`, as
2608 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2609 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002610 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002612 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2613 psa_hash_operation_t zero;
2614
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002616
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002617 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2619 PSA_ERROR_BAD_STATE);
2620 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2621 PSA_ERROR_BAD_STATE);
2622 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2623 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002624
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002625 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 PSA_ASSERT(psa_hash_abort(&func));
2627 PSA_ASSERT(psa_hash_abort(&init));
2628 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002629}
2630/* END_CASE */
2631
2632/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002633void hash_setup(int alg_arg,
2634 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002635{
2636 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002637 uint8_t *output = NULL;
2638 size_t output_size = 0;
2639 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002640 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002641 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002642 psa_status_t status;
2643
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002645
Neil Armstrongedb20862022-02-07 15:47:44 +01002646 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 output_size = PSA_HASH_LENGTH(alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002648 TEST_CALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002649
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 status = psa_hash_compute(alg, NULL, 0,
2651 output, output_size, &output_length);
2652 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002653
2654 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 status = psa_hash_setup(&operation, alg);
2656 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002657
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002658 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002660
2661 /* If setup failed, reproduce the failure, so as to
2662 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 if (status != PSA_SUCCESS) {
2664 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2665 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002666
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002667 /* Now the operation object should be reusable. */
2668#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2670 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002671#endif
2672
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002673exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 mbedtls_free(output);
2675 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002676}
2677/* END_CASE */
2678
2679/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002680void hash_compute_fail(int alg_arg, data_t *input,
2681 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002682{
2683 psa_algorithm_t alg = alg_arg;
2684 uint8_t *output = NULL;
2685 size_t output_size = output_size_arg;
2686 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002687 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002688 psa_status_t expected_status = expected_status_arg;
2689 psa_status_t status;
2690
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002691 TEST_CALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002694
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002695 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 status = psa_hash_compute(alg, input->x, input->len,
2697 output, output_size, &output_length);
2698 TEST_EQUAL(status, expected_status);
2699 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002700
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002701 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 status = psa_hash_setup(&operation, alg);
2703 if (status == PSA_SUCCESS) {
2704 status = psa_hash_update(&operation, input->x, input->len);
2705 if (status == PSA_SUCCESS) {
2706 status = psa_hash_finish(&operation, output, output_size,
2707 &output_length);
2708 if (status == PSA_SUCCESS) {
2709 TEST_LE_U(output_length, output_size);
2710 } else {
2711 TEST_EQUAL(status, expected_status);
2712 }
2713 } else {
2714 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002715 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 } else {
2717 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002718 }
2719
Gilles Peskine0a749c82019-11-28 19:33:58 +01002720exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 PSA_ASSERT(psa_hash_abort(&operation));
2722 mbedtls_free(output);
2723 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002724}
2725/* END_CASE */
2726
2727/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002728void hash_compare_fail(int alg_arg, data_t *input,
2729 data_t *reference_hash,
2730 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002731{
2732 psa_algorithm_t alg = alg_arg;
2733 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002734 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002735 psa_status_t status;
2736
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002738
Neil Armstrong55a1be12022-02-07 11:23:20 +01002739 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 status = psa_hash_compare(alg, input->x, input->len,
2741 reference_hash->x, reference_hash->len);
2742 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002743
Neil Armstrong55a1be12022-02-07 11:23:20 +01002744 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 status = psa_hash_setup(&operation, alg);
2746 if (status == PSA_SUCCESS) {
2747 status = psa_hash_update(&operation, input->x, input->len);
2748 if (status == PSA_SUCCESS) {
2749 status = psa_hash_verify(&operation, reference_hash->x,
2750 reference_hash->len);
2751 TEST_EQUAL(status, expected_status);
2752 } else {
2753 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002754 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 } else {
2756 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002757 }
2758
Gilles Peskine88e08462020-01-28 20:43:00 +01002759exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 PSA_ASSERT(psa_hash_abort(&operation));
2761 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002762}
2763/* END_CASE */
2764
2765/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002766void hash_compute_compare(int alg_arg, data_t *input,
2767 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002768{
2769 psa_algorithm_t alg = alg_arg;
2770 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2771 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002772 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002773 size_t i;
2774
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002776
Neil Armstrongca30a002022-02-07 11:40:23 +01002777 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2779 output, PSA_HASH_LENGTH(alg),
2780 &output_length));
2781 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002782 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002783 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002784
Neil Armstrongca30a002022-02-07 11:40:23 +01002785 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 PSA_ASSERT(psa_hash_setup(&operation, alg));
2787 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2788 PSA_ASSERT(psa_hash_finish(&operation, output,
2789 PSA_HASH_LENGTH(alg),
2790 &output_length));
2791 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002792 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002793 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002794
2795 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2797 output, sizeof(output),
2798 &output_length));
2799 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002800 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002801 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002802
Neil Armstrongca30a002022-02-07 11:40:23 +01002803 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 PSA_ASSERT(psa_hash_setup(&operation, alg));
2805 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2806 PSA_ASSERT(psa_hash_finish(&operation, output,
2807 sizeof(output), &output_length));
2808 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002809 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002810 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002811
2812 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2814 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002815
Neil Armstrongca30a002022-02-07 11:40:23 +01002816 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 PSA_ASSERT(psa_hash_setup(&operation, alg));
2818 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2819 PSA_ASSERT(psa_hash_verify(&operation, output,
2820 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002821
2822 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002823 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2824 output, output_length + 1),
2825 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002826
Neil Armstrongca30a002022-02-07 11:40:23 +01002827 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002828 PSA_ASSERT(psa_hash_setup(&operation, alg));
2829 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2830 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2831 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002832
2833 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2835 output, output_length - 1),
2836 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002837
Neil Armstrongca30a002022-02-07 11:40:23 +01002838 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 PSA_ASSERT(psa_hash_setup(&operation, alg));
2840 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2841 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2842 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002843
Gilles Peskine0a749c82019-11-28 19:33:58 +01002844 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 for (i = 0; i < output_length; i++) {
2846 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002847 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002848
2849 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2851 output, output_length),
2852 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002853
2854 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 PSA_ASSERT(psa_hash_setup(&operation, alg));
2856 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2857 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2858 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002859
Gilles Peskine0a749c82019-11-28 19:33:58 +01002860 output[i] ^= 1;
2861 }
2862
2863exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 PSA_ASSERT(psa_hash_abort(&operation));
2865 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002866}
2867/* END_CASE */
2868
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002869/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002870void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002871{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002872 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002873 unsigned char input[] = "";
2874 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002875 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002876 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2877 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2879 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002880 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002881 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002882 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002883
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002885
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002886 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 PSA_ASSERT(psa_hash_setup(&operation, alg));
2888 ASSERT_OPERATION_IS_ACTIVE(operation);
2889 TEST_EQUAL(psa_hash_setup(&operation, alg),
2890 PSA_ERROR_BAD_STATE);
2891 ASSERT_OPERATION_IS_INACTIVE(operation);
2892 PSA_ASSERT(psa_hash_abort(&operation));
2893 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002894
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002895 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2897 PSA_ERROR_BAD_STATE);
2898 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002899
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002900 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002902 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 ASSERT_OPERATION_IS_ACTIVE(operation);
2904 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2905 PSA_ERROR_BAD_STATE);
2906 ASSERT_OPERATION_IS_INACTIVE(operation);
2907 PSA_ASSERT(psa_hash_abort(&operation));
2908 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002909
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002910 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 PSA_ASSERT(psa_hash_setup(&operation, alg));
2912 PSA_ASSERT(psa_hash_finish(&operation,
2913 hash, sizeof(hash), &hash_len));
2914 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2915 PSA_ERROR_BAD_STATE);
2916 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002917
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002918 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 TEST_EQUAL(psa_hash_verify(&operation,
2920 valid_hash, sizeof(valid_hash)),
2921 PSA_ERROR_BAD_STATE);
2922 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002923
2924 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 PSA_ASSERT(psa_hash_setup(&operation, alg));
2926 PSA_ASSERT(psa_hash_finish(&operation,
2927 hash, sizeof(hash), &hash_len));
2928 TEST_EQUAL(psa_hash_verify(&operation,
2929 valid_hash, sizeof(valid_hash)),
2930 PSA_ERROR_BAD_STATE);
2931 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002932
2933 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 PSA_ASSERT(psa_hash_setup(&operation, alg));
2935 ASSERT_OPERATION_IS_ACTIVE(operation);
2936 PSA_ASSERT(psa_hash_verify(&operation,
2937 valid_hash, sizeof(valid_hash)));
2938 ASSERT_OPERATION_IS_INACTIVE(operation);
2939 TEST_EQUAL(psa_hash_verify(&operation,
2940 valid_hash, sizeof(valid_hash)),
2941 PSA_ERROR_BAD_STATE);
2942 ASSERT_OPERATION_IS_INACTIVE(operation);
2943 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002944
2945 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 TEST_EQUAL(psa_hash_finish(&operation,
2947 hash, sizeof(hash), &hash_len),
2948 PSA_ERROR_BAD_STATE);
2949 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002950
2951 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 PSA_ASSERT(psa_hash_setup(&operation, alg));
2953 PSA_ASSERT(psa_hash_finish(&operation,
2954 hash, sizeof(hash), &hash_len));
2955 TEST_EQUAL(psa_hash_finish(&operation,
2956 hash, sizeof(hash), &hash_len),
2957 PSA_ERROR_BAD_STATE);
2958 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002959
2960 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 PSA_ASSERT(psa_hash_setup(&operation, alg));
2962 PSA_ASSERT(psa_hash_verify(&operation,
2963 valid_hash, sizeof(valid_hash)));
2964 TEST_EQUAL(psa_hash_finish(&operation,
2965 hash, sizeof(hash), &hash_len),
2966 PSA_ERROR_BAD_STATE);
2967 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002968
2969exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002971}
2972/* END_CASE */
2973
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002974/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002975void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002976{
2977 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002978 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2979 * appended to it */
2980 unsigned char hash[] = {
2981 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2982 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2984 };
2985 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002986 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002987
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002989
itayzafrir27e69452018-11-01 14:26:34 +02002990 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 PSA_ASSERT(psa_hash_setup(&operation, alg));
2992 ASSERT_OPERATION_IS_ACTIVE(operation);
2993 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2994 PSA_ERROR_INVALID_SIGNATURE);
2995 ASSERT_OPERATION_IS_INACTIVE(operation);
2996 PSA_ASSERT(psa_hash_abort(&operation));
2997 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002998
itayzafrir27e69452018-11-01 14:26:34 +02002999 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 PSA_ASSERT(psa_hash_setup(&operation, alg));
3001 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
3002 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03003003
itayzafrir27e69452018-11-01 14:26:34 +02003004 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 PSA_ASSERT(psa_hash_setup(&operation, alg));
3006 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
3007 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03003008
itayzafrirec93d302018-10-18 18:01:10 +03003009exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03003011}
3012/* END_CASE */
3013
Ronald Cronee414c72021-03-18 18:50:08 +01003014/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003015void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03003016{
3017 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003018 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003020 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003021 size_t hash_len;
3022
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03003024
itayzafrir58028322018-10-25 10:22:01 +03003025 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 PSA_ASSERT(psa_hash_setup(&operation, alg));
3027 TEST_EQUAL(psa_hash_finish(&operation,
3028 hash, expected_size - 1, &hash_len),
3029 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003030
3031exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003033}
3034/* END_CASE */
3035
Ronald Cronee414c72021-03-18 18:50:08 +01003036/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003037void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003038{
3039 psa_algorithm_t alg = PSA_ALG_SHA_256;
3040 unsigned char hash[PSA_HASH_MAX_SIZE];
3041 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3042 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3043 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3044 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3045 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3046 size_t hash_len;
3047
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 PSA_ASSERT(psa_crypto_init());
3049 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003050
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3052 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3053 PSA_ASSERT(psa_hash_finish(&op_finished,
3054 hash, sizeof(hash), &hash_len));
3055 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3056 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003057
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3059 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003060
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3062 PSA_ASSERT(psa_hash_finish(&op_init,
3063 hash, sizeof(hash), &hash_len));
3064 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3065 PSA_ASSERT(psa_hash_finish(&op_finished,
3066 hash, sizeof(hash), &hash_len));
3067 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3068 PSA_ASSERT(psa_hash_finish(&op_aborted,
3069 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003070
3071exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 psa_hash_abort(&op_source);
3073 psa_hash_abort(&op_init);
3074 psa_hash_abort(&op_setup);
3075 psa_hash_abort(&op_finished);
3076 psa_hash_abort(&op_aborted);
3077 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003078}
3079/* END_CASE */
3080
Ronald Cronee414c72021-03-18 18:50:08 +01003081/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003082void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003083{
3084 psa_algorithm_t alg = PSA_ALG_SHA_256;
3085 unsigned char hash[PSA_HASH_MAX_SIZE];
3086 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3087 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3088 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3089 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3090 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3091 size_t hash_len;
3092
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003094
Gilles Peskine449bd832023-01-11 14:50:10 +01003095 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3096 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3097 PSA_ASSERT(psa_hash_finish(&op_finished,
3098 hash, sizeof(hash), &hash_len));
3099 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3100 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003101
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3103 PSA_ASSERT(psa_hash_finish(&op_target,
3104 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003105
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3107 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3108 PSA_ERROR_BAD_STATE);
3109 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3110 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003111
3112exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 psa_hash_abort(&op_target);
3114 psa_hash_abort(&op_init);
3115 psa_hash_abort(&op_setup);
3116 psa_hash_abort(&op_finished);
3117 psa_hash_abort(&op_aborted);
3118 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003119}
3120/* END_CASE */
3121
itayzafrir58028322018-10-25 10:22:01 +03003122/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003123void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003124{
Jaeden Amero252ef282019-02-15 14:05:35 +00003125 const uint8_t input[1] = { 0 };
3126
Jaeden Amero769ce272019-01-04 11:48:03 +00003127 /* Test each valid way of initializing the object, except for `= {0}`, as
3128 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3129 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003130 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003132 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3133 psa_mac_operation_t zero;
3134
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003136
Jaeden Amero252ef282019-02-15 14:05:35 +00003137 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 TEST_EQUAL(psa_mac_update(&func,
3139 input, sizeof(input)),
3140 PSA_ERROR_BAD_STATE);
3141 TEST_EQUAL(psa_mac_update(&init,
3142 input, sizeof(input)),
3143 PSA_ERROR_BAD_STATE);
3144 TEST_EQUAL(psa_mac_update(&zero,
3145 input, sizeof(input)),
3146 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003147
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003148 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 PSA_ASSERT(psa_mac_abort(&func));
3150 PSA_ASSERT(psa_mac_abort(&init));
3151 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003152}
3153/* END_CASE */
3154
3155/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003156void mac_setup(int key_type_arg,
3157 data_t *key,
3158 int alg_arg,
3159 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003160{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003161 psa_key_type_t key_type = key_type_arg;
3162 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003163 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003164 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003165 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3166#if defined(KNOWN_SUPPORTED_MAC_ALG)
3167 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3168#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003169
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003171
Gilles Peskine449bd832023-01-11 14:50:10 +01003172 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3173 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003174 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 }
3176 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003177
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003178 /* The operation object should be reusable. */
3179#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3181 smoke_test_key_data,
3182 sizeof(smoke_test_key_data),
3183 KNOWN_SUPPORTED_MAC_ALG,
3184 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003185 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 }
3187 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003188#endif
3189
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003190exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003192}
3193/* END_CASE */
3194
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003195/* 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 +01003196void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003197{
Ronald Cron5425a212020-08-04 14:58:35 +02003198 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003199 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3200 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003201 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003202 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3203 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3205 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003206 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003207 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3208 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3209 size_t sign_mac_length = 0;
3210 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3211 const uint8_t verify_mac[] = {
3212 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3213 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3215 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003216
Gilles Peskine449bd832023-01-11 14:50:10 +01003217 PSA_ASSERT(psa_crypto_init());
3218 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3219 psa_set_key_algorithm(&attributes, alg);
3220 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003221
Gilles Peskine449bd832023-01-11 14:50:10 +01003222 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3223 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003224
Jaeden Amero252ef282019-02-15 14:05:35 +00003225 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003226 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3227 PSA_ERROR_BAD_STATE);
3228 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003229
3230 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003231 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3232 &sign_mac_length),
3233 PSA_ERROR_BAD_STATE);
3234 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003235
3236 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003237 TEST_EQUAL(psa_mac_verify_finish(&operation,
3238 verify_mac, sizeof(verify_mac)),
3239 PSA_ERROR_BAD_STATE);
3240 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003241
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003242 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003243 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3244 ASSERT_OPERATION_IS_ACTIVE(operation);
3245 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3246 PSA_ERROR_BAD_STATE);
3247 ASSERT_OPERATION_IS_INACTIVE(operation);
3248 PSA_ASSERT(psa_mac_abort(&operation));
3249 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003250
Jaeden Amero252ef282019-02-15 14:05:35 +00003251 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3253 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3254 PSA_ASSERT(psa_mac_sign_finish(&operation,
3255 sign_mac, sizeof(sign_mac),
3256 &sign_mac_length));
3257 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3258 PSA_ERROR_BAD_STATE);
3259 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003260
3261 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003262 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3263 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3264 PSA_ASSERT(psa_mac_verify_finish(&operation,
3265 verify_mac, sizeof(verify_mac)));
3266 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3267 PSA_ERROR_BAD_STATE);
3268 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003269
3270 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3272 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3273 PSA_ASSERT(psa_mac_sign_finish(&operation,
3274 sign_mac, sizeof(sign_mac),
3275 &sign_mac_length));
3276 TEST_EQUAL(psa_mac_sign_finish(&operation,
3277 sign_mac, sizeof(sign_mac),
3278 &sign_mac_length),
3279 PSA_ERROR_BAD_STATE);
3280 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003281
3282 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003283 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3284 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3285 PSA_ASSERT(psa_mac_verify_finish(&operation,
3286 verify_mac, sizeof(verify_mac)));
3287 TEST_EQUAL(psa_mac_verify_finish(&operation,
3288 verify_mac, sizeof(verify_mac)),
3289 PSA_ERROR_BAD_STATE);
3290 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003291
3292 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003293 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3294 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3295 ASSERT_OPERATION_IS_ACTIVE(operation);
3296 TEST_EQUAL(psa_mac_verify_finish(&operation,
3297 verify_mac, sizeof(verify_mac)),
3298 PSA_ERROR_BAD_STATE);
3299 ASSERT_OPERATION_IS_INACTIVE(operation);
3300 PSA_ASSERT(psa_mac_abort(&operation));
3301 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003302
3303 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003304 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3305 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3306 ASSERT_OPERATION_IS_ACTIVE(operation);
3307 TEST_EQUAL(psa_mac_sign_finish(&operation,
3308 sign_mac, sizeof(sign_mac),
3309 &sign_mac_length),
3310 PSA_ERROR_BAD_STATE);
3311 ASSERT_OPERATION_IS_INACTIVE(operation);
3312 PSA_ASSERT(psa_mac_abort(&operation));
3313 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003314
Gilles Peskine449bd832023-01-11 14:50:10 +01003315 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003316
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003317exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003318 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003319}
3320/* END_CASE */
3321
3322/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003323void mac_sign_verify_multi(int key_type_arg,
3324 data_t *key_data,
3325 int alg_arg,
3326 data_t *input,
3327 int is_verify,
3328 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003329{
3330 size_t data_part_len = 0;
3331
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003333 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003335
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 if (mac_multipart_internal_func(key_type_arg, key_data,
3337 alg_arg,
3338 input, data_part_len,
3339 expected_mac,
3340 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003341 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003342 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003343
3344 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003345 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003346
Gilles Peskine449bd832023-01-11 14:50:10 +01003347 if (mac_multipart_internal_func(key_type_arg, key_data,
3348 alg_arg,
3349 input, data_part_len,
3350 expected_mac,
3351 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003352 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003354 }
3355
3356 /* Goto is required to silence warnings about unused labels, as we
3357 * don't actually do any test assertions in this function. */
3358 goto exit;
3359}
3360/* END_CASE */
3361
3362/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003363void mac_sign(int key_type_arg,
3364 data_t *key_data,
3365 int alg_arg,
3366 data_t *input,
3367 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003368{
Ronald Cron5425a212020-08-04 14:58:35 +02003369 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003370 psa_key_type_t key_type = key_type_arg;
3371 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003372 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003373 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003374 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003375 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003376 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003377 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003378 const size_t output_sizes_to_test[] = {
3379 0,
3380 1,
3381 expected_mac->len - 1,
3382 expected_mac->len,
3383 expected_mac->len + 1,
3384 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003385
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003387 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003389
Gilles Peskine449bd832023-01-11 14:50:10 +01003390 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003391
Gilles Peskine449bd832023-01-11 14:50:10 +01003392 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3393 psa_set_key_algorithm(&attributes, alg);
3394 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003395
Gilles Peskine449bd832023-01-11 14:50:10 +01003396 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3397 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003398
Gilles Peskine449bd832023-01-11 14:50:10 +01003399 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003400 const size_t output_size = output_sizes_to_test[i];
3401 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 (output_size >= expected_mac->len ? PSA_SUCCESS :
3403 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003404
Gilles Peskine449bd832023-01-11 14:50:10 +01003405 mbedtls_test_set_step(output_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003406 TEST_CALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003407
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003408 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 TEST_EQUAL(psa_mac_compute(key, alg,
3410 input->x, input->len,
3411 actual_mac, output_size, &mac_length),
3412 expected_status);
3413 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003414 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003415 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003416 }
3417
Gilles Peskine449bd832023-01-11 14:50:10 +01003418 if (output_size > 0) {
3419 memset(actual_mac, 0, output_size);
3420 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003421
3422 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003423 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3424 PSA_ASSERT(psa_mac_update(&operation,
3425 input->x, input->len));
3426 TEST_EQUAL(psa_mac_sign_finish(&operation,
3427 actual_mac, output_size,
3428 &mac_length),
3429 expected_status);
3430 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003431
Gilles Peskine449bd832023-01-11 14:50:10 +01003432 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003433 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003434 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003435 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003436 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003437 actual_mac = NULL;
3438 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003439
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003440exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003441 psa_mac_abort(&operation);
3442 psa_destroy_key(key);
3443 PSA_DONE();
3444 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003445}
3446/* END_CASE */
3447
3448/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003449void mac_verify(int key_type_arg,
3450 data_t *key_data,
3451 int alg_arg,
3452 data_t *input,
3453 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003454{
Ronald Cron5425a212020-08-04 14:58:35 +02003455 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003456 psa_key_type_t key_type = key_type_arg;
3457 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003458 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003459 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003460 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003461
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003463
Gilles Peskine449bd832023-01-11 14:50:10 +01003464 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003465
Gilles Peskine449bd832023-01-11 14:50:10 +01003466 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3467 psa_set_key_algorithm(&attributes, alg);
3468 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003469
Gilles Peskine449bd832023-01-11 14:50:10 +01003470 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3471 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003472
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003473 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003474 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3475 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003476
3477 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003478 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3479 PSA_ASSERT(psa_mac_update(&operation,
3480 input->x, input->len));
3481 PSA_ASSERT(psa_mac_verify_finish(&operation,
3482 expected_mac->x,
3483 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003484
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003485 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003486 TEST_EQUAL(psa_mac_verify(key, alg,
3487 input->x, input->len,
3488 expected_mac->x,
3489 expected_mac->len - 1),
3490 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003491
3492 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003493 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3494 PSA_ASSERT(psa_mac_update(&operation,
3495 input->x, input->len));
3496 TEST_EQUAL(psa_mac_verify_finish(&operation,
3497 expected_mac->x,
3498 expected_mac->len - 1),
3499 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003500
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003501 /* Test a MAC that's too long, one-shot case. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003502 TEST_CALLOC(perturbed_mac, expected_mac->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01003503 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3504 TEST_EQUAL(psa_mac_verify(key, alg,
3505 input->x, input->len,
3506 perturbed_mac, expected_mac->len + 1),
3507 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003508
3509 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003510 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3511 PSA_ASSERT(psa_mac_update(&operation,
3512 input->x, input->len));
3513 TEST_EQUAL(psa_mac_verify_finish(&operation,
3514 perturbed_mac,
3515 expected_mac->len + 1),
3516 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003517
3518 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003519 for (size_t i = 0; i < expected_mac->len; i++) {
3520 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003521 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003522
Gilles Peskine449bd832023-01-11 14:50:10 +01003523 TEST_EQUAL(psa_mac_verify(key, alg,
3524 input->x, input->len,
3525 perturbed_mac, expected_mac->len),
3526 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003527
Gilles Peskine449bd832023-01-11 14:50:10 +01003528 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3529 PSA_ASSERT(psa_mac_update(&operation,
3530 input->x, input->len));
3531 TEST_EQUAL(psa_mac_verify_finish(&operation,
3532 perturbed_mac,
3533 expected_mac->len),
3534 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003535 perturbed_mac[i] ^= 1;
3536 }
3537
Gilles Peskine8c9def32018-02-08 10:02:12 +01003538exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003539 psa_mac_abort(&operation);
3540 psa_destroy_key(key);
3541 PSA_DONE();
3542 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003543}
3544/* END_CASE */
3545
3546/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003547void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003548{
Jaeden Ameroab439972019-02-15 14:12:05 +00003549 const uint8_t input[1] = { 0 };
3550 unsigned char output[1] = { 0 };
3551 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003552 /* Test each valid way of initializing the object, except for `= {0}`, as
3553 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3554 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003555 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003556 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003557 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3558 psa_cipher_operation_t zero;
3559
Gilles Peskine449bd832023-01-11 14:50:10 +01003560 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003561
Jaeden Ameroab439972019-02-15 14:12:05 +00003562 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003563 TEST_EQUAL(psa_cipher_update(&func,
3564 input, sizeof(input),
3565 output, sizeof(output),
3566 &output_length),
3567 PSA_ERROR_BAD_STATE);
3568 TEST_EQUAL(psa_cipher_update(&init,
3569 input, sizeof(input),
3570 output, sizeof(output),
3571 &output_length),
3572 PSA_ERROR_BAD_STATE);
3573 TEST_EQUAL(psa_cipher_update(&zero,
3574 input, sizeof(input),
3575 output, sizeof(output),
3576 &output_length),
3577 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003578
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003579 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003580 PSA_ASSERT(psa_cipher_abort(&func));
3581 PSA_ASSERT(psa_cipher_abort(&init));
3582 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003583}
3584/* END_CASE */
3585
3586/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003587void cipher_setup(int key_type_arg,
3588 data_t *key,
3589 int alg_arg,
3590 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003591{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003592 psa_key_type_t key_type = key_type_arg;
3593 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003594 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003595 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003596 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003597#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003598 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3599#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003600
Gilles Peskine449bd832023-01-11 14:50:10 +01003601 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003602
Gilles Peskine449bd832023-01-11 14:50:10 +01003603 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3604 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003605 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003606 }
3607 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003608
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003609 /* The operation object should be reusable. */
3610#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003611 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3612 smoke_test_key_data,
3613 sizeof(smoke_test_key_data),
3614 KNOWN_SUPPORTED_CIPHER_ALG,
3615 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003616 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003617 }
3618 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003619#endif
3620
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003621exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003622 psa_cipher_abort(&operation);
3623 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003624}
3625/* END_CASE */
3626
Ronald Cronee414c72021-03-18 18:50:08 +01003627/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003628void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003629{
Ronald Cron5425a212020-08-04 14:58:35 +02003630 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003631 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3632 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003633 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003634 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003635 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003636 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003637 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003638 0xaa, 0xaa, 0xaa, 0xaa
3639 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003640 const uint8_t text[] = {
3641 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003642 0xbb, 0xbb, 0xbb, 0xbb
3643 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003644 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003645 size_t length = 0;
3646
Gilles Peskine449bd832023-01-11 14:50:10 +01003647 PSA_ASSERT(psa_crypto_init());
3648 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3649 psa_set_key_algorithm(&attributes, alg);
3650 psa_set_key_type(&attributes, key_type);
3651 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3652 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003653
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003654 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003655 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3656 ASSERT_OPERATION_IS_ACTIVE(operation);
3657 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3658 PSA_ERROR_BAD_STATE);
3659 ASSERT_OPERATION_IS_INACTIVE(operation);
3660 PSA_ASSERT(psa_cipher_abort(&operation));
3661 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003662
3663 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003664 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3665 ASSERT_OPERATION_IS_ACTIVE(operation);
3666 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3667 PSA_ERROR_BAD_STATE);
3668 ASSERT_OPERATION_IS_INACTIVE(operation);
3669 PSA_ASSERT(psa_cipher_abort(&operation));
3670 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003671
Jaeden Ameroab439972019-02-15 14:12:05 +00003672 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003673 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3674 buffer, sizeof(buffer),
3675 &length),
3676 PSA_ERROR_BAD_STATE);
3677 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003678
3679 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003680 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3681 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3682 buffer, sizeof(buffer),
3683 &length));
3684 ASSERT_OPERATION_IS_ACTIVE(operation);
3685 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3686 buffer, sizeof(buffer),
3687 &length),
3688 PSA_ERROR_BAD_STATE);
3689 ASSERT_OPERATION_IS_INACTIVE(operation);
3690 PSA_ASSERT(psa_cipher_abort(&operation));
3691 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003692
3693 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003694 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3695 PSA_ASSERT(psa_cipher_set_iv(&operation,
3696 iv, sizeof(iv)));
3697 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3698 buffer, sizeof(buffer),
3699 &length),
3700 PSA_ERROR_BAD_STATE);
3701 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003702
3703 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003704 TEST_EQUAL(psa_cipher_set_iv(&operation,
3705 iv, sizeof(iv)),
3706 PSA_ERROR_BAD_STATE);
3707 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003708
3709 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003710 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3711 PSA_ASSERT(psa_cipher_set_iv(&operation,
3712 iv, sizeof(iv)));
3713 ASSERT_OPERATION_IS_ACTIVE(operation);
3714 TEST_EQUAL(psa_cipher_set_iv(&operation,
3715 iv, sizeof(iv)),
3716 PSA_ERROR_BAD_STATE);
3717 ASSERT_OPERATION_IS_INACTIVE(operation);
3718 PSA_ASSERT(psa_cipher_abort(&operation));
3719 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003720
3721 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003722 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3723 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3724 buffer, sizeof(buffer),
3725 &length));
3726 TEST_EQUAL(psa_cipher_set_iv(&operation,
3727 iv, sizeof(iv)),
3728 PSA_ERROR_BAD_STATE);
3729 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003730
3731 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003732 TEST_EQUAL(psa_cipher_update(&operation,
3733 text, sizeof(text),
3734 buffer, sizeof(buffer),
3735 &length),
3736 PSA_ERROR_BAD_STATE);
3737 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003738
3739 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003740 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3741 ASSERT_OPERATION_IS_ACTIVE(operation);
3742 TEST_EQUAL(psa_cipher_update(&operation,
3743 text, sizeof(text),
3744 buffer, sizeof(buffer),
3745 &length),
3746 PSA_ERROR_BAD_STATE);
3747 ASSERT_OPERATION_IS_INACTIVE(operation);
3748 PSA_ASSERT(psa_cipher_abort(&operation));
3749 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003750
3751 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003752 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3753 PSA_ASSERT(psa_cipher_set_iv(&operation,
3754 iv, sizeof(iv)));
3755 PSA_ASSERT(psa_cipher_finish(&operation,
3756 buffer, sizeof(buffer), &length));
3757 TEST_EQUAL(psa_cipher_update(&operation,
3758 text, sizeof(text),
3759 buffer, sizeof(buffer),
3760 &length),
3761 PSA_ERROR_BAD_STATE);
3762 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003763
3764 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003765 TEST_EQUAL(psa_cipher_finish(&operation,
3766 buffer, sizeof(buffer), &length),
3767 PSA_ERROR_BAD_STATE);
3768 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003769
3770 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003771 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003772 /* Not calling update means we are encrypting an empty buffer, which is OK
3773 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003774 ASSERT_OPERATION_IS_ACTIVE(operation);
3775 TEST_EQUAL(psa_cipher_finish(&operation,
3776 buffer, sizeof(buffer), &length),
3777 PSA_ERROR_BAD_STATE);
3778 ASSERT_OPERATION_IS_INACTIVE(operation);
3779 PSA_ASSERT(psa_cipher_abort(&operation));
3780 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003781
3782 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003783 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3784 PSA_ASSERT(psa_cipher_set_iv(&operation,
3785 iv, sizeof(iv)));
3786 PSA_ASSERT(psa_cipher_finish(&operation,
3787 buffer, sizeof(buffer), &length));
3788 TEST_EQUAL(psa_cipher_finish(&operation,
3789 buffer, sizeof(buffer), &length),
3790 PSA_ERROR_BAD_STATE);
3791 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003792
Gilles Peskine449bd832023-01-11 14:50:10 +01003793 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003794
Jaeden Ameroab439972019-02-15 14:12:05 +00003795exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003796 psa_cipher_abort(&operation);
3797 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003798}
3799/* END_CASE */
3800
3801/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003802void cipher_encrypt_fail(int alg_arg,
3803 int key_type_arg,
3804 data_t *key_data,
3805 data_t *input,
3806 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003807{
Ronald Cron5425a212020-08-04 14:58:35 +02003808 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003809 psa_status_t status;
3810 psa_key_type_t key_type = key_type_arg;
3811 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003812 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003813 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003814 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3815 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003816 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003817 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003818 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003819 size_t function_output_length;
3820 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003821 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3822
Gilles Peskine449bd832023-01-11 14:50:10 +01003823 if (PSA_ERROR_BAD_STATE != expected_status) {
3824 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003825
Gilles Peskine449bd832023-01-11 14:50:10 +01003826 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3827 psa_set_key_algorithm(&attributes, alg);
3828 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003829
Gilles Peskine449bd832023-01-11 14:50:10 +01003830 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3831 input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003832 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003833
Gilles Peskine449bd832023-01-11 14:50:10 +01003834 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3835 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003836 }
3837
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003838 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003839 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3840 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003841
Gilles Peskine449bd832023-01-11 14:50:10 +01003842 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003843
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003844 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003845 status = psa_cipher_encrypt_setup(&operation, key, alg);
3846 if (status == PSA_SUCCESS) {
3847 if (alg != PSA_ALG_ECB_NO_PADDING) {
3848 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3849 iv, iv_size,
3850 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003851 }
3852
Gilles Peskine449bd832023-01-11 14:50:10 +01003853 status = psa_cipher_update(&operation, input->x, input->len,
3854 output, output_buffer_size,
3855 &function_output_length);
3856 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003857 output_length += function_output_length;
3858
Gilles Peskine449bd832023-01-11 14:50:10 +01003859 status = psa_cipher_finish(&operation, output + output_length,
3860 output_buffer_size - output_length,
3861 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003862
Gilles Peskine449bd832023-01-11 14:50:10 +01003863 TEST_EQUAL(status, expected_status);
3864 } else {
3865 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003866 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003867 } else {
3868 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003869 }
3870
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003871exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003872 psa_cipher_abort(&operation);
3873 mbedtls_free(output);
3874 psa_destroy_key(key);
3875 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003876}
3877/* END_CASE */
3878
3879/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003880void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3881 data_t *input, int iv_length,
3882 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003883{
3884 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3885 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3886 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3887 size_t output_buffer_size = 0;
3888 unsigned char *output = NULL;
3889
Gilles Peskine449bd832023-01-11 14:50:10 +01003890 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003891 TEST_CALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003892
Gilles Peskine449bd832023-01-11 14:50:10 +01003893 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003894
Gilles Peskine449bd832023-01-11 14:50:10 +01003895 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3896 psa_set_key_algorithm(&attributes, alg);
3897 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003898
Gilles Peskine449bd832023-01-11 14:50:10 +01003899 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3900 &key));
3901 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3902 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3903 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003904
3905exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003906 psa_cipher_abort(&operation);
3907 mbedtls_free(output);
3908 psa_destroy_key(key);
3909 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003910}
3911/* END_CASE */
3912
3913/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003914void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3915 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003916{
3917 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3918 psa_key_type_t key_type = key_type_arg;
3919 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003920 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3921 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003922 unsigned char *output = NULL;
3923 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003924 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003925 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3926
Gilles Peskine449bd832023-01-11 14:50:10 +01003927 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003928
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003929 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003930 TEST_LE_U(ciphertext->len,
3931 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3932 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3933 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3934 TEST_LE_U(plaintext->len,
3935 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3936 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3937 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003938
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003939
3940 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003941 psa_set_key_usage_flags(&attributes,
3942 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3943 psa_set_key_algorithm(&attributes, alg);
3944 psa_set_key_type(&attributes, key_type);
3945 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3946 &key));
3947 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3948 plaintext->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003949 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003950
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003951 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003952 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3953 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3954 PSA_ERROR_BAD_STATE);
3955 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3956 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3957 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003958
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003959 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003960 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3961 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3962 &length),
3963 PSA_ERROR_BAD_STATE);
3964 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3965 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3966 &length),
3967 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003968
Gilles Peskine286c3142022-04-20 17:09:38 +02003969 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003970 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003971 output_length = 0;
3972 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003973 PSA_ASSERT(psa_cipher_update(&operation,
3974 plaintext->x, plaintext->len,
3975 output, output_buffer_size,
3976 &length));
3977 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003978 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003979 PSA_ASSERT(psa_cipher_finish(&operation,
3980 mbedtls_buffer_offset(output, output_length),
3981 output_buffer_size - output_length,
3982 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003983 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003984 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003985 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003986
Gilles Peskine286c3142022-04-20 17:09:38 +02003987 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003988 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003989 output_length = 0;
3990 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003991 PSA_ASSERT(psa_cipher_update(&operation,
3992 ciphertext->x, ciphertext->len,
3993 output, output_buffer_size,
3994 &length));
3995 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003996 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003997 PSA_ASSERT(psa_cipher_finish(&operation,
3998 mbedtls_buffer_offset(output, output_length),
3999 output_buffer_size - output_length,
4000 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02004001 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004002 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004003 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004004
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004005 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004006 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004007 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
4008 output, output_buffer_size,
4009 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004010 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004011 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004012
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004013 /* One-shot decryption */
4014 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004015 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
4016 output, output_buffer_size,
4017 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004018 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004019 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004020
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004021exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004022 PSA_ASSERT(psa_cipher_abort(&operation));
4023 mbedtls_free(output);
4024 psa_cipher_abort(&operation);
4025 psa_destroy_key(key);
4026 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004027}
4028/* END_CASE */
4029
4030/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004031void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004032{
4033 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4034 psa_algorithm_t alg = alg_arg;
4035 psa_key_type_t key_type = key_type_arg;
4036 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4037 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4038 psa_status_t status;
4039
Gilles Peskine449bd832023-01-11 14:50:10 +01004040 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004041
Gilles Peskine449bd832023-01-11 14:50:10 +01004042 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4043 psa_set_key_algorithm(&attributes, alg);
4044 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004045
4046 /* Usage of either of these two size macros would cause divide by zero
4047 * with incorrect key types previously. Input length should be irrelevant
4048 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004049 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4050 0);
4051 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004052
4053
Gilles Peskine449bd832023-01-11 14:50:10 +01004054 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4055 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004056
4057 /* Should fail due to invalid alg type (to support invalid key type).
4058 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004060
Gilles Peskine449bd832023-01-11 14:50:10 +01004061 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004062
4063exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004064 psa_cipher_abort(&operation);
4065 psa_destroy_key(key);
4066 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004067}
4068/* END_CASE */
4069
4070/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004071void cipher_encrypt_validation(int alg_arg,
4072 int key_type_arg,
4073 data_t *key_data,
4074 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004075{
4076 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4077 psa_key_type_t key_type = key_type_arg;
4078 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004080 unsigned char *output1 = NULL;
4081 size_t output1_buffer_size = 0;
4082 size_t output1_length = 0;
4083 unsigned char *output2 = NULL;
4084 size_t output2_buffer_size = 0;
4085 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004086 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004087 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004088 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004089
Gilles Peskine449bd832023-01-11 14:50:10 +01004090 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004091
Gilles Peskine449bd832023-01-11 14:50:10 +01004092 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4093 psa_set_key_algorithm(&attributes, alg);
4094 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004095
Gilles Peskine449bd832023-01-11 14:50:10 +01004096 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4097 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4098 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004099 TEST_CALLOC(output1, output1_buffer_size);
4100 TEST_CALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004101
Gilles Peskine449bd832023-01-11 14:50:10 +01004102 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4103 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004104
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004105 /* The one-shot cipher encryption uses generated iv so validating
4106 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004107 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4108 output1_buffer_size, &output1_length));
4109 TEST_LE_U(output1_length,
4110 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4111 TEST_LE_U(output1_length,
4112 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004113
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4115 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004116
Gilles Peskine449bd832023-01-11 14:50:10 +01004117 PSA_ASSERT(psa_cipher_update(&operation,
4118 input->x, input->len,
4119 output2, output2_buffer_size,
4120 &function_output_length));
4121 TEST_LE_U(function_output_length,
4122 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4123 TEST_LE_U(function_output_length,
4124 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004125 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004126
Gilles Peskine449bd832023-01-11 14:50:10 +01004127 PSA_ASSERT(psa_cipher_finish(&operation,
4128 output2 + output2_length,
4129 output2_buffer_size - output2_length,
4130 &function_output_length));
4131 TEST_LE_U(function_output_length,
4132 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4133 TEST_LE_U(function_output_length,
4134 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004135 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004136
Gilles Peskine449bd832023-01-11 14:50:10 +01004137 PSA_ASSERT(psa_cipher_abort(&operation));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004138 TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004139 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004140
Gilles Peskine50e586b2018-06-08 14:28:46 +02004141exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004142 psa_cipher_abort(&operation);
4143 mbedtls_free(output1);
4144 mbedtls_free(output2);
4145 psa_destroy_key(key);
4146 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004147}
4148/* END_CASE */
4149
4150/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004151void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4152 data_t *key_data, data_t *iv,
4153 data_t *input,
4154 int first_part_size_arg,
4155 int output1_length_arg, int output2_length_arg,
4156 data_t *expected_output,
4157 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004158{
Ronald Cron5425a212020-08-04 14:58:35 +02004159 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004160 psa_key_type_t key_type = key_type_arg;
4161 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004162 psa_status_t status;
4163 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004164 size_t first_part_size = first_part_size_arg;
4165 size_t output1_length = output1_length_arg;
4166 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004167 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004168 size_t output_buffer_size = 0;
4169 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004170 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004171 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004172 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004173
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004175
Gilles Peskine449bd832023-01-11 14:50:10 +01004176 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4177 psa_set_key_algorithm(&attributes, alg);
4178 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004179
Gilles Peskine449bd832023-01-11 14:50:10 +01004180 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4181 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004182
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004184
Gilles Peskine449bd832023-01-11 14:50:10 +01004185 if (iv->len > 0) {
4186 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004187 }
4188
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4190 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004191 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004192
Gilles Peskine449bd832023-01-11 14:50:10 +01004193 TEST_LE_U(first_part_size, input->len);
4194 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4195 output, output_buffer_size,
4196 &function_output_length));
4197 TEST_ASSERT(function_output_length == output1_length);
4198 TEST_LE_U(function_output_length,
4199 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4200 TEST_LE_U(function_output_length,
4201 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004202 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004203
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 if (first_part_size < input->len) {
4205 PSA_ASSERT(psa_cipher_update(&operation,
4206 input->x + first_part_size,
4207 input->len - first_part_size,
4208 (output_buffer_size == 0 ? NULL :
4209 output + total_output_length),
4210 output_buffer_size - total_output_length,
4211 &function_output_length));
4212 TEST_ASSERT(function_output_length == output2_length);
4213 TEST_LE_U(function_output_length,
4214 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4215 alg,
4216 input->len - first_part_size));
4217 TEST_LE_U(function_output_length,
4218 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004219 total_output_length += function_output_length;
4220 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004221
Gilles Peskine449bd832023-01-11 14:50:10 +01004222 status = psa_cipher_finish(&operation,
4223 (output_buffer_size == 0 ? NULL :
4224 output + total_output_length),
4225 output_buffer_size - total_output_length,
4226 &function_output_length);
4227 TEST_LE_U(function_output_length,
4228 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4229 TEST_LE_U(function_output_length,
4230 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004231 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004232 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004233
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 if (expected_status == PSA_SUCCESS) {
4235 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004236
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004237 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004238 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004239 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004240
4241exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004242 psa_cipher_abort(&operation);
4243 mbedtls_free(output);
4244 psa_destroy_key(key);
4245 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004246}
4247/* END_CASE */
4248
4249/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004250void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4251 data_t *key_data, data_t *iv,
4252 data_t *input,
4253 int first_part_size_arg,
4254 int output1_length_arg, int output2_length_arg,
4255 data_t *expected_output,
4256 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004257{
Ronald Cron5425a212020-08-04 14:58:35 +02004258 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004259 psa_key_type_t key_type = key_type_arg;
4260 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004261 psa_status_t status;
4262 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004263 size_t first_part_size = first_part_size_arg;
4264 size_t output1_length = output1_length_arg;
4265 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004266 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004267 size_t output_buffer_size = 0;
4268 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004269 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004270 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004271 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004272
Gilles Peskine449bd832023-01-11 14:50:10 +01004273 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004274
Gilles Peskine449bd832023-01-11 14:50:10 +01004275 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4276 psa_set_key_algorithm(&attributes, alg);
4277 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004278
Gilles Peskine449bd832023-01-11 14:50:10 +01004279 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4280 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004281
Gilles Peskine449bd832023-01-11 14:50:10 +01004282 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004283
Gilles Peskine449bd832023-01-11 14:50:10 +01004284 if (iv->len > 0) {
4285 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004286 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004287
Gilles Peskine449bd832023-01-11 14:50:10 +01004288 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4289 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004290 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004291
Gilles Peskine449bd832023-01-11 14:50:10 +01004292 TEST_LE_U(first_part_size, input->len);
4293 PSA_ASSERT(psa_cipher_update(&operation,
4294 input->x, first_part_size,
4295 output, output_buffer_size,
4296 &function_output_length));
4297 TEST_ASSERT(function_output_length == output1_length);
4298 TEST_LE_U(function_output_length,
4299 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4300 TEST_LE_U(function_output_length,
4301 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004302 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004303
Gilles Peskine449bd832023-01-11 14:50:10 +01004304 if (first_part_size < input->len) {
4305 PSA_ASSERT(psa_cipher_update(&operation,
4306 input->x + first_part_size,
4307 input->len - first_part_size,
4308 (output_buffer_size == 0 ? NULL :
4309 output + total_output_length),
4310 output_buffer_size - total_output_length,
4311 &function_output_length));
4312 TEST_ASSERT(function_output_length == output2_length);
4313 TEST_LE_U(function_output_length,
4314 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4315 alg,
4316 input->len - first_part_size));
4317 TEST_LE_U(function_output_length,
4318 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004319 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004320 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004321
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 status = psa_cipher_finish(&operation,
4323 (output_buffer_size == 0 ? NULL :
4324 output + total_output_length),
4325 output_buffer_size - total_output_length,
4326 &function_output_length);
4327 TEST_LE_U(function_output_length,
4328 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4329 TEST_LE_U(function_output_length,
4330 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004331 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004333
Gilles Peskine449bd832023-01-11 14:50:10 +01004334 if (expected_status == PSA_SUCCESS) {
4335 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004336
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004337 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004338 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004339 }
4340
Gilles Peskine50e586b2018-06-08 14:28:46 +02004341exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004342 psa_cipher_abort(&operation);
4343 mbedtls_free(output);
4344 psa_destroy_key(key);
4345 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004346}
4347/* END_CASE */
4348
Gilles Peskine50e586b2018-06-08 14:28:46 +02004349/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004350void cipher_decrypt_fail(int alg_arg,
4351 int key_type_arg,
4352 data_t *key_data,
4353 data_t *iv,
4354 data_t *input_arg,
4355 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004356{
4357 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4358 psa_status_t status;
4359 psa_key_type_t key_type = key_type_arg;
4360 psa_algorithm_t alg = alg_arg;
4361 psa_status_t expected_status = expected_status_arg;
4362 unsigned char *input = NULL;
4363 size_t input_buffer_size = 0;
4364 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004365 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004366 size_t output_buffer_size = 0;
4367 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004368 size_t function_output_length;
4369 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004370 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4371
Gilles Peskine449bd832023-01-11 14:50:10 +01004372 if (PSA_ERROR_BAD_STATE != expected_status) {
4373 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004374
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4376 psa_set_key_algorithm(&attributes, alg);
4377 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004378
Gilles Peskine449bd832023-01-11 14:50:10 +01004379 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4380 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004381 }
4382
4383 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004384 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4385 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004386 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 memcpy(input, iv->x, iv->len);
4388 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004389 }
4390
Gilles Peskine449bd832023-01-11 14:50:10 +01004391 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004392 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004393
Neil Armstrong66a479f2022-02-07 15:41:19 +01004394 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4396 output_buffer_size, &output_length);
4397 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004398
Neil Armstrong66a479f2022-02-07 15:41:19 +01004399 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 status = psa_cipher_decrypt_setup(&operation, key, alg);
4401 if (status == PSA_SUCCESS) {
4402 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4403 input_arg->len) +
4404 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004405 TEST_CALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004406
Gilles Peskine449bd832023-01-11 14:50:10 +01004407 if (iv->len > 0) {
4408 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004409
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 if (status != PSA_SUCCESS) {
4411 TEST_EQUAL(status, expected_status);
4412 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004413 }
4414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 if (status == PSA_SUCCESS) {
4416 status = psa_cipher_update(&operation,
4417 input_arg->x, input_arg->len,
4418 output_multi, output_buffer_size,
4419 &function_output_length);
4420 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004421 output_length = function_output_length;
4422
Gilles Peskine449bd832023-01-11 14:50:10 +01004423 status = psa_cipher_finish(&operation,
4424 output_multi + output_length,
4425 output_buffer_size - output_length,
4426 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004427
Gilles Peskine449bd832023-01-11 14:50:10 +01004428 TEST_EQUAL(status, expected_status);
4429 } else {
4430 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004431 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004432 } else {
4433 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004434 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 } else {
4436 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004437 }
4438
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004439exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 psa_cipher_abort(&operation);
4441 mbedtls_free(input);
4442 mbedtls_free(output);
4443 mbedtls_free(output_multi);
4444 psa_destroy_key(key);
4445 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004446}
4447/* END_CASE */
4448
4449/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004450void cipher_decrypt(int alg_arg,
4451 int key_type_arg,
4452 data_t *key_data,
4453 data_t *iv,
4454 data_t *input_arg,
4455 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004456{
4457 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4458 psa_key_type_t key_type = key_type_arg;
4459 psa_algorithm_t alg = alg_arg;
4460 unsigned char *input = NULL;
4461 size_t input_buffer_size = 0;
4462 unsigned char *output = NULL;
4463 size_t output_buffer_size = 0;
4464 size_t output_length = 0;
4465 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4466
Gilles Peskine449bd832023-01-11 14:50:10 +01004467 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004468
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4470 psa_set_key_algorithm(&attributes, alg);
4471 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004472
4473 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004474 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4475 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004476 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004477 memcpy(input, iv->x, iv->len);
4478 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004479 }
4480
Gilles Peskine449bd832023-01-11 14:50:10 +01004481 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004482 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004483
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4485 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004486
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4488 output_buffer_size, &output_length));
4489 TEST_LE_U(output_length,
4490 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4491 TEST_LE_U(output_length,
4492 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004493
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004494 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004495 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004496exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 mbedtls_free(input);
4498 mbedtls_free(output);
4499 psa_destroy_key(key);
4500 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004501}
4502/* END_CASE */
4503
4504/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004505void cipher_verify_output(int alg_arg,
4506 int key_type_arg,
4507 data_t *key_data,
4508 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004509{
Ronald Cron5425a212020-08-04 14:58:35 +02004510 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004511 psa_key_type_t key_type = key_type_arg;
4512 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004513 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004514 size_t output1_size = 0;
4515 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004516 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004517 size_t output2_size = 0;
4518 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004519 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004520
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004522
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4524 psa_set_key_algorithm(&attributes, alg);
4525 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004526
Gilles Peskine449bd832023-01-11 14:50:10 +01004527 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4528 &key));
4529 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004530 TEST_CALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004531
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4533 output1, output1_size,
4534 &output1_length));
4535 TEST_LE_U(output1_length,
4536 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4537 TEST_LE_U(output1_length,
4538 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004539
4540 output2_size = output1_length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004541 TEST_CALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004542
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4544 output2, output2_size,
4545 &output2_length));
4546 TEST_LE_U(output2_length,
4547 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4548 TEST_LE_U(output2_length,
4549 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004550
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004551 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004552
4553exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004554 mbedtls_free(output1);
4555 mbedtls_free(output2);
4556 psa_destroy_key(key);
4557 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004558}
4559/* END_CASE */
4560
4561/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004562void cipher_verify_output_multipart(int alg_arg,
4563 int key_type_arg,
4564 data_t *key_data,
4565 data_t *input,
4566 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004567{
Ronald Cron5425a212020-08-04 14:58:35 +02004568 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004569 psa_key_type_t key_type = key_type_arg;
4570 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004571 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004573 size_t iv_size = 16;
4574 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004575 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004576 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004577 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004578 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004579 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004580 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004581 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004582 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4583 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004584 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004585
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004587
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4589 psa_set_key_algorithm(&attributes, alg);
4590 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004591
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4593 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004594
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4596 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004597
Gilles Peskine449bd832023-01-11 14:50:10 +01004598 if (alg != PSA_ALG_ECB_NO_PADDING) {
4599 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4600 iv, iv_size,
4601 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004602 }
4603
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4605 TEST_LE_U(output1_buffer_size,
4606 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004607 TEST_CALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004608
Gilles Peskine449bd832023-01-11 14:50:10 +01004609 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004610
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4612 output1, output1_buffer_size,
4613 &function_output_length));
4614 TEST_LE_U(function_output_length,
4615 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4616 TEST_LE_U(function_output_length,
4617 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004618 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004619
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 PSA_ASSERT(psa_cipher_update(&operation1,
4621 input->x + first_part_size,
4622 input->len - first_part_size,
4623 output1, output1_buffer_size,
4624 &function_output_length));
4625 TEST_LE_U(function_output_length,
4626 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4627 alg,
4628 input->len - first_part_size));
4629 TEST_LE_U(function_output_length,
4630 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004631 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004632
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 PSA_ASSERT(psa_cipher_finish(&operation1,
4634 output1 + output1_length,
4635 output1_buffer_size - output1_length,
4636 &function_output_length));
4637 TEST_LE_U(function_output_length,
4638 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4639 TEST_LE_U(function_output_length,
4640 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004641 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004642
Gilles Peskine449bd832023-01-11 14:50:10 +01004643 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004644
Gilles Peskine048b7f02018-06-08 14:20:49 +02004645 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004646 TEST_LE_U(output2_buffer_size,
4647 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4648 TEST_LE_U(output2_buffer_size,
4649 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004650 TEST_CALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004651
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 if (iv_length > 0) {
4653 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4654 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004655 }
Moran Pekerded84402018-06-06 16:36:50 +03004656
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4658 output2, output2_buffer_size,
4659 &function_output_length));
4660 TEST_LE_U(function_output_length,
4661 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4662 TEST_LE_U(function_output_length,
4663 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004664 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004665
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 PSA_ASSERT(psa_cipher_update(&operation2,
4667 output1 + first_part_size,
4668 output1_length - first_part_size,
4669 output2, output2_buffer_size,
4670 &function_output_length));
4671 TEST_LE_U(function_output_length,
4672 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4673 alg,
4674 output1_length - first_part_size));
4675 TEST_LE_U(function_output_length,
4676 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004677 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004678
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 PSA_ASSERT(psa_cipher_finish(&operation2,
4680 output2 + output2_length,
4681 output2_buffer_size - output2_length,
4682 &function_output_length));
4683 TEST_LE_U(function_output_length,
4684 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4685 TEST_LE_U(function_output_length,
4686 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004687 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004688
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004690
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004691 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004692
4693exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004694 psa_cipher_abort(&operation1);
4695 psa_cipher_abort(&operation2);
4696 mbedtls_free(output1);
4697 mbedtls_free(output2);
4698 psa_destroy_key(key);
4699 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004700}
4701/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004702
Gilles Peskine20035e32018-02-03 22:44:14 +01004703/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004704void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4705 int alg_arg,
4706 data_t *nonce,
4707 data_t *additional_data,
4708 data_t *input_data,
4709 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004710{
Ronald Cron5425a212020-08-04 14:58:35 +02004711 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004712 psa_key_type_t key_type = key_type_arg;
4713 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004714 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004715 unsigned char *output_data = NULL;
4716 size_t output_size = 0;
4717 size_t output_length = 0;
4718 unsigned char *output_data2 = NULL;
4719 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004720 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004721 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004723
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004725
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4727 psa_set_key_algorithm(&attributes, alg);
4728 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004729
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4731 &key));
4732 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4733 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004734
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4736 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004737 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4738 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004739 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4740 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4741 TEST_EQUAL(output_size,
4742 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4743 TEST_LE_U(output_size,
4744 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004745 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004746 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004747
Gilles Peskine449bd832023-01-11 14:50:10 +01004748 status = psa_aead_encrypt(key, alg,
4749 nonce->x, nonce->len,
4750 additional_data->x,
4751 additional_data->len,
4752 input_data->x, input_data->len,
4753 output_data, output_size,
4754 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004755
4756 /* If the operation is not supported, just skip and not fail in case the
4757 * encryption involves a common limitation of cryptography hardwares and
4758 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 if (status == PSA_ERROR_NOT_SUPPORTED) {
4760 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4761 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004762 }
4763
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004765
Gilles Peskine449bd832023-01-11 14:50:10 +01004766 if (PSA_SUCCESS == expected_result) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004767 TEST_CALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004768
Gilles Peskine003a4a92019-05-14 16:09:40 +02004769 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4770 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 TEST_EQUAL(input_data->len,
4772 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004773
Gilles Peskine449bd832023-01-11 14:50:10 +01004774 TEST_LE_U(input_data->len,
4775 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004776
Gilles Peskine449bd832023-01-11 14:50:10 +01004777 TEST_EQUAL(psa_aead_decrypt(key, alg,
4778 nonce->x, nonce->len,
4779 additional_data->x,
4780 additional_data->len,
4781 output_data, output_length,
4782 output_data2, output_length,
4783 &output_length2),
4784 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004785
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004786 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004787 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004788 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004789
Gilles Peskinea1cac842018-06-11 19:33:02 +02004790exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004791 psa_destroy_key(key);
4792 mbedtls_free(output_data);
4793 mbedtls_free(output_data2);
4794 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004795}
4796/* END_CASE */
4797
4798/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004799void aead_encrypt(int key_type_arg, data_t *key_data,
4800 int alg_arg,
4801 data_t *nonce,
4802 data_t *additional_data,
4803 data_t *input_data,
4804 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004805{
Ronald Cron5425a212020-08-04 14:58:35 +02004806 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004807 psa_key_type_t key_type = key_type_arg;
4808 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004809 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004810 unsigned char *output_data = NULL;
4811 size_t output_size = 0;
4812 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004813 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004814 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004815
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004817
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4819 psa_set_key_algorithm(&attributes, alg);
4820 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004821
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4823 &key));
4824 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4825 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004826
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4828 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004829 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4830 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 TEST_EQUAL(output_size,
4832 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4833 TEST_LE_U(output_size,
4834 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004835 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004836
Gilles Peskine449bd832023-01-11 14:50:10 +01004837 status = psa_aead_encrypt(key, alg,
4838 nonce->x, nonce->len,
4839 additional_data->x, additional_data->len,
4840 input_data->x, input_data->len,
4841 output_data, output_size,
4842 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004843
Ronald Cron28a45ed2021-02-09 20:35:42 +01004844 /* If the operation is not supported, just skip and not fail in case the
4845 * encryption involves a common limitation of cryptography hardwares and
4846 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004847 if (status == PSA_ERROR_NOT_SUPPORTED) {
4848 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4849 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004850 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004851
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 PSA_ASSERT(status);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004853 TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004854 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004855
Gilles Peskinea1cac842018-06-11 19:33:02 +02004856exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 psa_destroy_key(key);
4858 mbedtls_free(output_data);
4859 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004860}
4861/* END_CASE */
4862
4863/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004864void aead_decrypt(int key_type_arg, data_t *key_data,
4865 int alg_arg,
4866 data_t *nonce,
4867 data_t *additional_data,
4868 data_t *input_data,
4869 data_t *expected_data,
4870 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004871{
Ronald Cron5425a212020-08-04 14:58:35 +02004872 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004873 psa_key_type_t key_type = key_type_arg;
4874 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004875 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004876 unsigned char *output_data = NULL;
4877 size_t output_size = 0;
4878 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004879 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004880 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004881 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004882
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004884
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4886 psa_set_key_algorithm(&attributes, alg);
4887 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004888
Gilles Peskine449bd832023-01-11 14:50:10 +01004889 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4890 &key));
4891 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4892 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004893
Gilles Peskine449bd832023-01-11 14:50:10 +01004894 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4895 alg);
4896 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4897 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004898 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4899 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004900 TEST_EQUAL(output_size,
4901 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4902 TEST_LE_U(output_size,
4903 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004904 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004905 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004906
Gilles Peskine449bd832023-01-11 14:50:10 +01004907 status = psa_aead_decrypt(key, alg,
4908 nonce->x, nonce->len,
4909 additional_data->x,
4910 additional_data->len,
4911 input_data->x, input_data->len,
4912 output_data, output_size,
4913 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004914
Ronald Cron28a45ed2021-02-09 20:35:42 +01004915 /* If the operation is not supported, just skip and not fail in case the
4916 * decryption involves a common limitation of cryptography hardwares and
4917 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 if (status == PSA_ERROR_NOT_SUPPORTED) {
4919 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4920 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004921 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004922
Gilles Peskine449bd832023-01-11 14:50:10 +01004923 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004924
Gilles Peskine449bd832023-01-11 14:50:10 +01004925 if (expected_result == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004926 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004927 output_data, output_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004928 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004929
Gilles Peskinea1cac842018-06-11 19:33:02 +02004930exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004931 psa_destroy_key(key);
4932 mbedtls_free(output_data);
4933 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004934}
4935/* END_CASE */
4936
4937/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004938void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4939 int alg_arg,
4940 data_t *nonce,
4941 data_t *additional_data,
4942 data_t *input_data,
4943 int do_set_lengths,
4944 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004945{
Paul Elliottd3f82412021-06-16 16:52:21 +01004946 size_t ad_part_len = 0;
4947 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004948 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004949
Gilles Peskine449bd832023-01-11 14:50:10 +01004950 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4951 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004952
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 if (do_set_lengths) {
4954 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004955 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004956 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004957 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004958 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004959 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004960
4961 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004962 if (!aead_multipart_internal_func(key_type_arg, key_data,
4963 alg_arg, nonce,
4964 additional_data,
4965 ad_part_len,
4966 input_data, -1,
4967 set_lengths_method,
4968 expected_output,
4969 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004970 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004971 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004972
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 /* length(0) part, length(ad_part_len) part, length(0) part... */
4974 mbedtls_test_set_step(1000 + ad_part_len);
4975
4976 if (!aead_multipart_internal_func(key_type_arg, key_data,
4977 alg_arg, nonce,
4978 additional_data,
4979 ad_part_len,
4980 input_data, -1,
4981 set_lengths_method,
4982 expected_output,
4983 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004984 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004985 }
4986 }
4987
4988 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4989 /* Split data into length(data_part_len) parts. */
4990 mbedtls_test_set_step(2000 + data_part_len);
4991
4992 if (do_set_lengths) {
4993 if (data_part_len & 0x01) {
4994 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4995 } else {
4996 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4997 }
4998 }
4999
5000 if (!aead_multipart_internal_func(key_type_arg, key_data,
5001 alg_arg, nonce,
5002 additional_data, -1,
5003 input_data, data_part_len,
5004 set_lengths_method,
5005 expected_output,
5006 1, 0)) {
5007 break;
5008 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005009
5010 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005011 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005012
Gilles Peskine449bd832023-01-11 14:50:10 +01005013 if (!aead_multipart_internal_func(key_type_arg, key_data,
5014 alg_arg, nonce,
5015 additional_data, -1,
5016 input_data, data_part_len,
5017 set_lengths_method,
5018 expected_output,
5019 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005020 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005022 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005023
Paul Elliott8fc45162021-06-23 16:06:01 +01005024 /* Goto is required to silence warnings about unused labels, as we
5025 * don't actually do any test assertions in this function. */
5026 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005027}
5028/* END_CASE */
5029
5030/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005031void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5032 int alg_arg,
5033 data_t *nonce,
5034 data_t *additional_data,
5035 data_t *input_data,
5036 int do_set_lengths,
5037 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005038{
Paul Elliottd3f82412021-06-16 16:52:21 +01005039 size_t ad_part_len = 0;
5040 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005041 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005042
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005044 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005046
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 if (do_set_lengths) {
5048 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005049 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005051 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005052 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005053 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005054
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 if (!aead_multipart_internal_func(key_type_arg, key_data,
5056 alg_arg, nonce,
5057 additional_data,
5058 ad_part_len,
5059 input_data, -1,
5060 set_lengths_method,
5061 expected_output,
5062 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005063 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005065
5066 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005067 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005068
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 if (!aead_multipart_internal_func(key_type_arg, key_data,
5070 alg_arg, nonce,
5071 additional_data,
5072 ad_part_len,
5073 input_data, -1,
5074 set_lengths_method,
5075 expected_output,
5076 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005077 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005079 }
5080
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005082 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005083 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005084
Gilles Peskine449bd832023-01-11 14:50:10 +01005085 if (do_set_lengths) {
5086 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005087 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005089 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005091 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005092
Gilles Peskine449bd832023-01-11 14:50:10 +01005093 if (!aead_multipart_internal_func(key_type_arg, key_data,
5094 alg_arg, nonce,
5095 additional_data, -1,
5096 input_data, data_part_len,
5097 set_lengths_method,
5098 expected_output,
5099 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005100 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005101 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005102
5103 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005104 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005105
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 if (!aead_multipart_internal_func(key_type_arg, key_data,
5107 alg_arg, nonce,
5108 additional_data, -1,
5109 input_data, data_part_len,
5110 set_lengths_method,
5111 expected_output,
5112 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005113 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005114 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005115 }
5116
Paul Elliott8fc45162021-06-23 16:06:01 +01005117 /* Goto is required to silence warnings about unused labels, as we
5118 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005119 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005120}
5121/* END_CASE */
5122
5123/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005124void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5125 int alg_arg,
5126 int nonce_length,
5127 int expected_nonce_length_arg,
5128 data_t *additional_data,
5129 data_t *input_data,
5130 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005131{
5132
5133 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5134 psa_key_type_t key_type = key_type_arg;
5135 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005136 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005137 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5138 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5139 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005140 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005141 size_t actual_nonce_length = 0;
5142 size_t expected_nonce_length = expected_nonce_length_arg;
5143 unsigned char *output = NULL;
5144 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005145 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005146 size_t ciphertext_size = 0;
5147 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005148 size_t tag_length = 0;
5149 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005150
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005152
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5154 psa_set_key_algorithm(&attributes, alg);
5155 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005156
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5158 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005159
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005161
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005163
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005164 TEST_CALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005165
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005167
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005169
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005170 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005171
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005173
5174 /* If the operation is not supported, just skip and not fail in case the
5175 * encryption involves a common limitation of cryptography hardwares and
5176 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 if (status == PSA_ERROR_NOT_SUPPORTED) {
5178 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5179 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005180 }
5181
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005183
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5185 nonce_length,
5186 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005187
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005189
Gilles Peskine449bd832023-01-11 14:50:10 +01005190 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005191
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 if (expected_status == PSA_SUCCESS) {
5193 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5194 alg));
5195 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005196
Gilles Peskine449bd832023-01-11 14:50:10 +01005197 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005198
Gilles Peskine449bd832023-01-11 14:50:10 +01005199 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005200 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005201 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5202 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005203
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5205 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005206
Gilles Peskine449bd832023-01-11 14:50:10 +01005207 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5208 output, output_size,
5209 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005210
Gilles Peskine449bd832023-01-11 14:50:10 +01005211 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5212 &ciphertext_length, tag_buffer,
5213 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005214 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005215
5216exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005217 psa_destroy_key(key);
5218 mbedtls_free(output);
5219 mbedtls_free(ciphertext);
5220 psa_aead_abort(&operation);
5221 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005222}
5223/* END_CASE */
5224
5225/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005226void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5227 int alg_arg,
5228 int nonce_length_arg,
5229 int set_lengths_method_arg,
5230 data_t *additional_data,
5231 data_t *input_data,
5232 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005233{
5234
5235 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5236 psa_key_type_t key_type = key_type_arg;
5237 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005238 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005239 uint8_t *nonce_buffer = NULL;
5240 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5241 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5242 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005243 unsigned char *output = NULL;
5244 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005245 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005246 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005247 size_t ciphertext_size = 0;
5248 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005249 size_t tag_length = 0;
5250 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005251 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005252 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005253
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005255
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5257 psa_set_key_algorithm(&attributes, alg);
5258 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005259
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5261 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005262
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005264
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005266
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005267 TEST_CALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005268
Gilles Peskine449bd832023-01-11 14:50:10 +01005269 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005270
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005272
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005273 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005274
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005276
5277 /* If the operation is not supported, just skip and not fail in case the
5278 * encryption involves a common limitation of cryptography hardwares and
5279 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005280 if (status == PSA_ERROR_NOT_SUPPORTED) {
5281 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5282 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005283 }
5284
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005286
Paul Elliott4023ffd2021-09-10 16:21:22 +01005287 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005288 if (nonce_length_arg == -1) {
5289 /* Arbitrary size buffer, to test zero length valid buffer. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005290 TEST_CALLOC(nonce_buffer, 4);
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 nonce_length = 0;
5292 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005293 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 nonce_length = (size_t) nonce_length_arg;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005295 TEST_CALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005296
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 if (nonce_buffer) {
5298 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005299 nonce_buffer[index] = 'a' + index;
5300 }
Paul Elliott66696b52021-08-16 18:42:41 +01005301 }
Paul Elliott863864a2021-07-23 17:28:31 +01005302 }
5303
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5305 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5306 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005307 }
5308
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005310
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005312
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 if (expected_status == PSA_SUCCESS) {
5314 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5315 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5316 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005317 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005319 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 }
Paul Elliott863864a2021-07-23 17:28:31 +01005321
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005322 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5324 additional_data->len),
5325 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005326
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5328 output, output_size,
5329 &ciphertext_length),
5330 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005331
Gilles Peskine449bd832023-01-11 14:50:10 +01005332 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5333 &ciphertext_length, tag_buffer,
5334 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5335 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005336 }
5337
5338exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 psa_destroy_key(key);
5340 mbedtls_free(output);
5341 mbedtls_free(ciphertext);
5342 mbedtls_free(nonce_buffer);
5343 psa_aead_abort(&operation);
5344 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005345}
5346/* END_CASE */
5347
5348/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005349void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005350 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005351 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005352 data_t *nonce,
5353 data_t *additional_data,
5354 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005356{
5357
5358 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5359 psa_key_type_t key_type = key_type_arg;
5360 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005361 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005362 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5363 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5364 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005365 unsigned char *output = NULL;
5366 unsigned char *ciphertext = NULL;
5367 size_t output_size = output_size_arg;
5368 size_t ciphertext_size = 0;
5369 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005370 size_t tag_length = 0;
5371 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5372
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005374
Gilles Peskine449bd832023-01-11 14:50:10 +01005375 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5376 psa_set_key_algorithm(&attributes, alg);
5377 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005378
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5380 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005381
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005383
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005384 TEST_CALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005385
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005387
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005388 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005389
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005391
5392 /* If the operation is not supported, just skip and not fail in case the
5393 * encryption involves a common limitation of cryptography hardwares and
5394 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 if (status == PSA_ERROR_NOT_SUPPORTED) {
5396 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5397 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005398 }
5399
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005401
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5403 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005404
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005406
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5408 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005409
Gilles Peskine449bd832023-01-11 14:50:10 +01005410 status = psa_aead_update(&operation, input_data->x, input_data->len,
5411 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005412
Gilles Peskine449bd832023-01-11 14:50:10 +01005413 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005414
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005416 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5418 &ciphertext_length, tag_buffer,
5419 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005420 }
5421
5422exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 psa_destroy_key(key);
5424 mbedtls_free(output);
5425 mbedtls_free(ciphertext);
5426 psa_aead_abort(&operation);
5427 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005428}
5429/* END_CASE */
5430
Paul Elliott91b021e2021-07-23 18:52:31 +01005431/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005432void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5433 int alg_arg,
5434 int finish_ciphertext_size_arg,
5435 int tag_size_arg,
5436 data_t *nonce,
5437 data_t *additional_data,
5438 data_t *input_data,
5439 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005440{
5441
5442 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5443 psa_key_type_t key_type = key_type_arg;
5444 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005445 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005446 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5447 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5448 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005449 unsigned char *ciphertext = NULL;
5450 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005451 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005452 size_t ciphertext_size = 0;
5453 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5455 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005456 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005457
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005459
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5461 psa_set_key_algorithm(&attributes, alg);
5462 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005463
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5465 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005466
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005468
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005470
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005471 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005472
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005473 TEST_CALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005474
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005475 TEST_CALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005476
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005478
5479 /* If the operation is not supported, just skip and not fail in case the
5480 * encryption involves a common limitation of cryptography hardwares and
5481 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 if (status == PSA_ERROR_NOT_SUPPORTED) {
5483 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5484 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005485 }
5486
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005488
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005490
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5492 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005493
Gilles Peskine449bd832023-01-11 14:50:10 +01005494 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5495 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005496
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5498 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005499
5500 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 status = psa_aead_finish(&operation, finish_ciphertext,
5502 finish_ciphertext_size,
5503 &ciphertext_length, tag_buffer,
5504 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005505
Gilles Peskine449bd832023-01-11 14:50:10 +01005506 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005507
5508exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 psa_destroy_key(key);
5510 mbedtls_free(ciphertext);
5511 mbedtls_free(finish_ciphertext);
5512 mbedtls_free(tag_buffer);
5513 psa_aead_abort(&operation);
5514 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005515}
5516/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005517
5518/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005519void aead_multipart_verify(int key_type_arg, data_t *key_data,
5520 int alg_arg,
5521 data_t *nonce,
5522 data_t *additional_data,
5523 data_t *input_data,
5524 data_t *tag,
5525 int tag_usage_arg,
5526 int expected_setup_status_arg,
5527 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005528{
5529 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5530 psa_key_type_t key_type = key_type_arg;
5531 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005532 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005533 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5534 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5535 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005536 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005537 unsigned char *plaintext = NULL;
5538 unsigned char *finish_plaintext = NULL;
5539 size_t plaintext_size = 0;
5540 size_t plaintext_length = 0;
5541 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005542 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005543 unsigned char *tag_buffer = NULL;
5544 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005545
Gilles Peskine449bd832023-01-11 14:50:10 +01005546 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005547
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5549 psa_set_key_algorithm(&attributes, alg);
5550 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005551
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5553 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005554
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005556
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5558 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005559
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005560 TEST_CALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005561
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005563
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005564 TEST_CALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005565
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005567
5568 /* If the operation is not supported, just skip and not fail in case the
5569 * encryption involves a common limitation of cryptography hardwares and
5570 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 if (status == PSA_ERROR_NOT_SUPPORTED) {
5572 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5573 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005574 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005576
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005578 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 }
Paul Elliott9961a662021-09-17 19:19:02 +01005580
Gilles Peskine449bd832023-01-11 14:50:10 +01005581 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005582
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005584
Gilles Peskine449bd832023-01-11 14:50:10 +01005585 status = psa_aead_set_lengths(&operation, additional_data->len,
5586 input_data->len);
5587 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005588
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5590 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005591
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5593 input_data->len,
5594 plaintext, plaintext_size,
5595 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005596
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005598 tag_buffer = tag->x;
5599 tag_size = tag->len;
5600 }
5601
Gilles Peskine449bd832023-01-11 14:50:10 +01005602 status = psa_aead_verify(&operation, finish_plaintext,
5603 verify_plaintext_size,
5604 &plaintext_length,
5605 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005606
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005608
5609exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 psa_destroy_key(key);
5611 mbedtls_free(plaintext);
5612 mbedtls_free(finish_plaintext);
5613 psa_aead_abort(&operation);
5614 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005615}
5616/* END_CASE */
5617
Paul Elliott9961a662021-09-17 19:19:02 +01005618/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005619void aead_multipart_setup(int key_type_arg, data_t *key_data,
5620 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005621{
5622 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5623 psa_key_type_t key_type = key_type_arg;
5624 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005625 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005626 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5627 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5628 psa_status_t expected_status = expected_status_arg;
5629
Gilles Peskine449bd832023-01-11 14:50:10 +01005630 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005631
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 psa_set_key_usage_flags(&attributes,
5633 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5634 psa_set_key_algorithm(&attributes, alg);
5635 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005636
Gilles Peskine449bd832023-01-11 14:50:10 +01005637 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5638 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005639
Gilles Peskine449bd832023-01-11 14:50:10 +01005640 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005641
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005643
Gilles Peskine449bd832023-01-11 14:50:10 +01005644 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005645
Gilles Peskine449bd832023-01-11 14:50:10 +01005646 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005647
Gilles Peskine449bd832023-01-11 14:50:10 +01005648 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005649
5650exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005651 psa_destroy_key(key);
5652 psa_aead_abort(&operation);
5653 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005654}
5655/* END_CASE */
5656
5657/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005658void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5659 int alg_arg,
5660 data_t *nonce,
5661 data_t *additional_data,
5662 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005663{
5664 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5665 psa_key_type_t key_type = key_type_arg;
5666 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005667 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005668 unsigned char *output_data = NULL;
5669 unsigned char *final_data = NULL;
5670 size_t output_size = 0;
5671 size_t finish_output_size = 0;
5672 size_t output_length = 0;
5673 size_t key_bits = 0;
5674 size_t tag_length = 0;
5675 size_t tag_size = 0;
5676 size_t nonce_length = 0;
5677 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5678 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5679 size_t output_part_length = 0;
5680 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5681
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005683
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 psa_set_key_usage_flags(&attributes,
5685 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5686 psa_set_key_algorithm(&attributes, alg);
5687 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005688
Gilles Peskine449bd832023-01-11 14:50:10 +01005689 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5690 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005691
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5693 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005694
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005696
Gilles Peskine449bd832023-01-11 14:50:10 +01005697 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005698
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005700
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005701 TEST_CALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005702
Gilles Peskine449bd832023-01-11 14:50:10 +01005703 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005704
Gilles Peskine449bd832023-01-11 14:50:10 +01005705 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005706
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005707 TEST_CALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005708
5709 /* Test all operations error without calling setup first. */
5710
Gilles Peskine449bd832023-01-11 14:50:10 +01005711 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5712 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005713
Gilles Peskine449bd832023-01-11 14:50:10 +01005714 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005715
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5717 PSA_AEAD_NONCE_MAX_SIZE,
5718 &nonce_length),
5719 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005720
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005722
Paul Elliott481be342021-07-16 17:38:47 +01005723 /* ------------------------------------------------------- */
5724
Gilles Peskine449bd832023-01-11 14:50:10 +01005725 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5726 input_data->len),
5727 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005728
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005730
Paul Elliott481be342021-07-16 17:38:47 +01005731 /* ------------------------------------------------------- */
5732
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5734 additional_data->len),
5735 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005736
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005738
Paul Elliott481be342021-07-16 17:38:47 +01005739 /* ------------------------------------------------------- */
5740
Gilles Peskine449bd832023-01-11 14:50:10 +01005741 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5742 input_data->len, output_data,
5743 output_size, &output_length),
5744 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005745
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005747
Paul Elliott481be342021-07-16 17:38:47 +01005748 /* ------------------------------------------------------- */
5749
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5751 finish_output_size,
5752 &output_part_length,
5753 tag_buffer, tag_length,
5754 &tag_size),
5755 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005756
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005758
Paul Elliott481be342021-07-16 17:38:47 +01005759 /* ------------------------------------------------------- */
5760
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5762 finish_output_size,
5763 &output_part_length,
5764 tag_buffer,
5765 tag_length),
5766 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005767
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005769
5770 /* Test for double setups. */
5771
Gilles Peskine449bd832023-01-11 14:50:10 +01005772 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005773
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5775 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005776
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005778
Paul Elliott481be342021-07-16 17:38:47 +01005779 /* ------------------------------------------------------- */
5780
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005782
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5784 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005785
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005787
Paul Elliott374a2be2021-07-16 17:53:40 +01005788 /* ------------------------------------------------------- */
5789
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005791
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5793 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005794
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005796
5797 /* ------------------------------------------------------- */
5798
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005800
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5802 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005803
Gilles Peskine449bd832023-01-11 14:50:10 +01005804 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005805
Paul Elliottc23a9a02021-06-21 18:32:46 +01005806 /* Test for not setting a nonce. */
5807
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005809
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5811 additional_data->len),
5812 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005813
Gilles Peskine449bd832023-01-11 14:50:10 +01005814 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005815
Paul Elliott7f628422021-09-01 12:08:29 +01005816 /* ------------------------------------------------------- */
5817
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005819
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5821 input_data->len, output_data,
5822 output_size, &output_length),
5823 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005824
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005826
Paul Elliottbdc2c682021-09-21 18:37:10 +01005827 /* ------------------------------------------------------- */
5828
Gilles Peskine449bd832023-01-11 14:50:10 +01005829 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005830
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5832 finish_output_size,
5833 &output_part_length,
5834 tag_buffer, tag_length,
5835 &tag_size),
5836 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005837
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005839
5840 /* ------------------------------------------------------- */
5841
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005843
Gilles Peskine449bd832023-01-11 14:50:10 +01005844 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5845 finish_output_size,
5846 &output_part_length,
5847 tag_buffer,
5848 tag_length),
5849 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005850
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005852
Paul Elliottc23a9a02021-06-21 18:32:46 +01005853 /* Test for double setting nonce. */
5854
Gilles Peskine449bd832023-01-11 14:50:10 +01005855 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005856
Gilles Peskine449bd832023-01-11 14:50:10 +01005857 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005858
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5860 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005861
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005863
Paul Elliott374a2be2021-07-16 17:53:40 +01005864 /* Test for double generating nonce. */
5865
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005867
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5869 PSA_AEAD_NONCE_MAX_SIZE,
5870 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005871
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5873 PSA_AEAD_NONCE_MAX_SIZE,
5874 &nonce_length),
5875 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005876
5877
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005879
5880 /* Test for generate nonce then set and vice versa */
5881
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005883
Gilles Peskine449bd832023-01-11 14:50:10 +01005884 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5885 PSA_AEAD_NONCE_MAX_SIZE,
5886 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005887
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5889 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005890
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005892
Andrzej Kurekad837522021-12-15 15:28:49 +01005893 /* Test for generating nonce after calling set lengths */
5894
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005896
Gilles Peskine449bd832023-01-11 14:50:10 +01005897 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5898 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005899
Gilles Peskine449bd832023-01-11 14:50:10 +01005900 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5901 PSA_AEAD_NONCE_MAX_SIZE,
5902 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005903
Gilles Peskine449bd832023-01-11 14:50:10 +01005904 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005905
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005906 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005907
Gilles Peskine449bd832023-01-11 14:50:10 +01005908 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005909
Gilles Peskine449bd832023-01-11 14:50:10 +01005910 if (operation.alg == PSA_ALG_CCM) {
5911 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5912 input_data->len),
5913 PSA_ERROR_INVALID_ARGUMENT);
5914 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5915 PSA_AEAD_NONCE_MAX_SIZE,
5916 &nonce_length),
5917 PSA_ERROR_BAD_STATE);
5918 } else {
5919 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5920 input_data->len));
5921 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5922 PSA_AEAD_NONCE_MAX_SIZE,
5923 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005924 }
5925
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005927
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005928 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005929#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005931
Gilles Peskine449bd832023-01-11 14:50:10 +01005932 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5933 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5934 input_data->len),
5935 PSA_ERROR_INVALID_ARGUMENT);
5936 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5937 PSA_AEAD_NONCE_MAX_SIZE,
5938 &nonce_length),
5939 PSA_ERROR_BAD_STATE);
5940 } else {
5941 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5942 input_data->len));
5943 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 }
5947
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005949#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005950
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005951 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005952
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005954
Gilles Peskine449bd832023-01-11 14:50:10 +01005955 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5956 PSA_AEAD_NONCE_MAX_SIZE,
5957 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005958
Gilles Peskine449bd832023-01-11 14:50:10 +01005959 if (operation.alg == PSA_ALG_CCM) {
5960 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5961 input_data->len),
5962 PSA_ERROR_INVALID_ARGUMENT);
5963 } else {
5964 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5965 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005966 }
5967
Gilles Peskine449bd832023-01-11 14:50:10 +01005968 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005969
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005970 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005971 /* Test for setting nonce after calling set lengths */
5972
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005974
Gilles Peskine449bd832023-01-11 14:50:10 +01005975 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5976 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005977
Gilles Peskine449bd832023-01-11 14:50:10 +01005978 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005979
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005981
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005982 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005983
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005985
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 if (operation.alg == PSA_ALG_CCM) {
5987 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5988 input_data->len),
5989 PSA_ERROR_INVALID_ARGUMENT);
5990 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5991 PSA_ERROR_BAD_STATE);
5992 } else {
5993 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5994 input_data->len));
5995 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005996 }
5997
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005999
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006000 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00006001#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006002 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006003
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
6005 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
6006 input_data->len),
6007 PSA_ERROR_INVALID_ARGUMENT);
6008 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6009 PSA_ERROR_BAD_STATE);
6010 } else {
6011 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6012 input_data->len));
6013 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006014 }
6015
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00006017#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006018
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006019 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006020
Gilles Peskine449bd832023-01-11 14:50:10 +01006021 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006022
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006024
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 if (operation.alg == PSA_ALG_CCM) {
6026 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6027 input_data->len),
6028 PSA_ERROR_INVALID_ARGUMENT);
6029 } else {
6030 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6031 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006032 }
6033
Gilles Peskine449bd832023-01-11 14:50:10 +01006034 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006035
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006036 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006037#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006038 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006039
Gilles Peskine449bd832023-01-11 14:50:10 +01006040 if (operation.alg == PSA_ALG_GCM) {
6041 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6042 SIZE_MAX),
6043 PSA_ERROR_INVALID_ARGUMENT);
6044 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6045 PSA_ERROR_BAD_STATE);
6046 } else if (operation.alg != PSA_ALG_CCM) {
6047 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6048 SIZE_MAX));
6049 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006050 }
6051
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006053#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006054
Tom Cosgrove1797b052022-12-04 17:19:59 +00006055 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006056#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006058
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006060
Gilles Peskine449bd832023-01-11 14:50:10 +01006061 if (operation.alg == PSA_ALG_GCM) {
6062 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6063 SIZE_MAX),
6064 PSA_ERROR_INVALID_ARGUMENT);
6065 } else if (operation.alg != PSA_ALG_CCM) {
6066 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6067 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006068 }
6069
Gilles Peskine449bd832023-01-11 14:50:10 +01006070 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006071#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006072
6073 /* ------------------------------------------------------- */
6074
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006076
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006078
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6080 PSA_AEAD_NONCE_MAX_SIZE,
6081 &nonce_length),
6082 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006083
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006085
Paul Elliott7220cae2021-06-22 17:25:57 +01006086 /* Test for generating nonce in decrypt setup. */
6087
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006089
Gilles Peskine449bd832023-01-11 14:50:10 +01006090 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6091 PSA_AEAD_NONCE_MAX_SIZE,
6092 &nonce_length),
6093 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006094
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006096
Paul Elliottc23a9a02021-06-21 18:32:46 +01006097 /* Test for setting lengths twice. */
6098
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006100
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006102
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6104 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006105
Gilles Peskine449bd832023-01-11 14:50:10 +01006106 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6107 input_data->len),
6108 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006109
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006111
Andrzej Kurekad837522021-12-15 15:28:49 +01006112 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006113
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006115
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006117
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006119
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6121 additional_data->len),
6122 PSA_ERROR_BAD_STATE);
6123 } else {
6124 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6125 additional_data->len));
6126
6127 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6128 input_data->len),
6129 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006130 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006131 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006132
6133 /* ------------------------------------------------------- */
6134
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006136
Gilles Peskine449bd832023-01-11 14:50:10 +01006137 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006138
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 if (operation.alg == PSA_ALG_CCM) {
6140 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6141 input_data->len, output_data,
6142 output_size, &output_length),
6143 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006144
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 } else {
6146 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6147 input_data->len, output_data,
6148 output_size, &output_length));
6149
6150 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6151 input_data->len),
6152 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006153 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006155
6156 /* ------------------------------------------------------- */
6157
Gilles Peskine449bd832023-01-11 14:50:10 +01006158 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006159
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006161
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 if (operation.alg == PSA_ALG_CCM) {
6163 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6164 finish_output_size,
6165 &output_part_length,
6166 tag_buffer, tag_length,
6167 &tag_size));
6168 } else {
6169 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6170 finish_output_size,
6171 &output_part_length,
6172 tag_buffer, tag_length,
6173 &tag_size));
6174
6175 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6176 input_data->len),
6177 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006178 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006179 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006180
6181 /* Test for setting lengths after generating nonce + already starting data. */
6182
Gilles Peskine449bd832023-01-11 14:50:10 +01006183 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006184
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6186 PSA_AEAD_NONCE_MAX_SIZE,
6187 &nonce_length));
6188 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006189
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6191 additional_data->len),
6192 PSA_ERROR_BAD_STATE);
6193 } else {
6194 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6195 additional_data->len));
6196
6197 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6198 input_data->len),
6199 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006200 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006202
6203 /* ------------------------------------------------------- */
6204
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006206
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6208 PSA_AEAD_NONCE_MAX_SIZE,
6209 &nonce_length));
6210 if (operation.alg == PSA_ALG_CCM) {
6211 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6212 input_data->len, output_data,
6213 output_size, &output_length),
6214 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006215
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 } else {
6217 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6218 input_data->len, output_data,
6219 output_size, &output_length));
6220
6221 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6222 input_data->len),
6223 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006224 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006226
6227 /* ------------------------------------------------------- */
6228
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006230
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6232 PSA_AEAD_NONCE_MAX_SIZE,
6233 &nonce_length));
6234 if (operation.alg == PSA_ALG_CCM) {
6235 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6236 finish_output_size,
6237 &output_part_length,
6238 tag_buffer, tag_length,
6239 &tag_size));
6240 } else {
6241 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6242 finish_output_size,
6243 &output_part_length,
6244 tag_buffer, tag_length,
6245 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006246
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6248 input_data->len),
6249 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006250 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006251 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006252
Paul Elliott243080c2021-07-21 19:01:17 +01006253 /* Test for not sending any additional data or data after setting non zero
6254 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006257
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6261 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006262
Gilles Peskine449bd832023-01-11 14:50:10 +01006263 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6264 finish_output_size,
6265 &output_part_length,
6266 tag_buffer, tag_length,
6267 &tag_size),
6268 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006269
Gilles Peskine449bd832023-01-11 14:50:10 +01006270 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006271
Paul Elliott243080c2021-07-21 19:01:17 +01006272 /* Test for not sending any additional data or data after setting non-zero
6273 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006274
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006276
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006278
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6280 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006281
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6283 finish_output_size,
6284 &output_part_length,
6285 tag_buffer,
6286 tag_length),
6287 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006288
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006290
Paul Elliott243080c2021-07-21 19:01:17 +01006291 /* Test for not sending any additional data after setting a non-zero length
6292 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006293
Gilles Peskine449bd832023-01-11 14:50:10 +01006294 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006295
Gilles Peskine449bd832023-01-11 14:50:10 +01006296 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006297
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6299 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006300
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6302 input_data->len, output_data,
6303 output_size, &output_length),
6304 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006305
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006307
Paul Elliottf94bd992021-09-19 18:15:59 +01006308 /* Test for not sending any data after setting a non-zero length for it.*/
6309
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006311
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006313
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6315 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006316
Gilles Peskine449bd832023-01-11 14:50:10 +01006317 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6318 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006319
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6321 finish_output_size,
6322 &output_part_length,
6323 tag_buffer, tag_length,
6324 &tag_size),
6325 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006326
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006328
Paul Elliottb0450fe2021-09-01 15:06:26 +01006329 /* Test for sending too much additional data after setting lengths. */
6330
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006332
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006334
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006336
6337
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6339 additional_data->len),
6340 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006341
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006343
Paul Elliotta2a09b02021-09-22 14:56:40 +01006344 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006345
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006347
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006349
Gilles Peskine449bd832023-01-11 14:50:10 +01006350 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6351 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006352
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6354 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006355
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6357 1),
6358 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006359
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006361
Paul Elliottb0450fe2021-09-01 15:06:26 +01006362 /* Test for sending too much data after setting lengths. */
6363
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006365
Gilles Peskine449bd832023-01-11 14:50:10 +01006366 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006367
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006369
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6371 input_data->len, output_data,
6372 output_size, &output_length),
6373 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006374
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006376
Paul Elliotta2a09b02021-09-22 14:56:40 +01006377 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006378
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006380
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006382
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6384 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006385
Gilles Peskine449bd832023-01-11 14:50:10 +01006386 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6387 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006388
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6390 input_data->len, output_data,
6391 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006392
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6394 1, output_data,
6395 output_size, &output_length),
6396 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006397
Gilles Peskine449bd832023-01-11 14:50:10 +01006398 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006399
Paul Elliottc23a9a02021-06-21 18:32:46 +01006400 /* Test sending additional data after data. */
6401
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006403
Gilles Peskine449bd832023-01-11 14:50:10 +01006404 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006405
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 if (operation.alg != PSA_ALG_CCM) {
6407 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6408 input_data->len, output_data,
6409 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006410
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6412 additional_data->len),
6413 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006414 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006416
Paul Elliott534d0b42021-06-22 19:15:20 +01006417 /* Test calling finish on decryption. */
6418
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006420
Gilles Peskine449bd832023-01-11 14:50:10 +01006421 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006422
Gilles Peskine449bd832023-01-11 14:50:10 +01006423 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6424 finish_output_size,
6425 &output_part_length,
6426 tag_buffer, tag_length,
6427 &tag_size),
6428 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006429
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006431
6432 /* Test calling verify on encryption. */
6433
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006435
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006437
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6439 finish_output_size,
6440 &output_part_length,
6441 tag_buffer,
6442 tag_length),
6443 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006444
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006446
6447
Paul Elliottc23a9a02021-06-21 18:32:46 +01006448exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 psa_destroy_key(key);
6450 psa_aead_abort(&operation);
6451 mbedtls_free(output_data);
6452 mbedtls_free(final_data);
6453 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006454}
6455/* END_CASE */
6456
6457/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006458void signature_size(int type_arg,
6459 int bits,
6460 int alg_arg,
6461 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006462{
6463 psa_key_type_t type = type_arg;
6464 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006466
Gilles Peskine449bd832023-01-11 14:50:10 +01006467 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006468
Gilles Peskinee59236f2018-01-27 23:32:46 +01006469exit:
6470 ;
6471}
6472/* END_CASE */
6473
6474/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006475void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6476 int alg_arg, data_t *input_data,
6477 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006478{
Ronald Cron5425a212020-08-04 14:58:35 +02006479 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006480 psa_key_type_t key_type = key_type_arg;
6481 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006482 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006483 unsigned char *signature = NULL;
6484 size_t signature_size;
6485 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006486 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006487
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006489
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6491 psa_set_key_algorithm(&attributes, alg);
6492 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006493
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6495 &key));
6496 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6497 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006498
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006499 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006500 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006501 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6502 key_bits, alg);
6503 TEST_ASSERT(signature_size != 0);
6504 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006505 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006506
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006507 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 PSA_ASSERT(psa_sign_hash(key, alg,
6509 input_data->x, input_data->len,
6510 signature, signature_size,
6511 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006512 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006513 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006514 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006515
6516exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006517 /*
6518 * Key attributes may have been returned by psa_get_key_attributes()
6519 * thus reset them as required.
6520 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006522
Gilles Peskine449bd832023-01-11 14:50:10 +01006523 psa_destroy_key(key);
6524 mbedtls_free(signature);
6525 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006526}
6527/* END_CASE */
6528
Paul Elliott712d5122022-12-07 14:03:10 +00006529/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006530/**
6531 * sign_hash_interruptible() test intentions:
6532 *
6533 * Note: This test can currently only handle ECDSA.
6534 *
6535 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006536 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006537 *
6538 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6539 * expected for different max_ops values.
6540 *
6541 * 3. Test that the number of ops done prior to start and after abort is zero
6542 * and that each successful stage completes some ops (this is not mandated by
6543 * the PSA specification, but is currently the case).
6544 *
6545 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6546 * complete() calls does not alter the number of ops returned.
6547 */
Paul Elliott712d5122022-12-07 14:03:10 +00006548void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6549 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006550 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006551{
6552 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6553 psa_key_type_t key_type = key_type_arg;
6554 psa_algorithm_t alg = alg_arg;
6555 size_t key_bits;
6556 unsigned char *signature = NULL;
6557 size_t signature_size;
6558 size_t signature_length = 0xdeadbeef;
6559 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6560 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006561 uint32_t num_ops = 0;
6562 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006563 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006564 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006565 size_t min_completes = 0;
6566 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006567
Paul Elliott712d5122022-12-07 14:03:10 +00006568 psa_sign_hash_interruptible_operation_t operation =
6569 psa_sign_hash_interruptible_operation_init();
6570
6571 PSA_ASSERT(psa_crypto_init());
6572
6573 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6574 psa_set_key_algorithm(&attributes, alg);
6575 psa_set_key_type(&attributes, key_type);
6576
6577 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6578 &key));
6579 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6580 key_bits = psa_get_key_bits(&attributes);
6581
6582 /* Allocate a buffer which has the size advertised by the
6583 * library. */
6584 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6585 key_bits, alg);
6586 TEST_ASSERT(signature_size != 0);
6587 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006588 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006589
Paul Elliott0c683352022-12-16 19:16:56 +00006590 psa_interruptible_set_max_ops(max_ops);
6591
Paul Elliott6f600372023-02-06 18:41:05 +00006592 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6593 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006594
Paul Elliott712d5122022-12-07 14:03:10 +00006595 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6596 TEST_ASSERT(num_ops_prior == 0);
6597
6598 /* Start performing the signature. */
6599 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6600 input_data->x, input_data->len));
6601
6602 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6603 TEST_ASSERT(num_ops_prior == 0);
6604
6605 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006606 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006607 status = psa_sign_hash_complete(&operation, signature, signature_size,
6608 &signature_length);
6609
Paul Elliott0c683352022-12-16 19:16:56 +00006610 num_completes++;
6611
Paul Elliott712d5122022-12-07 14:03:10 +00006612 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6613 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006614 /* We are asserting here that every complete makes progress
6615 * (completes some ops), which is true of the internal
6616 * implementation and probably any implementation, however this is
6617 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006618 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006619
Paul Elliott712d5122022-12-07 14:03:10 +00006620 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006621
6622 /* Ensure calling get_num_ops() twice still returns the same
6623 * number of ops as previously reported. */
6624 num_ops = psa_sign_hash_get_num_ops(&operation);
6625
6626 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006627 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006628 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006629
6630 TEST_ASSERT(status == PSA_SUCCESS);
6631
Paul Elliott0c683352022-12-16 19:16:56 +00006632 TEST_LE_U(min_completes, num_completes);
6633 TEST_LE_U(num_completes, max_completes);
6634
Paul Elliott712d5122022-12-07 14:03:10 +00006635 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006636 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006637 signature, signature_length);
Paul Elliott712d5122022-12-07 14:03:10 +00006638
6639 PSA_ASSERT(psa_sign_hash_abort(&operation));
6640
Paul Elliott59ad9452022-12-18 15:09:02 +00006641 num_ops = psa_sign_hash_get_num_ops(&operation);
6642 TEST_ASSERT(num_ops == 0);
6643
Paul Elliott712d5122022-12-07 14:03:10 +00006644exit:
6645
6646 /*
6647 * Key attributes may have been returned by psa_get_key_attributes()
6648 * thus reset them as required.
6649 */
6650 psa_reset_key_attributes(&attributes);
6651
6652 psa_destroy_key(key);
6653 mbedtls_free(signature);
6654 PSA_DONE();
6655}
6656/* END_CASE */
6657
Gilles Peskine20035e32018-02-03 22:44:14 +01006658/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006659void sign_hash_fail(int key_type_arg, data_t *key_data,
6660 int alg_arg, data_t *input_data,
6661 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006662{
Ronald Cron5425a212020-08-04 14:58:35 +02006663 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006664 psa_key_type_t key_type = key_type_arg;
6665 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006666 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006667 psa_status_t actual_status;
6668 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006669 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006670 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006671 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006672
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006673 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006674
Gilles Peskine449bd832023-01-11 14:50:10 +01006675 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006676
Gilles Peskine449bd832023-01-11 14:50:10 +01006677 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6678 psa_set_key_algorithm(&attributes, alg);
6679 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006680
Gilles Peskine449bd832023-01-11 14:50:10 +01006681 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6682 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006683
Gilles Peskine449bd832023-01-11 14:50:10 +01006684 actual_status = psa_sign_hash(key, alg,
6685 input_data->x, input_data->len,
6686 signature, signature_size,
6687 &signature_length);
6688 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006689 /* The value of *signature_length is unspecified on error, but
6690 * whatever it is, it should be less than signature_size, so that
6691 * if the caller tries to read *signature_length bytes without
6692 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006693 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006694
6695exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006696 psa_reset_key_attributes(&attributes);
6697 psa_destroy_key(key);
6698 mbedtls_free(signature);
6699 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006700}
6701/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006702
Paul Elliott91007972022-12-16 12:21:24 +00006703/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006704/**
6705 * sign_hash_fail_interruptible() test intentions:
6706 *
6707 * Note: This test can currently only handle ECDSA.
6708 *
6709 * 1. Test that various failure cases for interruptible sign hash fail with the
6710 * correct error codes, and at the correct point (at start or during
6711 * complete).
6712 *
6713 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6714 * expected for different max_ops values.
6715 *
6716 * 3. Test that the number of ops done prior to start and after abort is zero
6717 * and that each successful stage completes some ops (this is not mandated by
6718 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006719 *
6720 * 4. Check that calling complete() when start() fails and complete()
6721 * after completion results in a BAD_STATE error.
6722 *
6723 * 5. Check that calling start() again after start fails results in a BAD_STATE
6724 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006725 */
Paul Elliott91007972022-12-16 12:21:24 +00006726void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6727 int alg_arg, data_t *input_data,
6728 int signature_size_arg,
6729 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006730 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006731 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006732{
6733 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6734 psa_key_type_t key_type = key_type_arg;
6735 psa_algorithm_t alg = alg_arg;
6736 size_t signature_size = signature_size_arg;
6737 psa_status_t actual_status;
6738 psa_status_t expected_start_status = expected_start_status_arg;
6739 psa_status_t expected_complete_status = expected_complete_status_arg;
6740 unsigned char *signature = NULL;
6741 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006742 uint32_t num_ops = 0;
6743 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006744 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006745 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006746 size_t min_completes = 0;
6747 size_t max_completes = 0;
6748
Paul Elliott91007972022-12-16 12:21:24 +00006749 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6750 psa_sign_hash_interruptible_operation_t operation =
6751 psa_sign_hash_interruptible_operation_init();
6752
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006753 TEST_CALLOC(signature, signature_size);
Paul Elliott91007972022-12-16 12:21:24 +00006754
6755 PSA_ASSERT(psa_crypto_init());
6756
6757 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6758 psa_set_key_algorithm(&attributes, alg);
6759 psa_set_key_type(&attributes, key_type);
6760
6761 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6762 &key));
6763
Paul Elliott0c683352022-12-16 19:16:56 +00006764 psa_interruptible_set_max_ops(max_ops);
6765
Paul Elliott6f600372023-02-06 18:41:05 +00006766 interruptible_signverify_get_minmax_completes(max_ops,
6767 expected_complete_status,
6768 &min_completes,
6769 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006770
Paul Elliott91007972022-12-16 12:21:24 +00006771 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6772 TEST_ASSERT(num_ops_prior == 0);
6773
6774 /* Start performing the signature. */
6775 actual_status = psa_sign_hash_start(&operation, key, alg,
6776 input_data->x, input_data->len);
6777
6778 TEST_EQUAL(actual_status, expected_start_status);
6779
Paul Elliottc9774412023-02-06 15:14:07 +00006780 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006781 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006782 * start failed. */
6783 actual_status = psa_sign_hash_complete(&operation, signature,
6784 signature_size,
6785 &signature_length);
6786
6787 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6788
6789 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006790 actual_status = psa_sign_hash_start(&operation, key, alg,
6791 input_data->x, input_data->len);
6792
6793 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6794 }
6795
Paul Elliott91007972022-12-16 12:21:24 +00006796 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6797 TEST_ASSERT(num_ops_prior == 0);
6798
Paul Elliott91007972022-12-16 12:21:24 +00006799 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006800 do {
Paul Elliott91007972022-12-16 12:21:24 +00006801 actual_status = psa_sign_hash_complete(&operation, signature,
6802 signature_size,
6803 &signature_length);
6804
Paul Elliott0c683352022-12-16 19:16:56 +00006805 num_completes++;
6806
Paul Elliott334d7262023-01-20 17:29:41 +00006807 if (actual_status == PSA_SUCCESS ||
6808 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006809 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006810 /* We are asserting here that every complete makes progress
6811 * (completes some ops), which is true of the internal
6812 * implementation and probably any implementation, however this is
6813 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006814 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006815
Paul Elliott91007972022-12-16 12:21:24 +00006816 num_ops_prior = num_ops;
6817 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006818 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006819
Paul Elliottc9774412023-02-06 15:14:07 +00006820 TEST_EQUAL(actual_status, expected_complete_status);
6821
Paul Elliottefebad02023-02-15 16:56:45 +00006822 /* Check that another complete returns BAD_STATE. */
6823 actual_status = psa_sign_hash_complete(&operation, signature,
6824 signature_size,
6825 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006826
Paul Elliottefebad02023-02-15 16:56:45 +00006827 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006828
Paul Elliott91007972022-12-16 12:21:24 +00006829 PSA_ASSERT(psa_sign_hash_abort(&operation));
6830
Paul Elliott59ad9452022-12-18 15:09:02 +00006831 num_ops = psa_sign_hash_get_num_ops(&operation);
6832 TEST_ASSERT(num_ops == 0);
6833
Paul Elliott91007972022-12-16 12:21:24 +00006834 /* The value of *signature_length is unspecified on error, but
6835 * whatever it is, it should be less than signature_size, so that
6836 * if the caller tries to read *signature_length bytes without
6837 * checking the error code then they don't overflow a buffer. */
6838 TEST_LE_U(signature_length, signature_size);
6839
Paul Elliott0c683352022-12-16 19:16:56 +00006840 TEST_LE_U(min_completes, num_completes);
6841 TEST_LE_U(num_completes, max_completes);
6842
Paul Elliott91007972022-12-16 12:21:24 +00006843exit:
6844 psa_reset_key_attributes(&attributes);
6845 psa_destroy_key(key);
6846 mbedtls_free(signature);
6847 PSA_DONE();
6848}
6849/* END_CASE */
6850
mohammad16038cc1cee2018-03-28 01:21:33 +03006851/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006852void sign_verify_hash(int key_type_arg, data_t *key_data,
6853 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006854{
Ronald Cron5425a212020-08-04 14:58:35 +02006855 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006856 psa_key_type_t key_type = key_type_arg;
6857 psa_algorithm_t alg = alg_arg;
6858 size_t key_bits;
6859 unsigned char *signature = NULL;
6860 size_t signature_size;
6861 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006862 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006863
Gilles Peskine449bd832023-01-11 14:50:10 +01006864 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006865
Gilles Peskine449bd832023-01-11 14:50:10 +01006866 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6867 psa_set_key_algorithm(&attributes, alg);
6868 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006869
Gilles Peskine449bd832023-01-11 14:50:10 +01006870 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6871 &key));
6872 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6873 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006874
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006875 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006876 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006877 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6878 key_bits, alg);
6879 TEST_ASSERT(signature_size != 0);
6880 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006881 TEST_CALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006882
6883 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006884 PSA_ASSERT(psa_sign_hash(key, alg,
6885 input_data->x, input_data->len,
6886 signature, signature_size,
6887 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006888 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006889 TEST_LE_U(signature_length, signature_size);
6890 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006891
6892 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006893 PSA_ASSERT(psa_verify_hash(key, alg,
6894 input_data->x, input_data->len,
6895 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006896
Gilles Peskine449bd832023-01-11 14:50:10 +01006897 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006898 /* Flip a bit in the input and verify that the signature is now
6899 * detected as invalid. Flip a bit at the beginning, not at the end,
6900 * because ECDSA may ignore the last few bits of the input. */
6901 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 TEST_EQUAL(psa_verify_hash(key, alg,
6903 input_data->x, input_data->len,
6904 signature, signature_length),
6905 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006906 }
6907
6908exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006909 /*
6910 * Key attributes may have been returned by psa_get_key_attributes()
6911 * thus reset them as required.
6912 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006913 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006914
Gilles Peskine449bd832023-01-11 14:50:10 +01006915 psa_destroy_key(key);
6916 mbedtls_free(signature);
6917 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006918}
6919/* END_CASE */
6920
Paul Elliott712d5122022-12-07 14:03:10 +00006921/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006922/**
6923 * sign_verify_hash_interruptible() test intentions:
6924 *
6925 * Note: This test can currently only handle ECDSA.
6926 *
Paul Elliott8c092052023-03-06 17:49:14 +00006927 * 1. Test that we can sign an input hash with the given keypair and then
6928 * afterwards verify that signature. This is currently the only way to test
6929 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00006930 *
6931 * 2. Test that after corrupting the hash, the verification detects an invalid
6932 * signature.
6933 *
6934 * 3. Test the number of calls to psa_sign_hash_complete() required are as
6935 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00006936 *
6937 * 4. Test that the number of ops done prior to starting signing and after abort
6938 * is zero and that each successful signing stage completes some ops (this is
6939 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00006940 */
Paul Elliott712d5122022-12-07 14:03:10 +00006941void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006942 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006943 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006944{
6945 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6946 psa_key_type_t key_type = key_type_arg;
6947 psa_algorithm_t alg = alg_arg;
6948 size_t key_bits;
6949 unsigned char *signature = NULL;
6950 size_t signature_size;
6951 size_t signature_length = 0xdeadbeef;
6952 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6953 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006954 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00006955 uint32_t num_ops = 0;
6956 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006957 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006958 size_t min_completes = 0;
6959 size_t max_completes = 0;
6960
Paul Elliott712d5122022-12-07 14:03:10 +00006961 psa_sign_hash_interruptible_operation_t sign_operation =
6962 psa_sign_hash_interruptible_operation_init();
6963 psa_verify_hash_interruptible_operation_t verify_operation =
6964 psa_verify_hash_interruptible_operation_init();
6965
6966 PSA_ASSERT(psa_crypto_init());
6967
Paul Elliott0c683352022-12-16 19:16:56 +00006968 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6969 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006970 psa_set_key_algorithm(&attributes, alg);
6971 psa_set_key_type(&attributes, key_type);
6972
6973 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6974 &key));
6975 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6976 key_bits = psa_get_key_bits(&attributes);
6977
6978 /* Allocate a buffer which has the size advertised by the
6979 * library. */
6980 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6981 key_bits, alg);
6982 TEST_ASSERT(signature_size != 0);
6983 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006984 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006985
Paul Elliott0c683352022-12-16 19:16:56 +00006986 psa_interruptible_set_max_ops(max_ops);
6987
Paul Elliott6f600372023-02-06 18:41:05 +00006988 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6989 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006990
Paul Elliott7c173082023-02-26 18:44:45 +00006991 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6992 TEST_ASSERT(num_ops_prior == 0);
6993
Paul Elliott712d5122022-12-07 14:03:10 +00006994 /* Start performing the signature. */
6995 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6996 input_data->x, input_data->len));
6997
Paul Elliott7c173082023-02-26 18:44:45 +00006998 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6999 TEST_ASSERT(num_ops_prior == 0);
7000
Paul Elliott712d5122022-12-07 14:03:10 +00007001 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007002 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007003
Paul Elliott0c683352022-12-16 19:16:56 +00007004 status = psa_sign_hash_complete(&sign_operation, signature,
7005 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00007006 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00007007
7008 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00007009
7010 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7011 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7012 /* We are asserting here that every complete makes progress
7013 * (completes some ops), which is true of the internal
7014 * implementation and probably any implementation, however this is
7015 * not mandated by the PSA specification. */
7016 TEST_ASSERT(num_ops > num_ops_prior);
7017
7018 num_ops_prior = num_ops;
7019 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007020 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007021
7022 TEST_ASSERT(status == PSA_SUCCESS);
7023
Paul Elliott0c683352022-12-16 19:16:56 +00007024 TEST_LE_U(min_completes, num_completes);
7025 TEST_LE_U(num_completes, max_completes);
7026
Paul Elliott712d5122022-12-07 14:03:10 +00007027 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7028
Paul Elliott7c173082023-02-26 18:44:45 +00007029 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7030 TEST_ASSERT(num_ops == 0);
7031
Paul Elliott712d5122022-12-07 14:03:10 +00007032 /* Check that the signature length looks sensible. */
7033 TEST_LE_U(signature_length, signature_size);
7034 TEST_ASSERT(signature_length > 0);
7035
Paul Elliott0c683352022-12-16 19:16:56 +00007036 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007037
7038 /* Start verification. */
7039 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7040 input_data->x, input_data->len,
7041 signature, signature_length));
7042
7043 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007044 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007045 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007046
7047 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007048 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007049
7050 TEST_ASSERT(status == PSA_SUCCESS);
7051
Paul Elliott0c683352022-12-16 19:16:56 +00007052 TEST_LE_U(min_completes, num_completes);
7053 TEST_LE_U(num_completes, max_completes);
7054
Paul Elliott712d5122022-12-07 14:03:10 +00007055 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7056
7057 verify_operation = psa_verify_hash_interruptible_operation_init();
7058
7059 if (input_data->len != 0) {
7060 /* Flip a bit in the input and verify that the signature is now
7061 * detected as invalid. Flip a bit at the beginning, not at the end,
7062 * because ECDSA may ignore the last few bits of the input. */
7063 input_data->x[0] ^= 1;
7064
Paul Elliott712d5122022-12-07 14:03:10 +00007065 /* Start verification. */
7066 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7067 input_data->x, input_data->len,
7068 signature, signature_length));
7069
7070 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007071 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007072 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007073 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007074
7075 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7076 }
7077
7078 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7079
7080exit:
7081 /*
7082 * Key attributes may have been returned by psa_get_key_attributes()
7083 * thus reset them as required.
7084 */
7085 psa_reset_key_attributes(&attributes);
7086
7087 psa_destroy_key(key);
7088 mbedtls_free(signature);
7089 PSA_DONE();
7090}
7091/* END_CASE */
7092
Gilles Peskine9911b022018-06-29 17:30:48 +02007093/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007094void verify_hash(int key_type_arg, data_t *key_data,
7095 int alg_arg, data_t *hash_data,
7096 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007097{
Ronald Cron5425a212020-08-04 14:58:35 +02007098 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007099 psa_key_type_t key_type = key_type_arg;
7100 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007101 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007102
Gilles Peskine449bd832023-01-11 14:50:10 +01007103 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007104
Gilles Peskine449bd832023-01-11 14:50:10 +01007105 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007106
Gilles Peskine449bd832023-01-11 14:50:10 +01007107 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7108 psa_set_key_algorithm(&attributes, alg);
7109 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007110
Gilles Peskine449bd832023-01-11 14:50:10 +01007111 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7112 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007113
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 PSA_ASSERT(psa_verify_hash(key, alg,
7115 hash_data->x, hash_data->len,
7116 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007117
itayzafrir5c753392018-05-08 11:18:38 +03007118exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007119 psa_reset_key_attributes(&attributes);
7120 psa_destroy_key(key);
7121 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007122}
7123/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007124
Paul Elliott712d5122022-12-07 14:03:10 +00007125/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007126/**
7127 * verify_hash_interruptible() test intentions:
7128 *
7129 * Note: This test can currently only handle ECDSA.
7130 *
7131 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007132 * only). Given this test only does verification it can accept public keys as
7133 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007134 *
7135 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7136 * expected for different max_ops values.
7137 *
7138 * 3. Test that the number of ops done prior to start and after abort is zero
7139 * and that each successful stage completes some ops (this is not mandated by
7140 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007141 *
7142 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7143 * complete() calls does not alter the number of ops returned.
7144 *
7145 * 5. Test that after corrupting the hash, the verification detects an invalid
7146 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007147 */
Paul Elliott712d5122022-12-07 14:03:10 +00007148void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7149 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007150 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007151{
7152 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7153 psa_key_type_t key_type = key_type_arg;
7154 psa_algorithm_t alg = alg_arg;
7155 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7156 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007157 uint32_t num_ops = 0;
7158 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007159 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007160 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007161 size_t min_completes = 0;
7162 size_t max_completes = 0;
7163
Paul Elliott712d5122022-12-07 14:03:10 +00007164 psa_verify_hash_interruptible_operation_t operation =
7165 psa_verify_hash_interruptible_operation_init();
7166
7167 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7168
7169 PSA_ASSERT(psa_crypto_init());
7170
7171 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7172 psa_set_key_algorithm(&attributes, alg);
7173 psa_set_key_type(&attributes, key_type);
7174
7175 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7176 &key));
7177
Paul Elliott0c683352022-12-16 19:16:56 +00007178 psa_interruptible_set_max_ops(max_ops);
7179
Paul Elliott6f600372023-02-06 18:41:05 +00007180 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7181 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007182
Paul Elliott712d5122022-12-07 14:03:10 +00007183 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7184
7185 TEST_ASSERT(num_ops_prior == 0);
7186
7187 /* Start verification. */
7188 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7189 hash_data->x, hash_data->len,
7190 signature_data->x, signature_data->len)
7191 );
7192
7193 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7194
7195 TEST_ASSERT(num_ops_prior == 0);
7196
7197 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007198 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007199 status = psa_verify_hash_complete(&operation);
7200
Paul Elliott0c683352022-12-16 19:16:56 +00007201 num_completes++;
7202
Paul Elliott712d5122022-12-07 14:03:10 +00007203 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7204 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007205 /* We are asserting here that every complete makes progress
7206 * (completes some ops), which is true of the internal
7207 * implementation and probably any implementation, however this is
7208 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007209 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007210
Paul Elliott712d5122022-12-07 14:03:10 +00007211 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007212
7213 /* Ensure calling get_num_ops() twice still returns the same
7214 * number of ops as previously reported. */
7215 num_ops = psa_verify_hash_get_num_ops(&operation);
7216
7217 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007218 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007219 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007220
7221 TEST_ASSERT(status == PSA_SUCCESS);
7222
Paul Elliott0c683352022-12-16 19:16:56 +00007223 TEST_LE_U(min_completes, num_completes);
7224 TEST_LE_U(num_completes, max_completes);
7225
Paul Elliott712d5122022-12-07 14:03:10 +00007226 PSA_ASSERT(psa_verify_hash_abort(&operation));
7227
Paul Elliott59ad9452022-12-18 15:09:02 +00007228 num_ops = psa_verify_hash_get_num_ops(&operation);
7229 TEST_ASSERT(num_ops == 0);
7230
Paul Elliott8359c142023-02-24 18:40:10 +00007231 if (hash_data->len != 0) {
7232 /* Flip a bit in the hash and verify that the signature is now detected
7233 * as invalid. Flip a bit at the beginning, not at the end, because
7234 * ECDSA may ignore the last few bits of the input. */
7235 hash_data->x[0] ^= 1;
7236
7237 /* Start verification. */
7238 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7239 hash_data->x, hash_data->len,
7240 signature_data->x, signature_data->len));
7241
7242 /* Continue performing the signature until complete. */
7243 do {
7244 status = psa_verify_hash_complete(&operation);
7245 } while (status == PSA_OPERATION_INCOMPLETE);
7246
7247 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7248 }
7249
Paul Elliott712d5122022-12-07 14:03:10 +00007250exit:
7251 psa_reset_key_attributes(&attributes);
7252 psa_destroy_key(key);
7253 PSA_DONE();
7254}
7255/* END_CASE */
7256
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007257/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007258void verify_hash_fail(int key_type_arg, data_t *key_data,
7259 int alg_arg, data_t *hash_data,
7260 data_t *signature_data,
7261 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007262{
Ronald Cron5425a212020-08-04 14:58:35 +02007263 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007264 psa_key_type_t key_type = key_type_arg;
7265 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007266 psa_status_t actual_status;
7267 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007268 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007269
Gilles Peskine449bd832023-01-11 14:50:10 +01007270 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007271
Gilles Peskine449bd832023-01-11 14:50:10 +01007272 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7273 psa_set_key_algorithm(&attributes, alg);
7274 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007275
Gilles Peskine449bd832023-01-11 14:50:10 +01007276 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7277 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007278
Gilles Peskine449bd832023-01-11 14:50:10 +01007279 actual_status = psa_verify_hash(key, alg,
7280 hash_data->x, hash_data->len,
7281 signature_data->x, signature_data->len);
7282 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007283
7284exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007285 psa_reset_key_attributes(&attributes);
7286 psa_destroy_key(key);
7287 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007288}
7289/* END_CASE */
7290
Paul Elliott91007972022-12-16 12:21:24 +00007291/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007292/**
7293 * verify_hash_fail_interruptible() test intentions:
7294 *
7295 * Note: This test can currently only handle ECDSA.
7296 *
7297 * 1. Test that various failure cases for interruptible verify hash fail with
7298 * the correct error codes, and at the correct point (at start or during
7299 * complete).
7300 *
7301 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7302 * expected for different max_ops values.
7303 *
7304 * 3. Test that the number of ops done prior to start and after abort is zero
7305 * and that each successful stage completes some ops (this is not mandated by
7306 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007307 *
7308 * 4. Check that calling complete() when start() fails and complete()
7309 * after completion results in a BAD_STATE error.
7310 *
7311 * 5. Check that calling start() again after start fails results in a BAD_STATE
7312 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007313 */
Paul Elliott91007972022-12-16 12:21:24 +00007314void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7315 int alg_arg, data_t *hash_data,
7316 data_t *signature_data,
7317 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007318 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007319 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007320{
7321 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7322 psa_key_type_t key_type = key_type_arg;
7323 psa_algorithm_t alg = alg_arg;
7324 psa_status_t actual_status;
7325 psa_status_t expected_start_status = expected_start_status_arg;
7326 psa_status_t expected_complete_status = expected_complete_status_arg;
7327 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007328 uint32_t num_ops = 0;
7329 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007330 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007331 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007332 size_t min_completes = 0;
7333 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007334 psa_verify_hash_interruptible_operation_t operation =
7335 psa_verify_hash_interruptible_operation_init();
7336
7337 PSA_ASSERT(psa_crypto_init());
7338
7339 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7340 psa_set_key_algorithm(&attributes, alg);
7341 psa_set_key_type(&attributes, key_type);
7342
7343 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7344 &key));
7345
Paul Elliott0c683352022-12-16 19:16:56 +00007346 psa_interruptible_set_max_ops(max_ops);
7347
Paul Elliott6f600372023-02-06 18:41:05 +00007348 interruptible_signverify_get_minmax_completes(max_ops,
7349 expected_complete_status,
7350 &min_completes,
7351 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007352
Paul Elliott91007972022-12-16 12:21:24 +00007353 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7354 TEST_ASSERT(num_ops_prior == 0);
7355
7356 /* Start verification. */
7357 actual_status = psa_verify_hash_start(&operation, key, alg,
7358 hash_data->x, hash_data->len,
7359 signature_data->x,
7360 signature_data->len);
7361
7362 TEST_EQUAL(actual_status, expected_start_status);
7363
Paul Elliottc9774412023-02-06 15:14:07 +00007364 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007365 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007366 * start failed. */
7367 actual_status = psa_verify_hash_complete(&operation);
7368
7369 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7370
7371 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007372 actual_status = psa_verify_hash_start(&operation, key, alg,
7373 hash_data->x, hash_data->len,
7374 signature_data->x,
7375 signature_data->len);
7376
7377 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7378 }
7379
Paul Elliott91007972022-12-16 12:21:24 +00007380 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7381 TEST_ASSERT(num_ops_prior == 0);
7382
Paul Elliott91007972022-12-16 12:21:24 +00007383 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007384 do {
Paul Elliott91007972022-12-16 12:21:24 +00007385 actual_status = psa_verify_hash_complete(&operation);
7386
Paul Elliott0c683352022-12-16 19:16:56 +00007387 num_completes++;
7388
Paul Elliott334d7262023-01-20 17:29:41 +00007389 if (actual_status == PSA_SUCCESS ||
7390 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007391 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007392 /* We are asserting here that every complete makes progress
7393 * (completes some ops), which is true of the internal
7394 * implementation and probably any implementation, however this is
7395 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007396 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007397
Paul Elliott91007972022-12-16 12:21:24 +00007398 num_ops_prior = num_ops;
7399 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007400 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007401
Paul Elliottc9774412023-02-06 15:14:07 +00007402 TEST_EQUAL(actual_status, expected_complete_status);
7403
Paul Elliottefebad02023-02-15 16:56:45 +00007404 /* Check that another complete returns BAD_STATE. */
7405 actual_status = psa_verify_hash_complete(&operation);
7406 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007407
Paul Elliott0c683352022-12-16 19:16:56 +00007408 TEST_LE_U(min_completes, num_completes);
7409 TEST_LE_U(num_completes, max_completes);
7410
Paul Elliott91007972022-12-16 12:21:24 +00007411 PSA_ASSERT(psa_verify_hash_abort(&operation));
7412
Paul Elliott59ad9452022-12-18 15:09:02 +00007413 num_ops = psa_verify_hash_get_num_ops(&operation);
7414 TEST_ASSERT(num_ops == 0);
7415
Paul Elliott91007972022-12-16 12:21:24 +00007416exit:
7417 psa_reset_key_attributes(&attributes);
7418 psa_destroy_key(key);
7419 PSA_DONE();
7420}
7421/* END_CASE */
7422
Paul Elliott20a36062022-12-18 13:21:25 +00007423/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007424/**
7425 * interruptible_signverify_hash_state_test() test intentions:
7426 *
7427 * Note: This test can currently only handle ECDSA.
7428 *
7429 * 1. Test that calling the various interruptible sign and verify hash functions
7430 * in incorrect orders returns BAD_STATE errors.
7431 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007432void interruptible_signverify_hash_state_test(int key_type_arg,
7433 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007434{
7435 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7436 psa_key_type_t key_type = key_type_arg;
7437 psa_algorithm_t alg = alg_arg;
7438 size_t key_bits;
7439 unsigned char *signature = NULL;
7440 size_t signature_size;
7441 size_t signature_length = 0xdeadbeef;
7442 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7443 psa_sign_hash_interruptible_operation_t sign_operation =
7444 psa_sign_hash_interruptible_operation_init();
7445 psa_verify_hash_interruptible_operation_t verify_operation =
7446 psa_verify_hash_interruptible_operation_init();
7447
7448 PSA_ASSERT(psa_crypto_init());
7449
7450 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7451 PSA_KEY_USAGE_VERIFY_HASH);
7452 psa_set_key_algorithm(&attributes, alg);
7453 psa_set_key_type(&attributes, key_type);
7454
7455 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7456 &key));
7457 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7458 key_bits = psa_get_key_bits(&attributes);
7459
7460 /* Allocate a buffer which has the size advertised by the
7461 * library. */
7462 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7463 key_bits, alg);
7464 TEST_ASSERT(signature_size != 0);
7465 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007466 TEST_CALLOC(signature, signature_size);
Paul Elliott20a36062022-12-18 13:21:25 +00007467
7468 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7469
7470 /* --- Attempt completes prior to starts --- */
7471 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7472 signature_size,
7473 &signature_length),
7474 PSA_ERROR_BAD_STATE);
7475
7476 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7477
7478 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7479 PSA_ERROR_BAD_STATE);
7480
7481 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7482
7483 /* --- Aborts in all other places. --- */
7484 psa_sign_hash_abort(&sign_operation);
7485
7486 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7487 input_data->x, input_data->len));
7488
7489 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7490
7491 psa_interruptible_set_max_ops(1);
7492
7493 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7494 input_data->x, input_data->len));
7495
7496 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7497 signature_size,
7498 &signature_length),
7499 PSA_OPERATION_INCOMPLETE);
7500
7501 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7502
7503 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7504
7505 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7506 input_data->x, input_data->len));
7507
7508 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7509 signature_size,
7510 &signature_length));
7511
7512 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7513
7514 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7515
7516 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7517 input_data->x, input_data->len,
7518 signature, signature_length));
7519
7520 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7521
7522 psa_interruptible_set_max_ops(1);
7523
7524 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7525 input_data->x, input_data->len,
7526 signature, signature_length));
7527
7528 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7529 PSA_OPERATION_INCOMPLETE);
7530
7531 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7532
7533 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7534
7535 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7536 input_data->x, input_data->len,
7537 signature, signature_length));
7538
7539 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7540
7541 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7542
7543 /* --- Attempt double starts. --- */
7544
7545 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7546 input_data->x, input_data->len));
7547
7548 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7549 input_data->x, input_data->len),
7550 PSA_ERROR_BAD_STATE);
7551
7552 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7553
7554 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7555 input_data->x, input_data->len,
7556 signature, signature_length));
7557
7558 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7559 input_data->x, input_data->len,
7560 signature, signature_length),
7561 PSA_ERROR_BAD_STATE);
7562
7563 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7564
Paul Elliott76d671a2023-02-07 17:45:18 +00007565exit:
7566 /*
7567 * Key attributes may have been returned by psa_get_key_attributes()
7568 * thus reset them as required.
7569 */
7570 psa_reset_key_attributes(&attributes);
7571
7572 psa_destroy_key(key);
7573 mbedtls_free(signature);
7574 PSA_DONE();
7575}
7576/* END_CASE */
7577
7578/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007579/**
Paul Elliottc2033502023-02-26 17:09:14 +00007580 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007581 *
7582 * Note: This test can currently only handle ECDSA.
7583 *
7584 * 1. Test various edge cases in the interruptible sign and verify hash
7585 * interfaces.
7586 */
Paul Elliottc2033502023-02-26 17:09:14 +00007587void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007588 data_t *key_data, int alg_arg, data_t *input_data)
7589{
7590 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7591 psa_key_type_t key_type = key_type_arg;
7592 psa_algorithm_t alg = alg_arg;
7593 size_t key_bits;
7594 unsigned char *signature = NULL;
7595 size_t signature_size;
7596 size_t signature_length = 0xdeadbeef;
7597 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7598 uint8_t *input_buffer = NULL;
7599 psa_sign_hash_interruptible_operation_t sign_operation =
7600 psa_sign_hash_interruptible_operation_init();
7601 psa_verify_hash_interruptible_operation_t verify_operation =
7602 psa_verify_hash_interruptible_operation_init();
7603
7604 PSA_ASSERT(psa_crypto_init());
7605
7606 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7607 PSA_KEY_USAGE_VERIFY_HASH);
7608 psa_set_key_algorithm(&attributes, alg);
7609 psa_set_key_type(&attributes, key_type);
7610
7611 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7612 &key));
7613 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7614 key_bits = psa_get_key_bits(&attributes);
7615
7616 /* Allocate a buffer which has the size advertised by the
7617 * library. */
7618 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7619 key_bits, alg);
7620 TEST_ASSERT(signature_size != 0);
7621 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007622 TEST_CALLOC(signature, signature_size);
Paul Elliott76d671a2023-02-07 17:45:18 +00007623
Paul Elliott20a36062022-12-18 13:21:25 +00007624 /* --- Change function inputs mid run, to cause an error (sign only,
7625 * verify passes all inputs to start. --- */
7626
7627 psa_interruptible_set_max_ops(1);
7628
7629 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7630 input_data->x, input_data->len));
7631
7632 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7633 signature_size,
7634 &signature_length),
7635 PSA_OPERATION_INCOMPLETE);
7636
7637 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7638 0,
7639 &signature_length),
7640 PSA_ERROR_BUFFER_TOO_SMALL);
7641
Paul Elliottc9774412023-02-06 15:14:07 +00007642 /* And test that this invalidates the operation. */
7643 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7644 0,
7645 &signature_length),
7646 PSA_ERROR_BAD_STATE);
7647
Paul Elliott20a36062022-12-18 13:21:25 +00007648 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7649
Paul Elliottf9c91a72023-02-05 18:06:38 +00007650 /* Trash the hash buffer in between start and complete, to ensure
7651 * no reliance on external buffers. */
7652 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7653
Paul Elliott6c68df42023-10-23 15:33:37 +01007654 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007655
7656 memcpy(input_buffer, input_data->x, input_data->len);
7657
7658 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7659 input_buffer, input_data->len));
7660
7661 memset(input_buffer, '!', input_data->len);
7662 mbedtls_free(input_buffer);
7663 input_buffer = NULL;
7664
7665 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7666 signature_size,
7667 &signature_length));
7668
7669 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7670
Paul Elliott6c68df42023-10-23 15:33:37 +01007671 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007672
7673 memcpy(input_buffer, input_data->x, input_data->len);
7674
7675 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7676 input_buffer, input_data->len,
7677 signature, signature_length));
7678
7679 memset(input_buffer, '!', input_data->len);
7680 mbedtls_free(input_buffer);
7681 input_buffer = NULL;
7682
7683 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7684
7685 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7686
Paul Elliott20a36062022-12-18 13:21:25 +00007687exit:
7688 /*
7689 * Key attributes may have been returned by psa_get_key_attributes()
7690 * thus reset them as required.
7691 */
7692 psa_reset_key_attributes(&attributes);
7693
7694 psa_destroy_key(key);
7695 mbedtls_free(signature);
Paul Elliott6c68df42023-10-23 15:33:37 +01007696 mbedtls_free(input_buffer);
Paul Elliott20a36062022-12-18 13:21:25 +00007697 PSA_DONE();
7698}
7699/* END_CASE */
7700
Paul Elliotta4cb9092023-02-07 18:01:55 +00007701/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007702/**
Paul Elliott57702242023-02-26 20:36:10 +00007703 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007704 *
7705 * Note: This test can currently only handle ECDSA.
7706 *
7707 * 1. Test that setting max ops is reflected in both interruptible sign and
7708 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007709 * 2. Test that changing the value of max_ops to unlimited during an operation
7710 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007711 *
7712 * 3. Test that calling get_num_ops() between complete calls gives the same
7713 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007714 */
Paul Elliott57702242023-02-26 20:36:10 +00007715void interruptible_signverify_hash_ops_tests(int key_type_arg,
7716 data_t *key_data, int alg_arg,
7717 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007718{
7719 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7720 psa_key_type_t key_type = key_type_arg;
7721 psa_algorithm_t alg = alg_arg;
7722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007723 size_t key_bits;
7724 unsigned char *signature = NULL;
7725 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007726 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007727 uint32_t num_ops = 0;
7728 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7729
Paul Elliotta4cb9092023-02-07 18:01:55 +00007730 psa_sign_hash_interruptible_operation_t sign_operation =
7731 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007732 psa_verify_hash_interruptible_operation_t verify_operation =
7733 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007734
7735 PSA_ASSERT(psa_crypto_init());
7736
7737 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7738 PSA_KEY_USAGE_VERIFY_HASH);
7739 psa_set_key_algorithm(&attributes, alg);
7740 psa_set_key_type(&attributes, key_type);
7741
Paul Elliottf1743e22023-02-15 18:44:16 +00007742 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7743 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7744 key_bits = psa_get_key_bits(&attributes);
7745
7746 /* Allocate a buffer which has the size advertised by the
7747 * library. */
7748 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7749
7750 TEST_ASSERT(signature_size != 0);
7751 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007752 TEST_CALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007753
7754 /* Check that default max ops gets set if we don't set it. */
7755 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7756 input_data->x, input_data->len));
7757
7758 TEST_EQUAL(psa_interruptible_get_max_ops(),
7759 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7760
7761 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7762
Paul Elliottf1743e22023-02-15 18:44:16 +00007763 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7764 input_data->x, input_data->len,
7765 signature, signature_size));
7766
7767 TEST_EQUAL(psa_interruptible_get_max_ops(),
7768 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7769
7770 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7771
Paul Elliotta4cb9092023-02-07 18:01:55 +00007772 /* Check that max ops gets set properly. */
7773
7774 psa_interruptible_set_max_ops(0xbeef);
7775
Paul Elliottf1743e22023-02-15 18:44:16 +00007776 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007777
Paul Elliott9e8819f2023-02-26 19:01:35 +00007778 /* --- Ensure changing the max ops mid operation works (operation should
7779 * complete successfully after setting max ops to unlimited --- */
7780 psa_interruptible_set_max_ops(1);
7781
7782 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7783 input_data->x, input_data->len));
7784
7785 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7786 signature_size,
7787 &signature_length),
7788 PSA_OPERATION_INCOMPLETE);
7789
7790 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7791
7792 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7793 signature_size,
7794 &signature_length));
7795
7796 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7797
7798 psa_interruptible_set_max_ops(1);
7799
7800 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7801 input_data->x, input_data->len,
7802 signature, signature_length));
7803
7804 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7805 PSA_OPERATION_INCOMPLETE);
7806
7807 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7808
7809 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7810
7811 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7812
Paul Elliottc1e04002023-02-26 20:27:23 +00007813 /* --- Test that not calling get_num_ops inbetween complete calls does not
7814 * result in lost ops. ---*/
7815
7816 psa_interruptible_set_max_ops(1);
7817
7818 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7819 input_data->x, input_data->len));
7820
7821 /* Continue performing the signature until complete. */
7822 do {
7823 status = psa_sign_hash_complete(&sign_operation, signature,
7824 signature_size,
7825 &signature_length);
7826
7827 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7828
7829 } while (status == PSA_OPERATION_INCOMPLETE);
7830
7831 PSA_ASSERT(status);
7832
7833 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7834
7835 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7836 input_data->x, input_data->len));
7837
7838 /* Continue performing the signature until complete. */
7839 do {
7840 status = psa_sign_hash_complete(&sign_operation, signature,
7841 signature_size,
7842 &signature_length);
7843 } while (status == PSA_OPERATION_INCOMPLETE);
7844
7845 PSA_ASSERT(status);
7846
7847 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7848
7849 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7850
7851 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7852 input_data->x, input_data->len,
7853 signature, signature_length));
7854
7855 /* Continue performing the verification until complete. */
7856 do {
7857 status = psa_verify_hash_complete(&verify_operation);
7858
7859 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7860
7861 } while (status == PSA_OPERATION_INCOMPLETE);
7862
7863 PSA_ASSERT(status);
7864
7865 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7866
7867 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7868 input_data->x, input_data->len,
7869 signature, signature_length));
7870
7871 /* Continue performing the verification until complete. */
7872 do {
7873 status = psa_verify_hash_complete(&verify_operation);
7874
7875 } while (status == PSA_OPERATION_INCOMPLETE);
7876
7877 PSA_ASSERT(status);
7878
7879 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7880
7881 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7882
Paul Elliotta4cb9092023-02-07 18:01:55 +00007883exit:
7884 /*
7885 * Key attributes may have been returned by psa_get_key_attributes()
7886 * thus reset them as required.
7887 */
7888 psa_reset_key_attributes(&attributes);
7889
7890 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007891 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007892 PSA_DONE();
7893}
7894/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007895
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007896/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007897void sign_message_deterministic(int key_type_arg,
7898 data_t *key_data,
7899 int alg_arg,
7900 data_t *input_data,
7901 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007902{
7903 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7904 psa_key_type_t key_type = key_type_arg;
7905 psa_algorithm_t alg = alg_arg;
7906 size_t key_bits;
7907 unsigned char *signature = NULL;
7908 size_t signature_size;
7909 size_t signature_length = 0xdeadbeef;
7910 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7911
Gilles Peskine449bd832023-01-11 14:50:10 +01007912 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007913
Gilles Peskine449bd832023-01-11 14:50:10 +01007914 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7915 psa_set_key_algorithm(&attributes, alg);
7916 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007917
Gilles Peskine449bd832023-01-11 14:50:10 +01007918 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7919 &key));
7920 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7921 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007922
Gilles Peskine449bd832023-01-11 14:50:10 +01007923 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7924 TEST_ASSERT(signature_size != 0);
7925 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007926 TEST_CALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007927
Gilles Peskine449bd832023-01-11 14:50:10 +01007928 PSA_ASSERT(psa_sign_message(key, alg,
7929 input_data->x, input_data->len,
7930 signature, signature_size,
7931 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007932
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01007933 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01007934 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007935
7936exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007937 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007938
Gilles Peskine449bd832023-01-11 14:50:10 +01007939 psa_destroy_key(key);
7940 mbedtls_free(signature);
7941 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007942
7943}
7944/* END_CASE */
7945
7946/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007947void sign_message_fail(int key_type_arg,
7948 data_t *key_data,
7949 int alg_arg,
7950 data_t *input_data,
7951 int signature_size_arg,
7952 int expected_status_arg)
7953{
7954 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7955 psa_key_type_t key_type = key_type_arg;
7956 psa_algorithm_t alg = alg_arg;
7957 size_t signature_size = signature_size_arg;
7958 psa_status_t actual_status;
7959 psa_status_t expected_status = expected_status_arg;
7960 unsigned char *signature = NULL;
7961 size_t signature_length = 0xdeadbeef;
7962 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7963
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007964 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01007965
7966 PSA_ASSERT(psa_crypto_init());
7967
7968 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7969 psa_set_key_algorithm(&attributes, alg);
7970 psa_set_key_type(&attributes, key_type);
7971
7972 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7973 &key));
7974
7975 actual_status = psa_sign_message(key, alg,
7976 input_data->x, input_data->len,
7977 signature, signature_size,
7978 &signature_length);
7979 TEST_EQUAL(actual_status, expected_status);
7980 /* The value of *signature_length is unspecified on error, but
7981 * whatever it is, it should be less than signature_size, so that
7982 * if the caller tries to read *signature_length bytes without
7983 * checking the error code then they don't overflow a buffer. */
7984 TEST_LE_U(signature_length, signature_size);
7985
7986exit:
7987 psa_reset_key_attributes(&attributes);
7988 psa_destroy_key(key);
7989 mbedtls_free(signature);
7990 PSA_DONE();
7991}
7992/* END_CASE */
7993
7994/* BEGIN_CASE */
7995void sign_verify_message(int key_type_arg,
7996 data_t *key_data,
7997 int alg_arg,
7998 data_t *input_data)
7999{
8000 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8001 psa_key_type_t key_type = key_type_arg;
8002 psa_algorithm_t alg = alg_arg;
8003 size_t key_bits;
8004 unsigned char *signature = NULL;
8005 size_t signature_size;
8006 size_t signature_length = 0xdeadbeef;
8007 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8008
8009 PSA_ASSERT(psa_crypto_init());
8010
8011 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
8012 PSA_KEY_USAGE_VERIFY_MESSAGE);
8013 psa_set_key_algorithm(&attributes, alg);
8014 psa_set_key_type(&attributes, key_type);
8015
8016 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8017 &key));
8018 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8019 key_bits = psa_get_key_bits(&attributes);
8020
8021 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8022 TEST_ASSERT(signature_size != 0);
8023 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008024 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008025
8026 PSA_ASSERT(psa_sign_message(key, alg,
8027 input_data->x, input_data->len,
8028 signature, signature_size,
8029 &signature_length));
8030 TEST_LE_U(signature_length, signature_size);
8031 TEST_ASSERT(signature_length > 0);
8032
8033 PSA_ASSERT(psa_verify_message(key, alg,
8034 input_data->x, input_data->len,
8035 signature, signature_length));
8036
8037 if (input_data->len != 0) {
8038 /* Flip a bit in the input and verify that the signature is now
8039 * detected as invalid. Flip a bit at the beginning, not at the end,
8040 * because ECDSA may ignore the last few bits of the input. */
8041 input_data->x[0] ^= 1;
8042 TEST_EQUAL(psa_verify_message(key, alg,
8043 input_data->x, input_data->len,
8044 signature, signature_length),
8045 PSA_ERROR_INVALID_SIGNATURE);
8046 }
8047
8048exit:
8049 psa_reset_key_attributes(&attributes);
8050
8051 psa_destroy_key(key);
8052 mbedtls_free(signature);
8053 PSA_DONE();
8054}
8055/* END_CASE */
8056
8057/* BEGIN_CASE */
8058void verify_message(int key_type_arg,
8059 data_t *key_data,
8060 int alg_arg,
8061 data_t *input_data,
8062 data_t *signature_data)
8063{
8064 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8065 psa_key_type_t key_type = key_type_arg;
8066 psa_algorithm_t alg = alg_arg;
8067 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8068
8069 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8070
8071 PSA_ASSERT(psa_crypto_init());
8072
8073 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8074 psa_set_key_algorithm(&attributes, alg);
8075 psa_set_key_type(&attributes, key_type);
8076
8077 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8078 &key));
8079
8080 PSA_ASSERT(psa_verify_message(key, alg,
8081 input_data->x, input_data->len,
8082 signature_data->x, signature_data->len));
8083
8084exit:
8085 psa_reset_key_attributes(&attributes);
8086 psa_destroy_key(key);
8087 PSA_DONE();
8088}
8089/* END_CASE */
8090
8091/* BEGIN_CASE */
8092void verify_message_fail(int key_type_arg,
8093 data_t *key_data,
8094 int alg_arg,
8095 data_t *hash_data,
8096 data_t *signature_data,
8097 int expected_status_arg)
8098{
8099 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8100 psa_key_type_t key_type = key_type_arg;
8101 psa_algorithm_t alg = alg_arg;
8102 psa_status_t actual_status;
8103 psa_status_t expected_status = expected_status_arg;
8104 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8105
8106 PSA_ASSERT(psa_crypto_init());
8107
8108 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8109 psa_set_key_algorithm(&attributes, alg);
8110 psa_set_key_type(&attributes, key_type);
8111
8112 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8113 &key));
8114
8115 actual_status = psa_verify_message(key, alg,
8116 hash_data->x, hash_data->len,
8117 signature_data->x,
8118 signature_data->len);
8119 TEST_EQUAL(actual_status, expected_status);
8120
8121exit:
8122 psa_reset_key_attributes(&attributes);
8123 psa_destroy_key(key);
8124 PSA_DONE();
8125}
8126/* END_CASE */
8127
8128/* BEGIN_CASE */
8129void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008130 data_t *key_data,
8131 int alg_arg,
8132 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008133 data_t *label,
8134 int expected_output_length_arg,
8135 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008136{
Ronald Cron5425a212020-08-04 14:58:35 +02008137 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008138 psa_key_type_t key_type = key_type_arg;
8139 psa_algorithm_t alg = alg_arg;
8140 size_t expected_output_length = expected_output_length_arg;
8141 size_t key_bits;
8142 unsigned char *output = NULL;
8143 size_t output_size;
8144 size_t output_length = ~0;
8145 psa_status_t actual_status;
8146 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008147 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008148
Gilles Peskine449bd832023-01-11 14:50:10 +01008149 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008150
Gilles Peskine656896e2018-06-29 19:12:28 +02008151 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008152 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8153 psa_set_key_algorithm(&attributes, alg);
8154 psa_set_key_type(&attributes, key_type);
8155 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8156 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008157
8158 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008159 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8160 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008161
Gilles Peskine449bd832023-01-11 14:50:10 +01008162 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8163 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008164 TEST_CALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008165
8166 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008167 actual_status = psa_asymmetric_encrypt(key, alg,
8168 input_data->x, input_data->len,
8169 label->x, label->len,
8170 output, output_size,
8171 &output_length);
8172 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008173 if (actual_status == PSA_SUCCESS) {
8174 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008175 } else {
8176 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008177 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008178
Gilles Peskine68428122018-06-30 18:42:41 +02008179 /* If the label is empty, the test framework puts a non-null pointer
8180 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008181 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008182 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008183 if (output_size != 0) {
8184 memset(output, 0, output_size);
8185 }
8186 actual_status = psa_asymmetric_encrypt(key, alg,
8187 input_data->x, input_data->len,
8188 NULL, label->len,
8189 output, output_size,
8190 &output_length);
8191 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008192 if (actual_status == PSA_SUCCESS) {
8193 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008194 } else {
8195 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008196 }
Gilles Peskine68428122018-06-30 18:42:41 +02008197 }
8198
Gilles Peskine656896e2018-06-29 19:12:28 +02008199exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008200 /*
8201 * Key attributes may have been returned by psa_get_key_attributes()
8202 * thus reset them as required.
8203 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008204 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008205
Gilles Peskine449bd832023-01-11 14:50:10 +01008206 psa_destroy_key(key);
8207 mbedtls_free(output);
8208 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008209}
8210/* END_CASE */
8211
8212/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008213void asymmetric_encrypt_decrypt(int key_type_arg,
8214 data_t *key_data,
8215 int alg_arg,
8216 data_t *input_data,
8217 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008218{
Ronald Cron5425a212020-08-04 14:58:35 +02008219 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008220 psa_key_type_t key_type = key_type_arg;
8221 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008222 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008223 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008224 size_t output_size;
8225 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008226 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008227 size_t output2_size;
8228 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008229 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008230
Gilles Peskine449bd832023-01-11 14:50:10 +01008231 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008232
Gilles Peskine449bd832023-01-11 14:50:10 +01008233 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8234 psa_set_key_algorithm(&attributes, alg);
8235 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008236
Gilles Peskine449bd832023-01-11 14:50:10 +01008237 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8238 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008239
8240 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008241 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8242 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008243
Gilles Peskine449bd832023-01-11 14:50:10 +01008244 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8245 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008246 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008247
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008248 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008249 TEST_LE_U(output2_size,
8250 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8251 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008252 TEST_CALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008253
Gilles Peskineeebd7382018-06-08 18:11:54 +02008254 /* We test encryption by checking that encrypt-then-decrypt gives back
8255 * the original plaintext because of the non-optional random
8256 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008257 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8258 input_data->x, input_data->len,
8259 label->x, label->len,
8260 output, output_size,
8261 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008262 /* We don't know what ciphertext length to expect, but check that
8263 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008264 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008265
Gilles Peskine449bd832023-01-11 14:50:10 +01008266 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8267 output, output_length,
8268 label->x, label->len,
8269 output2, output2_size,
8270 &output2_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008271 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008272 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008273
8274exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008275 /*
8276 * Key attributes may have been returned by psa_get_key_attributes()
8277 * thus reset them as required.
8278 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008279 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008280
Gilles Peskine449bd832023-01-11 14:50:10 +01008281 psa_destroy_key(key);
8282 mbedtls_free(output);
8283 mbedtls_free(output2);
8284 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008285}
8286/* END_CASE */
8287
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008288/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008289void asymmetric_decrypt(int key_type_arg,
8290 data_t *key_data,
8291 int alg_arg,
8292 data_t *input_data,
8293 data_t *label,
8294 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008295{
Ronald Cron5425a212020-08-04 14:58:35 +02008296 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008297 psa_key_type_t key_type = key_type_arg;
8298 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008299 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008300 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008301 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008302 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008303 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008304
Gilles Peskine449bd832023-01-11 14:50:10 +01008305 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008306
Gilles Peskine449bd832023-01-11 14:50:10 +01008307 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8308 psa_set_key_algorithm(&attributes, alg);
8309 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008310
Gilles Peskine449bd832023-01-11 14:50:10 +01008311 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8312 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008313
Gilles Peskine449bd832023-01-11 14:50:10 +01008314 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8315 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008316
8317 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008318 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8319 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008320 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008321
Gilles Peskine449bd832023-01-11 14:50:10 +01008322 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8323 input_data->x, input_data->len,
8324 label->x, label->len,
8325 output,
8326 output_size,
8327 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008328 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008329 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008330
Gilles Peskine68428122018-06-30 18:42:41 +02008331 /* If the label is empty, the test framework puts a non-null pointer
8332 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008333 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008334 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008335 if (output_size != 0) {
8336 memset(output, 0, output_size);
8337 }
8338 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8339 input_data->x, input_data->len,
8340 NULL, label->len,
8341 output,
8342 output_size,
8343 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008344 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008345 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008346 }
8347
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008348exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008349 psa_reset_key_attributes(&attributes);
8350 psa_destroy_key(key);
8351 mbedtls_free(output);
8352 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008353}
8354/* END_CASE */
8355
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008356/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008357void asymmetric_decrypt_fail(int key_type_arg,
8358 data_t *key_data,
8359 int alg_arg,
8360 data_t *input_data,
8361 data_t *label,
8362 int output_size_arg,
8363 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008364{
Ronald Cron5425a212020-08-04 14:58:35 +02008365 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008366 psa_key_type_t key_type = key_type_arg;
8367 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008368 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008369 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008370 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008371 psa_status_t actual_status;
8372 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008373 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008374
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008375 TEST_CALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008376
Gilles Peskine449bd832023-01-11 14:50:10 +01008377 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008378
Gilles Peskine449bd832023-01-11 14:50:10 +01008379 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8380 psa_set_key_algorithm(&attributes, alg);
8381 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008382
Gilles Peskine449bd832023-01-11 14:50:10 +01008383 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8384 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008385
Gilles Peskine449bd832023-01-11 14:50:10 +01008386 actual_status = psa_asymmetric_decrypt(key, alg,
8387 input_data->x, input_data->len,
8388 label->x, label->len,
8389 output, output_size,
8390 &output_length);
8391 TEST_EQUAL(actual_status, expected_status);
8392 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008393
Gilles Peskine68428122018-06-30 18:42:41 +02008394 /* If the label is empty, the test framework puts a non-null pointer
8395 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008396 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008397 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008398 if (output_size != 0) {
8399 memset(output, 0, output_size);
8400 }
8401 actual_status = psa_asymmetric_decrypt(key, alg,
8402 input_data->x, input_data->len,
8403 NULL, label->len,
8404 output, output_size,
8405 &output_length);
8406 TEST_EQUAL(actual_status, expected_status);
8407 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008408 }
8409
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008410exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008411 psa_reset_key_attributes(&attributes);
8412 psa_destroy_key(key);
8413 mbedtls_free(output);
8414 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008415}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008416/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008417
8418/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008419void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008420{
8421 /* Test each valid way of initializing the object, except for `= {0}`, as
8422 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8423 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008424 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008425 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008426 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008427 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8428 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008429
Gilles Peskine449bd832023-01-11 14:50:10 +01008430 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008431
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008432 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008433 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8434 PSA_ERROR_BAD_STATE);
8435 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8436 PSA_ERROR_BAD_STATE);
8437 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8438 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008439
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008440 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008441 PSA_ASSERT(psa_key_derivation_abort(&func));
8442 PSA_ASSERT(psa_key_derivation_abort(&init));
8443 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008444}
8445/* END_CASE */
8446
Janos Follath16de4a42019-06-13 16:32:24 +01008447/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008448void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008449{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008450 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008451 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008452 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008453
Gilles Peskine449bd832023-01-11 14:50:10 +01008454 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008455
Gilles Peskine449bd832023-01-11 14:50:10 +01008456 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8457 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008458
8459exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008460 psa_key_derivation_abort(&operation);
8461 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008462}
8463/* END_CASE */
8464
Janos Follathaf3c2a02019-06-12 12:34:34 +01008465/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008466void derive_set_capacity(int alg_arg, int capacity_arg,
8467 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008468{
8469 psa_algorithm_t alg = alg_arg;
8470 size_t capacity = capacity_arg;
8471 psa_status_t expected_status = expected_status_arg;
8472 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8473
Gilles Peskine449bd832023-01-11 14:50:10 +01008474 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008475
Gilles Peskine449bd832023-01-11 14:50:10 +01008476 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008477
Gilles Peskine449bd832023-01-11 14:50:10 +01008478 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8479 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008480
8481exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008482 psa_key_derivation_abort(&operation);
8483 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008484}
8485/* END_CASE */
8486
8487/* BEGIN_CASE */
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308488void parse_binary_string_test(data_t *input, int output)
8489{
8490 uint64_t value;
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308491 value = mbedtls_test_parse_binary_string(input);
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308492 TEST_EQUAL(value, output);
8493}
8494/* END_CASE */
8495
8496/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008497void derive_input(int alg_arg,
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308498 int step_arg1, int key_type_arg1, data_t *input1,
8499 int expected_status_arg1,
8500 int step_arg2, int key_type_arg2, data_t *input2,
8501 int expected_status_arg2,
8502 int step_arg3, int key_type_arg3, data_t *input3,
8503 int expected_status_arg3,
Gilles Peskine449bd832023-01-11 14:50:10 +01008504 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008505{
8506 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008507 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308508 uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
Gilles Peskine449bd832023-01-11 14:50:10 +01008509 psa_status_t expected_statuses[] = { expected_status_arg1,
8510 expected_status_arg2,
8511 expected_status_arg3 };
8512 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008513 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8514 MBEDTLS_SVC_KEY_ID_INIT,
8515 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008516 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8517 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8518 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008519 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008520 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008521 psa_status_t expected_output_status = expected_output_status_arg;
8522 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008523
Gilles Peskine449bd832023-01-11 14:50:10 +01008524 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008525
Gilles Peskine449bd832023-01-11 14:50:10 +01008526 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8527 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008528
Gilles Peskine449bd832023-01-11 14:50:10 +01008529 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008530
Gilles Peskine449bd832023-01-11 14:50:10 +01008531 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8532 mbedtls_test_set_step(i);
8533 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008534 /* Skip this step */
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308535 } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE &&
8536 key_types[i] != INPUT_INTEGER) {
8537 psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i]));
Gilles Peskine449bd832023-01-11 14:50:10 +01008538 PSA_ASSERT(psa_import_key(&attributes,
8539 inputs[i]->x, inputs[i]->len,
8540 &keys[i]));
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308541 if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008542 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008543 // When taking a private key as secret input, use key agreement
8544 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008545 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8546 &operation, keys[i]),
8547 expected_statuses[i]);
8548 } else {
8549 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8550 keys[i]),
8551 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008552 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008553 } else {
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308554 if (key_types[i] == INPUT_INTEGER) {
8555 TEST_EQUAL(psa_key_derivation_input_integer(
8556 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308557 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308558 expected_statuses[i]);
8559 } else {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308560 TEST_EQUAL(psa_key_derivation_input_bytes(
8561 &operation, steps[i],
8562 inputs[i]->x, inputs[i]->len),
8563 expected_statuses[i]);
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308564 }
Janos Follathaf3c2a02019-06-12 12:34:34 +01008565 }
8566 }
8567
Gilles Peskine449bd832023-01-11 14:50:10 +01008568 if (output_key_type != PSA_KEY_TYPE_NONE) {
8569 psa_reset_key_attributes(&attributes);
8570 psa_set_key_type(&attributes, output_key_type);
8571 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008572 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008573 psa_key_derivation_output_key(&attributes, &operation,
8574 &output_key);
8575 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008576 uint8_t buffer[1];
8577 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008578 psa_key_derivation_output_bytes(&operation,
8579 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008580 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008581 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008582
Janos Follathaf3c2a02019-06-12 12:34:34 +01008583exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008584 psa_key_derivation_abort(&operation);
8585 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8586 psa_destroy_key(keys[i]);
8587 }
8588 psa_destroy_key(output_key);
8589 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008590}
8591/* END_CASE */
8592
Kusumit Ghoderao42b02b92023-06-06 16:48:46 +05308593/* BEGIN_CASE*/
8594void derive_input_invalid_cost(int alg_arg, int64_t cost)
8595{
8596 psa_algorithm_t alg = alg_arg;
8597 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8598
8599 PSA_ASSERT(psa_crypto_init());
8600 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8601
8602 TEST_EQUAL(psa_key_derivation_input_integer(&operation,
8603 PSA_KEY_DERIVATION_INPUT_COST,
8604 cost),
8605 PSA_ERROR_NOT_SUPPORTED);
8606
8607exit:
8608 psa_key_derivation_abort(&operation);
8609 PSA_DONE();
8610}
8611/* END_CASE*/
8612
Janos Follathd958bb72019-07-03 15:02:16 +01008613/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008614void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008615{
Janos Follathd958bb72019-07-03 15:02:16 +01008616 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008617 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008618 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008619 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008620 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008621 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008622 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008623 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008624 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008625 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008626 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8627 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008628 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008629 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008630
Gilles Peskine449bd832023-01-11 14:50:10 +01008631 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008632
Gilles Peskine449bd832023-01-11 14:50:10 +01008633 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8634 psa_set_key_algorithm(&attributes, alg);
8635 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008636
Gilles Peskine449bd832023-01-11 14:50:10 +01008637 PSA_ASSERT(psa_import_key(&attributes,
8638 key_data, sizeof(key_data),
8639 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008640
8641 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008642 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8643 input1, input1_length,
8644 input2, input2_length,
8645 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008646 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008647 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008648
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008649 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008650 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8651 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008652
Gilles Peskine449bd832023-01-11 14:50:10 +01008653 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008654
Gilles Peskine449bd832023-01-11 14:50:10 +01008655 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8656 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008657
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008658exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008659 psa_key_derivation_abort(&operation);
8660 psa_destroy_key(key);
8661 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008662}
8663/* END_CASE */
8664
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008665/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008666void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008667{
8668 uint8_t output_buffer[16];
8669 size_t buffer_size = 16;
8670 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008671 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008672
Gilles Peskine449bd832023-01-11 14:50:10 +01008673 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8674 output_buffer, buffer_size)
8675 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008676
Gilles Peskine449bd832023-01-11 14:50:10 +01008677 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8678 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008679
Gilles Peskine449bd832023-01-11 14:50:10 +01008680 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008681
Gilles Peskine449bd832023-01-11 14:50:10 +01008682 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8683 output_buffer, buffer_size)
8684 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008685
Gilles Peskine449bd832023-01-11 14:50:10 +01008686 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8687 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008688
8689exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008690 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008691}
8692/* END_CASE */
8693
8694/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008695void derive_output(int alg_arg,
8696 int step1_arg, data_t *input1, int expected_status_arg1,
8697 int step2_arg, data_t *input2, int expected_status_arg2,
8698 int step3_arg, data_t *input3, int expected_status_arg3,
8699 int step4_arg, data_t *input4, int expected_status_arg4,
8700 data_t *key_agreement_peer_key,
8701 int requested_capacity_arg,
8702 data_t *expected_output1,
8703 data_t *expected_output2,
8704 int other_key_input_type,
8705 int key_input_type,
8706 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008707{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008708 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008709 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8710 data_t *inputs[] = { input1, input2, input3, input4 };
8711 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8712 MBEDTLS_SVC_KEY_ID_INIT,
8713 MBEDTLS_SVC_KEY_ID_INIT,
8714 MBEDTLS_SVC_KEY_ID_INIT };
8715 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8716 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008717 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008718 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008719 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008720 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008721 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008722 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008723 size_t output_buffer_size = 0;
8724 uint8_t *output_buffer = NULL;
8725 size_t expected_capacity;
8726 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008727 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8728 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8729 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8730 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008731 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008732 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008733 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008734
Gilles Peskine449bd832023-01-11 14:50:10 +01008735 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8736 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008737 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008738 }
8739 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008740 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008741 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008742 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008743 TEST_CALLOC(output_buffer, output_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008744 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008745
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008746 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008747 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8748 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8749 requested_capacity));
8750 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8751 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008752 case 0:
8753 break;
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308754 case PSA_KEY_DERIVATION_INPUT_COST:
8755 TEST_EQUAL(psa_key_derivation_input_integer(
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308756 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308757 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308758 statuses[i]);
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308759 if (statuses[i] != PSA_SUCCESS) {
8760 goto exit;
8761 }
8762 break;
8763 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Gilles Peskine1468da72019-05-29 17:35:49 +02008764 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008765 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008766 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008767 TEST_EQUAL(psa_key_derivation_input_bytes(
8768 &operation, steps[i],
8769 inputs[i]->x, inputs[i]->len),
8770 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008771
Gilles Peskine449bd832023-01-11 14:50:10 +01008772 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008773 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008774 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008775 break;
8776 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008777 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8778 psa_set_key_algorithm(&attributes1, alg);
8779 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008780
Gilles Peskine449bd832023-01-11 14:50:10 +01008781 PSA_ASSERT(psa_import_key(&attributes1,
8782 inputs[i]->x, inputs[i]->len,
8783 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008784
Gilles Peskine449bd832023-01-11 14:50:10 +01008785 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8786 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8787 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8788 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008789 }
8790
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308791 TEST_EQUAL(psa_key_derivation_input_key(&operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01008792 steps[i],
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308793 keys[i]),
8794 statuses[i]);
8795
8796 if (statuses[i] != PSA_SUCCESS) {
8797 goto exit;
8798 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008799 break;
8800 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008801 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008802 break;
8803 }
8804 break;
8805 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008806 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008807 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008808 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8809 steps[i],
8810 inputs[i]->x,
8811 inputs[i]->len),
8812 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008813 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008814 case 1: // input key, type DERIVE
8815 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8817 psa_set_key_algorithm(&attributes2, alg);
8818 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008819
8820 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 if (other_key_input_type == 11) {
8822 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8823 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008824
Gilles Peskine449bd832023-01-11 14:50:10 +01008825 PSA_ASSERT(psa_import_key(&attributes2,
8826 inputs[i]->x, inputs[i]->len,
8827 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008828
Gilles Peskine449bd832023-01-11 14:50:10 +01008829 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8830 steps[i],
8831 keys[i]),
8832 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008833 break;
8834 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008835 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8836 psa_set_key_algorithm(&attributes3, alg);
8837 psa_set_key_type(&attributes3,
8838 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008839
Gilles Peskine449bd832023-01-11 14:50:10 +01008840 PSA_ASSERT(psa_import_key(&attributes3,
8841 inputs[i]->x, inputs[i]->len,
8842 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008843
Gilles Peskine449bd832023-01-11 14:50:10 +01008844 TEST_EQUAL(psa_key_derivation_key_agreement(
8845 &operation,
8846 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8847 keys[i], key_agreement_peer_key->x,
8848 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008849 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008850 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008851 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008852 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008853 }
8854
Gilles Peskine449bd832023-01-11 14:50:10 +01008855 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008856 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008857 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008858 break;
8859 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008860 TEST_EQUAL(psa_key_derivation_input_bytes(
8861 &operation, steps[i],
8862 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008863
Gilles Peskine449bd832023-01-11 14:50:10 +01008864 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008865 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008866 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008867 break;
8868 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008869 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008870
Gilles Peskine449bd832023-01-11 14:50:10 +01008871 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8872 &current_capacity));
8873 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008874 expected_capacity = requested_capacity;
8875
Gilles Peskine449bd832023-01-11 14:50:10 +01008876 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008877 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8878
8879 /* For output key derivation secret must be provided using
8880 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008881 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008882 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008883 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008884
Gilles Peskine449bd832023-01-11 14:50:10 +01008885 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8886 psa_set_key_algorithm(&attributes4, alg);
8887 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8888 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008889
Gilles Peskine449bd832023-01-11 14:50:10 +01008890 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8891 &derived_key), expected_status);
8892 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008893 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008894 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008895 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008896 status = psa_key_derivation_output_bytes(&operation,
8897 output_buffer, output_sizes[i]);
8898 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008899 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008900 TEST_ASSERT(status == PSA_SUCCESS ||
8901 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008902 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008903 } else if (expected_capacity == 0 ||
8904 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008905 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008906 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008907 expected_capacity = 0;
8908 continue;
8909 }
8910 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008911 PSA_ASSERT(status);
8912 if (output_sizes[i] != 0) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008913 TEST_MEMORY_COMPARE(output_buffer, output_sizes[i],
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008914 expected_outputs[i], output_sizes[i]);
Gilles Peskine449bd832023-01-11 14:50:10 +01008915 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008916 /* Check the operation status. */
8917 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008918 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8919 &current_capacity));
8920 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008921 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008922 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008923 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008924
8925exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008926 mbedtls_free(output_buffer);
8927 psa_key_derivation_abort(&operation);
8928 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8929 psa_destroy_key(keys[i]);
8930 }
8931 psa_destroy_key(derived_key);
8932 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008933}
8934/* END_CASE */
8935
8936/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008937void derive_full(int alg_arg,
8938 data_t *key_data,
8939 data_t *input1,
8940 data_t *input2,
8941 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008942{
Ronald Cron5425a212020-08-04 14:58:35 +02008943 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008944 psa_algorithm_t alg = alg_arg;
8945 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008946 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008947 unsigned char output_buffer[16];
8948 size_t expected_capacity = requested_capacity;
8949 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008950 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008951
Gilles Peskine449bd832023-01-11 14:50:10 +01008952 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008953
Gilles Peskine449bd832023-01-11 14:50:10 +01008954 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8955 psa_set_key_algorithm(&attributes, alg);
8956 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008957
Gilles Peskine449bd832023-01-11 14:50:10 +01008958 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8959 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008960
Gilles Peskine449bd832023-01-11 14:50:10 +01008961 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8962 input1->x, input1->len,
8963 input2->x, input2->len,
8964 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008965 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008966 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008967
Gilles Peskine449bd832023-01-11 14:50:10 +01008968 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8969 &current_capacity));
8970 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008971
8972 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008973 while (current_capacity > 0) {
8974 size_t read_size = sizeof(output_buffer);
8975 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008976 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008977 }
8978 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8979 output_buffer,
8980 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008981 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008982 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8983 &current_capacity));
8984 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008985 }
8986
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008987 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008988 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8989 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008990
Gilles Peskine449bd832023-01-11 14:50:10 +01008991 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008992
8993exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008994 psa_key_derivation_abort(&operation);
8995 psa_destroy_key(key);
8996 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008997}
8998/* END_CASE */
8999
Stephan Koch78109f52023-04-12 14:19:36 +02009000/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01009001void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
9002 int derivation_step,
9003 int capacity, int expected_capacity_status_arg,
9004 data_t *expected_output,
9005 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009006{
9007 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
9008 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04009009 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009010 uint8_t *output_buffer = NULL;
9011 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04009012 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
9013 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
9014 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009015
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009016 TEST_CALLOC(output_buffer, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009017 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009018
Gilles Peskine449bd832023-01-11 14:50:10 +01009019 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9020 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
9021 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009022
Gilles Peskine449bd832023-01-11 14:50:10 +01009023 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
9024 step, input->x, input->len),
9025 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009026
Gilles Peskine449bd832023-01-11 14:50:10 +01009027 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009028 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009029 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009030
Gilles Peskine449bd832023-01-11 14:50:10 +01009031 status = psa_key_derivation_output_bytes(&operation, output_buffer,
9032 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009033
Gilles Peskine449bd832023-01-11 14:50:10 +01009034 TEST_EQUAL(status, expected_output_status);
9035 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009036 TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009037 expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009038 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009039
9040exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009041 mbedtls_free(output_buffer);
9042 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009043 PSA_DONE();
9044}
9045/* END_CASE */
9046
Janos Follathe60c9052019-07-03 13:51:30 +01009047/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009048void derive_key_exercise(int alg_arg,
9049 data_t *key_data,
9050 data_t *input1,
9051 data_t *input2,
9052 int derived_type_arg,
9053 int derived_bits_arg,
9054 int derived_usage_arg,
9055 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009056{
Ronald Cron5425a212020-08-04 14:58:35 +02009057 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9058 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009059 psa_algorithm_t alg = alg_arg;
9060 psa_key_type_t derived_type = derived_type_arg;
9061 size_t derived_bits = derived_bits_arg;
9062 psa_key_usage_t derived_usage = derived_usage_arg;
9063 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009064 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009065 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009066 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009067 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009068
Gilles Peskine449bd832023-01-11 14:50:10 +01009069 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009070
Gilles Peskine449bd832023-01-11 14:50:10 +01009071 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9072 psa_set_key_algorithm(&attributes, alg);
9073 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9074 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9075 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009076
9077 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009078 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9079 input1->x, input1->len,
9080 input2->x, input2->len,
9081 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009082 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009083 }
Janos Follathe60c9052019-07-03 13:51:30 +01009084
Gilles Peskine449bd832023-01-11 14:50:10 +01009085 psa_set_key_usage_flags(&attributes, derived_usage);
9086 psa_set_key_algorithm(&attributes, derived_alg);
9087 psa_set_key_type(&attributes, derived_type);
9088 psa_set_key_bits(&attributes, derived_bits);
9089 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9090 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009091
9092 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009093 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9094 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9095 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009096
9097 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009098 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009099 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009100 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009101
9102exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009103 /*
9104 * Key attributes may have been returned by psa_get_key_attributes()
9105 * thus reset them as required.
9106 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009107 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009108
Gilles Peskine449bd832023-01-11 14:50:10 +01009109 psa_key_derivation_abort(&operation);
9110 psa_destroy_key(base_key);
9111 psa_destroy_key(derived_key);
9112 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009113}
9114/* END_CASE */
9115
Janos Follath42fd8882019-07-03 14:17:09 +01009116/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009117void derive_key_export(int alg_arg,
9118 data_t *key_data,
9119 data_t *input1,
9120 data_t *input2,
9121 int bytes1_arg,
9122 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009123{
Ronald Cron5425a212020-08-04 14:58:35 +02009124 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9125 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009126 psa_algorithm_t alg = alg_arg;
9127 size_t bytes1 = bytes1_arg;
9128 size_t bytes2 = bytes2_arg;
9129 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009130 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009131 uint8_t *output_buffer = NULL;
9132 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009133 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9134 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009135 size_t length;
9136
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009137 TEST_CALLOC(output_buffer, capacity);
9138 TEST_CALLOC(export_buffer, capacity);
Gilles Peskine449bd832023-01-11 14:50:10 +01009139 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009140
Gilles Peskine449bd832023-01-11 14:50:10 +01009141 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9142 psa_set_key_algorithm(&base_attributes, alg);
9143 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9144 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9145 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009146
9147 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009148 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9149 input1->x, input1->len,
9150 input2->x, input2->len,
9151 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009152 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009153 }
Janos Follath42fd8882019-07-03 14:17:09 +01009154
Gilles Peskine449bd832023-01-11 14:50:10 +01009155 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9156 output_buffer,
9157 capacity));
9158 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009159
9160 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009161 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9162 input1->x, input1->len,
9163 input2->x, input2->len,
9164 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009165 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009166 }
Janos Follath42fd8882019-07-03 14:17:09 +01009167
Gilles Peskine449bd832023-01-11 14:50:10 +01009168 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9169 psa_set_key_algorithm(&derived_attributes, 0);
9170 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9171 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9172 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9173 &derived_key));
9174 PSA_ASSERT(psa_export_key(derived_key,
9175 export_buffer, bytes1,
9176 &length));
9177 TEST_EQUAL(length, bytes1);
9178 PSA_ASSERT(psa_destroy_key(derived_key));
9179 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9180 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9181 &derived_key));
9182 PSA_ASSERT(psa_export_key(derived_key,
9183 export_buffer + bytes1, bytes2,
9184 &length));
9185 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009186
9187 /* Compare the outputs from the two runs. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009188 TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009189 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009190
9191exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009192 mbedtls_free(output_buffer);
9193 mbedtls_free(export_buffer);
9194 psa_key_derivation_abort(&operation);
9195 psa_destroy_key(base_key);
9196 psa_destroy_key(derived_key);
9197 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009198}
9199/* END_CASE */
9200
9201/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009202void derive_key_type(int alg_arg,
9203 data_t *key_data,
9204 data_t *input1,
9205 data_t *input2,
9206 int key_type_arg, int bits_arg,
9207 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009208{
9209 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9210 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9211 const psa_algorithm_t alg = alg_arg;
9212 const psa_key_type_t key_type = key_type_arg;
9213 const size_t bits = bits_arg;
9214 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9215 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009216 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009217 uint8_t *export_buffer = NULL;
9218 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9219 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9220 size_t export_length;
9221
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009222 TEST_CALLOC(export_buffer, export_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01009223 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009224
Gilles Peskine449bd832023-01-11 14:50:10 +01009225 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9226 psa_set_key_algorithm(&base_attributes, alg);
9227 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9228 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9229 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009230
Gilles Peskine449bd832023-01-11 14:50:10 +01009231 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009232 &operation, base_key, alg,
9233 input1->x, input1->len,
9234 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009235 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009236 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009237 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009238
Gilles Peskine449bd832023-01-11 14:50:10 +01009239 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9240 psa_set_key_algorithm(&derived_attributes, 0);
9241 psa_set_key_type(&derived_attributes, key_type);
9242 psa_set_key_bits(&derived_attributes, bits);
9243 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9244 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009245
Gilles Peskine449bd832023-01-11 14:50:10 +01009246 PSA_ASSERT(psa_export_key(derived_key,
9247 export_buffer, export_buffer_size,
9248 &export_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009249 TEST_MEMORY_COMPARE(export_buffer, export_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009250 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009251
9252exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009253 mbedtls_free(export_buffer);
9254 psa_key_derivation_abort(&operation);
9255 psa_destroy_key(base_key);
9256 psa_destroy_key(derived_key);
9257 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009258}
9259/* END_CASE */
9260
9261/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009262void derive_key(int alg_arg,
9263 data_t *key_data, data_t *input1, data_t *input2,
9264 int type_arg, int bits_arg,
9265 int expected_status_arg,
9266 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009267{
Ronald Cron5425a212020-08-04 14:58:35 +02009268 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9269 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009270 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009271 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009272 size_t bits = bits_arg;
9273 psa_status_t expected_status = expected_status_arg;
9274 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9275 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9276 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9277
Gilles Peskine449bd832023-01-11 14:50:10 +01009278 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009279
Gilles Peskine449bd832023-01-11 14:50:10 +01009280 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9281 psa_set_key_algorithm(&base_attributes, alg);
9282 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9283 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9284 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009285
Gilles Peskine449bd832023-01-11 14:50:10 +01009286 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9287 input1->x, input1->len,
9288 input2->x, input2->len,
9289 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009290 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009291 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009292
Gilles Peskine449bd832023-01-11 14:50:10 +01009293 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9294 psa_set_key_algorithm(&derived_attributes, 0);
9295 psa_set_key_type(&derived_attributes, type);
9296 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009297
9298 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009299 psa_key_derivation_output_key(&derived_attributes,
9300 &operation,
9301 &derived_key);
9302 if (is_large_output > 0) {
9303 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9304 }
9305 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009306
9307exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009308 psa_key_derivation_abort(&operation);
9309 psa_destroy_key(base_key);
9310 psa_destroy_key(derived_key);
9311 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009312}
9313/* END_CASE */
9314
9315/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009316void key_agreement_setup(int alg_arg,
9317 int our_key_type_arg, int our_key_alg_arg,
9318 data_t *our_key_data, data_t *peer_key_data,
9319 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009320{
Ronald Cron5425a212020-08-04 14:58:35 +02009321 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009322 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009323 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009324 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009325 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009326 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009327 psa_status_t expected_status = expected_status_arg;
9328 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009329
Gilles Peskine449bd832023-01-11 14:50:10 +01009330 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009331
Gilles Peskine449bd832023-01-11 14:50:10 +01009332 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9333 psa_set_key_algorithm(&attributes, our_key_alg);
9334 psa_set_key_type(&attributes, our_key_type);
9335 PSA_ASSERT(psa_import_key(&attributes,
9336 our_key_data->x, our_key_data->len,
9337 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009338
Gilles Peskine77f40d82019-04-11 21:27:06 +02009339 /* The tests currently include inputs that should fail at either step.
9340 * Test cases that fail at the setup step should be changed to call
9341 * key_derivation_setup instead, and this function should be renamed
9342 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009343 status = psa_key_derivation_setup(&operation, alg);
9344 if (status == PSA_SUCCESS) {
9345 TEST_EQUAL(psa_key_derivation_key_agreement(
9346 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9347 our_key,
9348 peer_key_data->x, peer_key_data->len),
9349 expected_status);
9350 } else {
9351 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009352 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009353
9354exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009355 psa_key_derivation_abort(&operation);
9356 psa_destroy_key(our_key);
9357 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009358}
9359/* END_CASE */
9360
9361/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009362void raw_key_agreement(int alg_arg,
9363 int our_key_type_arg, data_t *our_key_data,
9364 data_t *peer_key_data,
9365 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009366{
Ronald Cron5425a212020-08-04 14:58:35 +02009367 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009368 psa_algorithm_t alg = alg_arg;
9369 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009370 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009371 unsigned char *output = NULL;
9372 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009373 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009374
Gilles Peskine449bd832023-01-11 14:50:10 +01009375 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009376
Gilles Peskine449bd832023-01-11 14:50:10 +01009377 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9378 psa_set_key_algorithm(&attributes, alg);
9379 psa_set_key_type(&attributes, our_key_type);
9380 PSA_ASSERT(psa_import_key(&attributes,
9381 our_key_data->x, our_key_data->len,
9382 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009383
Gilles Peskine449bd832023-01-11 14:50:10 +01009384 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9385 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009386
Gilles Peskine992bee82022-04-13 23:25:52 +02009387 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009388 TEST_LE_U(expected_output->len,
9389 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9390 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9391 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009392
9393 /* Good case with exact output size */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009394 TEST_CALLOC(output, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009395 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9396 peer_key_data->x, peer_key_data->len,
9397 output, expected_output->len,
9398 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009399 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009400 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009401 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009402 output = NULL;
9403 output_length = ~0;
9404
9405 /* Larger buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009406 TEST_CALLOC(output, expected_output->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009407 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9408 peer_key_data->x, peer_key_data->len,
9409 output, expected_output->len + 1,
9410 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009411 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009412 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009413 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009414 output = NULL;
9415 output_length = ~0;
9416
9417 /* Buffer too small */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009418 TEST_CALLOC(output, expected_output->len - 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009419 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9420 peer_key_data->x, peer_key_data->len,
9421 output, expected_output->len - 1,
9422 &output_length),
9423 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009424 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009425 TEST_LE_U(output_length, expected_output->len - 1);
9426 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009427 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009428
9429exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009430 mbedtls_free(output);
9431 psa_destroy_key(our_key);
9432 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009433}
9434/* END_CASE */
9435
9436/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009437void key_agreement_capacity(int alg_arg,
9438 int our_key_type_arg, data_t *our_key_data,
9439 data_t *peer_key_data,
9440 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009441{
Ronald Cron5425a212020-08-04 14:58:35 +02009442 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009443 psa_algorithm_t alg = alg_arg;
9444 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009445 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009446 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009447 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009448 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009449
Gilles Peskine449bd832023-01-11 14:50:10 +01009450 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009451
Gilles Peskine449bd832023-01-11 14:50:10 +01009452 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9453 psa_set_key_algorithm(&attributes, alg);
9454 psa_set_key_type(&attributes, our_key_type);
9455 PSA_ASSERT(psa_import_key(&attributes,
9456 our_key_data->x, our_key_data->len,
9457 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009458
Gilles Peskine449bd832023-01-11 14:50:10 +01009459 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9460 PSA_ASSERT(psa_key_derivation_key_agreement(
9461 &operation,
9462 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9463 peer_key_data->x, peer_key_data->len));
9464 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009465 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009466 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9467 PSA_KEY_DERIVATION_INPUT_INFO,
9468 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009469 }
Gilles Peskine59685592018-09-18 12:11:34 +02009470
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009471 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009472 PSA_ASSERT(psa_key_derivation_get_capacity(
9473 &operation, &actual_capacity));
9474 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009475
Gilles Peskinebf491972018-10-25 22:36:12 +02009476 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009477 while (actual_capacity > sizeof(output)) {
9478 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9479 output, sizeof(output)));
9480 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009481 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009482 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9483 output, actual_capacity));
9484 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9485 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009486
Gilles Peskine59685592018-09-18 12:11:34 +02009487exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009488 psa_key_derivation_abort(&operation);
9489 psa_destroy_key(our_key);
9490 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009491}
9492/* END_CASE */
9493
9494/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009495void key_agreement_output(int alg_arg,
9496 int our_key_type_arg, data_t *our_key_data,
9497 data_t *peer_key_data,
9498 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009499{
Ronald Cron5425a212020-08-04 14:58:35 +02009500 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009501 psa_algorithm_t alg = alg_arg;
9502 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009503 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009504 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009505 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009506
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009507 TEST_CALLOC(actual_output, MAX(expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009508 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009509
Gilles Peskine449bd832023-01-11 14:50:10 +01009510 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009511
Gilles Peskine449bd832023-01-11 14:50:10 +01009512 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9513 psa_set_key_algorithm(&attributes, alg);
9514 psa_set_key_type(&attributes, our_key_type);
9515 PSA_ASSERT(psa_import_key(&attributes,
9516 our_key_data->x, our_key_data->len,
9517 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009518
Gilles Peskine449bd832023-01-11 14:50:10 +01009519 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9520 PSA_ASSERT(psa_key_derivation_key_agreement(
9521 &operation,
9522 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9523 peer_key_data->x, peer_key_data->len));
9524 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009525 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009526 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9527 PSA_KEY_DERIVATION_INPUT_INFO,
9528 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009529 }
Gilles Peskine59685592018-09-18 12:11:34 +02009530
Gilles Peskine449bd832023-01-11 14:50:10 +01009531 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9532 actual_output,
9533 expected_output1->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009534 TEST_MEMORY_COMPARE(actual_output, expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009535 expected_output1->x, expected_output1->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009536 if (expected_output2->len != 0) {
9537 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9538 actual_output,
9539 expected_output2->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009540 TEST_MEMORY_COMPARE(actual_output, expected_output2->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009541 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009542 }
Gilles Peskine59685592018-09-18 12:11:34 +02009543
9544exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009545 psa_key_derivation_abort(&operation);
9546 psa_destroy_key(our_key);
9547 PSA_DONE();
9548 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009549}
9550/* END_CASE */
9551
9552/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009553void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009554{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009555 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009556 unsigned char *output = NULL;
9557 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009558 size_t i;
9559 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009560
Gilles Peskine449bd832023-01-11 14:50:10 +01009561 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009562
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009563 TEST_CALLOC(output, bytes);
9564 TEST_CALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009565
Gilles Peskine449bd832023-01-11 14:50:10 +01009566 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009567
Gilles Peskinea50d7392018-06-21 10:22:13 +02009568 /* Run several times, to ensure that every output byte will be
9569 * nonzero at least once with overwhelming probability
9570 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009571 for (run = 0; run < 10; run++) {
9572 if (bytes != 0) {
9573 memset(output, 0, bytes);
9574 }
9575 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009576
Gilles Peskine449bd832023-01-11 14:50:10 +01009577 for (i = 0; i < bytes; i++) {
9578 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009579 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009580 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009581 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009582 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009583
9584 /* Check that every byte was changed to nonzero at least once. This
9585 * validates that psa_generate_random is overwriting every byte of
9586 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009587 for (i = 0; i < bytes; i++) {
9588 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009589 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009590
9591exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009592 PSA_DONE();
9593 mbedtls_free(output);
9594 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009595}
9596/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009597
9598/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009599void generate_key(int type_arg,
9600 int bits_arg,
9601 int usage_arg,
9602 int alg_arg,
9603 int expected_status_arg,
9604 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009605{
Ronald Cron5425a212020-08-04 14:58:35 +02009606 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009607 psa_key_type_t type = type_arg;
9608 psa_key_usage_t usage = usage_arg;
9609 size_t bits = bits_arg;
9610 psa_algorithm_t alg = alg_arg;
9611 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009612 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009613 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009614
Gilles Peskine449bd832023-01-11 14:50:10 +01009615 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009616
Gilles Peskine449bd832023-01-11 14:50:10 +01009617 psa_set_key_usage_flags(&attributes, usage);
9618 psa_set_key_algorithm(&attributes, alg);
9619 psa_set_key_type(&attributes, type);
9620 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009621
9622 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009623 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009624
Gilles Peskine449bd832023-01-11 14:50:10 +01009625 if (is_large_key > 0) {
9626 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9627 }
9628 TEST_EQUAL(status, expected_status);
9629 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009630 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009631 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009632
9633 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009634 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9635 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9636 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009637
Gilles Peskine818ca122018-06-20 18:16:48 +02009638 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009639 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009640 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009641 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009642
9643exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009644 /*
9645 * Key attributes may have been returned by psa_get_key_attributes()
9646 * thus reset them as required.
9647 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009648 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009649
Gilles Peskine449bd832023-01-11 14:50:10 +01009650 psa_destroy_key(key);
9651 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009652}
9653/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009654
Valerio Setti19fec542023-07-25 12:31:50 +02009655/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine449bd832023-01-11 14:50:10 +01009656void generate_key_rsa(int bits_arg,
9657 data_t *e_arg,
9658 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009659{
Ronald Cron5425a212020-08-04 14:58:35 +02009660 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009661 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009662 size_t bits = bits_arg;
9663 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9664 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9665 psa_status_t expected_status = expected_status_arg;
9666 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9667 uint8_t *exported = NULL;
9668 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009669 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009670 size_t exported_length = SIZE_MAX;
9671 uint8_t *e_read_buffer = NULL;
9672 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009673 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009674 size_t e_read_length = SIZE_MAX;
9675
Gilles Peskine449bd832023-01-11 14:50:10 +01009676 if (e_arg->len == 0 ||
9677 (e_arg->len == 3 &&
9678 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009679 is_default_public_exponent = 1;
9680 e_read_size = 0;
9681 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009682 TEST_CALLOC(e_read_buffer, e_read_size);
9683 TEST_CALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009684
Gilles Peskine449bd832023-01-11 14:50:10 +01009685 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009686
Gilles Peskine449bd832023-01-11 14:50:10 +01009687 psa_set_key_usage_flags(&attributes, usage);
9688 psa_set_key_algorithm(&attributes, alg);
9689 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9690 e_arg->x, e_arg->len));
9691 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009692
9693 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009694 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9695 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009696 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009697 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009698
9699 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009700 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9701 TEST_EQUAL(psa_get_key_type(&attributes), type);
9702 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009703 psa_status_t status = psa_get_key_domain_parameters(&attributes,
9704 e_read_buffer, e_read_size,
9705 &e_read_length);
9706
9707
9708#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
9709 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
9710 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +01009711 if (is_default_public_exponent) {
9712 TEST_EQUAL(e_read_length, 0);
9713 } else {
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009714 TEST_EQUAL(status, PSA_SUCCESS);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009715 TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009716 }
Pengyu Lv9e976f32023-12-06 16:58:05 +08009717#else
Pengyu Lv9e976f32023-12-06 16:58:05 +08009718 (void) is_default_public_exponent;
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009719 TEST_EQUAL(status, PSA_ERROR_NOT_SUPPORTED);
Pengyu Lv9e976f32023-12-06 16:58:05 +08009720#endif
Gilles Peskinee56e8782019-04-26 17:34:02 +02009721
9722 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009723 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009724 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009725 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009726
9727 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009728 PSA_ASSERT(psa_export_public_key(key,
9729 exported, exported_size,
9730 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009731 {
9732 uint8_t *p = exported;
9733 uint8_t *end = exported + exported_length;
9734 size_t len;
9735 /* RSAPublicKey ::= SEQUENCE {
9736 * modulus INTEGER, -- n
9737 * publicExponent INTEGER } -- e
9738 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009739 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9740 MBEDTLS_ASN1_SEQUENCE |
9741 MBEDTLS_ASN1_CONSTRUCTED));
9742 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9743 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9744 MBEDTLS_ASN1_INTEGER));
9745 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009746 ++p;
9747 --len;
9748 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009749 if (e_arg->len == 0) {
9750 TEST_EQUAL(len, 3);
9751 TEST_EQUAL(p[0], 1);
9752 TEST_EQUAL(p[1], 0);
9753 TEST_EQUAL(p[2], 1);
9754 } else {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009755 TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009756 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009757 }
9758
9759exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009760 /*
9761 * Key attributes may have been returned by psa_get_key_attributes() or
9762 * set by psa_set_key_domain_parameters() thus reset them as required.
9763 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009764 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009765
Gilles Peskine449bd832023-01-11 14:50:10 +01009766 psa_destroy_key(key);
9767 PSA_DONE();
9768 mbedtls_free(e_read_buffer);
9769 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009770}
9771/* END_CASE */
9772
Darryl Greend49a4992018-06-18 17:27:26 +01009773/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009774void persistent_key_load_key_from_storage(data_t *data,
9775 int type_arg, int bits_arg,
9776 int usage_flags_arg, int alg_arg,
9777 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009778{
Gilles Peskine449bd832023-01-11 14:50:10 +01009779 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009780 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009781 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9782 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009783 psa_key_type_t type = type_arg;
9784 size_t bits = bits_arg;
9785 psa_key_usage_t usage_flags = usage_flags_arg;
9786 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009787 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009788 unsigned char *first_export = NULL;
9789 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009790 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Sergeybef1f632023-03-06 15:25:06 -07009791 size_t first_exported_length = 0;
Darryl Greend49a4992018-06-18 17:27:26 +01009792 size_t second_exported_length;
9793
Gilles Peskine449bd832023-01-11 14:50:10 +01009794 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009795 TEST_CALLOC(first_export, export_size);
9796 TEST_CALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009797 }
Darryl Greend49a4992018-06-18 17:27:26 +01009798
Gilles Peskine449bd832023-01-11 14:50:10 +01009799 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009800
Gilles Peskine449bd832023-01-11 14:50:10 +01009801 psa_set_key_id(&attributes, key_id);
9802 psa_set_key_usage_flags(&attributes, usage_flags);
9803 psa_set_key_algorithm(&attributes, alg);
9804 psa_set_key_type(&attributes, type);
9805 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009806
Gilles Peskine449bd832023-01-11 14:50:10 +01009807 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009808 case IMPORT_KEY:
9809 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009810 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9811 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009812 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009813
Darryl Green0c6575a2018-11-07 16:05:30 +00009814 case GENERATE_KEY:
9815 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009816 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009817 break;
9818
9819 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009820#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009821 {
9822 /* Create base key */
9823 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9824 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9825 psa_set_key_usage_flags(&base_attributes,
9826 PSA_KEY_USAGE_DERIVE);
9827 psa_set_key_algorithm(&base_attributes, derive_alg);
9828 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9829 PSA_ASSERT(psa_import_key(&base_attributes,
9830 data->x, data->len,
9831 &base_key));
9832 /* Derive a key. */
9833 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9834 PSA_ASSERT(psa_key_derivation_input_key(
9835 &operation,
9836 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9837 PSA_ASSERT(psa_key_derivation_input_bytes(
9838 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9839 NULL, 0));
9840 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9841 &operation,
9842 &key));
9843 PSA_ASSERT(psa_key_derivation_abort(&operation));
9844 PSA_ASSERT(psa_destroy_key(base_key));
9845 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9846 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009847#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009848 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009849#endif
9850 break;
9851
9852 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01009853 TEST_FAIL("generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009854 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009855 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009856 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009857
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009858 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009859 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9860 PSA_ASSERT(psa_export_key(key,
9861 first_export, export_size,
9862 &first_exported_length));
9863 if (generation_method == IMPORT_KEY) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009864 TEST_MEMORY_COMPARE(data->x, data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009865 first_export, first_exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01009866 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009867 }
Darryl Greend49a4992018-06-18 17:27:26 +01009868
9869 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009870 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009871 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009872 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009873
Darryl Greend49a4992018-06-18 17:27:26 +01009874 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009875 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9876 TEST_ASSERT(mbedtls_svc_key_id_equal(
9877 psa_get_key_id(&attributes), key_id));
9878 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9879 PSA_KEY_LIFETIME_PERSISTENT);
9880 TEST_EQUAL(psa_get_key_type(&attributes), type);
9881 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9882 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9883 mbedtls_test_update_key_usage_flags(usage_flags));
9884 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009885
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009886 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009887 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9888 PSA_ASSERT(psa_export_key(key,
9889 second_export, export_size,
9890 &second_exported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009891 TEST_MEMORY_COMPARE(first_export, first_exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009892 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009893 }
9894
9895 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009896 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009897 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009898 }
Darryl Greend49a4992018-06-18 17:27:26 +01009899
9900exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009901 /*
9902 * Key attributes may have been returned by psa_get_key_attributes()
9903 * thus reset them as required.
9904 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009905 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009906
Gilles Peskine449bd832023-01-11 14:50:10 +01009907 mbedtls_free(first_export);
9908 mbedtls_free(second_export);
9909 psa_key_derivation_abort(&operation);
9910 psa_destroy_key(base_key);
9911 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009912 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009913}
9914/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009915
Neil Armstronga557cb82022-06-10 08:58:32 +02009916/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009917void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9918 int primitive_arg, int hash_arg, int role_arg,
9919 int test_input, data_t *pw_data,
9920 int inj_err_type_arg,
9921 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009922{
9923 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9924 psa_pake_operation_t operation = psa_pake_operation_init();
9925 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009926 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009927 psa_key_type_t key_type_pw = key_type_pw_arg;
9928 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009929 psa_algorithm_t hash_alg = hash_arg;
9930 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009931 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9932 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009933 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9934 psa_status_t expected_error = expected_error_arg;
9935 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009936 unsigned char *output_buffer = NULL;
9937 size_t output_len = 0;
9938
Gilles Peskine449bd832023-01-11 14:50:10 +01009939 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009940
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009941 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009942 PSA_PAKE_STEP_KEY_SHARE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009943 TEST_CALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009944
Gilles Peskine449bd832023-01-11 14:50:10 +01009945 if (pw_data->len > 0) {
9946 psa_set_key_usage_flags(&attributes, key_usage_pw);
9947 psa_set_key_algorithm(&attributes, alg);
9948 psa_set_key_type(&attributes, key_type_pw);
9949 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9950 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009951 }
9952
Gilles Peskine449bd832023-01-11 14:50:10 +01009953 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9954 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9955 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009956
Gilles Peskine449bd832023-01-11 14:50:10 +01009957 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009958
Gilles Peskine449bd832023-01-11 14:50:10 +01009959 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9960 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9961 expected_error);
9962 PSA_ASSERT(psa_pake_abort(&operation));
9963 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9964 expected_error);
9965 PSA_ASSERT(psa_pake_abort(&operation));
9966 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9967 expected_error);
9968 PSA_ASSERT(psa_pake_abort(&operation));
9969 TEST_EQUAL(psa_pake_set_role(&operation, role),
9970 expected_error);
9971 PSA_ASSERT(psa_pake_abort(&operation));
9972 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9973 NULL, 0, NULL),
9974 expected_error);
9975 PSA_ASSERT(psa_pake_abort(&operation));
9976 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9977 expected_error);
9978 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009979 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009980 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009981
Gilles Peskine449bd832023-01-11 14:50:10 +01009982 status = psa_pake_setup(&operation, &cipher_suite);
9983 if (status != PSA_SUCCESS) {
9984 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009985 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009986 }
9987
Gilles Peskine449bd832023-01-11 14:50:10 +01009988 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9989 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9990 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009991 goto exit;
9992 }
9993
Gilles Peskine449bd832023-01-11 14:50:10 +01009994 status = psa_pake_set_role(&operation, role);
9995 if (status != PSA_SUCCESS) {
9996 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009997 goto exit;
9998 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009999
Gilles Peskine449bd832023-01-11 14:50:10 +010010000 if (pw_data->len > 0) {
10001 status = psa_pake_set_password_key(&operation, key);
10002 if (status != PSA_SUCCESS) {
10003 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010004 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010005 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010006 }
10007
Gilles Peskine449bd832023-01-11 14:50:10 +010010008 if (inj_err_type == INJECT_ERR_INVALID_USER) {
10009 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10010 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010011 goto exit;
10012 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010013
Gilles Peskine449bd832023-01-11 14:50:10 +010010014 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
10015 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10016 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010017 goto exit;
10018 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010019
Gilles Peskine449bd832023-01-11 14:50:10 +010010020 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010021 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010022 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
10023 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010024 goto exit;
10025 }
10026
Gilles Peskine449bd832023-01-11 14:50:10 +010010027 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010028 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010029 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
10030 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010031 goto exit;
10032 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010033
Gilles Peskine449bd832023-01-11 14:50:10 +010010034 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
10035 PSA_PAKE_STEP_KEY_SHARE);
10036 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
10037 PSA_PAKE_STEP_ZK_PUBLIC);
10038 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
10039 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010040
Gilles Peskine449bd832023-01-11 14:50:10 +010010041 if (test_input) {
10042 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10043 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
10044 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010045 goto exit;
10046 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010047
Gilles Peskine449bd832023-01-11 14:50:10 +010010048 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10049 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10050 output_buffer, size_zk_proof),
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_INVALID_FIRST_STEP) {
10056 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
10057 output_buffer, size_zk_proof),
10058 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010059 goto exit;
10060 }
10061
Gilles Peskine449bd832023-01-11 14:50:10 +010010062 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
10063 output_buffer, size_key_share);
10064 if (status != PSA_SUCCESS) {
10065 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010066 goto exit;
10067 }
10068
Gilles Peskine449bd832023-01-11 14:50:10 +010010069 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10070 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10071 output_buffer, size_zk_public + 1),
10072 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010073 goto exit;
10074 }
10075
Gilles Peskine449bd832023-01-11 14:50:10 +010010076 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010077 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010078 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10079 output_buffer, size_zk_public + 1);
10080 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10081 output_buffer, size_zk_public),
10082 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010083 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010084 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010085 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010086 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10087 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10088 NULL, 0, NULL),
10089 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010090 goto exit;
10091 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010092
Gilles Peskine449bd832023-01-11 14:50:10 +010010093 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10094 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10095 output_buffer, buf_size, &output_len),
10096 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010097 goto exit;
10098 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010099
Gilles Peskine449bd832023-01-11 14:50:10 +010010100 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10101 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10102 output_buffer, buf_size, &output_len),
10103 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010104 goto exit;
10105 }
10106
Gilles Peskine449bd832023-01-11 14:50:10 +010010107 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10108 output_buffer, buf_size, &output_len);
10109 if (status != PSA_SUCCESS) {
10110 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010111 goto exit;
10112 }
10113
Gilles Peskine449bd832023-01-11 14:50:10 +010010114 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010115
Gilles Peskine449bd832023-01-11 14:50:10 +010010116 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10117 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10118 output_buffer, size_zk_public - 1, &output_len),
10119 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010120 goto exit;
10121 }
10122
Gilles Peskine449bd832023-01-11 14:50:10 +010010123 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010124 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010125 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10126 output_buffer, size_zk_public - 1, &output_len);
10127 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10128 output_buffer, buf_size, &output_len),
10129 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010130 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010131 }
10132 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010133
10134exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010135 PSA_ASSERT(psa_destroy_key(key));
10136 PSA_ASSERT(psa_pake_abort(&operation));
10137 mbedtls_free(output_buffer);
10138 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010139}
10140/* END_CASE */
10141
Neil Armstronga557cb82022-06-10 08:58:32 +020010142/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010143void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10144 int client_input_first, int inject_error,
10145 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010146{
10147 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10148 psa_pake_operation_t server = psa_pake_operation_init();
10149 psa_pake_operation_t client = psa_pake_operation_init();
10150 psa_algorithm_t alg = alg_arg;
10151 psa_algorithm_t hash_alg = hash_arg;
10152 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10153 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10154
Gilles Peskine449bd832023-01-11 14:50:10 +010010155 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010156
Gilles Peskine449bd832023-01-11 14:50:10 +010010157 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10158 psa_set_key_algorithm(&attributes, alg);
10159 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10160 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10161 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010162
Gilles Peskine449bd832023-01-11 14:50:10 +010010163 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10164 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10165 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010166
10167
Gilles Peskine449bd832023-01-11 14:50:10 +010010168 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10169 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010170
Gilles Peskine449bd832023-01-11 14:50:10 +010010171 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10172 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010173
Gilles Peskine449bd832023-01-11 14:50:10 +010010174 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10175 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010176
Gilles Peskine449bd832023-01-11 14:50:10 +010010177 ecjpake_do_round(alg, primitive_arg, &server, &client,
10178 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010179
Gilles Peskine449bd832023-01-11 14:50:10 +010010180 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010181 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010182 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010183
Gilles Peskine449bd832023-01-11 14:50:10 +010010184 ecjpake_do_round(alg, primitive_arg, &server, &client,
10185 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010186
10187exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010188 psa_destroy_key(key);
10189 psa_pake_abort(&server);
10190 psa_pake_abort(&client);
10191 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010192}
10193/* END_CASE */
10194
10195/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010196void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10197 int derive_alg_arg, data_t *pw_data,
10198 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010199{
10200 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10201 psa_pake_operation_t server = psa_pake_operation_init();
10202 psa_pake_operation_t client = psa_pake_operation_init();
10203 psa_algorithm_t alg = alg_arg;
10204 psa_algorithm_t hash_alg = hash_arg;
10205 psa_algorithm_t derive_alg = derive_alg_arg;
10206 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10208 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010209 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010210 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010211 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010212 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010213
Gilles Peskine449bd832023-01-11 14:50:10 +010010214 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010215
Gilles Peskine449bd832023-01-11 14:50:10 +010010216 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10217 psa_set_key_algorithm(&attributes, alg);
10218 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10219 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10220 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010221
Gilles Peskine449bd832023-01-11 14:50:10 +010010222 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10223 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10224 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010225
Neil Armstrong1e855602022-06-15 11:32:11 +020010226 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010227 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10228 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010229
Gilles Peskine449bd832023-01-11 14:50:10 +010010230 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10231 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10232 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10233 PSA_KEY_DERIVATION_INPUT_SEED,
10234 (const uint8_t *) "", 0));
10235 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10236 PSA_KEY_DERIVATION_INPUT_SEED,
10237 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010238 }
10239
Gilles Peskine449bd832023-01-11 14:50:10 +010010240 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10241 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010242
Gilles Peskine449bd832023-01-11 14:50:10 +010010243 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10244 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010245
Gilles Peskine449bd832023-01-11 14:50:10 +010010246 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10247 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010248
Gilles Peskine449bd832023-01-11 14:50:10 +010010249 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10250 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10251 PSA_ERROR_BAD_STATE);
10252 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10253 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010254 goto exit;
10255 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010256
Neil Armstrongf983caf2022-06-15 15:27:48 +020010257 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010258 ecjpake_do_round(alg, primitive_arg, &server, &client,
10259 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010260
Gilles Peskine449bd832023-01-11 14:50:10 +010010261 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10262 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10263 PSA_ERROR_BAD_STATE);
10264 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10265 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010266 goto exit;
10267 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010268
Neil Armstrongf983caf2022-06-15 15:27:48 +020010269 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010270 ecjpake_do_round(alg, primitive_arg, &server, &client,
10271 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010272
Gilles Peskine449bd832023-01-11 14:50:10 +010010273 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10274 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010275
10276exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010277 psa_key_derivation_abort(&server_derive);
10278 psa_key_derivation_abort(&client_derive);
10279 psa_destroy_key(key);
10280 psa_pake_abort(&server);
10281 psa_pake_abort(&client);
10282 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010283}
10284/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010285
10286/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010287void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010288{
10289 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10290 const size_t bits = 256;
10291 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010292 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010293 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010294 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010295
10296 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10297 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010298 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10299 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10300 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10301 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010302 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010303 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10304 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010305
10306 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010307 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10308 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10309 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10310 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10311 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10312 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010313
10314 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010315 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10316 PSA_PAKE_OUTPUT_MAX_SIZE);
10317 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10318 PSA_PAKE_OUTPUT_MAX_SIZE);
10319 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10320 PSA_PAKE_OUTPUT_MAX_SIZE);
10321 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10322 PSA_PAKE_INPUT_MAX_SIZE);
10323 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10324 PSA_PAKE_INPUT_MAX_SIZE);
10325 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10326 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010327}
10328/* END_CASE */