blob: 4e8853d8e44c535dfe7d257e68169389dcff9a0e [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#include "psa_crypto_core.h"
17
Gilles Peskine8e94efe2021-02-13 00:25:53 +010018#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010019#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010020#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053021#if defined(PSA_CRYPTO_DRIVER_TEST)
22#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053023#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
24#else
25#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053026#endif
Ronald Cron28a45ed2021-02-09 20:35:42 +010027
Gilles Peskine4023c012021-05-27 13:21:20 +020028/* If this comes up, it's a bug in the test code or in the test data. */
29#define UNUSED 0xdeadbeef
30
Dave Rodgman647791d2021-06-23 12:49:59 +010031/* Assert that an operation is (not) active.
32 * This serves as a proxy for checking if the operation is aborted. */
Gilles Peskine449bd832023-01-11 14:50:10 +010033#define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
34#define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010035
Przemek Stekiel7c795482022-11-15 22:26:12 +010036#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +010037int ecjpake_operation_setup(psa_pake_operation_t *operation,
38 psa_pake_cipher_suite_t *cipher_suite,
39 psa_pake_role_t role,
40 mbedtls_svc_key_id_t key,
41 size_t key_available)
Przemek Stekiel7c795482022-11-15 22:26:12 +010042{
Gilles Peskine449bd832023-01-11 14:50:10 +010043 PSA_ASSERT(psa_pake_abort(operation));
Przemek Stekiel7c795482022-11-15 22:26:12 +010044
Gilles Peskine449bd832023-01-11 14:50:10 +010045 PSA_ASSERT(psa_pake_setup(operation, cipher_suite));
Przemek Stekiel7c795482022-11-15 22:26:12 +010046
Gilles Peskine449bd832023-01-11 14:50:10 +010047 PSA_ASSERT(psa_pake_set_role(operation, role));
Przemek Stekiel7c795482022-11-15 22:26:12 +010048
Gilles Peskine449bd832023-01-11 14:50:10 +010049 if (key_available) {
50 PSA_ASSERT(psa_pake_set_password_key(operation, key));
51 }
Przemek Stekielf82effa2022-11-21 15:10:32 +010052 return 0;
Przemek Stekiel7c795482022-11-15 22:26:12 +010053exit:
Przemek Stekielf82effa2022-11-21 15:10:32 +010054 return 1;
Przemek Stekiel7c795482022-11-15 22:26:12 +010055}
56#endif
57
Jaeden Amerof24c7f82018-06-27 17:20:43 +010058/** An invalid export length that will never be set by psa_export_key(). */
59static const size_t INVALID_EXPORT_LENGTH = ~0U;
60
Gilles Peskinea7aa4422018-08-14 15:17:54 +020061/** Test if a buffer contains a constant byte value.
62 *
63 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020064 *
65 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020066 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020067 * \param size Size of the buffer in bytes.
68 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020069 * \return 1 if the buffer is all-bits-zero.
70 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020071 */
Gilles Peskine449bd832023-01-11 14:50:10 +010072static int mem_is_char(void *buffer, unsigned char c, size_t size)
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020073{
74 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010075 for (i = 0; i < size; i++) {
76 if (((unsigned char *) buffer)[i] != c) {
77 return 0;
78 }
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020079 }
Gilles Peskine449bd832023-01-11 14:50:10 +010080 return 1;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020081}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010082#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020083/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
Gilles Peskine449bd832023-01-11 14:50:10 +010084static int asn1_write_10x(unsigned char **p,
85 unsigned char *start,
86 size_t bits,
87 unsigned char x)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020088{
89 int ret;
90 int len = bits / 8 + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010091 if (bits == 0) {
92 return MBEDTLS_ERR_ASN1_INVALID_DATA;
93 }
94 if (bits <= 8 && x >= 1 << (bits - 1)) {
95 return MBEDTLS_ERR_ASN1_INVALID_DATA;
96 }
97 if (*p < start || *p - start < (ptrdiff_t) len) {
98 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
99 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200100 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 (*p)[len-1] = x;
102 if (bits % 8 == 0) {
103 (*p)[1] |= 1;
104 } else {
105 (*p)[0] |= 1 << (bits % 8);
106 }
107 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
108 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
109 MBEDTLS_ASN1_INTEGER));
110 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200111}
112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113static int construct_fake_rsa_key(unsigned char *buffer,
114 size_t buffer_size,
115 unsigned char **p,
116 size_t bits,
117 int keypair)
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200118{
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 size_t half_bits = (bits + 1) / 2;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200120 int ret;
121 int len = 0;
122 /* Construct something that looks like a DER encoding of
123 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
124 * RSAPrivateKey ::= SEQUENCE {
125 * version Version,
126 * modulus INTEGER, -- n
127 * publicExponent INTEGER, -- e
128 * privateExponent INTEGER, -- d
129 * prime1 INTEGER, -- p
130 * prime2 INTEGER, -- q
131 * exponent1 INTEGER, -- d mod (p-1)
132 * exponent2 INTEGER, -- d mod (q-1)
133 * coefficient INTEGER, -- (inverse of q) mod p
134 * otherPrimeInfos OtherPrimeInfos OPTIONAL
135 * }
136 * Or, for a public key, the same structure with only
137 * version, modulus and publicExponent.
138 */
139 *p = buffer + buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 if (keypair) {
141 MBEDTLS_ASN1_CHK_ADD(len, /* pq */
142 asn1_write_10x(p, buffer, half_bits, 1));
143 MBEDTLS_ASN1_CHK_ADD(len, /* dq */
144 asn1_write_10x(p, buffer, half_bits, 1));
145 MBEDTLS_ASN1_CHK_ADD(len, /* dp */
146 asn1_write_10x(p, buffer, half_bits, 1));
147 MBEDTLS_ASN1_CHK_ADD(len, /* q */
148 asn1_write_10x(p, buffer, half_bits, 1));
149 MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
150 asn1_write_10x(p, buffer, half_bits, 3));
151 MBEDTLS_ASN1_CHK_ADD(len, /* d */
152 asn1_write_10x(p, buffer, bits, 1));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200153 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
155 asn1_write_10x(p, buffer, 17, 1));
156 MBEDTLS_ASN1_CHK_ADD(len, /* n */
157 asn1_write_10x(p, buffer, bits, 1));
158 if (keypair) {
159 MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
160 mbedtls_asn1_write_int(p, buffer, 0));
161 }
162 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200163 {
164 const unsigned char tag =
165 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200167 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200169}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100170#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172int exercise_mac_setup(psa_key_type_t key_type,
173 const unsigned char *key_bytes,
174 size_t key_length,
175 psa_algorithm_t alg,
176 psa_mac_operation_t *operation,
177 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100178{
Ronald Cron5425a212020-08-04 14:58:35 +0200179 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200180 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
183 psa_set_key_algorithm(&attributes, alg);
184 psa_set_key_type(&attributes, key_type);
185 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 *status = psa_mac_sign_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100188 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 PSA_ASSERT(psa_mac_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100190 /* If setup failed, reproduce the failure, so that the caller can
191 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if (*status != PSA_SUCCESS) {
193 TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100194 }
195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 psa_destroy_key(key);
197 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100198
199exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 psa_destroy_key(key);
201 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100202}
203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204int exercise_cipher_setup(psa_key_type_t key_type,
205 const unsigned char *key_bytes,
206 size_t key_length,
207 psa_algorithm_t alg,
208 psa_cipher_operation_t *operation,
209 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100210{
Ronald Cron5425a212020-08-04 14:58:35 +0200211 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
215 psa_set_key_algorithm(&attributes, alg);
216 psa_set_key_type(&attributes, key_type);
217 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 *status = psa_cipher_encrypt_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100220 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 PSA_ASSERT(psa_cipher_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100222 /* If setup failed, reproduce the failure, so that the caller can
223 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if (*status != PSA_SUCCESS) {
225 TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
226 *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100227 }
228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 psa_destroy_key(key);
230 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100231
232exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 psa_destroy_key(key);
234 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100235}
236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200238{
239 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200241 uint8_t buffer[1];
242 size_t length;
243 int ok = 0;
244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 psa_set_key_id(&attributes, key_id);
246 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
247 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
248 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
249 TEST_EQUAL(psa_get_key_attributes(key, &attributes),
250 PSA_ERROR_INVALID_HANDLE);
Ronald Cronecfb2372020-07-23 17:13:42 +0200251 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
Ronald Cronecfb2372020-07-23 17:13:42 +0200253 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
255 TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
256 TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
257 TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
258 TEST_EQUAL(psa_get_key_type(&attributes), 0);
259 TEST_EQUAL(psa_get_key_bits(&attributes), 0);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
262 PSA_ERROR_INVALID_HANDLE);
263 TEST_EQUAL(psa_export_public_key(key,
264 buffer, sizeof(buffer), &length),
265 PSA_ERROR_INVALID_HANDLE);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200266
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200267 ok = 1;
268
269exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100270 /*
271 * Key attributes may have been returned by psa_get_key_attributes()
272 * thus reset them as required.
273 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 return ok;
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200277}
278
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200279/* Assert that a key isn't reported as having a slot number. */
280#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100281#define ASSERT_NO_SLOT_NUMBER(attributes) \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200282 do \
283 { \
284 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 TEST_EQUAL(psa_get_key_slot_number( \
286 attributes, \
287 &ASSERT_NO_SLOT_NUMBER_slot_number), \
288 PSA_ERROR_INVALID_ARGUMENT); \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200289 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 while (0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200291#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100292#define ASSERT_NO_SLOT_NUMBER(attributes) \
293 ((void) 0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200294#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
295
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +0530296#define INPUT_INTEGER 0x10000 /* Out of range of psa_key_type_t */
297
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100298/* An overapproximation of the amount of storage needed for a key of the
299 * given type and with the given content. The API doesn't make it easy
300 * to find a good value for the size. The current implementation doesn't
301 * care about the value anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302#define KEY_BITS_FROM_DATA(type, data) \
303 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100304
Darryl Green0c6575a2018-11-07 16:05:30 +0000305typedef enum {
306 IMPORT_KEY = 0,
307 GENERATE_KEY = 1,
308 DERIVE_KEY = 2
309} generate_method;
310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311typedef enum {
Paul Elliott33746aa2021-09-15 16:40:40 +0100312 DO_NOT_SET_LENGTHS = 0,
313 SET_LENGTHS_BEFORE_NONCE = 1,
314 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100315} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317typedef enum {
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100318 USE_NULL_TAG = 0,
319 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100320} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100321
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +0530322
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100323/*!
324 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100325 * \param key_type_arg Type of key passed in
326 * \param key_data The encryption / decryption key data
327 * \param alg_arg The type of algorithm used
328 * \param nonce Nonce data
329 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100330 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100331 * feed additional data in to be encrypted /
332 * decrypted. If -1, no chunking.
333 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100334 * \param data_part_len_arg If not -1, the length of chunks to feed
335 * the data in to be encrypted / decrypted. If
336 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100337 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100338 * expected here, this controls whether or not
339 * to set lengths, and in what order with
340 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100341 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100342 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100343 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100344 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100345 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100346 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100347static int aead_multipart_internal_func(int key_type_arg, data_t *key_data,
348 int alg_arg,
349 data_t *nonce,
350 data_t *additional_data,
351 int ad_part_len_arg,
352 data_t *input_data,
353 int data_part_len_arg,
354 set_lengths_method_t set_lengths_method,
355 data_t *expected_output,
356 int is_encrypt,
357 int do_zero_parts)
Paul Elliottd3f82412021-06-16 16:52:21 +0100358{
359 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
360 psa_key_type_t key_type = key_type_arg;
361 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100362 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100363 unsigned char *output_data = NULL;
364 unsigned char *part_data = NULL;
365 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100366 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100367 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100368 size_t output_size = 0;
369 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100370 size_t output_length = 0;
371 size_t key_bits = 0;
372 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100373 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100374 size_t part_length = 0;
375 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100376 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100377 size_t ad_part_len = 0;
378 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100379 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100380 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
381 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
382
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100383 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100384 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 PSA_ASSERT(psa_crypto_init());
Paul Elliottd3f82412021-06-16 16:52:21 +0100387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 if (is_encrypt) {
389 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
390 } else {
391 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100392 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100393
394 psa_set_key_algorithm(&attributes, alg);
395 psa_set_key_type(&attributes, key_type);
396
397 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
398 &key));
399
400 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
401 key_bits = psa_get_key_bits(&attributes);
402
403 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
404
405 if (is_encrypt) {
406 /* Tag gets written at end of buffer. */
407 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
408 (input_data->len +
409 tag_length));
410 data_true_size = input_data->len;
411 } else {
412 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
413 (input_data->len -
414 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100415
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100416 /* Do not want to attempt to decrypt tag. */
417 data_true_size = input_data->len - tag_length;
418 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100419
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100420 TEST_CALLOC(output_data, output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 if (is_encrypt) {
423 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
424 TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
425 } else {
426 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
427 TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100428 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100429
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100430 TEST_CALLOC(final_data, final_output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (is_encrypt) {
433 status = psa_aead_encrypt_setup(&operation, key, alg);
434 } else {
435 status = psa_aead_decrypt_setup(&operation, key, alg);
436 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100437
438 /* If the operation is not supported, just skip and not fail in case the
439 * encryption involves a common limitation of cryptography hardwares and
440 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 if (status == PSA_ERROR_NOT_SUPPORTED) {
442 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
443 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100444 }
445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 PSA_ASSERT(status);
Paul Elliottd3f82412021-06-16 16:52:21 +0100447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 if (set_lengths_method == DO_NOT_SET_LENGTHS) {
449 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
450 } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
451 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
452 data_true_size));
453 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
454 } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
455 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottebf91632021-07-22 17:54:42 +0100456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
458 data_true_size));
Paul Elliottd3f82412021-06-16 16:52:21 +0100459 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if (ad_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100462 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100463 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100466 part_offset < additional_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 part_offset += part_length, part_count++) {
468 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100469 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 } else if (additional_data->len - part_offset < ad_part_len) {
Paul Elliotte49fe452021-09-15 16:52:11 +0100471 part_length = additional_data->len - part_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100473 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100474 }
475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 PSA_ASSERT(psa_aead_update_ad(&operation,
477 additional_data->x + part_offset,
478 part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100479
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 } else {
Paul Elliottd3f82412021-06-16 16:52:21 +0100482 /* Pass additional data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
484 additional_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100485 }
486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 if (data_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 data_part_len = (size_t) data_part_len_arg;
490 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
491 (size_t) data_part_len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100492
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100493 TEST_CALLOC(part_data, part_data_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100496 part_offset < data_true_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 part_offset += part_length, part_count++) {
498 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100499 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 } else if ((data_true_size - part_offset) < data_part_len) {
501 part_length = (data_true_size - part_offset);
502 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100503 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100504 }
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 PSA_ASSERT(psa_aead_update(&operation,
507 (input_data->x + part_offset),
508 part_length, part_data,
509 part_data_size,
510 &output_part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 if (output_data && output_part_length) {
513 memcpy((output_data + output_length), part_data,
514 output_part_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100515 }
516
Paul Elliottd3f82412021-06-16 16:52:21 +0100517 output_length += output_part_length;
518 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 } else {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100520 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
522 data_true_size, output_data,
523 output_size, &output_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100524 }
525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if (is_encrypt) {
527 PSA_ASSERT(psa_aead_finish(&operation, final_data,
528 final_output_size,
529 &output_part_length,
530 tag_buffer, tag_length,
531 &tag_size));
532 } else {
533 PSA_ASSERT(psa_aead_verify(&operation, final_data,
534 final_output_size,
535 &output_part_length,
536 (input_data->x + data_true_size),
537 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100538 }
539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 if (output_data && output_part_length) {
541 memcpy((output_data + output_length), final_data,
542 output_part_length);
543 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100544
545 output_length += output_part_length;
546
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100547
548 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
549 * should be exact.*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if (is_encrypt) {
551 TEST_EQUAL(tag_length, tag_size);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 if (output_data && tag_length) {
554 memcpy((output_data + output_length), tag_buffer,
555 tag_length);
556 }
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100557
558 output_length += tag_length;
559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 TEST_EQUAL(output_length,
561 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
562 input_data->len));
563 TEST_LE_U(output_length,
564 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
565 } else {
566 TEST_EQUAL(output_length,
567 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
568 input_data->len));
569 TEST_LE_U(output_length,
570 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100571 }
572
Paul Elliottd3f82412021-06-16 16:52:21 +0100573
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100574 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100575 output_data, output_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100576
Paul Elliottd3f82412021-06-16 16:52:21 +0100577
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100578 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100579
580exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 psa_destroy_key(key);
582 psa_aead_abort(&operation);
583 mbedtls_free(output_data);
584 mbedtls_free(part_data);
585 mbedtls_free(final_data);
586 PSA_DONE();
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return test_ok;
Paul Elliottd3f82412021-06-16 16:52:21 +0100589}
590
Neil Armstrong4766f992022-02-28 16:23:59 +0100591/*!
592 * \brief Internal Function for MAC multipart tests.
593 * \param key_type_arg Type of key passed in
594 * \param key_data The encryption / decryption key data
595 * \param alg_arg The type of algorithm used
596 * \param input_data Data to encrypt / decrypt
597 * \param data_part_len_arg If not -1, the length of chunks to feed
598 * the data in to be encrypted / decrypted. If
599 * -1, no chunking
600 * \param expected_output Expected output
Tom Cosgrove1797b052022-12-04 17:19:59 +0000601 * \param is_verify If non-zero this is a verify operation.
Neil Armstrong4766f992022-02-28 16:23:59 +0100602 * \param do_zero_parts If non-zero, interleave zero length chunks
603 * with normal length chunks.
604 * \return int Zero on failure, non-zero on success.
605 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100606static int mac_multipart_internal_func(int key_type_arg, data_t *key_data,
607 int alg_arg,
608 data_t *input_data,
609 int data_part_len_arg,
610 data_t *expected_output,
611 int is_verify,
612 int do_zero_parts)
Neil Armstrong4766f992022-02-28 16:23:59 +0100613{
614 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
615 psa_key_type_t key_type = key_type_arg;
616 psa_algorithm_t alg = alg_arg;
617 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
618 unsigned char mac[PSA_MAC_MAX_SIZE];
619 size_t part_offset = 0;
620 size_t part_length = 0;
621 size_t data_part_len = 0;
622 size_t mac_len = 0;
623 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
624 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
625
626 int test_ok = 0;
627 size_t part_count = 0;
628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 PSA_INIT();
Neil Armstrong4766f992022-02-28 16:23:59 +0100630
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 if (is_verify) {
632 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
633 } else {
634 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
635 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100636
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 psa_set_key_algorithm(&attributes, alg);
638 psa_set_key_type(&attributes, key_type);
Neil Armstrong4766f992022-02-28 16:23:59 +0100639
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
641 &key));
Neil Armstrong4766f992022-02-28 16:23:59 +0100642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 if (is_verify) {
644 status = psa_mac_verify_setup(&operation, key, alg);
645 } else {
646 status = psa_mac_sign_setup(&operation, key, alg);
647 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100648
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 PSA_ASSERT(status);
Neil Armstrong4766f992022-02-28 16:23:59 +0100650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 if (data_part_len_arg != -1) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100652 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 data_part_len = (size_t) data_part_len_arg;
Neil Armstrong4766f992022-02-28 16:23:59 +0100654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 for (part_offset = 0, part_count = 0;
Neil Armstrong4766f992022-02-28 16:23:59 +0100656 part_offset < input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 part_offset += part_length, part_count++) {
658 if (do_zero_parts && (part_count & 0x01)) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100659 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 } else if ((input_data->len - part_offset) < data_part_len) {
661 part_length = (input_data->len - part_offset);
662 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100663 part_length = data_part_len;
664 }
665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 PSA_ASSERT(psa_mac_update(&operation,
667 (input_data->x + part_offset),
668 part_length));
Neil Armstrong4766f992022-02-28 16:23:59 +0100669 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100671 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 PSA_ASSERT(psa_mac_update(&operation, input_data->x,
673 input_data->len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100674 }
675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 if (is_verify) {
677 PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x,
678 expected_output->len));
679 } else {
680 PSA_ASSERT(psa_mac_sign_finish(&operation, mac,
681 PSA_MAC_MAX_SIZE, &mac_len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100682
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100683 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100684 mac, mac_len);
Neil Armstrong4766f992022-02-28 16:23:59 +0100685 }
686
687 test_ok = 1;
688
689exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 psa_destroy_key(key);
691 psa_mac_abort(&operation);
692 PSA_DONE();
Neil Armstrong4766f992022-02-28 16:23:59 +0100693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 return test_ok;
Neil Armstrong4766f992022-02-28 16:23:59 +0100695}
696
Neil Armstrong75673ab2022-06-15 17:39:01 +0200697#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100698static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
699 psa_pake_operation_t *server,
700 psa_pake_operation_t *client,
701 int client_input_first,
702 int round, int inject_error)
Neil Armstrongf983caf2022-06-15 15:27:48 +0200703{
704 unsigned char *buffer0 = NULL, *buffer1 = NULL;
705 size_t buffer_length = (
706 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
707 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
708 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200709 /* The output should be exactly this size according to the spec */
710 const size_t expected_size_key_share =
711 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
712 /* The output should be exactly this size according to the spec */
713 const size_t expected_size_zk_public =
714 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
715 /* The output can be smaller: the spec allows stripping leading zeroes */
716 const size_t max_expected_size_zk_proof =
717 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200718 size_t buffer0_off = 0;
719 size_t buffer1_off = 0;
720 size_t s_g1_len, s_g2_len, s_a_len;
721 size_t s_g1_off, s_g2_off, s_a_off;
722 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
723 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
724 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
725 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
726 size_t c_g1_len, c_g2_len, c_a_len;
727 size_t c_g1_off, c_g2_off, c_a_off;
728 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
729 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
730 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
731 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
732 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200733 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200734
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100735 TEST_CALLOC(buffer0, buffer_length);
736 TEST_CALLOC(buffer1, buffer_length);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 switch (round) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200739 case 1:
740 /* Server first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
742 buffer0 + buffer0_off,
743 512 - buffer0_off, &s_g1_len));
744 TEST_EQUAL(s_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200745 s_g1_off = buffer0_off;
746 buffer0_off += s_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
748 buffer0 + buffer0_off,
749 512 - buffer0_off, &s_x1_pk_len));
750 TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200751 s_x1_pk_off = buffer0_off;
752 buffer0_off += s_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
754 buffer0 + buffer0_off,
755 512 - buffer0_off, &s_x1_pr_len));
756 TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200757 s_x1_pr_off = buffer0_off;
758 buffer0_off += s_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
760 buffer0 + buffer0_off,
761 512 - buffer0_off, &s_g2_len));
762 TEST_EQUAL(s_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200763 s_g2_off = buffer0_off;
764 buffer0_off += s_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
766 buffer0 + buffer0_off,
767 512 - buffer0_off, &s_x2_pk_len));
768 TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200769 s_x2_pk_off = buffer0_off;
770 buffer0_off += s_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
772 buffer0 + buffer0_off,
773 512 - buffer0_off, &s_x2_pr_len));
774 TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200775 s_x2_pr_off = buffer0_off;
776 buffer0_off += s_x2_pr_len;
777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (inject_error == 1) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500779 buffer0[s_x1_pr_off + 8] ^= 1;
780 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200781 expected_status = PSA_ERROR_DATA_INVALID;
782 }
783
Neil Armstrong51009d72022-09-05 17:59:54 +0200784 /*
785 * When injecting errors in inputs, the implementation is
786 * free to detect it right away of with a delay.
787 * This permits delaying the error until the end of the input
788 * sequence, if no error appears then, this will be treated
789 * as an error.
790 */
791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200793 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
795 buffer0 + s_g1_off, s_g1_len);
796 if (inject_error == 1 && status != PSA_SUCCESS) {
797 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200798 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 } else {
800 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200801 }
802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
804 buffer0 + s_x1_pk_off,
805 s_x1_pk_len);
806 if (inject_error == 1 && status != PSA_SUCCESS) {
807 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200808 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 } else {
810 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200811 }
812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
814 buffer0 + s_x1_pr_off,
815 s_x1_pr_len);
816 if (inject_error == 1 && status != PSA_SUCCESS) {
817 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200818 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 } else {
820 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200821 }
822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
824 buffer0 + s_g2_off,
825 s_g2_len);
826 if (inject_error == 1 && status != PSA_SUCCESS) {
827 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200828 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 } else {
830 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200831 }
832
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
834 buffer0 + s_x2_pk_off,
835 s_x2_pk_len);
836 if (inject_error == 1 && status != PSA_SUCCESS) {
837 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200838 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 } else {
840 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200841 }
842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
844 buffer0 + s_x2_pr_off,
845 s_x2_pr_len);
846 if (inject_error == 1 && status != PSA_SUCCESS) {
847 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200848 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 } else {
850 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200851 }
852
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200853 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if (inject_error == 1) {
855 TEST_ASSERT(
856 !"One of the last psa_pake_input() calls should have returned the expected error.");
857 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200858 }
859
860 /* Client first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
862 buffer1 + buffer1_off,
863 512 - buffer1_off, &c_g1_len));
864 TEST_EQUAL(c_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200865 c_g1_off = buffer1_off;
866 buffer1_off += c_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
868 buffer1 + buffer1_off,
869 512 - buffer1_off, &c_x1_pk_len));
870 TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200871 c_x1_pk_off = buffer1_off;
872 buffer1_off += c_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
874 buffer1 + buffer1_off,
875 512 - buffer1_off, &c_x1_pr_len));
876 TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200877 c_x1_pr_off = buffer1_off;
878 buffer1_off += c_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
880 buffer1 + buffer1_off,
881 512 - buffer1_off, &c_g2_len));
882 TEST_EQUAL(c_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200883 c_g2_off = buffer1_off;
884 buffer1_off += c_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
886 buffer1 + buffer1_off,
887 512 - buffer1_off, &c_x2_pk_len));
888 TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200889 c_x2_pk_off = buffer1_off;
890 buffer1_off += c_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
892 buffer1 + buffer1_off,
893 512 - buffer1_off, &c_x2_pr_len));
894 TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200895 c_x2_pr_off = buffer1_off;
896 buffer1_off += c_x2_pr_len;
897
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200899 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
901 buffer0 + s_g1_off, s_g1_len);
902 if (inject_error == 1 && status != PSA_SUCCESS) {
903 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200904 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 } else {
906 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200907 }
908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
910 buffer0 + s_x1_pk_off,
911 s_x1_pk_len);
912 if (inject_error == 1 && status != PSA_SUCCESS) {
913 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200914 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 } else {
916 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200917 }
918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
920 buffer0 + s_x1_pr_off,
921 s_x1_pr_len);
922 if (inject_error == 1 && status != PSA_SUCCESS) {
923 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200924 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 } else {
926 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200927 }
928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
930 buffer0 + s_g2_off,
931 s_g2_len);
932 if (inject_error == 1 && status != PSA_SUCCESS) {
933 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200934 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 } else {
936 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200937 }
938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
940 buffer0 + s_x2_pk_off,
941 s_x2_pk_len);
942 if (inject_error == 1 && status != PSA_SUCCESS) {
943 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200944 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 } else {
946 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200947 }
948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
950 buffer0 + s_x2_pr_off,
951 s_x2_pr_len);
952 if (inject_error == 1 && status != PSA_SUCCESS) {
953 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200954 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 } else {
956 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200957 }
958
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200959 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 if (inject_error == 1) {
961 TEST_ASSERT(
962 !"One of the last psa_pake_input() calls should have returned the expected error.");
963 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200964 }
965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 if (inject_error == 2) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500967 buffer1[c_x1_pr_off + 12] ^= 1;
968 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200969 expected_status = PSA_ERROR_DATA_INVALID;
970 }
971
972 /* Server first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
974 buffer1 + c_g1_off, c_g1_len);
975 if (inject_error == 2 && status != PSA_SUCCESS) {
976 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200977 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 } else {
979 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200980 }
981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
983 buffer1 + c_x1_pk_off, c_x1_pk_len);
984 if (inject_error == 2 && status != PSA_SUCCESS) {
985 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200986 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 } else {
988 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200989 }
990
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
992 buffer1 + c_x1_pr_off, c_x1_pr_len);
993 if (inject_error == 2 && status != PSA_SUCCESS) {
994 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200995 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 } else {
997 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200998 }
999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1001 buffer1 + c_g2_off, c_g2_len);
1002 if (inject_error == 2 && status != PSA_SUCCESS) {
1003 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001004 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 } else {
1006 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001007 }
1008
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1010 buffer1 + c_x2_pk_off, c_x2_pk_len);
1011 if (inject_error == 2 && status != PSA_SUCCESS) {
1012 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001013 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 } else {
1015 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001016 }
1017
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1019 buffer1 + c_x2_pr_off, c_x2_pr_len);
1020 if (inject_error == 2 && status != PSA_SUCCESS) {
1021 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001022 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 } else {
1024 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001025 }
1026
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001027 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 if (inject_error == 2) {
1029 TEST_ASSERT(
1030 !"One of the last psa_pake_input() calls should have returned the expected error.");
1031 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001032
1033 break;
1034
1035 case 2:
1036 /* Server second round Output */
1037 buffer0_off = 0;
1038
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
1040 buffer0 + buffer0_off,
1041 512 - buffer0_off, &s_a_len));
1042 TEST_EQUAL(s_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001043 s_a_off = buffer0_off;
1044 buffer0_off += s_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
1046 buffer0 + buffer0_off,
1047 512 - buffer0_off, &s_x2s_pk_len));
1048 TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001049 s_x2s_pk_off = buffer0_off;
1050 buffer0_off += s_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
1052 buffer0 + buffer0_off,
1053 512 - buffer0_off, &s_x2s_pr_len));
1054 TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001055 s_x2s_pr_off = buffer0_off;
1056 buffer0_off += s_x2s_pr_len;
1057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if (inject_error == 3) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001059 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001060 expected_status = PSA_ERROR_DATA_INVALID;
1061 }
1062
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001064 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1066 buffer0 + s_a_off, s_a_len);
1067 if (inject_error == 3 && status != PSA_SUCCESS) {
1068 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001069 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 } else {
1071 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001072 }
1073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1075 buffer0 + s_x2s_pk_off,
1076 s_x2s_pk_len);
1077 if (inject_error == 3 && status != PSA_SUCCESS) {
1078 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001079 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 } else {
1081 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001082 }
1083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1085 buffer0 + s_x2s_pr_off,
1086 s_x2s_pr_len);
1087 if (inject_error == 3 && status != PSA_SUCCESS) {
1088 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001089 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 } else {
1091 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001092 }
1093
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001094 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if (inject_error == 3) {
1096 TEST_ASSERT(
1097 !"One of the last psa_pake_input() calls should have returned the expected error.");
1098 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001099 }
1100
1101 /* Client second round Output */
1102 buffer1_off = 0;
1103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
1105 buffer1 + buffer1_off,
1106 512 - buffer1_off, &c_a_len));
1107 TEST_EQUAL(c_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001108 c_a_off = buffer1_off;
1109 buffer1_off += c_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
1111 buffer1 + buffer1_off,
1112 512 - buffer1_off, &c_x2s_pk_len));
1113 TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001114 c_x2s_pk_off = buffer1_off;
1115 buffer1_off += c_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
1117 buffer1 + buffer1_off,
1118 512 - buffer1_off, &c_x2s_pr_len));
1119 TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001120 c_x2s_pr_off = buffer1_off;
1121 buffer1_off += c_x2s_pr_len;
1122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001124 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1126 buffer0 + s_a_off, s_a_len);
1127 if (inject_error == 3 && status != PSA_SUCCESS) {
1128 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001129 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 } else {
1131 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001132 }
1133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1135 buffer0 + s_x2s_pk_off,
1136 s_x2s_pk_len);
1137 if (inject_error == 3 && status != PSA_SUCCESS) {
1138 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001139 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 } else {
1141 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001142 }
1143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1145 buffer0 + s_x2s_pr_off,
1146 s_x2s_pr_len);
1147 if (inject_error == 3 && status != PSA_SUCCESS) {
1148 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001149 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 } else {
1151 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001152 }
1153
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001154 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 if (inject_error == 3) {
1156 TEST_ASSERT(
1157 !"One of the last psa_pake_input() calls should have returned the expected error.");
1158 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001159 }
1160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 if (inject_error == 4) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001162 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001163 expected_status = PSA_ERROR_DATA_INVALID;
1164 }
1165
1166 /* Server second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1168 buffer1 + c_a_off, c_a_len);
1169 if (inject_error == 4 && status != PSA_SUCCESS) {
1170 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001171 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 } else {
1173 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001174 }
1175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1177 buffer1 + c_x2s_pk_off, c_x2s_pk_len);
1178 if (inject_error == 4 && status != PSA_SUCCESS) {
1179 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001180 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 } else {
1182 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001183 }
1184
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1186 buffer1 + c_x2s_pr_off, c_x2s_pr_len);
1187 if (inject_error == 4 && status != PSA_SUCCESS) {
1188 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001189 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 } else {
1191 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001192 }
1193
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001194 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 if (inject_error == 4) {
1196 TEST_ASSERT(
1197 !"One of the last psa_pake_input() calls should have returned the expected error.");
1198 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001199
1200 break;
1201
1202 }
1203
Neil Armstrongf983caf2022-06-15 15:27:48 +02001204exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 mbedtls_free(buffer0);
1206 mbedtls_free(buffer1);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001207}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001208#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210typedef enum {
Valerio Setti1070aed2022-11-11 19:37:31 +01001211 INJECT_ERR_NONE = 0,
1212 INJECT_ERR_UNINITIALIZED_ACCESS,
1213 INJECT_ERR_DUPLICATE_SETUP,
1214 INJECT_ERR_INVALID_USER,
1215 INJECT_ERR_INVALID_PEER,
1216 INJECT_ERR_SET_USER,
1217 INJECT_ERR_SET_PEER,
1218 INJECT_EMPTY_IO_BUFFER,
1219 INJECT_UNKNOWN_STEP,
1220 INJECT_INVALID_FIRST_STEP,
1221 INJECT_WRONG_BUFFER_SIZE,
1222 INJECT_VALID_OPERATION_AFTER_FAILURE,
1223 INJECT_ANTICIPATE_KEY_DERIVATION_1,
1224 INJECT_ANTICIPATE_KEY_DERIVATION_2,
1225} ecjpake_injected_failure_t;
1226
Paul Elliott01885fa2023-02-09 12:07:30 +00001227#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott1243f932023-02-07 11:21:10 +00001228
Paul Elliott6f600372023-02-06 18:41:05 +00001229static void interruptible_signverify_get_minmax_completes(uint32_t max_ops,
1230 psa_status_t expected_status,
1231 size_t *min_completes,
1232 size_t *max_completes)
1233{
1234
1235 /* This is slightly contrived, but we only really know that with a minimum
1236 value of max_ops that a successful operation should take more than one op
1237 to complete, and likewise that with a max_ops of
1238 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */
1239 if (max_ops == 0 || max_ops == 1) {
Paul Elliottc86d45e2023-02-15 17:38:05 +00001240
Paul Elliott6f600372023-02-06 18:41:05 +00001241 if (expected_status == PSA_SUCCESS) {
1242 *min_completes = 2;
1243 } else {
1244 *min_completes = 1;
1245 }
1246
1247 *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
1248 } else {
1249 *min_completes = 1;
1250 *max_completes = 1;
1251 }
1252}
Paul Elliott01885fa2023-02-09 12:07:30 +00001253#endif /* MBEDTLS_ECP_RESTARTABLE */
Paul Elliott6f600372023-02-06 18:41:05 +00001254
Gilles Peskinee59236f2018-01-27 23:32:46 +01001255/* END_HEADER */
1256
1257/* BEGIN_DEPENDENCIES
1258 * depends_on:MBEDTLS_PSA_CRYPTO_C
1259 * END_DEPENDENCIES
1260 */
1261
1262/* BEGIN_CASE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01001263void psa_can_do_hash()
1264{
1265 /* We can't test that this is specific to drivers until partial init has
1266 * been implemented, but we can at least test before/after full init. */
1267 TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE));
1268 PSA_INIT();
1269 TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE));
1270 PSA_DONE();
1271}
1272/* END_CASE */
1273
1274/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001275void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001276{
1277 size_t max_truncated_mac_size =
1278 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1279
1280 /* Check that the length for a truncated MAC always fits in the algorithm
1281 * encoding. The shifted mask is the maximum truncated value. The
1282 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001284}
1285/* END_CASE */
1286
1287/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001288void import_with_policy(int type_arg,
1289 int usage_arg, int alg_arg,
1290 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001291{
1292 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1293 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001294 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001295 psa_key_type_t type = type_arg;
1296 psa_key_usage_t usage = usage_arg;
1297 psa_algorithm_t alg = alg_arg;
1298 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001300 psa_status_t status;
1301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 psa_set_key_type(&attributes, type);
1305 psa_set_key_usage_flags(&attributes, usage);
1306 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001307
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 status = psa_import_key(&attributes,
1309 key_material, sizeof(key_material),
1310 &key);
1311 TEST_EQUAL(status, expected_status);
1312 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001313 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001315
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1317 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1318 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1319 mbedtls_test_update_key_usage_flags(usage));
1320 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1321 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 PSA_ASSERT(psa_destroy_key(key));
1324 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001325
1326exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001327 /*
1328 * Key attributes may have been returned by psa_get_key_attributes()
1329 * thus reset them as required.
1330 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001332
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 psa_destroy_key(key);
1334 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001335}
1336/* END_CASE */
1337
1338/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001339void import_with_data(data_t *data, int type_arg,
1340 int attr_bits_arg,
1341 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001342{
1343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1344 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001345 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001346 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001347 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001348 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001349 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001352
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 psa_set_key_type(&attributes, type);
1354 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001355
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 status = psa_import_key(&attributes, data->x, data->len, &key);
Manuel Pégourié-Gonnard0509b582023-08-08 12:47:56 +02001357 /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED.
1358 *
1359 * This can happen with a type supported only by a driver:
1360 * - the driver sees the invalid data (for example wrong size) and thinks
1361 * "well perhaps this is a key size I don't support" so it returns
1362 * NOT_SUPPORTED which is correct at this point;
1363 * - we fallback to built-ins, which don't support this type, so return
1364 * NOT_SUPPORTED which again is correct at this point.
1365 */
1366 if (expected_status == PSA_ERROR_INVALID_ARGUMENT &&
1367 status == PSA_ERROR_NOT_SUPPORTED) {
1368 ; // OK
1369 } else {
1370 TEST_EQUAL(status, expected_status);
1371 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001373 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001375
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1377 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1378 if (attr_bits != 0) {
1379 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1380 }
1381 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 PSA_ASSERT(psa_destroy_key(key));
1384 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001385
1386exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001387 /*
1388 * Key attributes may have been returned by psa_get_key_attributes()
1389 * thus reset them as required.
1390 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001392
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 psa_destroy_key(key);
1394 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001395}
1396/* END_CASE */
1397
1398/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001399/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001400void import_large_key(int type_arg, int byte_size_arg,
1401 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001402{
1403 psa_key_type_t type = type_arg;
1404 size_t byte_size = byte_size_arg;
1405 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1406 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001407 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001408 psa_status_t status;
1409 uint8_t *buffer = NULL;
1410 size_t buffer_size = byte_size + 1;
1411 size_t n;
1412
Steven Cooreman69967ce2021-01-18 18:01:08 +01001413 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001414 * accommodate large keys due to heap size constraints */
Tom Cosgrove412a8132023-07-20 16:55:14 +01001415 TEST_CALLOC_OR_SKIP(buffer, buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001419
1420 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1422 psa_set_key_type(&attributes, type);
1423 status = psa_import_key(&attributes, buffer, byte_size, &key);
1424 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1425 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if (status == PSA_SUCCESS) {
1428 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1429 TEST_EQUAL(psa_get_key_type(&attributes), type);
1430 TEST_EQUAL(psa_get_key_bits(&attributes),
1431 PSA_BYTES_TO_BITS(byte_size));
1432 ASSERT_NO_SLOT_NUMBER(&attributes);
1433 memset(buffer, 0, byte_size + 1);
1434 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1435 for (n = 0; n < byte_size; n++) {
1436 TEST_EQUAL(buffer[n], 'K');
1437 }
1438 for (n = byte_size; n < buffer_size; n++) {
1439 TEST_EQUAL(buffer[n], 0);
1440 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001441 }
1442
1443exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001444 /*
1445 * Key attributes may have been returned by psa_get_key_attributes()
1446 * thus reset them as required.
1447 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001449
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 psa_destroy_key(key);
1451 PSA_DONE();
1452 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001453}
1454/* END_CASE */
1455
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001456/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001457/* Import an RSA key with a valid structure (but not valid numbers
1458 * inside, beyond having sensible size and parity). This is expected to
1459 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001460void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001461{
Ronald Cron5425a212020-08-04 14:58:35 +02001462 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001463 size_t bits = bits_arg;
1464 psa_status_t expected_status = expected_status_arg;
1465 psa_status_t status;
1466 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001467 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001468 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001470 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001471 unsigned char *p;
1472 int ret;
1473 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001474 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001475
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 PSA_ASSERT(psa_crypto_init());
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001477 TEST_CALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1480 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001481 length = ret;
1482
1483 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 psa_set_key_type(&attributes, type);
1485 status = psa_import_key(&attributes, p, length, &key);
1486 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 if (status == PSA_SUCCESS) {
1489 PSA_ASSERT(psa_destroy_key(key));
1490 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001491
1492exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 mbedtls_free(buffer);
1494 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001495}
1496/* END_CASE */
1497
1498/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001499void import_export(data_t *data,
1500 int type_arg,
1501 int usage_arg, int alg_arg,
1502 int lifetime_arg,
1503 int expected_bits,
1504 int export_size_delta,
1505 int expected_export_status_arg,
1506 /*whether reexport must give the original input exactly*/
1507 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001508{
Ronald Cron5425a212020-08-04 14:58:35 +02001509 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001510 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001511 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001512 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001513 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301514 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001515 unsigned char *exported = NULL;
1516 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001517 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001518 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001519 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001521 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001522
Moran Pekercb088e72018-07-17 17:36:59 +03001523 export_size = (ptrdiff_t) data->len + export_size_delta;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001524 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 if (!canonical_input) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001526 TEST_CALLOC(reexported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 }
1528 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 psa_set_key_lifetime(&attributes, lifetime);
1531 psa_set_key_usage_flags(&attributes, usage_arg);
1532 psa_set_key_algorithm(&attributes, alg);
1533 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001534
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001535 if (PSA_KEY_TYPE_IS_DH(type) &&
1536 expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) {
Przemek Stekiel654bef02022-12-15 13:28:02 +01001537 /* Simulate that buffer is too small, by decreasing its size by 1 byte. */
1538 export_size -= 1;
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001539 }
1540
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001541 /* Import the key */
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001542 TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key),
Przemek Stekiel2e7c33d2023-04-27 12:29:45 +02001543 PSA_SUCCESS);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001544
1545 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1547 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1548 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1549 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001550
1551 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 status = psa_export_key(key, exported, export_size, &exported_length);
1553 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001554
1555 /* The exported length must be set by psa_export_key() to a value between 0
1556 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1558 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1559 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1562 export_size - exported_length));
1563 if (status != PSA_SUCCESS) {
1564 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001565 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001566 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001567
Gilles Peskineea38a922021-02-13 00:05:16 +01001568 /* Run sanity checks on the exported key. For non-canonical inputs,
1569 * this validates the canonical representations. For canonical inputs,
1570 * this doesn't directly validate the implementation, but it still helps
1571 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 if (!psa_key_lifetime_is_external(lifetime)) {
1573 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301574 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 }
Archana4d7ae1d2021-07-07 02:50:22 +05301576 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 if (canonical_input) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001579 TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001581 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1583 &key2));
1584 PSA_ASSERT(psa_export_key(key2,
1585 reexported,
1586 export_size,
1587 &reexported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001588 TEST_MEMORY_COMPARE(exported, exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001589 reexported, reexported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001591 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 TEST_LE_U(exported_length,
1593 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1594 psa_get_key_bits(&got_attributes)));
Valerio Setti1eacae82023-07-28 16:07:03 +02001595 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
1596 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
1597 } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
1598 TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1599 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001600
1601destroy:
1602 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 PSA_ASSERT(psa_destroy_key(key));
1604 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001605
1606exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001607 /*
1608 * Key attributes may have been returned by psa_get_key_attributes()
1609 * thus reset them as required.
1610 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 psa_reset_key_attributes(&got_attributes);
1612 psa_destroy_key(key);
1613 mbedtls_free(exported);
1614 mbedtls_free(reexported);
1615 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001616}
1617/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001618
Moran Pekerf709f4a2018-06-06 17:26:04 +03001619/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001620void import_export_public_key(data_t *data,
1621 int type_arg, // key pair or public key
1622 int alg_arg,
1623 int lifetime_arg,
1624 int export_size_delta,
1625 int expected_export_status_arg,
1626 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001627{
Ronald Cron5425a212020-08-04 14:58:35 +02001628 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001629 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001630 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001631 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001632 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301633 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001634 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001635 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001636 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001637 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001638
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 psa_set_key_lifetime(&attributes, lifetime);
1642 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1643 psa_set_key_algorithm(&attributes, alg);
1644 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001645
1646 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001648
Gilles Peskine49c25912018-10-29 15:15:31 +01001649 /* Export the public key */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001650 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 status = psa_export_public_key(key,
1652 exported, export_size,
1653 &exported_length);
1654 TEST_EQUAL(status, expected_export_status);
1655 if (status == PSA_SUCCESS) {
1656 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001657 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1659 bits = psa_get_key_bits(&attributes);
1660 TEST_LE_U(expected_public_key->len,
1661 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1662 TEST_LE_U(expected_public_key->len,
1663 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1664 TEST_LE_U(expected_public_key->len,
1665 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001666 TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001667 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001668 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001669exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001670 /*
1671 * Key attributes may have been returned by psa_get_key_attributes()
1672 * thus reset them as required.
1673 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001675
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 mbedtls_free(exported);
1677 psa_destroy_key(key);
1678 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001679}
1680/* END_CASE */
1681
Gilles Peskine20035e32018-02-03 22:44:14 +01001682/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001683void import_and_exercise_key(data_t *data,
1684 int type_arg,
1685 int bits_arg,
1686 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001687{
Ronald Cron5425a212020-08-04 14:58:35 +02001688 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001689 psa_key_type_t type = type_arg;
1690 size_t bits = bits_arg;
1691 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001693 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001694 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 psa_set_key_usage_flags(&attributes, usage);
1699 psa_set_key_algorithm(&attributes, alg);
1700 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001701
1702 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001704
1705 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1707 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1708 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001709
1710 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001712 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001714
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 PSA_ASSERT(psa_destroy_key(key));
1716 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001717
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001718exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001719 /*
1720 * Key attributes may have been returned by psa_get_key_attributes()
1721 * thus reset them as required.
1722 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001724
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 psa_reset_key_attributes(&attributes);
1726 psa_destroy_key(key);
1727 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001728}
1729/* END_CASE */
1730
1731/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001732void effective_key_attributes(int type_arg, int expected_type_arg,
1733 int bits_arg, int expected_bits_arg,
1734 int usage_arg, int expected_usage_arg,
1735 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001736{
Ronald Cron5425a212020-08-04 14:58:35 +02001737 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001738 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001739 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001740 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001741 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001742 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001743 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001744 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001745 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001746 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001749
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 psa_set_key_usage_flags(&attributes, usage);
1751 psa_set_key_algorithm(&attributes, alg);
1752 psa_set_key_type(&attributes, key_type);
1753 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001754
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 PSA_ASSERT(psa_generate_key(&attributes, &key));
1756 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1759 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1760 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1761 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1762 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001763
1764exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001765 /*
1766 * Key attributes may have been returned by psa_get_key_attributes()
1767 * thus reset them as required.
1768 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001770
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 psa_destroy_key(key);
1772 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001773}
1774/* END_CASE */
1775
1776/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001777void check_key_policy(int type_arg, int bits_arg,
1778 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001779{
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1781 usage_arg,
1782 mbedtls_test_update_key_usage_flags(usage_arg),
1783 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001784 goto exit;
1785}
1786/* END_CASE */
1787
1788/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001789void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001790{
1791 /* Test each valid way of initializing the object, except for `= {0}`, as
1792 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1793 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001794 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001796 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1797 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001798
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001800
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1802 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1803 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001804
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 TEST_EQUAL(psa_get_key_type(&func), 0);
1806 TEST_EQUAL(psa_get_key_type(&init), 0);
1807 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001808
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 TEST_EQUAL(psa_get_key_bits(&func), 0);
1810 TEST_EQUAL(psa_get_key_bits(&init), 0);
1811 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001812
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1814 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1815 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001816
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1818 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1819 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001820}
1821/* END_CASE */
1822
1823/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001824void mac_key_policy(int policy_usage_arg,
1825 int policy_alg_arg,
1826 int key_type_arg,
1827 data_t *key_data,
1828 int exercise_alg_arg,
1829 int expected_status_sign_arg,
1830 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001831{
Ronald Cron5425a212020-08-04 14:58:35 +02001832 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001833 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001834 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001835 psa_key_type_t key_type = key_type_arg;
1836 psa_algorithm_t policy_alg = policy_alg_arg;
1837 psa_algorithm_t exercise_alg = exercise_alg_arg;
1838 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001839 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001840 psa_status_t expected_status_sign = expected_status_sign_arg;
1841 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001842 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001845
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 psa_set_key_usage_flags(&attributes, policy_usage);
1847 psa_set_key_algorithm(&attributes, policy_alg);
1848 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001849
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1851 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001852
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1854 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001855
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1857 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001858
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001859 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001861 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1863 input, 128,
1864 mac, PSA_MAC_MAX_SIZE, &mac_len),
1865 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001866
Neil Armstrong3af9b972022-02-07 12:20:21 +01001867 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 PSA_ASSERT(psa_mac_abort(&operation));
1869 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1870 if (status == PSA_SUCCESS) {
1871 status = psa_mac_update(&operation, input, 128);
1872 if (status == PSA_SUCCESS) {
1873 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1874 &mac_len),
1875 expected_status_sign);
1876 } else {
1877 TEST_EQUAL(status, expected_status_sign);
1878 }
1879 } else {
1880 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001881 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001883
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001884 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 status = psa_mac_verify(key, exercise_alg, input, 128,
1886 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001887
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1889 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1890 } else {
1891 TEST_EQUAL(status, expected_status_verify);
1892 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001893
Neil Armstrong3af9b972022-02-07 12:20:21 +01001894 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1896 if (status == PSA_SUCCESS) {
1897 status = psa_mac_update(&operation, input, 128);
1898 if (status == PSA_SUCCESS) {
1899 status = psa_mac_verify_finish(&operation, mac, mac_len);
1900 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1901 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1902 } else {
1903 TEST_EQUAL(status, expected_status_verify);
1904 }
1905 } else {
1906 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001907 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 } else {
1909 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001910 }
1911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001913
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 memset(mac, 0, sizeof(mac));
1915 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1916 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001917
1918exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 psa_mac_abort(&operation);
1920 psa_destroy_key(key);
1921 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001922}
1923/* END_CASE */
1924
1925/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001926void cipher_key_policy(int policy_usage_arg,
1927 int policy_alg,
1928 int key_type,
1929 data_t *key_data,
1930 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001931{
Ronald Cron5425a212020-08-04 14:58:35 +02001932 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001933 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001934 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001935 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001936 size_t output_buffer_size = 0;
1937 size_t input_buffer_size = 0;
1938 size_t output_length = 0;
1939 uint8_t *output = NULL;
1940 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001941 psa_status_t status;
1942
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1944 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1945 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001946
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001947 TEST_CALLOC(input, input_buffer_size);
1948 TEST_CALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001949
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 psa_set_key_usage_flags(&attributes, policy_usage);
1953 psa_set_key_algorithm(&attributes, policy_alg);
1954 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1957 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001958
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001959 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 TEST_EQUAL(policy_usage,
1961 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001962
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001963 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1965 output, output_buffer_size,
1966 &output_length);
1967 if (policy_alg == exercise_alg &&
1968 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1969 PSA_ASSERT(status);
1970 } else {
1971 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1972 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001973
1974 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1976 if (policy_alg == exercise_alg &&
1977 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1978 PSA_ASSERT(status);
1979 } else {
1980 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1981 }
1982 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001983
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001984 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1986 input, input_buffer_size,
1987 &output_length);
1988 if (policy_alg == exercise_alg &&
1989 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1990 PSA_ASSERT(status);
1991 } else {
1992 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1993 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001994
1995 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
1997 if (policy_alg == exercise_alg &&
1998 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1999 PSA_ASSERT(status);
2000 } else {
2001 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2002 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002003
2004exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 psa_cipher_abort(&operation);
2006 mbedtls_free(input);
2007 mbedtls_free(output);
2008 psa_destroy_key(key);
2009 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002010}
2011/* END_CASE */
2012
2013/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002014void aead_key_policy(int policy_usage_arg,
2015 int policy_alg,
2016 int key_type,
2017 data_t *key_data,
2018 int nonce_length_arg,
2019 int tag_length_arg,
2020 int exercise_alg,
2021 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002022{
Ronald Cron5425a212020-08-04 14:58:35 +02002023 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002024 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002025 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002026 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002027 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002028 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002030 size_t nonce_length = nonce_length_arg;
2031 unsigned char tag[16];
2032 size_t tag_length = tag_length_arg;
2033 size_t output_length;
2034
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 TEST_LE_U(nonce_length, sizeof(nonce));
2036 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002039
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 psa_set_key_usage_flags(&attributes, policy_usage);
2041 psa_set_key_algorithm(&attributes, policy_alg);
2042 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2045 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002046
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002047 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 TEST_EQUAL(policy_usage,
2049 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002050
Neil Armstrong752d8112022-02-07 14:51:11 +01002051 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 status = psa_aead_encrypt(key, exercise_alg,
2053 nonce, nonce_length,
2054 NULL, 0,
2055 NULL, 0,
2056 tag, tag_length,
2057 &output_length);
2058 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2059 TEST_EQUAL(status, expected_status);
2060 } else {
2061 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2062 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002063
Neil Armstrong752d8112022-02-07 14:51:11 +01002064 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2066 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2067 TEST_EQUAL(status, expected_status);
2068 } else {
2069 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2070 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002071
2072 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 memset(tag, 0, sizeof(tag));
2074 status = psa_aead_decrypt(key, exercise_alg,
2075 nonce, nonce_length,
2076 NULL, 0,
2077 tag, tag_length,
2078 NULL, 0,
2079 &output_length);
2080 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2081 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2082 } else if (expected_status == PSA_SUCCESS) {
2083 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2084 } else {
2085 TEST_EQUAL(status, expected_status);
2086 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002087
Neil Armstrong752d8112022-02-07 14:51:11 +01002088 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 PSA_ASSERT(psa_aead_abort(&operation));
2090 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2091 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2092 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2093 } else {
2094 TEST_EQUAL(status, expected_status);
2095 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002096
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002097exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 PSA_ASSERT(psa_aead_abort(&operation));
2099 psa_destroy_key(key);
2100 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002101}
2102/* END_CASE */
2103
2104/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002105void asymmetric_encryption_key_policy(int policy_usage_arg,
2106 int policy_alg,
2107 int key_type,
2108 data_t *key_data,
2109 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002110{
Ronald Cron5425a212020-08-04 14:58:35 +02002111 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002112 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002113 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002114 psa_status_t status;
2115 size_t key_bits;
2116 size_t buffer_length;
2117 unsigned char *buffer = NULL;
2118 size_t output_length;
2119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002121
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 psa_set_key_usage_flags(&attributes, policy_usage);
2123 psa_set_key_algorithm(&attributes, policy_alg);
2124 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2127 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002128
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002129 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 TEST_EQUAL(policy_usage,
2131 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2134 key_bits = psa_get_key_bits(&attributes);
2135 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2136 exercise_alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002137 TEST_CALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 status = psa_asymmetric_encrypt(key, exercise_alg,
2140 NULL, 0,
2141 NULL, 0,
2142 buffer, buffer_length,
2143 &output_length);
2144 if (policy_alg == exercise_alg &&
2145 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2146 PSA_ASSERT(status);
2147 } else {
2148 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2149 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002150
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 if (buffer_length != 0) {
2152 memset(buffer, 0, buffer_length);
2153 }
2154 status = psa_asymmetric_decrypt(key, exercise_alg,
2155 buffer, buffer_length,
2156 NULL, 0,
2157 buffer, buffer_length,
2158 &output_length);
2159 if (policy_alg == exercise_alg &&
2160 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2161 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2162 } else {
2163 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2164 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002165
2166exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002167 /*
2168 * Key attributes may have been returned by psa_get_key_attributes()
2169 * thus reset them as required.
2170 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 psa_destroy_key(key);
2174 PSA_DONE();
2175 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002176}
2177/* END_CASE */
2178
2179/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002180void asymmetric_signature_key_policy(int policy_usage_arg,
2181 int policy_alg,
2182 int key_type,
2183 data_t *key_data,
2184 int exercise_alg,
2185 int payload_length_arg,
2186 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002187{
Ronald Cron5425a212020-08-04 14:58:35 +02002188 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002189 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002190 psa_key_usage_t policy_usage = policy_usage_arg;
2191 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002192 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002194 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2195 * compatible with the policy and `payload_length_arg` is supposed to be
2196 * a valid input length to sign. If `payload_length_arg <= 0`,
2197 * `exercise_alg` is supposed to be forbidden by the policy. */
2198 int compatible_alg = payload_length_arg > 0;
2199 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002201 size_t signature_length;
2202
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002203 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002204 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 TEST_EQUAL(expected_usage,
2206 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 psa_set_key_usage_flags(&attributes, policy_usage);
2211 psa_set_key_algorithm(&attributes, policy_alg);
2212 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2215 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 status = psa_sign_hash(key, exercise_alg,
2220 payload, payload_length,
2221 signature, sizeof(signature),
2222 &signature_length);
2223 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2224 PSA_ASSERT(status);
2225 } else {
2226 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2227 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002228
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 memset(signature, 0, sizeof(signature));
2230 status = psa_verify_hash(key, exercise_alg,
2231 payload, payload_length,
2232 signature, sizeof(signature));
2233 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2234 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2235 } else {
2236 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2237 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2240 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2241 status = psa_sign_message(key, exercise_alg,
2242 payload, payload_length,
2243 signature, sizeof(signature),
2244 &signature_length);
2245 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2246 PSA_ASSERT(status);
2247 } else {
2248 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2249 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 memset(signature, 0, sizeof(signature));
2252 status = psa_verify_message(key, exercise_alg,
2253 payload, payload_length,
2254 signature, sizeof(signature));
2255 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2256 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2257 } else {
2258 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2259 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002260 }
2261
Gilles Peskined5b33222018-06-18 22:20:03 +02002262exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 psa_destroy_key(key);
2264 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002265}
2266/* END_CASE */
2267
Janos Follathba3fab92019-06-11 14:50:16 +01002268/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002269void derive_key_policy(int policy_usage,
2270 int policy_alg,
2271 int key_type,
2272 data_t *key_data,
2273 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002274{
Ronald Cron5425a212020-08-04 14:58:35 +02002275 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002276 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002277 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002278 psa_status_t status;
2279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002281
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 psa_set_key_usage_flags(&attributes, policy_usage);
2283 psa_set_key_algorithm(&attributes, policy_alg);
2284 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2287 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002290
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2292 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2293 PSA_ASSERT(psa_key_derivation_input_bytes(
2294 &operation,
2295 PSA_KEY_DERIVATION_INPUT_SEED,
2296 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002297 }
Janos Follathba3fab92019-06-11 14:50:16 +01002298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 status = psa_key_derivation_input_key(&operation,
2300 PSA_KEY_DERIVATION_INPUT_SECRET,
2301 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 if (policy_alg == exercise_alg &&
2304 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2305 PSA_ASSERT(status);
2306 } else {
2307 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2308 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002309
2310exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 psa_key_derivation_abort(&operation);
2312 psa_destroy_key(key);
2313 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002314}
2315/* END_CASE */
2316
2317/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002318void agreement_key_policy(int policy_usage,
2319 int policy_alg,
2320 int key_type_arg,
2321 data_t *key_data,
2322 int exercise_alg,
2323 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002324{
Ronald Cron5425a212020-08-04 14:58:35 +02002325 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002326 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002327 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002328 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002329 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002330 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002333
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 psa_set_key_usage_flags(&attributes, policy_usage);
2335 psa_set_key_algorithm(&attributes, policy_alg);
2336 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2339 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2342 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002345
2346exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 psa_key_derivation_abort(&operation);
2348 psa_destroy_key(key);
2349 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002350}
2351/* END_CASE */
2352
2353/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002354void key_policy_alg2(int key_type_arg, data_t *key_data,
2355 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002356{
Ronald Cron5425a212020-08-04 14:58:35 +02002357 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002358 psa_key_type_t key_type = key_type_arg;
2359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2360 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2361 psa_key_usage_t usage = usage_arg;
2362 psa_algorithm_t alg = alg_arg;
2363 psa_algorithm_t alg2 = alg2_arg;
2364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002366
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 psa_set_key_usage_flags(&attributes, usage);
2368 psa_set_key_algorithm(&attributes, alg);
2369 psa_set_key_enrollment_algorithm(&attributes, alg2);
2370 psa_set_key_type(&attributes, key_type);
2371 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2372 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002373
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002374 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 usage = mbedtls_test_update_key_usage_flags(usage);
2376 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2377 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2378 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2379 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002382 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 }
2384 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002385 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002387
2388exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002389 /*
2390 * Key attributes may have been returned by psa_get_key_attributes()
2391 * thus reset them as required.
2392 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 psa_destroy_key(key);
2396 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002397}
2398/* END_CASE */
2399
2400/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002401void raw_agreement_key_policy(int policy_usage,
2402 int policy_alg,
2403 int key_type_arg,
2404 data_t *key_data,
2405 int exercise_alg,
2406 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002407{
Ronald Cron5425a212020-08-04 14:58:35 +02002408 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002409 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002410 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002411 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002412 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002413 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 psa_set_key_usage_flags(&attributes, policy_usage);
2418 psa_set_key_algorithm(&attributes, policy_alg);
2419 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2422 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002425
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002427
2428exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 psa_key_derivation_abort(&operation);
2430 psa_destroy_key(key);
2431 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002432}
2433/* END_CASE */
2434
2435/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002436void copy_success(int source_usage_arg,
2437 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002438 int source_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 int type_arg, data_t *material,
2440 int copy_attributes,
2441 int target_usage_arg,
2442 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002443 int target_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 int expected_usage_arg,
2445 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002446{
Gilles Peskineca25db92019-04-19 11:43:08 +02002447 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2448 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002449 psa_key_usage_t expected_usage = expected_usage_arg;
2450 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002451 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302452 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2453 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002454 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2455 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002456 uint8_t *export_buffer = NULL;
2457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002459
Gilles Peskineca25db92019-04-19 11:43:08 +02002460 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2462 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2463 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2464 psa_set_key_type(&source_attributes, type_arg);
2465 psa_set_key_lifetime(&source_attributes, source_lifetime);
2466 PSA_ASSERT(psa_import_key(&source_attributes,
2467 material->x, material->len,
2468 &source_key));
2469 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002470
Gilles Peskineca25db92019-04-19 11:43:08 +02002471 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002473 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002474 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 if (target_usage_arg != -1) {
2478 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2479 }
2480 if (target_alg_arg != -1) {
2481 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2482 }
2483 if (target_alg2_arg != -1) {
2484 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2485 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002486
Archana8a180362021-07-05 02:18:48 +05302487
Gilles Peskine57ab7212019-01-28 13:03:09 +01002488 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 PSA_ASSERT(psa_copy_key(source_key,
2490 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002491
2492 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002494
2495 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2497 TEST_EQUAL(psa_get_key_type(&source_attributes),
2498 psa_get_key_type(&target_attributes));
2499 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2500 psa_get_key_bits(&target_attributes));
2501 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2502 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2503 TEST_EQUAL(expected_alg2,
2504 psa_get_key_enrollment_algorithm(&target_attributes));
2505 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002506 size_t length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002507 TEST_CALLOC(export_buffer, material->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2509 material->len, &length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002510 TEST_MEMORY_COMPARE(material->x, material->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002511 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002512 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002513
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 if (!psa_key_lifetime_is_external(target_lifetime)) {
2515 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302516 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 }
2518 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302519 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 }
Archana8a180362021-07-05 02:18:48 +05302521 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002522
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002524
2525exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002526 /*
2527 * Source and target key attributes may have been returned by
2528 * psa_get_key_attributes() thus reset them as required.
2529 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 psa_reset_key_attributes(&source_attributes);
2531 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002532
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 PSA_DONE();
2534 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002535}
2536/* END_CASE */
2537
2538/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002539void copy_fail(int source_usage_arg,
2540 int source_alg_arg, int source_alg2_arg,
2541 int source_lifetime_arg,
2542 int type_arg, data_t *material,
2543 int target_type_arg, int target_bits_arg,
2544 int target_usage_arg,
2545 int target_alg_arg, int target_alg2_arg,
2546 int target_id_arg, int target_lifetime_arg,
2547 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002548{
2549 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2550 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002551 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2552 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002556
2557 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2559 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2560 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2561 psa_set_key_type(&source_attributes, type_arg);
2562 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2563 PSA_ASSERT(psa_import_key(&source_attributes,
2564 material->x, material->len,
2565 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002566
2567 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 psa_set_key_id(&target_attributes, key_id);
2569 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2570 psa_set_key_type(&target_attributes, target_type_arg);
2571 psa_set_key_bits(&target_attributes, target_bits_arg);
2572 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2573 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2574 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002575
2576 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 TEST_EQUAL(psa_copy_key(source_key,
2578 &target_attributes, &target_key),
2579 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002580
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002582
Gilles Peskine4a644642019-05-03 17:14:08 +02002583exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 psa_reset_key_attributes(&source_attributes);
2585 psa_reset_key_attributes(&target_attributes);
2586 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002587}
2588/* END_CASE */
2589
2590/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002591void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002592{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002593 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002594 /* Test each valid way of initializing the object, except for `= {0}`, as
2595 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2596 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002597 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002599 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2600 psa_hash_operation_t zero;
2601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002603
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002604 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2606 PSA_ERROR_BAD_STATE);
2607 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2608 PSA_ERROR_BAD_STATE);
2609 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2610 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002611
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002612 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 PSA_ASSERT(psa_hash_abort(&func));
2614 PSA_ASSERT(psa_hash_abort(&init));
2615 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002616}
2617/* END_CASE */
2618
2619/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002620void hash_setup(int alg_arg,
2621 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002622{
2623 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002624 uint8_t *output = NULL;
2625 size_t output_size = 0;
2626 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002627 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002628 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002629 psa_status_t status;
2630
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002632
Neil Armstrongedb20862022-02-07 15:47:44 +01002633 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 output_size = PSA_HASH_LENGTH(alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002635 TEST_CALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 status = psa_hash_compute(alg, NULL, 0,
2638 output, output_size, &output_length);
2639 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002640
2641 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 status = psa_hash_setup(&operation, alg);
2643 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002644
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002645 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002647
2648 /* If setup failed, reproduce the failure, so as to
2649 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 if (status != PSA_SUCCESS) {
2651 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2652 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002653
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002654 /* Now the operation object should be reusable. */
2655#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2657 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002658#endif
2659
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002660exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 mbedtls_free(output);
2662 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002663}
2664/* END_CASE */
2665
2666/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002667void hash_compute_fail(int alg_arg, data_t *input,
2668 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002669{
2670 psa_algorithm_t alg = alg_arg;
2671 uint8_t *output = NULL;
2672 size_t output_size = output_size_arg;
2673 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002674 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002675 psa_status_t expected_status = expected_status_arg;
2676 psa_status_t status;
2677
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002678 TEST_CALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002679
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002681
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002682 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 status = psa_hash_compute(alg, input->x, input->len,
2684 output, output_size, &output_length);
2685 TEST_EQUAL(status, expected_status);
2686 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002687
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002688 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 status = psa_hash_setup(&operation, alg);
2690 if (status == PSA_SUCCESS) {
2691 status = psa_hash_update(&operation, input->x, input->len);
2692 if (status == PSA_SUCCESS) {
2693 status = psa_hash_finish(&operation, output, output_size,
2694 &output_length);
2695 if (status == PSA_SUCCESS) {
2696 TEST_LE_U(output_length, output_size);
2697 } else {
2698 TEST_EQUAL(status, expected_status);
2699 }
2700 } else {
2701 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002702 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 } else {
2704 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002705 }
2706
Gilles Peskine0a749c82019-11-28 19:33:58 +01002707exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 PSA_ASSERT(psa_hash_abort(&operation));
2709 mbedtls_free(output);
2710 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002711}
2712/* END_CASE */
2713
2714/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002715void hash_compare_fail(int alg_arg, data_t *input,
2716 data_t *reference_hash,
2717 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002718{
2719 psa_algorithm_t alg = alg_arg;
2720 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002721 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002722 psa_status_t status;
2723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002725
Neil Armstrong55a1be12022-02-07 11:23:20 +01002726 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002727 status = psa_hash_compare(alg, input->x, input->len,
2728 reference_hash->x, reference_hash->len);
2729 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002730
Neil Armstrong55a1be12022-02-07 11:23:20 +01002731 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 status = psa_hash_setup(&operation, alg);
2733 if (status == PSA_SUCCESS) {
2734 status = psa_hash_update(&operation, input->x, input->len);
2735 if (status == PSA_SUCCESS) {
2736 status = psa_hash_verify(&operation, reference_hash->x,
2737 reference_hash->len);
2738 TEST_EQUAL(status, expected_status);
2739 } else {
2740 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002741 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 } else {
2743 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002744 }
2745
Gilles Peskine88e08462020-01-28 20:43:00 +01002746exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 PSA_ASSERT(psa_hash_abort(&operation));
2748 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002749}
2750/* END_CASE */
2751
2752/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002753void hash_compute_compare(int alg_arg, data_t *input,
2754 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002755{
2756 psa_algorithm_t alg = alg_arg;
2757 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2758 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002759 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002760 size_t i;
2761
Gilles Peskine449bd832023-01-11 14:50:10 +01002762 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002763
Neil Armstrongca30a002022-02-07 11:40:23 +01002764 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2766 output, PSA_HASH_LENGTH(alg),
2767 &output_length));
2768 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002769 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002770 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002771
Neil Armstrongca30a002022-02-07 11:40:23 +01002772 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 PSA_ASSERT(psa_hash_setup(&operation, alg));
2774 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2775 PSA_ASSERT(psa_hash_finish(&operation, output,
2776 PSA_HASH_LENGTH(alg),
2777 &output_length));
2778 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002779 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002780 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002781
2782 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2784 output, sizeof(output),
2785 &output_length));
2786 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002787 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002788 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002789
Neil Armstrongca30a002022-02-07 11:40:23 +01002790 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 PSA_ASSERT(psa_hash_setup(&operation, alg));
2792 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2793 PSA_ASSERT(psa_hash_finish(&operation, output,
2794 sizeof(output), &output_length));
2795 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002796 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002797 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002798
2799 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2801 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002802
Neil Armstrongca30a002022-02-07 11:40:23 +01002803 /* Compare with correct hash, 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_verify(&operation, output,
2807 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002808
2809 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2811 output, output_length + 1),
2812 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002813
Neil Armstrongca30a002022-02-07 11:40:23 +01002814 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 PSA_ASSERT(psa_hash_setup(&operation, alg));
2816 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2817 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2818 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002819
2820 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2822 output, output_length - 1),
2823 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002824
Neil Armstrongca30a002022-02-07 11:40:23 +01002825 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002826 PSA_ASSERT(psa_hash_setup(&operation, alg));
2827 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2828 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2829 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002830
Gilles Peskine0a749c82019-11-28 19:33:58 +01002831 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002832 for (i = 0; i < output_length; i++) {
2833 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002834 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002835
2836 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2838 output, output_length),
2839 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002840
2841 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 PSA_ASSERT(psa_hash_setup(&operation, alg));
2843 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2844 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2845 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002846
Gilles Peskine0a749c82019-11-28 19:33:58 +01002847 output[i] ^= 1;
2848 }
2849
2850exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 PSA_ASSERT(psa_hash_abort(&operation));
2852 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002853}
2854/* END_CASE */
2855
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002856/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002857void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002858{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002859 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002860 unsigned char input[] = "";
2861 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002862 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002863 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2864 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2866 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002867 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002868 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002869 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002870
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002872
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002873 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 PSA_ASSERT(psa_hash_setup(&operation, alg));
2875 ASSERT_OPERATION_IS_ACTIVE(operation);
2876 TEST_EQUAL(psa_hash_setup(&operation, alg),
2877 PSA_ERROR_BAD_STATE);
2878 ASSERT_OPERATION_IS_INACTIVE(operation);
2879 PSA_ASSERT(psa_hash_abort(&operation));
2880 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002881
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002882 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2884 PSA_ERROR_BAD_STATE);
2885 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002886
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002887 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002889 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 ASSERT_OPERATION_IS_ACTIVE(operation);
2891 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2892 PSA_ERROR_BAD_STATE);
2893 ASSERT_OPERATION_IS_INACTIVE(operation);
2894 PSA_ASSERT(psa_hash_abort(&operation));
2895 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002896
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002897 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 PSA_ASSERT(psa_hash_setup(&operation, alg));
2899 PSA_ASSERT(psa_hash_finish(&operation,
2900 hash, sizeof(hash), &hash_len));
2901 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2902 PSA_ERROR_BAD_STATE);
2903 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002904
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002905 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 TEST_EQUAL(psa_hash_verify(&operation,
2907 valid_hash, sizeof(valid_hash)),
2908 PSA_ERROR_BAD_STATE);
2909 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002910
2911 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 PSA_ASSERT(psa_hash_setup(&operation, alg));
2913 PSA_ASSERT(psa_hash_finish(&operation,
2914 hash, sizeof(hash), &hash_len));
2915 TEST_EQUAL(psa_hash_verify(&operation,
2916 valid_hash, sizeof(valid_hash)),
2917 PSA_ERROR_BAD_STATE);
2918 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002919
2920 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 PSA_ASSERT(psa_hash_setup(&operation, alg));
2922 ASSERT_OPERATION_IS_ACTIVE(operation);
2923 PSA_ASSERT(psa_hash_verify(&operation,
2924 valid_hash, sizeof(valid_hash)));
2925 ASSERT_OPERATION_IS_INACTIVE(operation);
2926 TEST_EQUAL(psa_hash_verify(&operation,
2927 valid_hash, sizeof(valid_hash)),
2928 PSA_ERROR_BAD_STATE);
2929 ASSERT_OPERATION_IS_INACTIVE(operation);
2930 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002931
2932 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 TEST_EQUAL(psa_hash_finish(&operation,
2934 hash, sizeof(hash), &hash_len),
2935 PSA_ERROR_BAD_STATE);
2936 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002937
2938 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 PSA_ASSERT(psa_hash_setup(&operation, alg));
2940 PSA_ASSERT(psa_hash_finish(&operation,
2941 hash, sizeof(hash), &hash_len));
2942 TEST_EQUAL(psa_hash_finish(&operation,
2943 hash, sizeof(hash), &hash_len),
2944 PSA_ERROR_BAD_STATE);
2945 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002946
2947 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 PSA_ASSERT(psa_hash_setup(&operation, alg));
2949 PSA_ASSERT(psa_hash_verify(&operation,
2950 valid_hash, sizeof(valid_hash)));
2951 TEST_EQUAL(psa_hash_finish(&operation,
2952 hash, sizeof(hash), &hash_len),
2953 PSA_ERROR_BAD_STATE);
2954 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002955
2956exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002958}
2959/* END_CASE */
2960
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002961/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002962void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002963{
2964 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002965 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2966 * appended to it */
2967 unsigned char hash[] = {
2968 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2969 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2971 };
2972 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002973 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002974
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002976
itayzafrir27e69452018-11-01 14:26:34 +02002977 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 PSA_ASSERT(psa_hash_setup(&operation, alg));
2979 ASSERT_OPERATION_IS_ACTIVE(operation);
2980 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2981 PSA_ERROR_INVALID_SIGNATURE);
2982 ASSERT_OPERATION_IS_INACTIVE(operation);
2983 PSA_ASSERT(psa_hash_abort(&operation));
2984 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002985
itayzafrir27e69452018-11-01 14:26:34 +02002986 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 PSA_ASSERT(psa_hash_setup(&operation, alg));
2988 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
2989 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03002990
itayzafrir27e69452018-11-01 14:26:34 +02002991 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 PSA_ASSERT(psa_hash_setup(&operation, alg));
2993 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
2994 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03002995
itayzafrirec93d302018-10-18 18:01:10 +03002996exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03002998}
2999/* END_CASE */
3000
Ronald Cronee414c72021-03-18 18:50:08 +01003001/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003002void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03003003{
3004 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003005 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003007 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003008 size_t hash_len;
3009
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03003011
itayzafrir58028322018-10-25 10:22:01 +03003012 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 PSA_ASSERT(psa_hash_setup(&operation, alg));
3014 TEST_EQUAL(psa_hash_finish(&operation,
3015 hash, expected_size - 1, &hash_len),
3016 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003017
3018exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003020}
3021/* END_CASE */
3022
Ronald Cronee414c72021-03-18 18:50:08 +01003023/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003024void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003025{
3026 psa_algorithm_t alg = PSA_ALG_SHA_256;
3027 unsigned char hash[PSA_HASH_MAX_SIZE];
3028 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3029 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3030 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3031 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3032 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3033 size_t hash_len;
3034
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 PSA_ASSERT(psa_crypto_init());
3036 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3039 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3040 PSA_ASSERT(psa_hash_finish(&op_finished,
3041 hash, sizeof(hash), &hash_len));
3042 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3043 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003044
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3046 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003047
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3049 PSA_ASSERT(psa_hash_finish(&op_init,
3050 hash, sizeof(hash), &hash_len));
3051 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3052 PSA_ASSERT(psa_hash_finish(&op_finished,
3053 hash, sizeof(hash), &hash_len));
3054 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3055 PSA_ASSERT(psa_hash_finish(&op_aborted,
3056 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003057
3058exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 psa_hash_abort(&op_source);
3060 psa_hash_abort(&op_init);
3061 psa_hash_abort(&op_setup);
3062 psa_hash_abort(&op_finished);
3063 psa_hash_abort(&op_aborted);
3064 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003065}
3066/* END_CASE */
3067
Ronald Cronee414c72021-03-18 18:50:08 +01003068/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003069void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003070{
3071 psa_algorithm_t alg = PSA_ALG_SHA_256;
3072 unsigned char hash[PSA_HASH_MAX_SIZE];
3073 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3074 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3075 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3076 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3077 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3078 size_t hash_len;
3079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003081
Gilles Peskine449bd832023-01-11 14:50:10 +01003082 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3083 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3084 PSA_ASSERT(psa_hash_finish(&op_finished,
3085 hash, sizeof(hash), &hash_len));
3086 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3087 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003088
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3090 PSA_ASSERT(psa_hash_finish(&op_target,
3091 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003092
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3094 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3095 PSA_ERROR_BAD_STATE);
3096 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3097 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003098
3099exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 psa_hash_abort(&op_target);
3101 psa_hash_abort(&op_init);
3102 psa_hash_abort(&op_setup);
3103 psa_hash_abort(&op_finished);
3104 psa_hash_abort(&op_aborted);
3105 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003106}
3107/* END_CASE */
3108
itayzafrir58028322018-10-25 10:22:01 +03003109/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003110void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003111{
Jaeden Amero252ef282019-02-15 14:05:35 +00003112 const uint8_t input[1] = { 0 };
3113
Jaeden Amero769ce272019-01-04 11:48:03 +00003114 /* Test each valid way of initializing the object, except for `= {0}`, as
3115 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3116 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003117 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003119 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3120 psa_mac_operation_t zero;
3121
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003123
Jaeden Amero252ef282019-02-15 14:05:35 +00003124 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 TEST_EQUAL(psa_mac_update(&func,
3126 input, sizeof(input)),
3127 PSA_ERROR_BAD_STATE);
3128 TEST_EQUAL(psa_mac_update(&init,
3129 input, sizeof(input)),
3130 PSA_ERROR_BAD_STATE);
3131 TEST_EQUAL(psa_mac_update(&zero,
3132 input, sizeof(input)),
3133 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003134
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003135 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 PSA_ASSERT(psa_mac_abort(&func));
3137 PSA_ASSERT(psa_mac_abort(&init));
3138 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003139}
3140/* END_CASE */
3141
3142/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003143void mac_setup(int key_type_arg,
3144 data_t *key,
3145 int alg_arg,
3146 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003147{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003148 psa_key_type_t key_type = key_type_arg;
3149 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003150 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003151 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003152 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3153#if defined(KNOWN_SUPPORTED_MAC_ALG)
3154 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3155#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003156
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003158
Gilles Peskine449bd832023-01-11 14:50:10 +01003159 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3160 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003161 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 }
3163 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003164
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003165 /* The operation object should be reusable. */
3166#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3168 smoke_test_key_data,
3169 sizeof(smoke_test_key_data),
3170 KNOWN_SUPPORTED_MAC_ALG,
3171 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003172 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003173 }
3174 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003175#endif
3176
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003177exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003179}
3180/* END_CASE */
3181
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003182/* 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 +01003183void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003184{
Ronald Cron5425a212020-08-04 14:58:35 +02003185 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003186 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3187 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003188 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003189 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3190 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3192 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003193 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003194 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3195 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3196 size_t sign_mac_length = 0;
3197 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3198 const uint8_t verify_mac[] = {
3199 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3200 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3202 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 PSA_ASSERT(psa_crypto_init());
3205 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3206 psa_set_key_algorithm(&attributes, alg);
3207 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003208
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3210 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003211
Jaeden Amero252ef282019-02-15 14:05:35 +00003212 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003213 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3214 PSA_ERROR_BAD_STATE);
3215 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003216
3217 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003218 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3219 &sign_mac_length),
3220 PSA_ERROR_BAD_STATE);
3221 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003222
3223 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 TEST_EQUAL(psa_mac_verify_finish(&operation,
3225 verify_mac, sizeof(verify_mac)),
3226 PSA_ERROR_BAD_STATE);
3227 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003228
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003229 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003230 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3231 ASSERT_OPERATION_IS_ACTIVE(operation);
3232 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3233 PSA_ERROR_BAD_STATE);
3234 ASSERT_OPERATION_IS_INACTIVE(operation);
3235 PSA_ASSERT(psa_mac_abort(&operation));
3236 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003237
Jaeden Amero252ef282019-02-15 14:05:35 +00003238 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3240 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3241 PSA_ASSERT(psa_mac_sign_finish(&operation,
3242 sign_mac, sizeof(sign_mac),
3243 &sign_mac_length));
3244 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3245 PSA_ERROR_BAD_STATE);
3246 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003247
3248 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003249 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3250 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3251 PSA_ASSERT(psa_mac_verify_finish(&operation,
3252 verify_mac, sizeof(verify_mac)));
3253 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3254 PSA_ERROR_BAD_STATE);
3255 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003256
3257 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003258 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3259 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3260 PSA_ASSERT(psa_mac_sign_finish(&operation,
3261 sign_mac, sizeof(sign_mac),
3262 &sign_mac_length));
3263 TEST_EQUAL(psa_mac_sign_finish(&operation,
3264 sign_mac, sizeof(sign_mac),
3265 &sign_mac_length),
3266 PSA_ERROR_BAD_STATE);
3267 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003268
3269 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3271 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3272 PSA_ASSERT(psa_mac_verify_finish(&operation,
3273 verify_mac, sizeof(verify_mac)));
3274 TEST_EQUAL(psa_mac_verify_finish(&operation,
3275 verify_mac, sizeof(verify_mac)),
3276 PSA_ERROR_BAD_STATE);
3277 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003278
3279 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003280 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3281 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3282 ASSERT_OPERATION_IS_ACTIVE(operation);
3283 TEST_EQUAL(psa_mac_verify_finish(&operation,
3284 verify_mac, sizeof(verify_mac)),
3285 PSA_ERROR_BAD_STATE);
3286 ASSERT_OPERATION_IS_INACTIVE(operation);
3287 PSA_ASSERT(psa_mac_abort(&operation));
3288 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003289
3290 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003291 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3292 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3293 ASSERT_OPERATION_IS_ACTIVE(operation);
3294 TEST_EQUAL(psa_mac_sign_finish(&operation,
3295 sign_mac, sizeof(sign_mac),
3296 &sign_mac_length),
3297 PSA_ERROR_BAD_STATE);
3298 ASSERT_OPERATION_IS_INACTIVE(operation);
3299 PSA_ASSERT(psa_mac_abort(&operation));
3300 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003301
Gilles Peskine449bd832023-01-11 14:50:10 +01003302 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003303
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003304exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003306}
3307/* END_CASE */
3308
3309/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003310void mac_sign_verify_multi(int key_type_arg,
3311 data_t *key_data,
3312 int alg_arg,
3313 data_t *input,
3314 int is_verify,
3315 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003316{
3317 size_t data_part_len = 0;
3318
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003320 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003322
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 if (mac_multipart_internal_func(key_type_arg, key_data,
3324 alg_arg,
3325 input, data_part_len,
3326 expected_mac,
3327 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003328 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003329 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003330
3331 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003333
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 if (mac_multipart_internal_func(key_type_arg, key_data,
3335 alg_arg,
3336 input, data_part_len,
3337 expected_mac,
3338 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003339 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003340 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003341 }
3342
3343 /* Goto is required to silence warnings about unused labels, as we
3344 * don't actually do any test assertions in this function. */
3345 goto exit;
3346}
3347/* END_CASE */
3348
3349/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003350void mac_sign(int key_type_arg,
3351 data_t *key_data,
3352 int alg_arg,
3353 data_t *input,
3354 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003355{
Ronald Cron5425a212020-08-04 14:58:35 +02003356 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003357 psa_key_type_t key_type = key_type_arg;
3358 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003359 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003360 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003361 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003362 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003364 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003365 const size_t output_sizes_to_test[] = {
3366 0,
3367 1,
3368 expected_mac->len - 1,
3369 expected_mac->len,
3370 expected_mac->len + 1,
3371 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003372
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003374 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003375 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003376
Gilles Peskine449bd832023-01-11 14:50:10 +01003377 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003378
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3380 psa_set_key_algorithm(&attributes, alg);
3381 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003382
Gilles Peskine449bd832023-01-11 14:50:10 +01003383 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3384 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003385
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003387 const size_t output_size = output_sizes_to_test[i];
3388 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 (output_size >= expected_mac->len ? PSA_SUCCESS :
3390 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003391
Gilles Peskine449bd832023-01-11 14:50:10 +01003392 mbedtls_test_set_step(output_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003393 TEST_CALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003394
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003395 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003396 TEST_EQUAL(psa_mac_compute(key, alg,
3397 input->x, input->len,
3398 actual_mac, output_size, &mac_length),
3399 expected_status);
3400 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003401 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003402 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003403 }
3404
Gilles Peskine449bd832023-01-11 14:50:10 +01003405 if (output_size > 0) {
3406 memset(actual_mac, 0, output_size);
3407 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003408
3409 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003410 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3411 PSA_ASSERT(psa_mac_update(&operation,
3412 input->x, input->len));
3413 TEST_EQUAL(psa_mac_sign_finish(&operation,
3414 actual_mac, output_size,
3415 &mac_length),
3416 expected_status);
3417 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003418
Gilles Peskine449bd832023-01-11 14:50:10 +01003419 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003420 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003421 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003422 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003423 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003424 actual_mac = NULL;
3425 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003426
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003427exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 psa_mac_abort(&operation);
3429 psa_destroy_key(key);
3430 PSA_DONE();
3431 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003432}
3433/* END_CASE */
3434
3435/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003436void mac_verify(int key_type_arg,
3437 data_t *key_data,
3438 int alg_arg,
3439 data_t *input,
3440 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003441{
Ronald Cron5425a212020-08-04 14:58:35 +02003442 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003443 psa_key_type_t key_type = key_type_arg;
3444 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003445 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003446 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003447 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003448
Gilles Peskine449bd832023-01-11 14:50:10 +01003449 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003450
Gilles Peskine449bd832023-01-11 14:50:10 +01003451 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003452
Gilles Peskine449bd832023-01-11 14:50:10 +01003453 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3454 psa_set_key_algorithm(&attributes, alg);
3455 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003456
Gilles Peskine449bd832023-01-11 14:50:10 +01003457 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3458 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003459
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003460 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3462 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003463
3464 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003465 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3466 PSA_ASSERT(psa_mac_update(&operation,
3467 input->x, input->len));
3468 PSA_ASSERT(psa_mac_verify_finish(&operation,
3469 expected_mac->x,
3470 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003471
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003472 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003473 TEST_EQUAL(psa_mac_verify(key, alg,
3474 input->x, input->len,
3475 expected_mac->x,
3476 expected_mac->len - 1),
3477 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003478
3479 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3481 PSA_ASSERT(psa_mac_update(&operation,
3482 input->x, input->len));
3483 TEST_EQUAL(psa_mac_verify_finish(&operation,
3484 expected_mac->x,
3485 expected_mac->len - 1),
3486 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003487
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003488 /* Test a MAC that's too long, one-shot case. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003489 TEST_CALLOC(perturbed_mac, expected_mac->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01003490 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3491 TEST_EQUAL(psa_mac_verify(key, alg,
3492 input->x, input->len,
3493 perturbed_mac, expected_mac->len + 1),
3494 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003495
3496 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003497 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3498 PSA_ASSERT(psa_mac_update(&operation,
3499 input->x, input->len));
3500 TEST_EQUAL(psa_mac_verify_finish(&operation,
3501 perturbed_mac,
3502 expected_mac->len + 1),
3503 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003504
3505 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003506 for (size_t i = 0; i < expected_mac->len; i++) {
3507 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003508 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003509
Gilles Peskine449bd832023-01-11 14:50:10 +01003510 TEST_EQUAL(psa_mac_verify(key, alg,
3511 input->x, input->len,
3512 perturbed_mac, expected_mac->len),
3513 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003514
Gilles Peskine449bd832023-01-11 14:50:10 +01003515 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3516 PSA_ASSERT(psa_mac_update(&operation,
3517 input->x, input->len));
3518 TEST_EQUAL(psa_mac_verify_finish(&operation,
3519 perturbed_mac,
3520 expected_mac->len),
3521 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003522 perturbed_mac[i] ^= 1;
3523 }
3524
Gilles Peskine8c9def32018-02-08 10:02:12 +01003525exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003526 psa_mac_abort(&operation);
3527 psa_destroy_key(key);
3528 PSA_DONE();
3529 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003530}
3531/* END_CASE */
3532
3533/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003534void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003535{
Jaeden Ameroab439972019-02-15 14:12:05 +00003536 const uint8_t input[1] = { 0 };
3537 unsigned char output[1] = { 0 };
3538 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003539 /* Test each valid way of initializing the object, except for `= {0}`, as
3540 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3541 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003542 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003543 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003544 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3545 psa_cipher_operation_t zero;
3546
Gilles Peskine449bd832023-01-11 14:50:10 +01003547 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003548
Jaeden Ameroab439972019-02-15 14:12:05 +00003549 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003550 TEST_EQUAL(psa_cipher_update(&func,
3551 input, sizeof(input),
3552 output, sizeof(output),
3553 &output_length),
3554 PSA_ERROR_BAD_STATE);
3555 TEST_EQUAL(psa_cipher_update(&init,
3556 input, sizeof(input),
3557 output, sizeof(output),
3558 &output_length),
3559 PSA_ERROR_BAD_STATE);
3560 TEST_EQUAL(psa_cipher_update(&zero,
3561 input, sizeof(input),
3562 output, sizeof(output),
3563 &output_length),
3564 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003565
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003566 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003567 PSA_ASSERT(psa_cipher_abort(&func));
3568 PSA_ASSERT(psa_cipher_abort(&init));
3569 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003570}
3571/* END_CASE */
3572
3573/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003574void cipher_setup(int key_type_arg,
3575 data_t *key,
3576 int alg_arg,
3577 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003578{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003579 psa_key_type_t key_type = key_type_arg;
3580 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003581 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003582 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003583 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003584#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003585 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3586#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003587
Gilles Peskine449bd832023-01-11 14:50:10 +01003588 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003589
Gilles Peskine449bd832023-01-11 14:50:10 +01003590 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3591 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003592 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003593 }
3594 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003595
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003596 /* The operation object should be reusable. */
3597#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003598 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3599 smoke_test_key_data,
3600 sizeof(smoke_test_key_data),
3601 KNOWN_SUPPORTED_CIPHER_ALG,
3602 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003603 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003604 }
3605 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003606#endif
3607
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003608exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003609 psa_cipher_abort(&operation);
3610 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003611}
3612/* END_CASE */
3613
Ronald Cronee414c72021-03-18 18:50:08 +01003614/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003615void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003616{
Ronald Cron5425a212020-08-04 14:58:35 +02003617 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003618 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3619 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003620 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003621 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003622 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003623 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003624 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003625 0xaa, 0xaa, 0xaa, 0xaa
3626 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003627 const uint8_t text[] = {
3628 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003629 0xbb, 0xbb, 0xbb, 0xbb
3630 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003631 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003632 size_t length = 0;
3633
Gilles Peskine449bd832023-01-11 14:50:10 +01003634 PSA_ASSERT(psa_crypto_init());
3635 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3636 psa_set_key_algorithm(&attributes, alg);
3637 psa_set_key_type(&attributes, key_type);
3638 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3639 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003640
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003641 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003642 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3643 ASSERT_OPERATION_IS_ACTIVE(operation);
3644 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3645 PSA_ERROR_BAD_STATE);
3646 ASSERT_OPERATION_IS_INACTIVE(operation);
3647 PSA_ASSERT(psa_cipher_abort(&operation));
3648 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003649
3650 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003651 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3652 ASSERT_OPERATION_IS_ACTIVE(operation);
3653 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3654 PSA_ERROR_BAD_STATE);
3655 ASSERT_OPERATION_IS_INACTIVE(operation);
3656 PSA_ASSERT(psa_cipher_abort(&operation));
3657 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003658
Jaeden Ameroab439972019-02-15 14:12:05 +00003659 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003660 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3661 buffer, sizeof(buffer),
3662 &length),
3663 PSA_ERROR_BAD_STATE);
3664 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003665
3666 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003667 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3668 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3669 buffer, sizeof(buffer),
3670 &length));
3671 ASSERT_OPERATION_IS_ACTIVE(operation);
3672 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3673 buffer, sizeof(buffer),
3674 &length),
3675 PSA_ERROR_BAD_STATE);
3676 ASSERT_OPERATION_IS_INACTIVE(operation);
3677 PSA_ASSERT(psa_cipher_abort(&operation));
3678 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003679
3680 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003681 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3682 PSA_ASSERT(psa_cipher_set_iv(&operation,
3683 iv, sizeof(iv)));
3684 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3685 buffer, sizeof(buffer),
3686 &length),
3687 PSA_ERROR_BAD_STATE);
3688 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003689
3690 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003691 TEST_EQUAL(psa_cipher_set_iv(&operation,
3692 iv, sizeof(iv)),
3693 PSA_ERROR_BAD_STATE);
3694 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003695
3696 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003697 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3698 PSA_ASSERT(psa_cipher_set_iv(&operation,
3699 iv, sizeof(iv)));
3700 ASSERT_OPERATION_IS_ACTIVE(operation);
3701 TEST_EQUAL(psa_cipher_set_iv(&operation,
3702 iv, sizeof(iv)),
3703 PSA_ERROR_BAD_STATE);
3704 ASSERT_OPERATION_IS_INACTIVE(operation);
3705 PSA_ASSERT(psa_cipher_abort(&operation));
3706 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003707
3708 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003709 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3710 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3711 buffer, sizeof(buffer),
3712 &length));
3713 TEST_EQUAL(psa_cipher_set_iv(&operation,
3714 iv, sizeof(iv)),
3715 PSA_ERROR_BAD_STATE);
3716 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003717
3718 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003719 TEST_EQUAL(psa_cipher_update(&operation,
3720 text, sizeof(text),
3721 buffer, sizeof(buffer),
3722 &length),
3723 PSA_ERROR_BAD_STATE);
3724 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003725
3726 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003727 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3728 ASSERT_OPERATION_IS_ACTIVE(operation);
3729 TEST_EQUAL(psa_cipher_update(&operation,
3730 text, sizeof(text),
3731 buffer, sizeof(buffer),
3732 &length),
3733 PSA_ERROR_BAD_STATE);
3734 ASSERT_OPERATION_IS_INACTIVE(operation);
3735 PSA_ASSERT(psa_cipher_abort(&operation));
3736 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003737
3738 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003739 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3740 PSA_ASSERT(psa_cipher_set_iv(&operation,
3741 iv, sizeof(iv)));
3742 PSA_ASSERT(psa_cipher_finish(&operation,
3743 buffer, sizeof(buffer), &length));
3744 TEST_EQUAL(psa_cipher_update(&operation,
3745 text, sizeof(text),
3746 buffer, sizeof(buffer),
3747 &length),
3748 PSA_ERROR_BAD_STATE);
3749 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003750
3751 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003752 TEST_EQUAL(psa_cipher_finish(&operation,
3753 buffer, sizeof(buffer), &length),
3754 PSA_ERROR_BAD_STATE);
3755 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003756
3757 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003758 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003759 /* Not calling update means we are encrypting an empty buffer, which is OK
3760 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003761 ASSERT_OPERATION_IS_ACTIVE(operation);
3762 TEST_EQUAL(psa_cipher_finish(&operation,
3763 buffer, sizeof(buffer), &length),
3764 PSA_ERROR_BAD_STATE);
3765 ASSERT_OPERATION_IS_INACTIVE(operation);
3766 PSA_ASSERT(psa_cipher_abort(&operation));
3767 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003768
3769 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003770 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3771 PSA_ASSERT(psa_cipher_set_iv(&operation,
3772 iv, sizeof(iv)));
3773 PSA_ASSERT(psa_cipher_finish(&operation,
3774 buffer, sizeof(buffer), &length));
3775 TEST_EQUAL(psa_cipher_finish(&operation,
3776 buffer, sizeof(buffer), &length),
3777 PSA_ERROR_BAD_STATE);
3778 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003779
Gilles Peskine449bd832023-01-11 14:50:10 +01003780 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003781
Jaeden Ameroab439972019-02-15 14:12:05 +00003782exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003783 psa_cipher_abort(&operation);
3784 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003785}
3786/* END_CASE */
3787
3788/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003789void cipher_encrypt_fail(int alg_arg,
3790 int key_type_arg,
3791 data_t *key_data,
3792 data_t *input,
3793 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003794{
Ronald Cron5425a212020-08-04 14:58:35 +02003795 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003796 psa_status_t status;
3797 psa_key_type_t key_type = key_type_arg;
3798 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003799 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003800 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003801 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3802 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003803 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003804 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003805 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003806 size_t function_output_length;
3807 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003808 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3809
Gilles Peskine449bd832023-01-11 14:50:10 +01003810 if (PSA_ERROR_BAD_STATE != expected_status) {
3811 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003812
Gilles Peskine449bd832023-01-11 14:50:10 +01003813 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3814 psa_set_key_algorithm(&attributes, alg);
3815 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003816
Gilles Peskine449bd832023-01-11 14:50:10 +01003817 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3818 input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003819 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003820
Gilles Peskine449bd832023-01-11 14:50:10 +01003821 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3822 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003823 }
3824
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003825 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003826 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3827 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003828
Gilles Peskine449bd832023-01-11 14:50:10 +01003829 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003830
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003831 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003832 status = psa_cipher_encrypt_setup(&operation, key, alg);
3833 if (status == PSA_SUCCESS) {
3834 if (alg != PSA_ALG_ECB_NO_PADDING) {
3835 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3836 iv, iv_size,
3837 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003838 }
3839
Gilles Peskine449bd832023-01-11 14:50:10 +01003840 status = psa_cipher_update(&operation, input->x, input->len,
3841 output, output_buffer_size,
3842 &function_output_length);
3843 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003844 output_length += function_output_length;
3845
Gilles Peskine449bd832023-01-11 14:50:10 +01003846 status = psa_cipher_finish(&operation, output + output_length,
3847 output_buffer_size - output_length,
3848 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003849
Gilles Peskine449bd832023-01-11 14:50:10 +01003850 TEST_EQUAL(status, expected_status);
3851 } else {
3852 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003853 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003854 } else {
3855 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003856 }
3857
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003858exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003859 psa_cipher_abort(&operation);
3860 mbedtls_free(output);
3861 psa_destroy_key(key);
3862 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003863}
3864/* END_CASE */
3865
3866/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003867void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3868 data_t *input, int iv_length,
3869 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003870{
3871 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3872 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3873 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3874 size_t output_buffer_size = 0;
3875 unsigned char *output = NULL;
3876
Gilles Peskine449bd832023-01-11 14:50:10 +01003877 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003878 TEST_CALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003879
Gilles Peskine449bd832023-01-11 14:50:10 +01003880 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003881
Gilles Peskine449bd832023-01-11 14:50:10 +01003882 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3883 psa_set_key_algorithm(&attributes, alg);
3884 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003885
Gilles Peskine449bd832023-01-11 14:50:10 +01003886 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3887 &key));
3888 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3889 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3890 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003891
3892exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003893 psa_cipher_abort(&operation);
3894 mbedtls_free(output);
3895 psa_destroy_key(key);
3896 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003897}
3898/* END_CASE */
3899
3900/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003901void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3902 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003903{
3904 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3905 psa_key_type_t key_type = key_type_arg;
3906 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003907 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3908 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003909 unsigned char *output = NULL;
3910 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003911 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003912 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3913
Gilles Peskine449bd832023-01-11 14:50:10 +01003914 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003915
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003916 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003917 TEST_LE_U(ciphertext->len,
3918 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3919 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3920 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3921 TEST_LE_U(plaintext->len,
3922 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3923 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3924 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003925
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003926
3927 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003928 psa_set_key_usage_flags(&attributes,
3929 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3930 psa_set_key_algorithm(&attributes, alg);
3931 psa_set_key_type(&attributes, key_type);
3932 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3933 &key));
3934 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3935 plaintext->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003936 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003937
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003938 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003939 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3940 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3941 PSA_ERROR_BAD_STATE);
3942 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3943 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3944 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003945
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003946 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003947 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3948 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3949 &length),
3950 PSA_ERROR_BAD_STATE);
3951 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3952 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3953 &length),
3954 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003955
Gilles Peskine286c3142022-04-20 17:09:38 +02003956 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003957 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003958 output_length = 0;
3959 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003960 PSA_ASSERT(psa_cipher_update(&operation,
3961 plaintext->x, plaintext->len,
3962 output, output_buffer_size,
3963 &length));
3964 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003965 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003966 PSA_ASSERT(psa_cipher_finish(&operation,
3967 mbedtls_buffer_offset(output, output_length),
3968 output_buffer_size - output_length,
3969 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003970 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003971 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003972 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003973
Gilles Peskine286c3142022-04-20 17:09:38 +02003974 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003975 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003976 output_length = 0;
3977 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003978 PSA_ASSERT(psa_cipher_update(&operation,
3979 ciphertext->x, ciphertext->len,
3980 output, output_buffer_size,
3981 &length));
3982 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003983 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003984 PSA_ASSERT(psa_cipher_finish(&operation,
3985 mbedtls_buffer_offset(output, output_length),
3986 output_buffer_size - output_length,
3987 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003988 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003989 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003990 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003991
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003992 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003993 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003994 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
3995 output, output_buffer_size,
3996 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003997 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003998 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003999
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004000 /* One-shot decryption */
4001 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004002 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
4003 output, output_buffer_size,
4004 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004005 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004006 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004007
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004008exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004009 PSA_ASSERT(psa_cipher_abort(&operation));
4010 mbedtls_free(output);
4011 psa_cipher_abort(&operation);
4012 psa_destroy_key(key);
4013 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004014}
4015/* END_CASE */
4016
4017/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004018void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004019{
4020 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4021 psa_algorithm_t alg = alg_arg;
4022 psa_key_type_t key_type = key_type_arg;
4023 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4024 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4025 psa_status_t status;
4026
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004028
Gilles Peskine449bd832023-01-11 14:50:10 +01004029 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4030 psa_set_key_algorithm(&attributes, alg);
4031 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004032
4033 /* Usage of either of these two size macros would cause divide by zero
4034 * with incorrect key types previously. Input length should be irrelevant
4035 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004036 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4037 0);
4038 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004039
4040
Gilles Peskine449bd832023-01-11 14:50:10 +01004041 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4042 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004043
4044 /* Should fail due to invalid alg type (to support invalid key type).
4045 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004046 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004047
Gilles Peskine449bd832023-01-11 14:50:10 +01004048 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004049
4050exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004051 psa_cipher_abort(&operation);
4052 psa_destroy_key(key);
4053 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004054}
4055/* END_CASE */
4056
4057/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004058void cipher_encrypt_validation(int alg_arg,
4059 int key_type_arg,
4060 data_t *key_data,
4061 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004062{
4063 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4064 psa_key_type_t key_type = key_type_arg;
4065 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004066 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004067 unsigned char *output1 = NULL;
4068 size_t output1_buffer_size = 0;
4069 size_t output1_length = 0;
4070 unsigned char *output2 = NULL;
4071 size_t output2_buffer_size = 0;
4072 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004073 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004074 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004075 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004076
Gilles Peskine449bd832023-01-11 14:50:10 +01004077 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004078
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4080 psa_set_key_algorithm(&attributes, alg);
4081 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004082
Gilles Peskine449bd832023-01-11 14:50:10 +01004083 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4084 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4085 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004086 TEST_CALLOC(output1, output1_buffer_size);
4087 TEST_CALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004088
Gilles Peskine449bd832023-01-11 14:50:10 +01004089 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4090 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004091
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004092 /* The one-shot cipher encryption uses generated iv so validating
4093 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4095 output1_buffer_size, &output1_length));
4096 TEST_LE_U(output1_length,
4097 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4098 TEST_LE_U(output1_length,
4099 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004100
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4102 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004103
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 PSA_ASSERT(psa_cipher_update(&operation,
4105 input->x, input->len,
4106 output2, output2_buffer_size,
4107 &function_output_length));
4108 TEST_LE_U(function_output_length,
4109 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4110 TEST_LE_U(function_output_length,
4111 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004112 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004113
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 PSA_ASSERT(psa_cipher_finish(&operation,
4115 output2 + output2_length,
4116 output2_buffer_size - output2_length,
4117 &function_output_length));
4118 TEST_LE_U(function_output_length,
4119 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4120 TEST_LE_U(function_output_length,
4121 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004122 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004123
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 PSA_ASSERT(psa_cipher_abort(&operation));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004125 TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004126 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004127
Gilles Peskine50e586b2018-06-08 14:28:46 +02004128exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004129 psa_cipher_abort(&operation);
4130 mbedtls_free(output1);
4131 mbedtls_free(output2);
4132 psa_destroy_key(key);
4133 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004134}
4135/* END_CASE */
4136
4137/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004138void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4139 data_t *key_data, data_t *iv,
4140 data_t *input,
4141 int first_part_size_arg,
4142 int output1_length_arg, int output2_length_arg,
4143 data_t *expected_output,
4144 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004145{
Ronald Cron5425a212020-08-04 14:58:35 +02004146 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004147 psa_key_type_t key_type = key_type_arg;
4148 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004149 psa_status_t status;
4150 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004151 size_t first_part_size = first_part_size_arg;
4152 size_t output1_length = output1_length_arg;
4153 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004154 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004155 size_t output_buffer_size = 0;
4156 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004157 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004158 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004159 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004160
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004162
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4164 psa_set_key_algorithm(&attributes, alg);
4165 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004166
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4168 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004169
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004171
Gilles Peskine449bd832023-01-11 14:50:10 +01004172 if (iv->len > 0) {
4173 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004174 }
4175
Gilles Peskine449bd832023-01-11 14:50:10 +01004176 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4177 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004178 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004179
Gilles Peskine449bd832023-01-11 14:50:10 +01004180 TEST_LE_U(first_part_size, input->len);
4181 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4182 output, output_buffer_size,
4183 &function_output_length));
4184 TEST_ASSERT(function_output_length == output1_length);
4185 TEST_LE_U(function_output_length,
4186 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4187 TEST_LE_U(function_output_length,
4188 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004189 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004190
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 if (first_part_size < input->len) {
4192 PSA_ASSERT(psa_cipher_update(&operation,
4193 input->x + first_part_size,
4194 input->len - first_part_size,
4195 (output_buffer_size == 0 ? NULL :
4196 output + total_output_length),
4197 output_buffer_size - total_output_length,
4198 &function_output_length));
4199 TEST_ASSERT(function_output_length == output2_length);
4200 TEST_LE_U(function_output_length,
4201 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4202 alg,
4203 input->len - first_part_size));
4204 TEST_LE_U(function_output_length,
4205 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004206 total_output_length += function_output_length;
4207 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004208
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 status = psa_cipher_finish(&operation,
4210 (output_buffer_size == 0 ? NULL :
4211 output + total_output_length),
4212 output_buffer_size - total_output_length,
4213 &function_output_length);
4214 TEST_LE_U(function_output_length,
4215 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4216 TEST_LE_U(function_output_length,
4217 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004218 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004219 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004220
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 if (expected_status == PSA_SUCCESS) {
4222 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004223
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004224 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004225 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004226 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004227
4228exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004229 psa_cipher_abort(&operation);
4230 mbedtls_free(output);
4231 psa_destroy_key(key);
4232 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004233}
4234/* END_CASE */
4235
4236/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004237void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4238 data_t *key_data, data_t *iv,
4239 data_t *input,
4240 int first_part_size_arg,
4241 int output1_length_arg, int output2_length_arg,
4242 data_t *expected_output,
4243 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004244{
Ronald Cron5425a212020-08-04 14:58:35 +02004245 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004246 psa_key_type_t key_type = key_type_arg;
4247 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004248 psa_status_t status;
4249 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004250 size_t first_part_size = first_part_size_arg;
4251 size_t output1_length = output1_length_arg;
4252 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004253 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004254 size_t output_buffer_size = 0;
4255 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004256 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004257 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004258 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004259
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004261
Gilles Peskine449bd832023-01-11 14:50:10 +01004262 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4263 psa_set_key_algorithm(&attributes, alg);
4264 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004265
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4267 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004268
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004270
Gilles Peskine449bd832023-01-11 14:50:10 +01004271 if (iv->len > 0) {
4272 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004273 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004274
Gilles Peskine449bd832023-01-11 14:50:10 +01004275 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4276 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004277 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004278
Gilles Peskine449bd832023-01-11 14:50:10 +01004279 TEST_LE_U(first_part_size, input->len);
4280 PSA_ASSERT(psa_cipher_update(&operation,
4281 input->x, first_part_size,
4282 output, output_buffer_size,
4283 &function_output_length));
4284 TEST_ASSERT(function_output_length == output1_length);
4285 TEST_LE_U(function_output_length,
4286 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4287 TEST_LE_U(function_output_length,
4288 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004289 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004290
Gilles Peskine449bd832023-01-11 14:50:10 +01004291 if (first_part_size < input->len) {
4292 PSA_ASSERT(psa_cipher_update(&operation,
4293 input->x + first_part_size,
4294 input->len - first_part_size,
4295 (output_buffer_size == 0 ? NULL :
4296 output + total_output_length),
4297 output_buffer_size - total_output_length,
4298 &function_output_length));
4299 TEST_ASSERT(function_output_length == output2_length);
4300 TEST_LE_U(function_output_length,
4301 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4302 alg,
4303 input->len - first_part_size));
4304 TEST_LE_U(function_output_length,
4305 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004306 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004307 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004308
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 status = psa_cipher_finish(&operation,
4310 (output_buffer_size == 0 ? NULL :
4311 output + total_output_length),
4312 output_buffer_size - total_output_length,
4313 &function_output_length);
4314 TEST_LE_U(function_output_length,
4315 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4316 TEST_LE_U(function_output_length,
4317 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004318 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004319 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004320
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 if (expected_status == PSA_SUCCESS) {
4322 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004323
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004324 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004325 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004326 }
4327
Gilles Peskine50e586b2018-06-08 14:28:46 +02004328exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004329 psa_cipher_abort(&operation);
4330 mbedtls_free(output);
4331 psa_destroy_key(key);
4332 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004333}
4334/* END_CASE */
4335
Gilles Peskine50e586b2018-06-08 14:28:46 +02004336/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004337void cipher_decrypt_fail(int alg_arg,
4338 int key_type_arg,
4339 data_t *key_data,
4340 data_t *iv,
4341 data_t *input_arg,
4342 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004343{
4344 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4345 psa_status_t status;
4346 psa_key_type_t key_type = key_type_arg;
4347 psa_algorithm_t alg = alg_arg;
4348 psa_status_t expected_status = expected_status_arg;
4349 unsigned char *input = NULL;
4350 size_t input_buffer_size = 0;
4351 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004352 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004353 size_t output_buffer_size = 0;
4354 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004355 size_t function_output_length;
4356 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004357 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4358
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 if (PSA_ERROR_BAD_STATE != expected_status) {
4360 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004361
Gilles Peskine449bd832023-01-11 14:50:10 +01004362 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4363 psa_set_key_algorithm(&attributes, alg);
4364 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004365
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4367 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004368 }
4369
4370 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4372 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004373 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 memcpy(input, iv->x, iv->len);
4375 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004376 }
4377
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004379 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004380
Neil Armstrong66a479f2022-02-07 15:41:19 +01004381 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4383 output_buffer_size, &output_length);
4384 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004385
Neil Armstrong66a479f2022-02-07 15:41:19 +01004386 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 status = psa_cipher_decrypt_setup(&operation, key, alg);
4388 if (status == PSA_SUCCESS) {
4389 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4390 input_arg->len) +
4391 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004392 TEST_CALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004393
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 if (iv->len > 0) {
4395 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004396
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 if (status != PSA_SUCCESS) {
4398 TEST_EQUAL(status, expected_status);
4399 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004400 }
4401
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 if (status == PSA_SUCCESS) {
4403 status = psa_cipher_update(&operation,
4404 input_arg->x, input_arg->len,
4405 output_multi, output_buffer_size,
4406 &function_output_length);
4407 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004408 output_length = function_output_length;
4409
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 status = psa_cipher_finish(&operation,
4411 output_multi + output_length,
4412 output_buffer_size - output_length,
4413 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 TEST_EQUAL(status, expected_status);
4416 } else {
4417 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004418 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004419 } else {
4420 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004421 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 } else {
4423 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004424 }
4425
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004426exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 psa_cipher_abort(&operation);
4428 mbedtls_free(input);
4429 mbedtls_free(output);
4430 mbedtls_free(output_multi);
4431 psa_destroy_key(key);
4432 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004433}
4434/* END_CASE */
4435
4436/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004437void cipher_decrypt(int alg_arg,
4438 int key_type_arg,
4439 data_t *key_data,
4440 data_t *iv,
4441 data_t *input_arg,
4442 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004443{
4444 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4445 psa_key_type_t key_type = key_type_arg;
4446 psa_algorithm_t alg = alg_arg;
4447 unsigned char *input = NULL;
4448 size_t input_buffer_size = 0;
4449 unsigned char *output = NULL;
4450 size_t output_buffer_size = 0;
4451 size_t output_length = 0;
4452 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4453
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004455
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4457 psa_set_key_algorithm(&attributes, alg);
4458 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004459
4460 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4462 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004463 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 memcpy(input, iv->x, iv->len);
4465 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004466 }
4467
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004469 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004470
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4472 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004473
Gilles Peskine449bd832023-01-11 14:50:10 +01004474 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4475 output_buffer_size, &output_length));
4476 TEST_LE_U(output_length,
4477 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4478 TEST_LE_U(output_length,
4479 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004480
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004481 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004482 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004483exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 mbedtls_free(input);
4485 mbedtls_free(output);
4486 psa_destroy_key(key);
4487 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004488}
4489/* END_CASE */
4490
4491/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004492void cipher_verify_output(int alg_arg,
4493 int key_type_arg,
4494 data_t *key_data,
4495 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004496{
Ronald Cron5425a212020-08-04 14:58:35 +02004497 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004498 psa_key_type_t key_type = key_type_arg;
4499 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004500 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004501 size_t output1_size = 0;
4502 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004503 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004504 size_t output2_size = 0;
4505 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004506 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004507
Gilles Peskine449bd832023-01-11 14:50:10 +01004508 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004509
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4511 psa_set_key_algorithm(&attributes, alg);
4512 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004513
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4515 &key));
4516 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004517 TEST_CALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004518
Gilles Peskine449bd832023-01-11 14:50:10 +01004519 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4520 output1, output1_size,
4521 &output1_length));
4522 TEST_LE_U(output1_length,
4523 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4524 TEST_LE_U(output1_length,
4525 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004526
4527 output2_size = output1_length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004528 TEST_CALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004529
Gilles Peskine449bd832023-01-11 14:50:10 +01004530 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4531 output2, output2_size,
4532 &output2_length));
4533 TEST_LE_U(output2_length,
4534 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4535 TEST_LE_U(output2_length,
4536 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004537
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004538 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004539
4540exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 mbedtls_free(output1);
4542 mbedtls_free(output2);
4543 psa_destroy_key(key);
4544 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004545}
4546/* END_CASE */
4547
4548/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004549void cipher_verify_output_multipart(int alg_arg,
4550 int key_type_arg,
4551 data_t *key_data,
4552 data_t *input,
4553 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004554{
Ronald Cron5425a212020-08-04 14:58:35 +02004555 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004556 psa_key_type_t key_type = key_type_arg;
4557 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004558 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004559 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004560 size_t iv_size = 16;
4561 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004562 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004563 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004564 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004565 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004566 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004567 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004568 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004569 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4570 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004571 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004572
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004574
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4576 psa_set_key_algorithm(&attributes, alg);
4577 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004578
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4580 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4583 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004584
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 if (alg != PSA_ALG_ECB_NO_PADDING) {
4586 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4587 iv, iv_size,
4588 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004589 }
4590
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4592 TEST_LE_U(output1_buffer_size,
4593 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004594 TEST_CALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004595
Gilles Peskine449bd832023-01-11 14:50:10 +01004596 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004597
Gilles Peskine449bd832023-01-11 14:50:10 +01004598 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4599 output1, output1_buffer_size,
4600 &function_output_length));
4601 TEST_LE_U(function_output_length,
4602 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4603 TEST_LE_U(function_output_length,
4604 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004605 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004606
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 PSA_ASSERT(psa_cipher_update(&operation1,
4608 input->x + first_part_size,
4609 input->len - first_part_size,
4610 output1, output1_buffer_size,
4611 &function_output_length));
4612 TEST_LE_U(function_output_length,
4613 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4614 alg,
4615 input->len - first_part_size));
4616 TEST_LE_U(function_output_length,
4617 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - 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_finish(&operation1,
4621 output1 + output1_length,
4622 output1_buffer_size - output1_length,
4623 &function_output_length));
4624 TEST_LE_U(function_output_length,
4625 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4626 TEST_LE_U(function_output_length,
4627 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004628 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004629
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004631
Gilles Peskine048b7f02018-06-08 14:20:49 +02004632 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004633 TEST_LE_U(output2_buffer_size,
4634 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4635 TEST_LE_U(output2_buffer_size,
4636 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004637 TEST_CALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004638
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 if (iv_length > 0) {
4640 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4641 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004642 }
Moran Pekerded84402018-06-06 16:36:50 +03004643
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4645 output2, output2_buffer_size,
4646 &function_output_length));
4647 TEST_LE_U(function_output_length,
4648 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4649 TEST_LE_U(function_output_length,
4650 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004651 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004652
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 PSA_ASSERT(psa_cipher_update(&operation2,
4654 output1 + first_part_size,
4655 output1_length - first_part_size,
4656 output2, output2_buffer_size,
4657 &function_output_length));
4658 TEST_LE_U(function_output_length,
4659 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4660 alg,
4661 output1_length - first_part_size));
4662 TEST_LE_U(function_output_length,
4663 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - 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_finish(&operation2,
4667 output2 + output2_length,
4668 output2_buffer_size - output2_length,
4669 &function_output_length));
4670 TEST_LE_U(function_output_length,
4671 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4672 TEST_LE_U(function_output_length,
4673 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004674 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004675
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004677
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004678 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004679
4680exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004681 psa_cipher_abort(&operation1);
4682 psa_cipher_abort(&operation2);
4683 mbedtls_free(output1);
4684 mbedtls_free(output2);
4685 psa_destroy_key(key);
4686 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004687}
4688/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004689
Gilles Peskine20035e32018-02-03 22:44:14 +01004690/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004691void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4692 int alg_arg,
4693 data_t *nonce,
4694 data_t *additional_data,
4695 data_t *input_data,
4696 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004697{
Ronald Cron5425a212020-08-04 14:58:35 +02004698 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004699 psa_key_type_t key_type = key_type_arg;
4700 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004701 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004702 unsigned char *output_data = NULL;
4703 size_t output_size = 0;
4704 size_t output_length = 0;
4705 unsigned char *output_data2 = NULL;
4706 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004707 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004708 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004709 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004710
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004712
Gilles Peskine449bd832023-01-11 14:50:10 +01004713 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4714 psa_set_key_algorithm(&attributes, alg);
4715 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004716
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4718 &key));
4719 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4720 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004721
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4723 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004724 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4725 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4727 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4728 TEST_EQUAL(output_size,
4729 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4730 TEST_LE_U(output_size,
4731 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004732 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004733 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004734
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 status = psa_aead_encrypt(key, alg,
4736 nonce->x, nonce->len,
4737 additional_data->x,
4738 additional_data->len,
4739 input_data->x, input_data->len,
4740 output_data, output_size,
4741 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004742
4743 /* If the operation is not supported, just skip and not fail in case the
4744 * encryption involves a common limitation of cryptography hardwares and
4745 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 if (status == PSA_ERROR_NOT_SUPPORTED) {
4747 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4748 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004749 }
4750
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004752
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 if (PSA_SUCCESS == expected_result) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004754 TEST_CALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004755
Gilles Peskine003a4a92019-05-14 16:09:40 +02004756 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4757 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 TEST_EQUAL(input_data->len,
4759 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004760
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 TEST_LE_U(input_data->len,
4762 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004763
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 TEST_EQUAL(psa_aead_decrypt(key, alg,
4765 nonce->x, nonce->len,
4766 additional_data->x,
4767 additional_data->len,
4768 output_data, output_length,
4769 output_data2, output_length,
4770 &output_length2),
4771 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004772
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004773 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004774 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004775 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004776
Gilles Peskinea1cac842018-06-11 19:33:02 +02004777exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004778 psa_destroy_key(key);
4779 mbedtls_free(output_data);
4780 mbedtls_free(output_data2);
4781 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004782}
4783/* END_CASE */
4784
4785/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004786void aead_encrypt(int key_type_arg, data_t *key_data,
4787 int alg_arg,
4788 data_t *nonce,
4789 data_t *additional_data,
4790 data_t *input_data,
4791 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004792{
Ronald Cron5425a212020-08-04 14:58:35 +02004793 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004794 psa_key_type_t key_type = key_type_arg;
4795 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004796 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004797 unsigned char *output_data = NULL;
4798 size_t output_size = 0;
4799 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004800 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004801 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004802
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004804
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4806 psa_set_key_algorithm(&attributes, alg);
4807 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004808
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4810 &key));
4811 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4812 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004813
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4815 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004816 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4817 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 TEST_EQUAL(output_size,
4819 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4820 TEST_LE_U(output_size,
4821 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004822 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004823
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 status = psa_aead_encrypt(key, alg,
4825 nonce->x, nonce->len,
4826 additional_data->x, additional_data->len,
4827 input_data->x, input_data->len,
4828 output_data, output_size,
4829 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004830
Ronald Cron28a45ed2021-02-09 20:35:42 +01004831 /* If the operation is not supported, just skip and not fail in case the
4832 * encryption involves a common limitation of cryptography hardwares and
4833 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 if (status == PSA_ERROR_NOT_SUPPORTED) {
4835 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4836 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004837 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004838
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 PSA_ASSERT(status);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004840 TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004841 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004842
Gilles Peskinea1cac842018-06-11 19:33:02 +02004843exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 psa_destroy_key(key);
4845 mbedtls_free(output_data);
4846 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004847}
4848/* END_CASE */
4849
4850/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004851void aead_decrypt(int key_type_arg, data_t *key_data,
4852 int alg_arg,
4853 data_t *nonce,
4854 data_t *additional_data,
4855 data_t *input_data,
4856 data_t *expected_data,
4857 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004858{
Ronald Cron5425a212020-08-04 14:58:35 +02004859 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004860 psa_key_type_t key_type = key_type_arg;
4861 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004862 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004863 unsigned char *output_data = NULL;
4864 size_t output_size = 0;
4865 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004866 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004867 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004868 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004869
Gilles Peskine449bd832023-01-11 14:50:10 +01004870 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004871
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4873 psa_set_key_algorithm(&attributes, alg);
4874 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004875
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4877 &key));
4878 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4879 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004880
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4882 alg);
4883 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4884 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004885 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4886 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 TEST_EQUAL(output_size,
4888 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4889 TEST_LE_U(output_size,
4890 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004891 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004892 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004893
Gilles Peskine449bd832023-01-11 14:50:10 +01004894 status = psa_aead_decrypt(key, alg,
4895 nonce->x, nonce->len,
4896 additional_data->x,
4897 additional_data->len,
4898 input_data->x, input_data->len,
4899 output_data, output_size,
4900 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004901
Ronald Cron28a45ed2021-02-09 20:35:42 +01004902 /* If the operation is not supported, just skip and not fail in case the
4903 * decryption involves a common limitation of cryptography hardwares and
4904 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 if (status == PSA_ERROR_NOT_SUPPORTED) {
4906 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4907 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004908 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004909
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004911
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 if (expected_result == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004913 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004914 output_data, output_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004915 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004916
Gilles Peskinea1cac842018-06-11 19:33:02 +02004917exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 psa_destroy_key(key);
4919 mbedtls_free(output_data);
4920 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004921}
4922/* END_CASE */
4923
4924/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004925void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4926 int alg_arg,
4927 data_t *nonce,
4928 data_t *additional_data,
4929 data_t *input_data,
4930 int do_set_lengths,
4931 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004932{
Paul Elliottd3f82412021-06-16 16:52:21 +01004933 size_t ad_part_len = 0;
4934 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004935 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004936
Gilles Peskine449bd832023-01-11 14:50:10 +01004937 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4938 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004939
Gilles Peskine449bd832023-01-11 14:50:10 +01004940 if (do_set_lengths) {
4941 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004942 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004944 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004945 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004946 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004947
4948 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 if (!aead_multipart_internal_func(key_type_arg, key_data,
4950 alg_arg, nonce,
4951 additional_data,
4952 ad_part_len,
4953 input_data, -1,
4954 set_lengths_method,
4955 expected_output,
4956 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004957 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004958 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004959
Gilles Peskine449bd832023-01-11 14:50:10 +01004960 /* length(0) part, length(ad_part_len) part, length(0) part... */
4961 mbedtls_test_set_step(1000 + ad_part_len);
4962
4963 if (!aead_multipart_internal_func(key_type_arg, key_data,
4964 alg_arg, nonce,
4965 additional_data,
4966 ad_part_len,
4967 input_data, -1,
4968 set_lengths_method,
4969 expected_output,
4970 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004971 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 }
4973 }
4974
4975 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4976 /* Split data into length(data_part_len) parts. */
4977 mbedtls_test_set_step(2000 + data_part_len);
4978
4979 if (do_set_lengths) {
4980 if (data_part_len & 0x01) {
4981 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4982 } else {
4983 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4984 }
4985 }
4986
4987 if (!aead_multipart_internal_func(key_type_arg, key_data,
4988 alg_arg, nonce,
4989 additional_data, -1,
4990 input_data, data_part_len,
4991 set_lengths_method,
4992 expected_output,
4993 1, 0)) {
4994 break;
4995 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004996
4997 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01004998 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004999
Gilles Peskine449bd832023-01-11 14:50:10 +01005000 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, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005007 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005009 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005010
Paul Elliott8fc45162021-06-23 16:06:01 +01005011 /* Goto is required to silence warnings about unused labels, as we
5012 * don't actually do any test assertions in this function. */
5013 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005014}
5015/* END_CASE */
5016
5017/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005018void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5019 int alg_arg,
5020 data_t *nonce,
5021 data_t *additional_data,
5022 data_t *input_data,
5023 int do_set_lengths,
5024 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005025{
Paul Elliottd3f82412021-06-16 16:52:21 +01005026 size_t ad_part_len = 0;
5027 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005028 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005029
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005031 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005033
Gilles Peskine449bd832023-01-11 14:50:10 +01005034 if (do_set_lengths) {
5035 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005036 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005038 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005039 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005040 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005041
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 if (!aead_multipart_internal_func(key_type_arg, key_data,
5043 alg_arg, nonce,
5044 additional_data,
5045 ad_part_len,
5046 input_data, -1,
5047 set_lengths_method,
5048 expected_output,
5049 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005050 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005052
5053 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005054 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005055
Gilles Peskine449bd832023-01-11 14:50:10 +01005056 if (!aead_multipart_internal_func(key_type_arg, key_data,
5057 alg_arg, nonce,
5058 additional_data,
5059 ad_part_len,
5060 input_data, -1,
5061 set_lengths_method,
5062 expected_output,
5063 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005064 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005065 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005066 }
5067
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005069 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005071
Gilles Peskine449bd832023-01-11 14:50:10 +01005072 if (do_set_lengths) {
5073 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005074 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005076 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005077 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005078 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005079
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 if (!aead_multipart_internal_func(key_type_arg, key_data,
5081 alg_arg, nonce,
5082 additional_data, -1,
5083 input_data, data_part_len,
5084 set_lengths_method,
5085 expected_output,
5086 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005087 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005089
5090 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 mbedtls_test_set_step(3000 + data_part_len);
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, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005100 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005101 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005102 }
5103
Paul Elliott8fc45162021-06-23 16:06:01 +01005104 /* Goto is required to silence warnings about unused labels, as we
5105 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005106 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005107}
5108/* END_CASE */
5109
5110/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005111void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5112 int alg_arg,
5113 int nonce_length,
5114 int expected_nonce_length_arg,
5115 data_t *additional_data,
5116 data_t *input_data,
5117 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005118{
5119
5120 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5121 psa_key_type_t key_type = key_type_arg;
5122 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005123 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005124 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5125 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5126 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005127 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005128 size_t actual_nonce_length = 0;
5129 size_t expected_nonce_length = expected_nonce_length_arg;
5130 unsigned char *output = NULL;
5131 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005132 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005133 size_t ciphertext_size = 0;
5134 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005135 size_t tag_length = 0;
5136 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005137
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005139
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5141 psa_set_key_algorithm(&attributes, alg);
5142 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005143
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5145 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005146
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005148
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005150
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005151 TEST_CALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005152
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005154
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005156
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005157 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005158
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005160
5161 /* If the operation is not supported, just skip and not fail in case the
5162 * encryption involves a common limitation of cryptography hardwares and
5163 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 if (status == PSA_ERROR_NOT_SUPPORTED) {
5165 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5166 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005167 }
5168
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005170
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5172 nonce_length,
5173 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005174
Gilles Peskine449bd832023-01-11 14:50:10 +01005175 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005176
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005178
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 if (expected_status == PSA_SUCCESS) {
5180 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5181 alg));
5182 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005183
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005185
Gilles Peskine449bd832023-01-11 14:50:10 +01005186 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005187 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5189 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005190
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5192 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005193
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5195 output, output_size,
5196 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005197
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5199 &ciphertext_length, tag_buffer,
5200 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005201 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005202
5203exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 psa_destroy_key(key);
5205 mbedtls_free(output);
5206 mbedtls_free(ciphertext);
5207 psa_aead_abort(&operation);
5208 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005209}
5210/* END_CASE */
5211
5212/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005213void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5214 int alg_arg,
5215 int nonce_length_arg,
5216 int set_lengths_method_arg,
5217 data_t *additional_data,
5218 data_t *input_data,
5219 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005220{
5221
5222 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5223 psa_key_type_t key_type = key_type_arg;
5224 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005225 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005226 uint8_t *nonce_buffer = NULL;
5227 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5228 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5229 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005230 unsigned char *output = NULL;
5231 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005232 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005233 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005234 size_t ciphertext_size = 0;
5235 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005236 size_t tag_length = 0;
5237 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005238 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005239 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005240
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005242
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5244 psa_set_key_algorithm(&attributes, alg);
5245 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005246
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5248 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005249
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005251
Gilles Peskine449bd832023-01-11 14:50:10 +01005252 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005253
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005254 TEST_CALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005255
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005257
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005259
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005260 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005261
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005263
5264 /* If the operation is not supported, just skip and not fail in case the
5265 * encryption involves a common limitation of cryptography hardwares and
5266 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 if (status == PSA_ERROR_NOT_SUPPORTED) {
5268 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5269 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005270 }
5271
Gilles Peskine449bd832023-01-11 14:50:10 +01005272 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005273
Paul Elliott4023ffd2021-09-10 16:21:22 +01005274 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 if (nonce_length_arg == -1) {
5276 /* Arbitrary size buffer, to test zero length valid buffer. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005277 TEST_CALLOC(nonce_buffer, 4);
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 nonce_length = 0;
5279 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005280 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 nonce_length = (size_t) nonce_length_arg;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005282 TEST_CALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005283
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 if (nonce_buffer) {
5285 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005286 nonce_buffer[index] = 'a' + index;
5287 }
Paul Elliott66696b52021-08-16 18:42:41 +01005288 }
Paul Elliott863864a2021-07-23 17:28:31 +01005289 }
5290
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5292 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5293 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005294 }
5295
Gilles Peskine449bd832023-01-11 14:50:10 +01005296 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005297
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005299
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 if (expected_status == PSA_SUCCESS) {
5301 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5302 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5303 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005304 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005305 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005306 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005307 }
Paul Elliott863864a2021-07-23 17:28:31 +01005308
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005309 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5311 additional_data->len),
5312 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005313
Gilles Peskine449bd832023-01-11 14:50:10 +01005314 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5315 output, output_size,
5316 &ciphertext_length),
5317 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005318
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5320 &ciphertext_length, tag_buffer,
5321 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5322 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005323 }
5324
5325exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 psa_destroy_key(key);
5327 mbedtls_free(output);
5328 mbedtls_free(ciphertext);
5329 mbedtls_free(nonce_buffer);
5330 psa_aead_abort(&operation);
5331 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005332}
5333/* END_CASE */
5334
5335/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005336void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005337 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005338 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005339 data_t *nonce,
5340 data_t *additional_data,
5341 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005343{
5344
5345 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5346 psa_key_type_t key_type = key_type_arg;
5347 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005348 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005349 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5350 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5351 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005352 unsigned char *output = NULL;
5353 unsigned char *ciphertext = NULL;
5354 size_t output_size = output_size_arg;
5355 size_t ciphertext_size = 0;
5356 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005357 size_t tag_length = 0;
5358 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5359
Gilles Peskine449bd832023-01-11 14:50:10 +01005360 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005361
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5363 psa_set_key_algorithm(&attributes, alg);
5364 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005365
Gilles Peskine449bd832023-01-11 14:50:10 +01005366 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5367 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005368
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005370
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005371 TEST_CALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005372
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005374
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005375 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005376
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005378
5379 /* If the operation is not supported, just skip and not fail in case the
5380 * encryption involves a common limitation of cryptography hardwares and
5381 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 if (status == PSA_ERROR_NOT_SUPPORTED) {
5383 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5384 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005385 }
5386
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005388
Gilles Peskine449bd832023-01-11 14:50:10 +01005389 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5390 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005391
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005393
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5395 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005396
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 status = psa_aead_update(&operation, input_data->x, input_data->len,
5398 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005399
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005401
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005403 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5405 &ciphertext_length, tag_buffer,
5406 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005407 }
5408
5409exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005410 psa_destroy_key(key);
5411 mbedtls_free(output);
5412 mbedtls_free(ciphertext);
5413 psa_aead_abort(&operation);
5414 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005415}
5416/* END_CASE */
5417
Paul Elliott91b021e2021-07-23 18:52:31 +01005418/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005419void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5420 int alg_arg,
5421 int finish_ciphertext_size_arg,
5422 int tag_size_arg,
5423 data_t *nonce,
5424 data_t *additional_data,
5425 data_t *input_data,
5426 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005427{
5428
5429 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5430 psa_key_type_t key_type = key_type_arg;
5431 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005432 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005433 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5434 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5435 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005436 unsigned char *ciphertext = NULL;
5437 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005438 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005439 size_t ciphertext_size = 0;
5440 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5442 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005443 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005444
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005446
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5448 psa_set_key_algorithm(&attributes, alg);
5449 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005450
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5452 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005453
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005455
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005457
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005458 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005459
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005460 TEST_CALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005461
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005462 TEST_CALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005463
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005465
5466 /* If the operation is not supported, just skip and not fail in case the
5467 * encryption involves a common limitation of cryptography hardwares and
5468 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 if (status == PSA_ERROR_NOT_SUPPORTED) {
5470 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5471 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005472 }
5473
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005475
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005477
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5479 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005480
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5482 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005483
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5485 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005486
5487 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 status = psa_aead_finish(&operation, finish_ciphertext,
5489 finish_ciphertext_size,
5490 &ciphertext_length, tag_buffer,
5491 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005492
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005494
5495exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 psa_destroy_key(key);
5497 mbedtls_free(ciphertext);
5498 mbedtls_free(finish_ciphertext);
5499 mbedtls_free(tag_buffer);
5500 psa_aead_abort(&operation);
5501 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005502}
5503/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005504
5505/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005506void aead_multipart_verify(int key_type_arg, data_t *key_data,
5507 int alg_arg,
5508 data_t *nonce,
5509 data_t *additional_data,
5510 data_t *input_data,
5511 data_t *tag,
5512 int tag_usage_arg,
5513 int expected_setup_status_arg,
5514 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005515{
5516 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5517 psa_key_type_t key_type = key_type_arg;
5518 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005519 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5521 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5522 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005523 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005524 unsigned char *plaintext = NULL;
5525 unsigned char *finish_plaintext = NULL;
5526 size_t plaintext_size = 0;
5527 size_t plaintext_length = 0;
5528 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005529 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005530 unsigned char *tag_buffer = NULL;
5531 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005532
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005534
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5536 psa_set_key_algorithm(&attributes, alg);
5537 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005538
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5540 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005541
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005543
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5545 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005546
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005547 TEST_CALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005548
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005550
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005551 TEST_CALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005552
Gilles Peskine449bd832023-01-11 14:50:10 +01005553 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005554
5555 /* If the operation is not supported, just skip and not fail in case the
5556 * encryption involves a common limitation of cryptography hardwares and
5557 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 if (status == PSA_ERROR_NOT_SUPPORTED) {
5559 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5560 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005561 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005563
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005565 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 }
Paul Elliott9961a662021-09-17 19:19:02 +01005567
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005569
Gilles Peskine449bd832023-01-11 14:50:10 +01005570 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005571
Gilles Peskine449bd832023-01-11 14:50:10 +01005572 status = psa_aead_set_lengths(&operation, additional_data->len,
5573 input_data->len);
5574 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005575
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5577 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005578
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5580 input_data->len,
5581 plaintext, plaintext_size,
5582 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005583
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005585 tag_buffer = tag->x;
5586 tag_size = tag->len;
5587 }
5588
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 status = psa_aead_verify(&operation, finish_plaintext,
5590 verify_plaintext_size,
5591 &plaintext_length,
5592 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005593
Gilles Peskine449bd832023-01-11 14:50:10 +01005594 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005595
5596exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 psa_destroy_key(key);
5598 mbedtls_free(plaintext);
5599 mbedtls_free(finish_plaintext);
5600 psa_aead_abort(&operation);
5601 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005602}
5603/* END_CASE */
5604
Paul Elliott9961a662021-09-17 19:19:02 +01005605/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005606void aead_multipart_setup(int key_type_arg, data_t *key_data,
5607 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005608{
5609 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5610 psa_key_type_t key_type = key_type_arg;
5611 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005612 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005613 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5614 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5615 psa_status_t expected_status = expected_status_arg;
5616
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005618
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 psa_set_key_usage_flags(&attributes,
5620 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5621 psa_set_key_algorithm(&attributes, alg);
5622 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005623
Gilles Peskine449bd832023-01-11 14:50:10 +01005624 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5625 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005626
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005628
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005630
Gilles Peskine449bd832023-01-11 14:50:10 +01005631 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005632
Gilles Peskine449bd832023-01-11 14:50:10 +01005633 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005634
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005636
5637exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005638 psa_destroy_key(key);
5639 psa_aead_abort(&operation);
5640 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005641}
5642/* END_CASE */
5643
5644/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005645void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5646 int alg_arg,
5647 data_t *nonce,
5648 data_t *additional_data,
5649 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005650{
5651 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5652 psa_key_type_t key_type = key_type_arg;
5653 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005654 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005655 unsigned char *output_data = NULL;
5656 unsigned char *final_data = NULL;
5657 size_t output_size = 0;
5658 size_t finish_output_size = 0;
5659 size_t output_length = 0;
5660 size_t key_bits = 0;
5661 size_t tag_length = 0;
5662 size_t tag_size = 0;
5663 size_t nonce_length = 0;
5664 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5665 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5666 size_t output_part_length = 0;
5667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5668
Gilles Peskine449bd832023-01-11 14:50:10 +01005669 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005670
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 psa_set_key_usage_flags(&attributes,
5672 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5673 psa_set_key_algorithm(&attributes, alg);
5674 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005675
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5677 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005678
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5680 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005681
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005683
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005685
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005687
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005688 TEST_CALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005689
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005691
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005693
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005694 TEST_CALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005695
5696 /* Test all operations error without calling setup first. */
5697
Gilles Peskine449bd832023-01-11 14:50:10 +01005698 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5699 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005700
Gilles Peskine449bd832023-01-11 14:50:10 +01005701 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005702
Gilles Peskine449bd832023-01-11 14:50:10 +01005703 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5704 PSA_AEAD_NONCE_MAX_SIZE,
5705 &nonce_length),
5706 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005707
Gilles Peskine449bd832023-01-11 14:50:10 +01005708 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005709
Paul Elliott481be342021-07-16 17:38:47 +01005710 /* ------------------------------------------------------- */
5711
Gilles Peskine449bd832023-01-11 14:50:10 +01005712 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5713 input_data->len),
5714 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005715
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005717
Paul Elliott481be342021-07-16 17:38:47 +01005718 /* ------------------------------------------------------- */
5719
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5721 additional_data->len),
5722 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005723
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005725
Paul Elliott481be342021-07-16 17:38:47 +01005726 /* ------------------------------------------------------- */
5727
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5729 input_data->len, output_data,
5730 output_size, &output_length),
5731 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005732
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005734
Paul Elliott481be342021-07-16 17:38:47 +01005735 /* ------------------------------------------------------- */
5736
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5738 finish_output_size,
5739 &output_part_length,
5740 tag_buffer, tag_length,
5741 &tag_size),
5742 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005743
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005745
Paul Elliott481be342021-07-16 17:38:47 +01005746 /* ------------------------------------------------------- */
5747
Gilles Peskine449bd832023-01-11 14:50:10 +01005748 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5749 finish_output_size,
5750 &output_part_length,
5751 tag_buffer,
5752 tag_length),
5753 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005754
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005756
5757 /* Test for double setups. */
5758
Gilles Peskine449bd832023-01-11 14:50:10 +01005759 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005760
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5762 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005763
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005765
Paul Elliott481be342021-07-16 17:38:47 +01005766 /* ------------------------------------------------------- */
5767
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005769
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5771 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005772
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005774
Paul Elliott374a2be2021-07-16 17:53:40 +01005775 /* ------------------------------------------------------- */
5776
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005778
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5780 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005781
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005783
5784 /* ------------------------------------------------------- */
5785
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005787
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5789 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005790
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005792
Paul Elliottc23a9a02021-06-21 18:32:46 +01005793 /* Test for not setting a nonce. */
5794
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005796
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5798 additional_data->len),
5799 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005800
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005802
Paul Elliott7f628422021-09-01 12:08:29 +01005803 /* ------------------------------------------------------- */
5804
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005806
Gilles Peskine449bd832023-01-11 14:50:10 +01005807 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5808 input_data->len, output_data,
5809 output_size, &output_length),
5810 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005811
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005813
Paul Elliottbdc2c682021-09-21 18:37:10 +01005814 /* ------------------------------------------------------- */
5815
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005817
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5819 finish_output_size,
5820 &output_part_length,
5821 tag_buffer, tag_length,
5822 &tag_size),
5823 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005824
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005826
5827 /* ------------------------------------------------------- */
5828
Gilles Peskine449bd832023-01-11 14:50:10 +01005829 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005830
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5832 finish_output_size,
5833 &output_part_length,
5834 tag_buffer,
5835 tag_length),
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
Paul Elliottc23a9a02021-06-21 18:32:46 +01005840 /* Test for double setting nonce. */
5841
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005843
Gilles Peskine449bd832023-01-11 14:50:10 +01005844 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005845
Gilles Peskine449bd832023-01-11 14:50:10 +01005846 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5847 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005848
Gilles Peskine449bd832023-01-11 14:50:10 +01005849 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005850
Paul Elliott374a2be2021-07-16 17:53:40 +01005851 /* Test for double generating nonce. */
5852
Gilles Peskine449bd832023-01-11 14:50:10 +01005853 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005854
Gilles Peskine449bd832023-01-11 14:50:10 +01005855 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5856 PSA_AEAD_NONCE_MAX_SIZE,
5857 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005858
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5860 PSA_AEAD_NONCE_MAX_SIZE,
5861 &nonce_length),
5862 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005863
5864
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005866
5867 /* Test for generate nonce then set and vice versa */
5868
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005870
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5872 PSA_AEAD_NONCE_MAX_SIZE,
5873 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005874
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5876 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005877
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005879
Andrzej Kurekad837522021-12-15 15:28:49 +01005880 /* Test for generating nonce after calling set lengths */
5881
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005883
Gilles Peskine449bd832023-01-11 14:50:10 +01005884 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5885 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005886
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5888 PSA_AEAD_NONCE_MAX_SIZE,
5889 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005890
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005892
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005893 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005894
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 if (operation.alg == PSA_ALG_CCM) {
5898 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5899 input_data->len),
5900 PSA_ERROR_INVALID_ARGUMENT);
5901 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5902 PSA_AEAD_NONCE_MAX_SIZE,
5903 &nonce_length),
5904 PSA_ERROR_BAD_STATE);
5905 } else {
5906 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5907 input_data->len));
5908 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5909 PSA_AEAD_NONCE_MAX_SIZE,
5910 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005911 }
5912
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005914
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005915 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005916#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005918
Gilles Peskine449bd832023-01-11 14:50:10 +01005919 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5920 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5921 input_data->len),
5922 PSA_ERROR_INVALID_ARGUMENT);
5923 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5924 PSA_AEAD_NONCE_MAX_SIZE,
5925 &nonce_length),
5926 PSA_ERROR_BAD_STATE);
5927 } else {
5928 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5929 input_data->len));
5930 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5931 PSA_AEAD_NONCE_MAX_SIZE,
5932 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005933 }
5934
Gilles Peskine449bd832023-01-11 14:50:10 +01005935 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005936#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005937
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005938 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005939
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005941
Gilles Peskine449bd832023-01-11 14:50:10 +01005942 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5943 PSA_AEAD_NONCE_MAX_SIZE,
5944 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005945
Gilles Peskine449bd832023-01-11 14:50:10 +01005946 if (operation.alg == PSA_ALG_CCM) {
5947 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5948 input_data->len),
5949 PSA_ERROR_INVALID_ARGUMENT);
5950 } else {
5951 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5952 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005953 }
5954
Gilles Peskine449bd832023-01-11 14:50:10 +01005955 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005956
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005957 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005958 /* Test for setting nonce after calling set lengths */
5959
Gilles Peskine449bd832023-01-11 14:50:10 +01005960 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005961
Gilles Peskine449bd832023-01-11 14:50:10 +01005962 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5963 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005964
Gilles Peskine449bd832023-01-11 14:50:10 +01005965 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005966
Gilles Peskine449bd832023-01-11 14:50:10 +01005967 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005968
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005969 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005970
Gilles Peskine449bd832023-01-11 14:50:10 +01005971 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005972
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 if (operation.alg == PSA_ALG_CCM) {
5974 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5975 input_data->len),
5976 PSA_ERROR_INVALID_ARGUMENT);
5977 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5978 PSA_ERROR_BAD_STATE);
5979 } else {
5980 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5981 input_data->len));
5982 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005983 }
5984
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005986
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005987 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005988#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005990
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5992 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5993 input_data->len),
5994 PSA_ERROR_INVALID_ARGUMENT);
5995 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5996 PSA_ERROR_BAD_STATE);
5997 } else {
5998 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5999 input_data->len));
6000 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006001 }
6002
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00006004#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006005
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006006 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006007
Gilles Peskine449bd832023-01-11 14:50:10 +01006008 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006009
Gilles Peskine449bd832023-01-11 14:50:10 +01006010 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006011
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 if (operation.alg == PSA_ALG_CCM) {
6013 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6014 input_data->len),
6015 PSA_ERROR_INVALID_ARGUMENT);
6016 } else {
6017 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6018 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006019 }
6020
Gilles Peskine449bd832023-01-11 14:50:10 +01006021 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006022
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006023 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006024#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006026
Gilles Peskine449bd832023-01-11 14:50:10 +01006027 if (operation.alg == PSA_ALG_GCM) {
6028 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6029 SIZE_MAX),
6030 PSA_ERROR_INVALID_ARGUMENT);
6031 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6032 PSA_ERROR_BAD_STATE);
6033 } else if (operation.alg != PSA_ALG_CCM) {
6034 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6035 SIZE_MAX));
6036 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006037 }
6038
Gilles Peskine449bd832023-01-11 14:50:10 +01006039 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006040#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006041
Tom Cosgrove1797b052022-12-04 17:19:59 +00006042 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006043#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006044 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006045
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006047
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 if (operation.alg == PSA_ALG_GCM) {
6049 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6050 SIZE_MAX),
6051 PSA_ERROR_INVALID_ARGUMENT);
6052 } else if (operation.alg != PSA_ALG_CCM) {
6053 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6054 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006055 }
6056
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006058#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006059
6060 /* ------------------------------------------------------- */
6061
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006063
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006065
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6067 PSA_AEAD_NONCE_MAX_SIZE,
6068 &nonce_length),
6069 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006070
Gilles Peskine449bd832023-01-11 14:50:10 +01006071 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006072
Paul Elliott7220cae2021-06-22 17:25:57 +01006073 /* Test for generating nonce in decrypt setup. */
6074
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006076
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6078 PSA_AEAD_NONCE_MAX_SIZE,
6079 &nonce_length),
6080 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006081
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006083
Paul Elliottc23a9a02021-06-21 18:32:46 +01006084 /* Test for setting lengths twice. */
6085
Gilles Peskine449bd832023-01-11 14:50:10 +01006086 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006087
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006089
Gilles Peskine449bd832023-01-11 14:50:10 +01006090 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6091 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006092
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6094 input_data->len),
6095 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006096
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006098
Andrzej Kurekad837522021-12-15 15:28:49 +01006099 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006100
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006102
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006104
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006106
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6108 additional_data->len),
6109 PSA_ERROR_BAD_STATE);
6110 } else {
6111 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6112 additional_data->len));
6113
6114 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6115 input_data->len),
6116 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006117 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006119
6120 /* ------------------------------------------------------- */
6121
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006123
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006125
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 if (operation.alg == PSA_ALG_CCM) {
6127 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6128 input_data->len, output_data,
6129 output_size, &output_length),
6130 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006131
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 } else {
6133 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6134 input_data->len, output_data,
6135 output_size, &output_length));
6136
6137 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6138 input_data->len),
6139 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006140 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006142
6143 /* ------------------------------------------------------- */
6144
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006146
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006148
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 if (operation.alg == PSA_ALG_CCM) {
6150 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6151 finish_output_size,
6152 &output_part_length,
6153 tag_buffer, tag_length,
6154 &tag_size));
6155 } else {
6156 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6157 finish_output_size,
6158 &output_part_length,
6159 tag_buffer, tag_length,
6160 &tag_size));
6161
6162 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6163 input_data->len),
6164 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006165 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006167
6168 /* Test for setting lengths after generating nonce + already starting data. */
6169
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006171
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6173 PSA_AEAD_NONCE_MAX_SIZE,
6174 &nonce_length));
6175 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006176
Gilles Peskine449bd832023-01-11 14:50:10 +01006177 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6178 additional_data->len),
6179 PSA_ERROR_BAD_STATE);
6180 } else {
6181 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6182 additional_data->len));
6183
6184 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6185 input_data->len),
6186 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006187 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006189
6190 /* ------------------------------------------------------- */
6191
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006193
Gilles Peskine449bd832023-01-11 14:50:10 +01006194 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6195 PSA_AEAD_NONCE_MAX_SIZE,
6196 &nonce_length));
6197 if (operation.alg == PSA_ALG_CCM) {
6198 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6199 input_data->len, output_data,
6200 output_size, &output_length),
6201 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006202
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 } else {
6204 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6205 input_data->len, output_data,
6206 output_size, &output_length));
6207
6208 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6209 input_data->len),
6210 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006211 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006213
6214 /* ------------------------------------------------------- */
6215
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006217
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6219 PSA_AEAD_NONCE_MAX_SIZE,
6220 &nonce_length));
6221 if (operation.alg == PSA_ALG_CCM) {
6222 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6223 finish_output_size,
6224 &output_part_length,
6225 tag_buffer, tag_length,
6226 &tag_size));
6227 } else {
6228 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6229 finish_output_size,
6230 &output_part_length,
6231 tag_buffer, tag_length,
6232 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006233
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6235 input_data->len),
6236 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006237 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006239
Paul Elliott243080c2021-07-21 19:01:17 +01006240 /* Test for not sending any additional data or data after setting non zero
6241 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006242
Gilles Peskine449bd832023-01-11 14:50:10 +01006243 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006244
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006246
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6248 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006249
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6251 finish_output_size,
6252 &output_part_length,
6253 tag_buffer, tag_length,
6254 &tag_size),
6255 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006256
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006258
Paul Elliott243080c2021-07-21 19:01:17 +01006259 /* Test for not sending any additional data or data after setting non-zero
6260 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006261
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006263
Gilles Peskine449bd832023-01-11 14:50:10 +01006264 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006265
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6267 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006268
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6270 finish_output_size,
6271 &output_part_length,
6272 tag_buffer,
6273 tag_length),
6274 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006275
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006277
Paul Elliott243080c2021-07-21 19:01:17 +01006278 /* Test for not sending any additional data after setting a non-zero length
6279 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006280
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006282
Gilles Peskine449bd832023-01-11 14:50:10 +01006283 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006284
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6286 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006287
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6289 input_data->len, output_data,
6290 output_size, &output_length),
6291 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006292
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006294
Paul Elliottf94bd992021-09-19 18:15:59 +01006295 /* Test for not sending any data after setting a non-zero length for it.*/
6296
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006298
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006300
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6302 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006303
Gilles Peskine449bd832023-01-11 14:50:10 +01006304 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6305 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006306
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6308 finish_output_size,
6309 &output_part_length,
6310 tag_buffer, tag_length,
6311 &tag_size),
6312 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006313
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006315
Paul Elliottb0450fe2021-09-01 15:06:26 +01006316 /* Test for sending too much additional data after setting lengths. */
6317
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006319
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006321
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006323
6324
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6326 additional_data->len),
6327 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006328
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006330
Paul Elliotta2a09b02021-09-22 14:56:40 +01006331 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006332
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006334
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006336
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6338 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006339
Gilles Peskine449bd832023-01-11 14:50:10 +01006340 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6341 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006342
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6344 1),
6345 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006346
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006348
Paul Elliottb0450fe2021-09-01 15:06:26 +01006349 /* Test for sending too much data after setting lengths. */
6350
Gilles Peskine449bd832023-01-11 14:50:10 +01006351 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006352
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006354
Gilles Peskine449bd832023-01-11 14:50:10 +01006355 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006356
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6358 input_data->len, output_data,
6359 output_size, &output_length),
6360 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006361
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006363
Paul Elliotta2a09b02021-09-22 14:56:40 +01006364 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006365
Gilles Peskine449bd832023-01-11 14:50:10 +01006366 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006367
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006369
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6371 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006372
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6374 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006375
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6377 input_data->len, output_data,
6378 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006379
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6381 1, output_data,
6382 output_size, &output_length),
6383 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006384
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006386
Paul Elliottc23a9a02021-06-21 18:32:46 +01006387 /* Test sending additional data after data. */
6388
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006390
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006392
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 if (operation.alg != PSA_ALG_CCM) {
6394 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6395 input_data->len, output_data,
6396 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006397
Gilles Peskine449bd832023-01-11 14:50:10 +01006398 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6399 additional_data->len),
6400 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006401 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006403
Paul Elliott534d0b42021-06-22 19:15:20 +01006404 /* Test calling finish on decryption. */
6405
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006407
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006409
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6411 finish_output_size,
6412 &output_part_length,
6413 tag_buffer, tag_length,
6414 &tag_size),
6415 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006416
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006418
6419 /* Test calling verify on encryption. */
6420
Gilles Peskine449bd832023-01-11 14:50:10 +01006421 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006422
Gilles Peskine449bd832023-01-11 14:50:10 +01006423 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006424
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6426 finish_output_size,
6427 &output_part_length,
6428 tag_buffer,
6429 tag_length),
6430 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006431
Gilles Peskine449bd832023-01-11 14:50:10 +01006432 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006433
6434
Paul Elliottc23a9a02021-06-21 18:32:46 +01006435exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 psa_destroy_key(key);
6437 psa_aead_abort(&operation);
6438 mbedtls_free(output_data);
6439 mbedtls_free(final_data);
6440 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006441}
6442/* END_CASE */
6443
6444/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006445void signature_size(int type_arg,
6446 int bits,
6447 int alg_arg,
6448 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006449{
6450 psa_key_type_t type = type_arg;
6451 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006453
Gilles Peskine449bd832023-01-11 14:50:10 +01006454 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006455
Gilles Peskinee59236f2018-01-27 23:32:46 +01006456exit:
6457 ;
6458}
6459/* END_CASE */
6460
6461/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006462void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6463 int alg_arg, data_t *input_data,
6464 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006465{
Ronald Cron5425a212020-08-04 14:58:35 +02006466 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006467 psa_key_type_t key_type = key_type_arg;
6468 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006469 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006470 unsigned char *signature = NULL;
6471 size_t signature_size;
6472 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006473 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006474
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006476
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6478 psa_set_key_algorithm(&attributes, alg);
6479 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006480
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6482 &key));
6483 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6484 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006485
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006486 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006487 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6489 key_bits, alg);
6490 TEST_ASSERT(signature_size != 0);
6491 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006492 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006493
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006494 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006495 PSA_ASSERT(psa_sign_hash(key, alg,
6496 input_data->x, input_data->len,
6497 signature, signature_size,
6498 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006499 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006500 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006501 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006502
6503exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006504 /*
6505 * Key attributes may have been returned by psa_get_key_attributes()
6506 * thus reset them as required.
6507 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006509
Gilles Peskine449bd832023-01-11 14:50:10 +01006510 psa_destroy_key(key);
6511 mbedtls_free(signature);
6512 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006513}
6514/* END_CASE */
6515
Paul Elliott712d5122022-12-07 14:03:10 +00006516/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006517/**
6518 * sign_hash_interruptible() test intentions:
6519 *
6520 * Note: This test can currently only handle ECDSA.
6521 *
6522 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006523 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006524 *
6525 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6526 * expected for different max_ops values.
6527 *
6528 * 3. Test that the number of ops done prior to start and after abort is zero
6529 * and that each successful stage completes some ops (this is not mandated by
6530 * the PSA specification, but is currently the case).
6531 *
6532 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6533 * complete() calls does not alter the number of ops returned.
6534 */
Paul Elliott712d5122022-12-07 14:03:10 +00006535void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6536 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006537 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006538{
6539 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6540 psa_key_type_t key_type = key_type_arg;
6541 psa_algorithm_t alg = alg_arg;
6542 size_t key_bits;
6543 unsigned char *signature = NULL;
6544 size_t signature_size;
6545 size_t signature_length = 0xdeadbeef;
6546 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6547 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006548 uint32_t num_ops = 0;
6549 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006550 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006551 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006552 size_t min_completes = 0;
6553 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006554
Paul Elliott712d5122022-12-07 14:03:10 +00006555 psa_sign_hash_interruptible_operation_t operation =
6556 psa_sign_hash_interruptible_operation_init();
6557
6558 PSA_ASSERT(psa_crypto_init());
6559
6560 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6561 psa_set_key_algorithm(&attributes, alg);
6562 psa_set_key_type(&attributes, key_type);
6563
6564 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6565 &key));
6566 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6567 key_bits = psa_get_key_bits(&attributes);
6568
6569 /* Allocate a buffer which has the size advertised by the
6570 * library. */
6571 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6572 key_bits, alg);
6573 TEST_ASSERT(signature_size != 0);
6574 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006575 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006576
Paul Elliott0c683352022-12-16 19:16:56 +00006577 psa_interruptible_set_max_ops(max_ops);
6578
Paul Elliott6f600372023-02-06 18:41:05 +00006579 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6580 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006581
Paul Elliott712d5122022-12-07 14:03:10 +00006582 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6583 TEST_ASSERT(num_ops_prior == 0);
6584
6585 /* Start performing the signature. */
6586 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6587 input_data->x, input_data->len));
6588
6589 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6590 TEST_ASSERT(num_ops_prior == 0);
6591
6592 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006593 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006594 status = psa_sign_hash_complete(&operation, signature, signature_size,
6595 &signature_length);
6596
Paul Elliott0c683352022-12-16 19:16:56 +00006597 num_completes++;
6598
Paul Elliott712d5122022-12-07 14:03:10 +00006599 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6600 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006601 /* We are asserting here that every complete makes progress
6602 * (completes some ops), which is true of the internal
6603 * implementation and probably any implementation, however this is
6604 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006605 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006606
Paul Elliott712d5122022-12-07 14:03:10 +00006607 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006608
6609 /* Ensure calling get_num_ops() twice still returns the same
6610 * number of ops as previously reported. */
6611 num_ops = psa_sign_hash_get_num_ops(&operation);
6612
6613 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006614 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006615 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006616
6617 TEST_ASSERT(status == PSA_SUCCESS);
6618
Paul Elliott0c683352022-12-16 19:16:56 +00006619 TEST_LE_U(min_completes, num_completes);
6620 TEST_LE_U(num_completes, max_completes);
6621
Paul Elliott712d5122022-12-07 14:03:10 +00006622 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006623 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006624 signature, signature_length);
Paul Elliott712d5122022-12-07 14:03:10 +00006625
6626 PSA_ASSERT(psa_sign_hash_abort(&operation));
6627
Paul Elliott59ad9452022-12-18 15:09:02 +00006628 num_ops = psa_sign_hash_get_num_ops(&operation);
6629 TEST_ASSERT(num_ops == 0);
6630
Paul Elliott712d5122022-12-07 14:03:10 +00006631exit:
6632
6633 /*
6634 * Key attributes may have been returned by psa_get_key_attributes()
6635 * thus reset them as required.
6636 */
6637 psa_reset_key_attributes(&attributes);
6638
6639 psa_destroy_key(key);
6640 mbedtls_free(signature);
6641 PSA_DONE();
6642}
6643/* END_CASE */
6644
Gilles Peskine20035e32018-02-03 22:44:14 +01006645/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006646void sign_hash_fail(int key_type_arg, data_t *key_data,
6647 int alg_arg, data_t *input_data,
6648 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006649{
Ronald Cron5425a212020-08-04 14:58:35 +02006650 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006651 psa_key_type_t key_type = key_type_arg;
6652 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006653 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006654 psa_status_t actual_status;
6655 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006656 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006657 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006658 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006659
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006660 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006661
Gilles Peskine449bd832023-01-11 14:50:10 +01006662 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006663
Gilles Peskine449bd832023-01-11 14:50:10 +01006664 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6665 psa_set_key_algorithm(&attributes, alg);
6666 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006667
Gilles Peskine449bd832023-01-11 14:50:10 +01006668 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6669 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006670
Gilles Peskine449bd832023-01-11 14:50:10 +01006671 actual_status = psa_sign_hash(key, alg,
6672 input_data->x, input_data->len,
6673 signature, signature_size,
6674 &signature_length);
6675 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006676 /* The value of *signature_length is unspecified on error, but
6677 * whatever it is, it should be less than signature_size, so that
6678 * if the caller tries to read *signature_length bytes without
6679 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006680 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006681
6682exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006683 psa_reset_key_attributes(&attributes);
6684 psa_destroy_key(key);
6685 mbedtls_free(signature);
6686 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006687}
6688/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006689
Paul Elliott91007972022-12-16 12:21:24 +00006690/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006691/**
6692 * sign_hash_fail_interruptible() test intentions:
6693 *
6694 * Note: This test can currently only handle ECDSA.
6695 *
6696 * 1. Test that various failure cases for interruptible sign hash fail with the
6697 * correct error codes, and at the correct point (at start or during
6698 * complete).
6699 *
6700 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6701 * expected for different max_ops values.
6702 *
6703 * 3. Test that the number of ops done prior to start and after abort is zero
6704 * and that each successful stage completes some ops (this is not mandated by
6705 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006706 *
6707 * 4. Check that calling complete() when start() fails and complete()
6708 * after completion results in a BAD_STATE error.
6709 *
6710 * 5. Check that calling start() again after start fails results in a BAD_STATE
6711 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006712 */
Paul Elliott91007972022-12-16 12:21:24 +00006713void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6714 int alg_arg, data_t *input_data,
6715 int signature_size_arg,
6716 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006717 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006718 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006719{
6720 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6721 psa_key_type_t key_type = key_type_arg;
6722 psa_algorithm_t alg = alg_arg;
6723 size_t signature_size = signature_size_arg;
6724 psa_status_t actual_status;
6725 psa_status_t expected_start_status = expected_start_status_arg;
6726 psa_status_t expected_complete_status = expected_complete_status_arg;
6727 unsigned char *signature = NULL;
6728 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006729 uint32_t num_ops = 0;
6730 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006731 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006732 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006733 size_t min_completes = 0;
6734 size_t max_completes = 0;
6735
Paul Elliott91007972022-12-16 12:21:24 +00006736 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6737 psa_sign_hash_interruptible_operation_t operation =
6738 psa_sign_hash_interruptible_operation_init();
6739
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006740 TEST_CALLOC(signature, signature_size);
Paul Elliott91007972022-12-16 12:21:24 +00006741
6742 PSA_ASSERT(psa_crypto_init());
6743
6744 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6745 psa_set_key_algorithm(&attributes, alg);
6746 psa_set_key_type(&attributes, key_type);
6747
6748 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6749 &key));
6750
Paul Elliott0c683352022-12-16 19:16:56 +00006751 psa_interruptible_set_max_ops(max_ops);
6752
Paul Elliott6f600372023-02-06 18:41:05 +00006753 interruptible_signverify_get_minmax_completes(max_ops,
6754 expected_complete_status,
6755 &min_completes,
6756 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006757
Paul Elliott91007972022-12-16 12:21:24 +00006758 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6759 TEST_ASSERT(num_ops_prior == 0);
6760
6761 /* Start performing the signature. */
6762 actual_status = psa_sign_hash_start(&operation, key, alg,
6763 input_data->x, input_data->len);
6764
6765 TEST_EQUAL(actual_status, expected_start_status);
6766
Paul Elliottc9774412023-02-06 15:14:07 +00006767 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006768 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006769 * start failed. */
6770 actual_status = psa_sign_hash_complete(&operation, signature,
6771 signature_size,
6772 &signature_length);
6773
6774 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6775
6776 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006777 actual_status = psa_sign_hash_start(&operation, key, alg,
6778 input_data->x, input_data->len);
6779
6780 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6781 }
6782
Paul Elliott91007972022-12-16 12:21:24 +00006783 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6784 TEST_ASSERT(num_ops_prior == 0);
6785
Paul Elliott91007972022-12-16 12:21:24 +00006786 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006787 do {
Paul Elliott91007972022-12-16 12:21:24 +00006788 actual_status = psa_sign_hash_complete(&operation, signature,
6789 signature_size,
6790 &signature_length);
6791
Paul Elliott0c683352022-12-16 19:16:56 +00006792 num_completes++;
6793
Paul Elliott334d7262023-01-20 17:29:41 +00006794 if (actual_status == PSA_SUCCESS ||
6795 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006796 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006797 /* We are asserting here that every complete makes progress
6798 * (completes some ops), which is true of the internal
6799 * implementation and probably any implementation, however this is
6800 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006801 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006802
Paul Elliott91007972022-12-16 12:21:24 +00006803 num_ops_prior = num_ops;
6804 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006805 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006806
Paul Elliottc9774412023-02-06 15:14:07 +00006807 TEST_EQUAL(actual_status, expected_complete_status);
6808
Paul Elliottefebad02023-02-15 16:56:45 +00006809 /* Check that another complete returns BAD_STATE. */
6810 actual_status = psa_sign_hash_complete(&operation, signature,
6811 signature_size,
6812 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006813
Paul Elliottefebad02023-02-15 16:56:45 +00006814 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006815
Paul Elliott91007972022-12-16 12:21:24 +00006816 PSA_ASSERT(psa_sign_hash_abort(&operation));
6817
Paul Elliott59ad9452022-12-18 15:09:02 +00006818 num_ops = psa_sign_hash_get_num_ops(&operation);
6819 TEST_ASSERT(num_ops == 0);
6820
Paul Elliott91007972022-12-16 12:21:24 +00006821 /* The value of *signature_length is unspecified on error, but
6822 * whatever it is, it should be less than signature_size, so that
6823 * if the caller tries to read *signature_length bytes without
6824 * checking the error code then they don't overflow a buffer. */
6825 TEST_LE_U(signature_length, signature_size);
6826
Paul Elliott0c683352022-12-16 19:16:56 +00006827 TEST_LE_U(min_completes, num_completes);
6828 TEST_LE_U(num_completes, max_completes);
6829
Paul Elliott91007972022-12-16 12:21:24 +00006830exit:
6831 psa_reset_key_attributes(&attributes);
6832 psa_destroy_key(key);
6833 mbedtls_free(signature);
6834 PSA_DONE();
6835}
6836/* END_CASE */
6837
mohammad16038cc1cee2018-03-28 01:21:33 +03006838/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006839void sign_verify_hash(int key_type_arg, data_t *key_data,
6840 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006841{
Ronald Cron5425a212020-08-04 14:58:35 +02006842 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006843 psa_key_type_t key_type = key_type_arg;
6844 psa_algorithm_t alg = alg_arg;
6845 size_t key_bits;
6846 unsigned char *signature = NULL;
6847 size_t signature_size;
6848 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006849 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006850
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006852
Gilles Peskine449bd832023-01-11 14:50:10 +01006853 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6854 psa_set_key_algorithm(&attributes, alg);
6855 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006856
Gilles Peskine449bd832023-01-11 14:50:10 +01006857 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6858 &key));
6859 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6860 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006861
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006862 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006863 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006864 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6865 key_bits, alg);
6866 TEST_ASSERT(signature_size != 0);
6867 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006868 TEST_CALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006869
6870 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006871 PSA_ASSERT(psa_sign_hash(key, alg,
6872 input_data->x, input_data->len,
6873 signature, signature_size,
6874 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006875 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006876 TEST_LE_U(signature_length, signature_size);
6877 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006878
6879 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006880 PSA_ASSERT(psa_verify_hash(key, alg,
6881 input_data->x, input_data->len,
6882 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006883
Gilles Peskine449bd832023-01-11 14:50:10 +01006884 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006885 /* Flip a bit in the input and verify that the signature is now
6886 * detected as invalid. Flip a bit at the beginning, not at the end,
6887 * because ECDSA may ignore the last few bits of the input. */
6888 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006889 TEST_EQUAL(psa_verify_hash(key, alg,
6890 input_data->x, input_data->len,
6891 signature, signature_length),
6892 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006893 }
6894
6895exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006896 /*
6897 * Key attributes may have been returned by psa_get_key_attributes()
6898 * thus reset them as required.
6899 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006900 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006901
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 psa_destroy_key(key);
6903 mbedtls_free(signature);
6904 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006905}
6906/* END_CASE */
6907
Paul Elliott712d5122022-12-07 14:03:10 +00006908/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006909/**
6910 * sign_verify_hash_interruptible() test intentions:
6911 *
6912 * Note: This test can currently only handle ECDSA.
6913 *
Paul Elliott8c092052023-03-06 17:49:14 +00006914 * 1. Test that we can sign an input hash with the given keypair and then
6915 * afterwards verify that signature. This is currently the only way to test
6916 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00006917 *
6918 * 2. Test that after corrupting the hash, the verification detects an invalid
6919 * signature.
6920 *
6921 * 3. Test the number of calls to psa_sign_hash_complete() required are as
6922 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00006923 *
6924 * 4. Test that the number of ops done prior to starting signing and after abort
6925 * is zero and that each successful signing stage completes some ops (this is
6926 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00006927 */
Paul Elliott712d5122022-12-07 14:03:10 +00006928void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006929 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006930 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006931{
6932 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6933 psa_key_type_t key_type = key_type_arg;
6934 psa_algorithm_t alg = alg_arg;
6935 size_t key_bits;
6936 unsigned char *signature = NULL;
6937 size_t signature_size;
6938 size_t signature_length = 0xdeadbeef;
6939 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6940 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006941 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00006942 uint32_t num_ops = 0;
6943 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006944 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006945 size_t min_completes = 0;
6946 size_t max_completes = 0;
6947
Paul Elliott712d5122022-12-07 14:03:10 +00006948 psa_sign_hash_interruptible_operation_t sign_operation =
6949 psa_sign_hash_interruptible_operation_init();
6950 psa_verify_hash_interruptible_operation_t verify_operation =
6951 psa_verify_hash_interruptible_operation_init();
6952
6953 PSA_ASSERT(psa_crypto_init());
6954
Paul Elliott0c683352022-12-16 19:16:56 +00006955 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6956 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006957 psa_set_key_algorithm(&attributes, alg);
6958 psa_set_key_type(&attributes, key_type);
6959
6960 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6961 &key));
6962 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6963 key_bits = psa_get_key_bits(&attributes);
6964
6965 /* Allocate a buffer which has the size advertised by the
6966 * library. */
6967 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6968 key_bits, alg);
6969 TEST_ASSERT(signature_size != 0);
6970 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006971 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006972
Paul Elliott0c683352022-12-16 19:16:56 +00006973 psa_interruptible_set_max_ops(max_ops);
6974
Paul Elliott6f600372023-02-06 18:41:05 +00006975 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6976 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006977
Paul Elliott7c173082023-02-26 18:44:45 +00006978 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6979 TEST_ASSERT(num_ops_prior == 0);
6980
Paul Elliott712d5122022-12-07 14:03:10 +00006981 /* Start performing the signature. */
6982 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6983 input_data->x, input_data->len));
6984
Paul Elliott7c173082023-02-26 18:44:45 +00006985 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6986 TEST_ASSERT(num_ops_prior == 0);
6987
Paul Elliott712d5122022-12-07 14:03:10 +00006988 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006989 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006990
Paul Elliott0c683352022-12-16 19:16:56 +00006991 status = psa_sign_hash_complete(&sign_operation, signature,
6992 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00006993 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00006994
6995 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00006996
6997 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6998 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
6999 /* We are asserting here that every complete makes progress
7000 * (completes some ops), which is true of the internal
7001 * implementation and probably any implementation, however this is
7002 * not mandated by the PSA specification. */
7003 TEST_ASSERT(num_ops > num_ops_prior);
7004
7005 num_ops_prior = num_ops;
7006 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007007 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007008
7009 TEST_ASSERT(status == PSA_SUCCESS);
7010
Paul Elliott0c683352022-12-16 19:16:56 +00007011 TEST_LE_U(min_completes, num_completes);
7012 TEST_LE_U(num_completes, max_completes);
7013
Paul Elliott712d5122022-12-07 14:03:10 +00007014 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7015
Paul Elliott7c173082023-02-26 18:44:45 +00007016 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7017 TEST_ASSERT(num_ops == 0);
7018
Paul Elliott712d5122022-12-07 14:03:10 +00007019 /* Check that the signature length looks sensible. */
7020 TEST_LE_U(signature_length, signature_size);
7021 TEST_ASSERT(signature_length > 0);
7022
Paul Elliott0c683352022-12-16 19:16:56 +00007023 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007024
7025 /* Start verification. */
7026 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7027 input_data->x, input_data->len,
7028 signature, signature_length));
7029
7030 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007031 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007032 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007033
7034 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007035 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007036
7037 TEST_ASSERT(status == PSA_SUCCESS);
7038
Paul Elliott0c683352022-12-16 19:16:56 +00007039 TEST_LE_U(min_completes, num_completes);
7040 TEST_LE_U(num_completes, max_completes);
7041
Paul Elliott712d5122022-12-07 14:03:10 +00007042 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7043
7044 verify_operation = psa_verify_hash_interruptible_operation_init();
7045
7046 if (input_data->len != 0) {
7047 /* Flip a bit in the input and verify that the signature is now
7048 * detected as invalid. Flip a bit at the beginning, not at the end,
7049 * because ECDSA may ignore the last few bits of the input. */
7050 input_data->x[0] ^= 1;
7051
Paul Elliott712d5122022-12-07 14:03:10 +00007052 /* Start verification. */
7053 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7054 input_data->x, input_data->len,
7055 signature, signature_length));
7056
7057 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007058 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007059 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007060 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007061
7062 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7063 }
7064
7065 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7066
7067exit:
7068 /*
7069 * Key attributes may have been returned by psa_get_key_attributes()
7070 * thus reset them as required.
7071 */
7072 psa_reset_key_attributes(&attributes);
7073
7074 psa_destroy_key(key);
7075 mbedtls_free(signature);
7076 PSA_DONE();
7077}
7078/* END_CASE */
7079
Gilles Peskine9911b022018-06-29 17:30:48 +02007080/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007081void verify_hash(int key_type_arg, data_t *key_data,
7082 int alg_arg, data_t *hash_data,
7083 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007084{
Ronald Cron5425a212020-08-04 14:58:35 +02007085 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007086 psa_key_type_t key_type = key_type_arg;
7087 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007088 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007089
Gilles Peskine449bd832023-01-11 14:50:10 +01007090 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007091
Gilles Peskine449bd832023-01-11 14:50:10 +01007092 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007093
Gilles Peskine449bd832023-01-11 14:50:10 +01007094 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7095 psa_set_key_algorithm(&attributes, alg);
7096 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007097
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7099 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007100
Gilles Peskine449bd832023-01-11 14:50:10 +01007101 PSA_ASSERT(psa_verify_hash(key, alg,
7102 hash_data->x, hash_data->len,
7103 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007104
itayzafrir5c753392018-05-08 11:18:38 +03007105exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007106 psa_reset_key_attributes(&attributes);
7107 psa_destroy_key(key);
7108 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007109}
7110/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007111
Paul Elliott712d5122022-12-07 14:03:10 +00007112/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007113/**
7114 * verify_hash_interruptible() test intentions:
7115 *
7116 * Note: This test can currently only handle ECDSA.
7117 *
7118 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007119 * only). Given this test only does verification it can accept public keys as
7120 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007121 *
7122 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7123 * expected for different max_ops values.
7124 *
7125 * 3. Test that the number of ops done prior to start and after abort is zero
7126 * and that each successful stage completes some ops (this is not mandated by
7127 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007128 *
7129 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7130 * complete() calls does not alter the number of ops returned.
7131 *
7132 * 5. Test that after corrupting the hash, the verification detects an invalid
7133 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007134 */
Paul Elliott712d5122022-12-07 14:03:10 +00007135void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7136 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007137 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007138{
7139 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7140 psa_key_type_t key_type = key_type_arg;
7141 psa_algorithm_t alg = alg_arg;
7142 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7143 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007144 uint32_t num_ops = 0;
7145 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007146 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007147 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007148 size_t min_completes = 0;
7149 size_t max_completes = 0;
7150
Paul Elliott712d5122022-12-07 14:03:10 +00007151 psa_verify_hash_interruptible_operation_t operation =
7152 psa_verify_hash_interruptible_operation_init();
7153
7154 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7155
7156 PSA_ASSERT(psa_crypto_init());
7157
7158 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7159 psa_set_key_algorithm(&attributes, alg);
7160 psa_set_key_type(&attributes, key_type);
7161
7162 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7163 &key));
7164
Paul Elliott0c683352022-12-16 19:16:56 +00007165 psa_interruptible_set_max_ops(max_ops);
7166
Paul Elliott6f600372023-02-06 18:41:05 +00007167 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7168 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007169
Paul Elliott712d5122022-12-07 14:03:10 +00007170 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7171
7172 TEST_ASSERT(num_ops_prior == 0);
7173
7174 /* Start verification. */
7175 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7176 hash_data->x, hash_data->len,
7177 signature_data->x, signature_data->len)
7178 );
7179
7180 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7181
7182 TEST_ASSERT(num_ops_prior == 0);
7183
7184 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007185 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007186 status = psa_verify_hash_complete(&operation);
7187
Paul Elliott0c683352022-12-16 19:16:56 +00007188 num_completes++;
7189
Paul Elliott712d5122022-12-07 14:03:10 +00007190 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7191 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007192 /* We are asserting here that every complete makes progress
7193 * (completes some ops), which is true of the internal
7194 * implementation and probably any implementation, however this is
7195 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007196 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007197
Paul Elliott712d5122022-12-07 14:03:10 +00007198 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007199
7200 /* Ensure calling get_num_ops() twice still returns the same
7201 * number of ops as previously reported. */
7202 num_ops = psa_verify_hash_get_num_ops(&operation);
7203
7204 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007205 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007206 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007207
7208 TEST_ASSERT(status == PSA_SUCCESS);
7209
Paul Elliott0c683352022-12-16 19:16:56 +00007210 TEST_LE_U(min_completes, num_completes);
7211 TEST_LE_U(num_completes, max_completes);
7212
Paul Elliott712d5122022-12-07 14:03:10 +00007213 PSA_ASSERT(psa_verify_hash_abort(&operation));
7214
Paul Elliott59ad9452022-12-18 15:09:02 +00007215 num_ops = psa_verify_hash_get_num_ops(&operation);
7216 TEST_ASSERT(num_ops == 0);
7217
Paul Elliott8359c142023-02-24 18:40:10 +00007218 if (hash_data->len != 0) {
7219 /* Flip a bit in the hash and verify that the signature is now detected
7220 * as invalid. Flip a bit at the beginning, not at the end, because
7221 * ECDSA may ignore the last few bits of the input. */
7222 hash_data->x[0] ^= 1;
7223
7224 /* Start verification. */
7225 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7226 hash_data->x, hash_data->len,
7227 signature_data->x, signature_data->len));
7228
7229 /* Continue performing the signature until complete. */
7230 do {
7231 status = psa_verify_hash_complete(&operation);
7232 } while (status == PSA_OPERATION_INCOMPLETE);
7233
7234 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7235 }
7236
Paul Elliott712d5122022-12-07 14:03:10 +00007237exit:
7238 psa_reset_key_attributes(&attributes);
7239 psa_destroy_key(key);
7240 PSA_DONE();
7241}
7242/* END_CASE */
7243
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007244/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007245void verify_hash_fail(int key_type_arg, data_t *key_data,
7246 int alg_arg, data_t *hash_data,
7247 data_t *signature_data,
7248 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007249{
Ronald Cron5425a212020-08-04 14:58:35 +02007250 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007251 psa_key_type_t key_type = key_type_arg;
7252 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007253 psa_status_t actual_status;
7254 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007255 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007256
Gilles Peskine449bd832023-01-11 14:50:10 +01007257 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007258
Gilles Peskine449bd832023-01-11 14:50:10 +01007259 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7260 psa_set_key_algorithm(&attributes, alg);
7261 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007262
Gilles Peskine449bd832023-01-11 14:50:10 +01007263 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7264 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007265
Gilles Peskine449bd832023-01-11 14:50:10 +01007266 actual_status = psa_verify_hash(key, alg,
7267 hash_data->x, hash_data->len,
7268 signature_data->x, signature_data->len);
7269 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007270
7271exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007272 psa_reset_key_attributes(&attributes);
7273 psa_destroy_key(key);
7274 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007275}
7276/* END_CASE */
7277
Paul Elliott91007972022-12-16 12:21:24 +00007278/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007279/**
7280 * verify_hash_fail_interruptible() test intentions:
7281 *
7282 * Note: This test can currently only handle ECDSA.
7283 *
7284 * 1. Test that various failure cases for interruptible verify hash fail with
7285 * the correct error codes, and at the correct point (at start or during
7286 * complete).
7287 *
7288 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7289 * expected for different max_ops values.
7290 *
7291 * 3. Test that the number of ops done prior to start and after abort is zero
7292 * and that each successful stage completes some ops (this is not mandated by
7293 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007294 *
7295 * 4. Check that calling complete() when start() fails and complete()
7296 * after completion results in a BAD_STATE error.
7297 *
7298 * 5. Check that calling start() again after start fails results in a BAD_STATE
7299 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007300 */
Paul Elliott91007972022-12-16 12:21:24 +00007301void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7302 int alg_arg, data_t *hash_data,
7303 data_t *signature_data,
7304 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007305 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007306 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007307{
7308 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7309 psa_key_type_t key_type = key_type_arg;
7310 psa_algorithm_t alg = alg_arg;
7311 psa_status_t actual_status;
7312 psa_status_t expected_start_status = expected_start_status_arg;
7313 psa_status_t expected_complete_status = expected_complete_status_arg;
7314 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007315 uint32_t num_ops = 0;
7316 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007317 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007318 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007319 size_t min_completes = 0;
7320 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007321 psa_verify_hash_interruptible_operation_t operation =
7322 psa_verify_hash_interruptible_operation_init();
7323
7324 PSA_ASSERT(psa_crypto_init());
7325
7326 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7327 psa_set_key_algorithm(&attributes, alg);
7328 psa_set_key_type(&attributes, key_type);
7329
7330 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7331 &key));
7332
Paul Elliott0c683352022-12-16 19:16:56 +00007333 psa_interruptible_set_max_ops(max_ops);
7334
Paul Elliott6f600372023-02-06 18:41:05 +00007335 interruptible_signverify_get_minmax_completes(max_ops,
7336 expected_complete_status,
7337 &min_completes,
7338 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007339
Paul Elliott91007972022-12-16 12:21:24 +00007340 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7341 TEST_ASSERT(num_ops_prior == 0);
7342
7343 /* Start verification. */
7344 actual_status = psa_verify_hash_start(&operation, key, alg,
7345 hash_data->x, hash_data->len,
7346 signature_data->x,
7347 signature_data->len);
7348
7349 TEST_EQUAL(actual_status, expected_start_status);
7350
Paul Elliottc9774412023-02-06 15:14:07 +00007351 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007352 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007353 * start failed. */
7354 actual_status = psa_verify_hash_complete(&operation);
7355
7356 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7357
7358 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007359 actual_status = psa_verify_hash_start(&operation, key, alg,
7360 hash_data->x, hash_data->len,
7361 signature_data->x,
7362 signature_data->len);
7363
7364 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7365 }
7366
Paul Elliott91007972022-12-16 12:21:24 +00007367 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7368 TEST_ASSERT(num_ops_prior == 0);
7369
Paul Elliott91007972022-12-16 12:21:24 +00007370 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007371 do {
Paul Elliott91007972022-12-16 12:21:24 +00007372 actual_status = psa_verify_hash_complete(&operation);
7373
Paul Elliott0c683352022-12-16 19:16:56 +00007374 num_completes++;
7375
Paul Elliott334d7262023-01-20 17:29:41 +00007376 if (actual_status == PSA_SUCCESS ||
7377 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007378 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007379 /* We are asserting here that every complete makes progress
7380 * (completes some ops), which is true of the internal
7381 * implementation and probably any implementation, however this is
7382 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007383 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007384
Paul Elliott91007972022-12-16 12:21:24 +00007385 num_ops_prior = num_ops;
7386 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007387 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007388
Paul Elliottc9774412023-02-06 15:14:07 +00007389 TEST_EQUAL(actual_status, expected_complete_status);
7390
Paul Elliottefebad02023-02-15 16:56:45 +00007391 /* Check that another complete returns BAD_STATE. */
7392 actual_status = psa_verify_hash_complete(&operation);
7393 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007394
Paul Elliott0c683352022-12-16 19:16:56 +00007395 TEST_LE_U(min_completes, num_completes);
7396 TEST_LE_U(num_completes, max_completes);
7397
Paul Elliott91007972022-12-16 12:21:24 +00007398 PSA_ASSERT(psa_verify_hash_abort(&operation));
7399
Paul Elliott59ad9452022-12-18 15:09:02 +00007400 num_ops = psa_verify_hash_get_num_ops(&operation);
7401 TEST_ASSERT(num_ops == 0);
7402
Paul Elliott91007972022-12-16 12:21:24 +00007403exit:
7404 psa_reset_key_attributes(&attributes);
7405 psa_destroy_key(key);
7406 PSA_DONE();
7407}
7408/* END_CASE */
7409
Paul Elliott20a36062022-12-18 13:21:25 +00007410/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007411/**
7412 * interruptible_signverify_hash_state_test() test intentions:
7413 *
7414 * Note: This test can currently only handle ECDSA.
7415 *
7416 * 1. Test that calling the various interruptible sign and verify hash functions
7417 * in incorrect orders returns BAD_STATE errors.
7418 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007419void interruptible_signverify_hash_state_test(int key_type_arg,
7420 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007421{
7422 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7423 psa_key_type_t key_type = key_type_arg;
7424 psa_algorithm_t alg = alg_arg;
7425 size_t key_bits;
7426 unsigned char *signature = NULL;
7427 size_t signature_size;
7428 size_t signature_length = 0xdeadbeef;
7429 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7430 psa_sign_hash_interruptible_operation_t sign_operation =
7431 psa_sign_hash_interruptible_operation_init();
7432 psa_verify_hash_interruptible_operation_t verify_operation =
7433 psa_verify_hash_interruptible_operation_init();
7434
7435 PSA_ASSERT(psa_crypto_init());
7436
7437 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7438 PSA_KEY_USAGE_VERIFY_HASH);
7439 psa_set_key_algorithm(&attributes, alg);
7440 psa_set_key_type(&attributes, key_type);
7441
7442 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7443 &key));
7444 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7445 key_bits = psa_get_key_bits(&attributes);
7446
7447 /* Allocate a buffer which has the size advertised by the
7448 * library. */
7449 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7450 key_bits, alg);
7451 TEST_ASSERT(signature_size != 0);
7452 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007453 TEST_CALLOC(signature, signature_size);
Paul Elliott20a36062022-12-18 13:21:25 +00007454
7455 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7456
7457 /* --- Attempt completes prior to starts --- */
7458 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7459 signature_size,
7460 &signature_length),
7461 PSA_ERROR_BAD_STATE);
7462
7463 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7464
7465 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7466 PSA_ERROR_BAD_STATE);
7467
7468 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7469
7470 /* --- Aborts in all other places. --- */
7471 psa_sign_hash_abort(&sign_operation);
7472
7473 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7474 input_data->x, input_data->len));
7475
7476 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7477
7478 psa_interruptible_set_max_ops(1);
7479
7480 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7481 input_data->x, input_data->len));
7482
7483 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7484 signature_size,
7485 &signature_length),
7486 PSA_OPERATION_INCOMPLETE);
7487
7488 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7489
7490 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7491
7492 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7493 input_data->x, input_data->len));
7494
7495 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7496 signature_size,
7497 &signature_length));
7498
7499 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7500
7501 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7502
7503 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7504 input_data->x, input_data->len,
7505 signature, signature_length));
7506
7507 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7508
7509 psa_interruptible_set_max_ops(1);
7510
7511 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7512 input_data->x, input_data->len,
7513 signature, signature_length));
7514
7515 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7516 PSA_OPERATION_INCOMPLETE);
7517
7518 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7519
7520 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7521
7522 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7523 input_data->x, input_data->len,
7524 signature, signature_length));
7525
7526 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7527
7528 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7529
7530 /* --- Attempt double starts. --- */
7531
7532 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7533 input_data->x, input_data->len));
7534
7535 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7536 input_data->x, input_data->len),
7537 PSA_ERROR_BAD_STATE);
7538
7539 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7540
7541 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7542 input_data->x, input_data->len,
7543 signature, signature_length));
7544
7545 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7546 input_data->x, input_data->len,
7547 signature, signature_length),
7548 PSA_ERROR_BAD_STATE);
7549
7550 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7551
Paul Elliott76d671a2023-02-07 17:45:18 +00007552exit:
7553 /*
7554 * Key attributes may have been returned by psa_get_key_attributes()
7555 * thus reset them as required.
7556 */
7557 psa_reset_key_attributes(&attributes);
7558
7559 psa_destroy_key(key);
7560 mbedtls_free(signature);
7561 PSA_DONE();
7562}
7563/* END_CASE */
7564
7565/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007566/**
Paul Elliottc2033502023-02-26 17:09:14 +00007567 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007568 *
7569 * Note: This test can currently only handle ECDSA.
7570 *
7571 * 1. Test various edge cases in the interruptible sign and verify hash
7572 * interfaces.
7573 */
Paul Elliottc2033502023-02-26 17:09:14 +00007574void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007575 data_t *key_data, int alg_arg, data_t *input_data)
7576{
7577 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7578 psa_key_type_t key_type = key_type_arg;
7579 psa_algorithm_t alg = alg_arg;
7580 size_t key_bits;
7581 unsigned char *signature = NULL;
7582 size_t signature_size;
7583 size_t signature_length = 0xdeadbeef;
7584 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7585 uint8_t *input_buffer = NULL;
7586 psa_sign_hash_interruptible_operation_t sign_operation =
7587 psa_sign_hash_interruptible_operation_init();
7588 psa_verify_hash_interruptible_operation_t verify_operation =
7589 psa_verify_hash_interruptible_operation_init();
7590
7591 PSA_ASSERT(psa_crypto_init());
7592
7593 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7594 PSA_KEY_USAGE_VERIFY_HASH);
7595 psa_set_key_algorithm(&attributes, alg);
7596 psa_set_key_type(&attributes, key_type);
7597
7598 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7599 &key));
7600 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7601 key_bits = psa_get_key_bits(&attributes);
7602
7603 /* Allocate a buffer which has the size advertised by the
7604 * library. */
7605 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7606 key_bits, alg);
7607 TEST_ASSERT(signature_size != 0);
7608 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007609 TEST_CALLOC(signature, signature_size);
Paul Elliott76d671a2023-02-07 17:45:18 +00007610
Paul Elliott20a36062022-12-18 13:21:25 +00007611 /* --- Change function inputs mid run, to cause an error (sign only,
7612 * verify passes all inputs to start. --- */
7613
7614 psa_interruptible_set_max_ops(1);
7615
7616 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7617 input_data->x, input_data->len));
7618
7619 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7620 signature_size,
7621 &signature_length),
7622 PSA_OPERATION_INCOMPLETE);
7623
7624 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7625 0,
7626 &signature_length),
7627 PSA_ERROR_BUFFER_TOO_SMALL);
7628
Paul Elliottc9774412023-02-06 15:14:07 +00007629 /* And test that this invalidates the operation. */
7630 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7631 0,
7632 &signature_length),
7633 PSA_ERROR_BAD_STATE);
7634
Paul Elliott20a36062022-12-18 13:21:25 +00007635 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7636
Paul Elliottf9c91a72023-02-05 18:06:38 +00007637 /* Trash the hash buffer in between start and complete, to ensure
7638 * no reliance on external buffers. */
7639 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7640
7641 input_buffer = mbedtls_calloc(1, input_data->len);
7642 TEST_ASSERT(input_buffer != NULL);
7643
7644 memcpy(input_buffer, input_data->x, input_data->len);
7645
7646 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7647 input_buffer, input_data->len));
7648
7649 memset(input_buffer, '!', input_data->len);
7650 mbedtls_free(input_buffer);
7651 input_buffer = NULL;
7652
7653 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7654 signature_size,
7655 &signature_length));
7656
7657 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7658
7659 input_buffer = mbedtls_calloc(1, input_data->len);
7660 TEST_ASSERT(input_buffer != NULL);
7661
7662 memcpy(input_buffer, input_data->x, input_data->len);
7663
7664 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7665 input_buffer, input_data->len,
7666 signature, signature_length));
7667
7668 memset(input_buffer, '!', input_data->len);
7669 mbedtls_free(input_buffer);
7670 input_buffer = NULL;
7671
7672 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7673
7674 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7675
Paul Elliott20a36062022-12-18 13:21:25 +00007676exit:
7677 /*
7678 * Key attributes may have been returned by psa_get_key_attributes()
7679 * thus reset them as required.
7680 */
7681 psa_reset_key_attributes(&attributes);
7682
7683 psa_destroy_key(key);
7684 mbedtls_free(signature);
7685 PSA_DONE();
7686}
7687/* END_CASE */
7688
Paul Elliotta4cb9092023-02-07 18:01:55 +00007689/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007690/**
Paul Elliott57702242023-02-26 20:36:10 +00007691 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007692 *
7693 * Note: This test can currently only handle ECDSA.
7694 *
7695 * 1. Test that setting max ops is reflected in both interruptible sign and
7696 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007697 * 2. Test that changing the value of max_ops to unlimited during an operation
7698 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007699 *
7700 * 3. Test that calling get_num_ops() between complete calls gives the same
7701 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007702 */
Paul Elliott57702242023-02-26 20:36:10 +00007703void interruptible_signverify_hash_ops_tests(int key_type_arg,
7704 data_t *key_data, int alg_arg,
7705 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007706{
7707 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7708 psa_key_type_t key_type = key_type_arg;
7709 psa_algorithm_t alg = alg_arg;
7710 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007711 size_t key_bits;
7712 unsigned char *signature = NULL;
7713 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007714 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007715 uint32_t num_ops = 0;
7716 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7717
Paul Elliotta4cb9092023-02-07 18:01:55 +00007718 psa_sign_hash_interruptible_operation_t sign_operation =
7719 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007720 psa_verify_hash_interruptible_operation_t verify_operation =
7721 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007722
7723 PSA_ASSERT(psa_crypto_init());
7724
7725 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7726 PSA_KEY_USAGE_VERIFY_HASH);
7727 psa_set_key_algorithm(&attributes, alg);
7728 psa_set_key_type(&attributes, key_type);
7729
Paul Elliottf1743e22023-02-15 18:44:16 +00007730 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7731 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7732 key_bits = psa_get_key_bits(&attributes);
7733
7734 /* Allocate a buffer which has the size advertised by the
7735 * library. */
7736 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7737
7738 TEST_ASSERT(signature_size != 0);
7739 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007740 TEST_CALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007741
7742 /* Check that default max ops gets set if we don't set it. */
7743 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7744 input_data->x, input_data->len));
7745
7746 TEST_EQUAL(psa_interruptible_get_max_ops(),
7747 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7748
7749 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7750
Paul Elliottf1743e22023-02-15 18:44:16 +00007751 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7752 input_data->x, input_data->len,
7753 signature, signature_size));
7754
7755 TEST_EQUAL(psa_interruptible_get_max_ops(),
7756 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7757
7758 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7759
Paul Elliotta4cb9092023-02-07 18:01:55 +00007760 /* Check that max ops gets set properly. */
7761
7762 psa_interruptible_set_max_ops(0xbeef);
7763
Paul Elliottf1743e22023-02-15 18:44:16 +00007764 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007765
Paul Elliott9e8819f2023-02-26 19:01:35 +00007766 /* --- Ensure changing the max ops mid operation works (operation should
7767 * complete successfully after setting max ops to unlimited --- */
7768 psa_interruptible_set_max_ops(1);
7769
7770 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7771 input_data->x, input_data->len));
7772
7773 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7774 signature_size,
7775 &signature_length),
7776 PSA_OPERATION_INCOMPLETE);
7777
7778 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7779
7780 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7781 signature_size,
7782 &signature_length));
7783
7784 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7785
7786 psa_interruptible_set_max_ops(1);
7787
7788 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7789 input_data->x, input_data->len,
7790 signature, signature_length));
7791
7792 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7793 PSA_OPERATION_INCOMPLETE);
7794
7795 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7796
7797 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7798
7799 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7800
Paul Elliottc1e04002023-02-26 20:27:23 +00007801 /* --- Test that not calling get_num_ops inbetween complete calls does not
7802 * result in lost ops. ---*/
7803
7804 psa_interruptible_set_max_ops(1);
7805
7806 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7807 input_data->x, input_data->len));
7808
7809 /* Continue performing the signature until complete. */
7810 do {
7811 status = psa_sign_hash_complete(&sign_operation, signature,
7812 signature_size,
7813 &signature_length);
7814
7815 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7816
7817 } while (status == PSA_OPERATION_INCOMPLETE);
7818
7819 PSA_ASSERT(status);
7820
7821 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7822
7823 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7824 input_data->x, input_data->len));
7825
7826 /* Continue performing the signature until complete. */
7827 do {
7828 status = psa_sign_hash_complete(&sign_operation, signature,
7829 signature_size,
7830 &signature_length);
7831 } while (status == PSA_OPERATION_INCOMPLETE);
7832
7833 PSA_ASSERT(status);
7834
7835 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7836
7837 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7838
7839 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7840 input_data->x, input_data->len,
7841 signature, signature_length));
7842
7843 /* Continue performing the verification until complete. */
7844 do {
7845 status = psa_verify_hash_complete(&verify_operation);
7846
7847 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7848
7849 } while (status == PSA_OPERATION_INCOMPLETE);
7850
7851 PSA_ASSERT(status);
7852
7853 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7854
7855 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7856 input_data->x, input_data->len,
7857 signature, signature_length));
7858
7859 /* Continue performing the verification until complete. */
7860 do {
7861 status = psa_verify_hash_complete(&verify_operation);
7862
7863 } while (status == PSA_OPERATION_INCOMPLETE);
7864
7865 PSA_ASSERT(status);
7866
7867 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7868
7869 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7870
Paul Elliotta4cb9092023-02-07 18:01:55 +00007871exit:
7872 /*
7873 * Key attributes may have been returned by psa_get_key_attributes()
7874 * thus reset them as required.
7875 */
7876 psa_reset_key_attributes(&attributes);
7877
7878 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007879 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007880 PSA_DONE();
7881}
7882/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007883
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007884/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007885void sign_message_deterministic(int key_type_arg,
7886 data_t *key_data,
7887 int alg_arg,
7888 data_t *input_data,
7889 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007890{
7891 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7892 psa_key_type_t key_type = key_type_arg;
7893 psa_algorithm_t alg = alg_arg;
7894 size_t key_bits;
7895 unsigned char *signature = NULL;
7896 size_t signature_size;
7897 size_t signature_length = 0xdeadbeef;
7898 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7899
Gilles Peskine449bd832023-01-11 14:50:10 +01007900 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007901
Gilles Peskine449bd832023-01-11 14:50:10 +01007902 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7903 psa_set_key_algorithm(&attributes, alg);
7904 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007905
Gilles Peskine449bd832023-01-11 14:50:10 +01007906 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7907 &key));
7908 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7909 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007910
Gilles Peskine449bd832023-01-11 14:50:10 +01007911 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7912 TEST_ASSERT(signature_size != 0);
7913 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007914 TEST_CALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007915
Gilles Peskine449bd832023-01-11 14:50:10 +01007916 PSA_ASSERT(psa_sign_message(key, alg,
7917 input_data->x, input_data->len,
7918 signature, signature_size,
7919 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007920
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01007921 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01007922 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007923
7924exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007925 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007926
Gilles Peskine449bd832023-01-11 14:50:10 +01007927 psa_destroy_key(key);
7928 mbedtls_free(signature);
7929 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007930
7931}
7932/* END_CASE */
7933
7934/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007935void sign_message_fail(int key_type_arg,
7936 data_t *key_data,
7937 int alg_arg,
7938 data_t *input_data,
7939 int signature_size_arg,
7940 int expected_status_arg)
7941{
7942 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7943 psa_key_type_t key_type = key_type_arg;
7944 psa_algorithm_t alg = alg_arg;
7945 size_t signature_size = signature_size_arg;
7946 psa_status_t actual_status;
7947 psa_status_t expected_status = expected_status_arg;
7948 unsigned char *signature = NULL;
7949 size_t signature_length = 0xdeadbeef;
7950 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7951
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007952 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01007953
7954 PSA_ASSERT(psa_crypto_init());
7955
7956 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7957 psa_set_key_algorithm(&attributes, alg);
7958 psa_set_key_type(&attributes, key_type);
7959
7960 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7961 &key));
7962
7963 actual_status = psa_sign_message(key, alg,
7964 input_data->x, input_data->len,
7965 signature, signature_size,
7966 &signature_length);
7967 TEST_EQUAL(actual_status, expected_status);
7968 /* The value of *signature_length is unspecified on error, but
7969 * whatever it is, it should be less than signature_size, so that
7970 * if the caller tries to read *signature_length bytes without
7971 * checking the error code then they don't overflow a buffer. */
7972 TEST_LE_U(signature_length, signature_size);
7973
7974exit:
7975 psa_reset_key_attributes(&attributes);
7976 psa_destroy_key(key);
7977 mbedtls_free(signature);
7978 PSA_DONE();
7979}
7980/* END_CASE */
7981
7982/* BEGIN_CASE */
7983void sign_verify_message(int key_type_arg,
7984 data_t *key_data,
7985 int alg_arg,
7986 data_t *input_data)
7987{
7988 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7989 psa_key_type_t key_type = key_type_arg;
7990 psa_algorithm_t alg = alg_arg;
7991 size_t key_bits;
7992 unsigned char *signature = NULL;
7993 size_t signature_size;
7994 size_t signature_length = 0xdeadbeef;
7995 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7996
7997 PSA_ASSERT(psa_crypto_init());
7998
7999 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
8000 PSA_KEY_USAGE_VERIFY_MESSAGE);
8001 psa_set_key_algorithm(&attributes, alg);
8002 psa_set_key_type(&attributes, key_type);
8003
8004 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8005 &key));
8006 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8007 key_bits = psa_get_key_bits(&attributes);
8008
8009 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8010 TEST_ASSERT(signature_size != 0);
8011 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008012 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008013
8014 PSA_ASSERT(psa_sign_message(key, alg,
8015 input_data->x, input_data->len,
8016 signature, signature_size,
8017 &signature_length));
8018 TEST_LE_U(signature_length, signature_size);
8019 TEST_ASSERT(signature_length > 0);
8020
8021 PSA_ASSERT(psa_verify_message(key, alg,
8022 input_data->x, input_data->len,
8023 signature, signature_length));
8024
8025 if (input_data->len != 0) {
8026 /* Flip a bit in the input and verify that the signature is now
8027 * detected as invalid. Flip a bit at the beginning, not at the end,
8028 * because ECDSA may ignore the last few bits of the input. */
8029 input_data->x[0] ^= 1;
8030 TEST_EQUAL(psa_verify_message(key, alg,
8031 input_data->x, input_data->len,
8032 signature, signature_length),
8033 PSA_ERROR_INVALID_SIGNATURE);
8034 }
8035
8036exit:
8037 psa_reset_key_attributes(&attributes);
8038
8039 psa_destroy_key(key);
8040 mbedtls_free(signature);
8041 PSA_DONE();
8042}
8043/* END_CASE */
8044
8045/* BEGIN_CASE */
8046void verify_message(int key_type_arg,
8047 data_t *key_data,
8048 int alg_arg,
8049 data_t *input_data,
8050 data_t *signature_data)
8051{
8052 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8053 psa_key_type_t key_type = key_type_arg;
8054 psa_algorithm_t alg = alg_arg;
8055 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8056
8057 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8058
8059 PSA_ASSERT(psa_crypto_init());
8060
8061 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8062 psa_set_key_algorithm(&attributes, alg);
8063 psa_set_key_type(&attributes, key_type);
8064
8065 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8066 &key));
8067
8068 PSA_ASSERT(psa_verify_message(key, alg,
8069 input_data->x, input_data->len,
8070 signature_data->x, signature_data->len));
8071
8072exit:
8073 psa_reset_key_attributes(&attributes);
8074 psa_destroy_key(key);
8075 PSA_DONE();
8076}
8077/* END_CASE */
8078
8079/* BEGIN_CASE */
8080void verify_message_fail(int key_type_arg,
8081 data_t *key_data,
8082 int alg_arg,
8083 data_t *hash_data,
8084 data_t *signature_data,
8085 int expected_status_arg)
8086{
8087 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8088 psa_key_type_t key_type = key_type_arg;
8089 psa_algorithm_t alg = alg_arg;
8090 psa_status_t actual_status;
8091 psa_status_t expected_status = expected_status_arg;
8092 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8093
8094 PSA_ASSERT(psa_crypto_init());
8095
8096 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8097 psa_set_key_algorithm(&attributes, alg);
8098 psa_set_key_type(&attributes, key_type);
8099
8100 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8101 &key));
8102
8103 actual_status = psa_verify_message(key, alg,
8104 hash_data->x, hash_data->len,
8105 signature_data->x,
8106 signature_data->len);
8107 TEST_EQUAL(actual_status, expected_status);
8108
8109exit:
8110 psa_reset_key_attributes(&attributes);
8111 psa_destroy_key(key);
8112 PSA_DONE();
8113}
8114/* END_CASE */
8115
8116/* BEGIN_CASE */
8117void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008118 data_t *key_data,
8119 int alg_arg,
8120 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008121 data_t *label,
8122 int expected_output_length_arg,
8123 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008124{
Ronald Cron5425a212020-08-04 14:58:35 +02008125 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008126 psa_key_type_t key_type = key_type_arg;
8127 psa_algorithm_t alg = alg_arg;
8128 size_t expected_output_length = expected_output_length_arg;
8129 size_t key_bits;
8130 unsigned char *output = NULL;
8131 size_t output_size;
8132 size_t output_length = ~0;
8133 psa_status_t actual_status;
8134 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008135 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008136
Gilles Peskine449bd832023-01-11 14:50:10 +01008137 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008138
Gilles Peskine656896e2018-06-29 19:12:28 +02008139 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008140 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8141 psa_set_key_algorithm(&attributes, alg);
8142 psa_set_key_type(&attributes, key_type);
8143 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8144 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008145
8146 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008147 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8148 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008149
Gilles Peskine449bd832023-01-11 14:50:10 +01008150 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8151 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008152 TEST_CALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008153
8154 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008155 actual_status = psa_asymmetric_encrypt(key, alg,
8156 input_data->x, input_data->len,
8157 label->x, label->len,
8158 output, output_size,
8159 &output_length);
8160 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008161 if (actual_status == PSA_SUCCESS) {
8162 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008163 } else {
8164 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008165 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008166
Gilles Peskine68428122018-06-30 18:42:41 +02008167 /* If the label is empty, the test framework puts a non-null pointer
8168 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008169 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008170 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008171 if (output_size != 0) {
8172 memset(output, 0, output_size);
8173 }
8174 actual_status = psa_asymmetric_encrypt(key, alg,
8175 input_data->x, input_data->len,
8176 NULL, label->len,
8177 output, output_size,
8178 &output_length);
8179 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008180 if (actual_status == PSA_SUCCESS) {
8181 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008182 } else {
8183 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008184 }
Gilles Peskine68428122018-06-30 18:42:41 +02008185 }
8186
Gilles Peskine656896e2018-06-29 19:12:28 +02008187exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008188 /*
8189 * Key attributes may have been returned by psa_get_key_attributes()
8190 * thus reset them as required.
8191 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008192 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008193
Gilles Peskine449bd832023-01-11 14:50:10 +01008194 psa_destroy_key(key);
8195 mbedtls_free(output);
8196 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008197}
8198/* END_CASE */
8199
8200/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008201void asymmetric_encrypt_decrypt(int key_type_arg,
8202 data_t *key_data,
8203 int alg_arg,
8204 data_t *input_data,
8205 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008206{
Ronald Cron5425a212020-08-04 14:58:35 +02008207 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008208 psa_key_type_t key_type = key_type_arg;
8209 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008210 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008211 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008212 size_t output_size;
8213 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008214 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008215 size_t output2_size;
8216 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008217 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008218
Gilles Peskine449bd832023-01-11 14:50:10 +01008219 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008220
Gilles Peskine449bd832023-01-11 14:50:10 +01008221 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8222 psa_set_key_algorithm(&attributes, alg);
8223 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008224
Gilles Peskine449bd832023-01-11 14:50:10 +01008225 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8226 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008227
8228 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008229 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8230 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008231
Gilles Peskine449bd832023-01-11 14:50:10 +01008232 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8233 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008234 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008235
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008236 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008237 TEST_LE_U(output2_size,
8238 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8239 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008240 TEST_CALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008241
Gilles Peskineeebd7382018-06-08 18:11:54 +02008242 /* We test encryption by checking that encrypt-then-decrypt gives back
8243 * the original plaintext because of the non-optional random
8244 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008245 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8246 input_data->x, input_data->len,
8247 label->x, label->len,
8248 output, output_size,
8249 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008250 /* We don't know what ciphertext length to expect, but check that
8251 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008252 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008253
Gilles Peskine449bd832023-01-11 14:50:10 +01008254 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8255 output, output_length,
8256 label->x, label->len,
8257 output2, output2_size,
8258 &output2_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008259 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008260 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008261
8262exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008263 /*
8264 * Key attributes may have been returned by psa_get_key_attributes()
8265 * thus reset them as required.
8266 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008267 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008268
Gilles Peskine449bd832023-01-11 14:50:10 +01008269 psa_destroy_key(key);
8270 mbedtls_free(output);
8271 mbedtls_free(output2);
8272 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008273}
8274/* END_CASE */
8275
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008276/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008277void asymmetric_decrypt(int key_type_arg,
8278 data_t *key_data,
8279 int alg_arg,
8280 data_t *input_data,
8281 data_t *label,
8282 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008283{
Ronald Cron5425a212020-08-04 14:58:35 +02008284 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008285 psa_key_type_t key_type = key_type_arg;
8286 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008287 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008288 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008289 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008290 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008291 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008292
Gilles Peskine449bd832023-01-11 14:50:10 +01008293 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008294
Gilles Peskine449bd832023-01-11 14:50:10 +01008295 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8296 psa_set_key_algorithm(&attributes, alg);
8297 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008298
Gilles Peskine449bd832023-01-11 14:50:10 +01008299 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8300 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008301
Gilles Peskine449bd832023-01-11 14:50:10 +01008302 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8303 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008304
8305 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008306 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8307 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008308 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008309
Gilles Peskine449bd832023-01-11 14:50:10 +01008310 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8311 input_data->x, input_data->len,
8312 label->x, label->len,
8313 output,
8314 output_size,
8315 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008316 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008317 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008318
Gilles Peskine68428122018-06-30 18:42:41 +02008319 /* If the label is empty, the test framework puts a non-null pointer
8320 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008321 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008322 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008323 if (output_size != 0) {
8324 memset(output, 0, output_size);
8325 }
8326 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8327 input_data->x, input_data->len,
8328 NULL, label->len,
8329 output,
8330 output_size,
8331 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008332 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008333 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008334 }
8335
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008336exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008337 psa_reset_key_attributes(&attributes);
8338 psa_destroy_key(key);
8339 mbedtls_free(output);
8340 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008341}
8342/* END_CASE */
8343
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008344/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008345void asymmetric_decrypt_fail(int key_type_arg,
8346 data_t *key_data,
8347 int alg_arg,
8348 data_t *input_data,
8349 data_t *label,
8350 int output_size_arg,
8351 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008352{
Ronald Cron5425a212020-08-04 14:58:35 +02008353 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008354 psa_key_type_t key_type = key_type_arg;
8355 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008356 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008357 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008358 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008359 psa_status_t actual_status;
8360 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008361 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008362
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008363 TEST_CALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008364
Gilles Peskine449bd832023-01-11 14:50:10 +01008365 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008366
Gilles Peskine449bd832023-01-11 14:50:10 +01008367 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8368 psa_set_key_algorithm(&attributes, alg);
8369 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008370
Gilles Peskine449bd832023-01-11 14:50:10 +01008371 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8372 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008373
Gilles Peskine449bd832023-01-11 14:50:10 +01008374 actual_status = psa_asymmetric_decrypt(key, alg,
8375 input_data->x, input_data->len,
8376 label->x, label->len,
8377 output, output_size,
8378 &output_length);
8379 TEST_EQUAL(actual_status, expected_status);
8380 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008381
Gilles Peskine68428122018-06-30 18:42:41 +02008382 /* If the label is empty, the test framework puts a non-null pointer
8383 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008384 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008385 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008386 if (output_size != 0) {
8387 memset(output, 0, output_size);
8388 }
8389 actual_status = psa_asymmetric_decrypt(key, alg,
8390 input_data->x, input_data->len,
8391 NULL, label->len,
8392 output, output_size,
8393 &output_length);
8394 TEST_EQUAL(actual_status, expected_status);
8395 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008396 }
8397
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008398exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008399 psa_reset_key_attributes(&attributes);
8400 psa_destroy_key(key);
8401 mbedtls_free(output);
8402 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008403}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008404/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008405
8406/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008407void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008408{
8409 /* Test each valid way of initializing the object, except for `= {0}`, as
8410 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8411 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008412 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008413 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008414 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008415 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8416 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008417
Gilles Peskine449bd832023-01-11 14:50:10 +01008418 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008419
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008420 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008421 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8422 PSA_ERROR_BAD_STATE);
8423 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8424 PSA_ERROR_BAD_STATE);
8425 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8426 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008427
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008428 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008429 PSA_ASSERT(psa_key_derivation_abort(&func));
8430 PSA_ASSERT(psa_key_derivation_abort(&init));
8431 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008432}
8433/* END_CASE */
8434
Janos Follath16de4a42019-06-13 16:32:24 +01008435/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008436void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008437{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008438 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008439 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008440 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008441
Gilles Peskine449bd832023-01-11 14:50:10 +01008442 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008443
Gilles Peskine449bd832023-01-11 14:50:10 +01008444 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8445 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008446
8447exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008448 psa_key_derivation_abort(&operation);
8449 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008450}
8451/* END_CASE */
8452
Janos Follathaf3c2a02019-06-12 12:34:34 +01008453/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008454void derive_set_capacity(int alg_arg, int capacity_arg,
8455 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008456{
8457 psa_algorithm_t alg = alg_arg;
8458 size_t capacity = capacity_arg;
8459 psa_status_t expected_status = expected_status_arg;
8460 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8461
Gilles Peskine449bd832023-01-11 14:50:10 +01008462 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008463
Gilles Peskine449bd832023-01-11 14:50:10 +01008464 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008465
Gilles Peskine449bd832023-01-11 14:50:10 +01008466 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8467 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008468
8469exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008470 psa_key_derivation_abort(&operation);
8471 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008472}
8473/* END_CASE */
8474
8475/* BEGIN_CASE */
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308476void parse_binary_string_test(data_t *input, int output)
8477{
8478 uint64_t value;
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308479 value = mbedtls_test_parse_binary_string(input);
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308480 TEST_EQUAL(value, output);
8481}
8482/* END_CASE */
8483
8484/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008485void derive_input(int alg_arg,
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308486 int step_arg1, int key_type_arg1, data_t *input1,
8487 int expected_status_arg1,
8488 int step_arg2, int key_type_arg2, data_t *input2,
8489 int expected_status_arg2,
8490 int step_arg3, int key_type_arg3, data_t *input3,
8491 int expected_status_arg3,
Gilles Peskine449bd832023-01-11 14:50:10 +01008492 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008493{
8494 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008495 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308496 uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
Gilles Peskine449bd832023-01-11 14:50:10 +01008497 psa_status_t expected_statuses[] = { expected_status_arg1,
8498 expected_status_arg2,
8499 expected_status_arg3 };
8500 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008501 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8502 MBEDTLS_SVC_KEY_ID_INIT,
8503 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008504 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8505 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8506 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008507 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008508 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008509 psa_status_t expected_output_status = expected_output_status_arg;
8510 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008511
Gilles Peskine449bd832023-01-11 14:50:10 +01008512 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008513
Gilles Peskine449bd832023-01-11 14:50:10 +01008514 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8515 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008516
Gilles Peskine449bd832023-01-11 14:50:10 +01008517 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008518
Gilles Peskine449bd832023-01-11 14:50:10 +01008519 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8520 mbedtls_test_set_step(i);
8521 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008522 /* Skip this step */
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308523 } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE &&
8524 key_types[i] != INPUT_INTEGER) {
8525 psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i]));
Gilles Peskine449bd832023-01-11 14:50:10 +01008526 PSA_ASSERT(psa_import_key(&attributes,
8527 inputs[i]->x, inputs[i]->len,
8528 &keys[i]));
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308529 if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008530 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008531 // When taking a private key as secret input, use key agreement
8532 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008533 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8534 &operation, keys[i]),
8535 expected_statuses[i]);
8536 } else {
8537 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8538 keys[i]),
8539 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008540 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008541 } else {
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308542 if (key_types[i] == INPUT_INTEGER) {
8543 TEST_EQUAL(psa_key_derivation_input_integer(
8544 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308545 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308546 expected_statuses[i]);
8547 } else {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308548 TEST_EQUAL(psa_key_derivation_input_bytes(
8549 &operation, steps[i],
8550 inputs[i]->x, inputs[i]->len),
8551 expected_statuses[i]);
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308552 }
Janos Follathaf3c2a02019-06-12 12:34:34 +01008553 }
8554 }
8555
Gilles Peskine449bd832023-01-11 14:50:10 +01008556 if (output_key_type != PSA_KEY_TYPE_NONE) {
8557 psa_reset_key_attributes(&attributes);
8558 psa_set_key_type(&attributes, output_key_type);
8559 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008560 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008561 psa_key_derivation_output_key(&attributes, &operation,
8562 &output_key);
8563 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008564 uint8_t buffer[1];
8565 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008566 psa_key_derivation_output_bytes(&operation,
8567 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008568 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008569 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008570
Janos Follathaf3c2a02019-06-12 12:34:34 +01008571exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008572 psa_key_derivation_abort(&operation);
8573 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8574 psa_destroy_key(keys[i]);
8575 }
8576 psa_destroy_key(output_key);
8577 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008578}
8579/* END_CASE */
8580
Kusumit Ghoderao42b02b92023-06-06 16:48:46 +05308581/* BEGIN_CASE*/
8582void derive_input_invalid_cost(int alg_arg, int64_t cost)
8583{
8584 psa_algorithm_t alg = alg_arg;
8585 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8586
8587 PSA_ASSERT(psa_crypto_init());
8588 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8589
8590 TEST_EQUAL(psa_key_derivation_input_integer(&operation,
8591 PSA_KEY_DERIVATION_INPUT_COST,
8592 cost),
8593 PSA_ERROR_NOT_SUPPORTED);
8594
8595exit:
8596 psa_key_derivation_abort(&operation);
8597 PSA_DONE();
8598}
8599/* END_CASE*/
8600
Janos Follathd958bb72019-07-03 15:02:16 +01008601/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008602void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008603{
Janos Follathd958bb72019-07-03 15:02:16 +01008604 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008605 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008606 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008607 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008608 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008609 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008610 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008611 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008612 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008613 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008614 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8615 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008616 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008617 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008618
Gilles Peskine449bd832023-01-11 14:50:10 +01008619 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008620
Gilles Peskine449bd832023-01-11 14:50:10 +01008621 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8622 psa_set_key_algorithm(&attributes, alg);
8623 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008624
Gilles Peskine449bd832023-01-11 14:50:10 +01008625 PSA_ASSERT(psa_import_key(&attributes,
8626 key_data, sizeof(key_data),
8627 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008628
8629 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008630 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8631 input1, input1_length,
8632 input2, input2_length,
8633 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008634 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008635 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008636
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008637 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008638 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8639 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008640
Gilles Peskine449bd832023-01-11 14:50:10 +01008641 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008642
Gilles Peskine449bd832023-01-11 14:50:10 +01008643 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8644 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008645
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008646exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008647 psa_key_derivation_abort(&operation);
8648 psa_destroy_key(key);
8649 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008650}
8651/* END_CASE */
8652
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008653/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008654void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008655{
8656 uint8_t output_buffer[16];
8657 size_t buffer_size = 16;
8658 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008659 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008660
Gilles Peskine449bd832023-01-11 14:50:10 +01008661 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8662 output_buffer, buffer_size)
8663 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008664
Gilles Peskine449bd832023-01-11 14:50:10 +01008665 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8666 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008667
Gilles Peskine449bd832023-01-11 14:50:10 +01008668 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008669
Gilles Peskine449bd832023-01-11 14:50:10 +01008670 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8671 output_buffer, buffer_size)
8672 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008673
Gilles Peskine449bd832023-01-11 14:50:10 +01008674 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8675 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008676
8677exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008678 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008679}
8680/* END_CASE */
8681
8682/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008683void derive_output(int alg_arg,
8684 int step1_arg, data_t *input1, int expected_status_arg1,
8685 int step2_arg, data_t *input2, int expected_status_arg2,
8686 int step3_arg, data_t *input3, int expected_status_arg3,
8687 int step4_arg, data_t *input4, int expected_status_arg4,
8688 data_t *key_agreement_peer_key,
8689 int requested_capacity_arg,
8690 data_t *expected_output1,
8691 data_t *expected_output2,
8692 int other_key_input_type,
8693 int key_input_type,
8694 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008695{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008696 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008697 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8698 data_t *inputs[] = { input1, input2, input3, input4 };
8699 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8700 MBEDTLS_SVC_KEY_ID_INIT,
8701 MBEDTLS_SVC_KEY_ID_INIT,
8702 MBEDTLS_SVC_KEY_ID_INIT };
8703 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8704 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008705 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008706 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008707 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008708 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008709 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008710 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008711 size_t output_buffer_size = 0;
8712 uint8_t *output_buffer = NULL;
8713 size_t expected_capacity;
8714 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008715 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8716 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8717 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8718 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008719 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008720 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008721 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008722
Gilles Peskine449bd832023-01-11 14:50:10 +01008723 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8724 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008725 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008726 }
8727 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008728 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008729 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008730 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008731 TEST_CALLOC(output_buffer, output_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008732 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008733
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008734 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008735 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8736 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8737 requested_capacity));
8738 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8739 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008740 case 0:
8741 break;
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308742 case PSA_KEY_DERIVATION_INPUT_COST:
8743 TEST_EQUAL(psa_key_derivation_input_integer(
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308744 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308745 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308746 statuses[i]);
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308747 if (statuses[i] != PSA_SUCCESS) {
8748 goto exit;
8749 }
8750 break;
8751 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Gilles Peskine1468da72019-05-29 17:35:49 +02008752 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008753 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008754 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008755 TEST_EQUAL(psa_key_derivation_input_bytes(
8756 &operation, steps[i],
8757 inputs[i]->x, inputs[i]->len),
8758 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008759
Gilles Peskine449bd832023-01-11 14:50:10 +01008760 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008761 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008762 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008763 break;
8764 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008765 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8766 psa_set_key_algorithm(&attributes1, alg);
8767 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008768
Gilles Peskine449bd832023-01-11 14:50:10 +01008769 PSA_ASSERT(psa_import_key(&attributes1,
8770 inputs[i]->x, inputs[i]->len,
8771 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008772
Gilles Peskine449bd832023-01-11 14:50:10 +01008773 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8774 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8775 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8776 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008777 }
8778
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308779 TEST_EQUAL(psa_key_derivation_input_key(&operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01008780 steps[i],
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308781 keys[i]),
8782 statuses[i]);
8783
8784 if (statuses[i] != PSA_SUCCESS) {
8785 goto exit;
8786 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008787 break;
8788 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008789 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008790 break;
8791 }
8792 break;
8793 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008794 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008795 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008796 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8797 steps[i],
8798 inputs[i]->x,
8799 inputs[i]->len),
8800 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008801 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008802 case 1: // input key, type DERIVE
8803 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008804 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8805 psa_set_key_algorithm(&attributes2, alg);
8806 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008807
8808 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008809 if (other_key_input_type == 11) {
8810 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8811 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008812
Gilles Peskine449bd832023-01-11 14:50:10 +01008813 PSA_ASSERT(psa_import_key(&attributes2,
8814 inputs[i]->x, inputs[i]->len,
8815 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008816
Gilles Peskine449bd832023-01-11 14:50:10 +01008817 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8818 steps[i],
8819 keys[i]),
8820 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008821 break;
8822 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008823 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8824 psa_set_key_algorithm(&attributes3, alg);
8825 psa_set_key_type(&attributes3,
8826 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008827
Gilles Peskine449bd832023-01-11 14:50:10 +01008828 PSA_ASSERT(psa_import_key(&attributes3,
8829 inputs[i]->x, inputs[i]->len,
8830 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008831
Gilles Peskine449bd832023-01-11 14:50:10 +01008832 TEST_EQUAL(psa_key_derivation_key_agreement(
8833 &operation,
8834 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8835 keys[i], key_agreement_peer_key->x,
8836 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008837 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008838 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008839 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008840 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008841 }
8842
Gilles Peskine449bd832023-01-11 14:50:10 +01008843 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008844 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008845 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008846 break;
8847 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008848 TEST_EQUAL(psa_key_derivation_input_bytes(
8849 &operation, steps[i],
8850 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008851
Gilles Peskine449bd832023-01-11 14:50:10 +01008852 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008853 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008854 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008855 break;
8856 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008857 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008858
Gilles Peskine449bd832023-01-11 14:50:10 +01008859 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8860 &current_capacity));
8861 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008862 expected_capacity = requested_capacity;
8863
Gilles Peskine449bd832023-01-11 14:50:10 +01008864 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008865 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8866
8867 /* For output key derivation secret must be provided using
8868 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008869 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008870 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008871 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008872
Gilles Peskine449bd832023-01-11 14:50:10 +01008873 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8874 psa_set_key_algorithm(&attributes4, alg);
8875 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8876 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008877
Gilles Peskine449bd832023-01-11 14:50:10 +01008878 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8879 &derived_key), expected_status);
8880 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008881 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008882 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008883 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008884 status = psa_key_derivation_output_bytes(&operation,
8885 output_buffer, output_sizes[i]);
8886 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008887 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008888 TEST_ASSERT(status == PSA_SUCCESS ||
8889 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008890 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008891 } else if (expected_capacity == 0 ||
8892 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008893 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008894 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008895 expected_capacity = 0;
8896 continue;
8897 }
8898 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008899 PSA_ASSERT(status);
8900 if (output_sizes[i] != 0) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008901 TEST_MEMORY_COMPARE(output_buffer, output_sizes[i],
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008902 expected_outputs[i], output_sizes[i]);
Gilles Peskine449bd832023-01-11 14:50:10 +01008903 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008904 /* Check the operation status. */
8905 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008906 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8907 &current_capacity));
8908 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008909 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008910 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008911 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008912
8913exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008914 mbedtls_free(output_buffer);
8915 psa_key_derivation_abort(&operation);
8916 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8917 psa_destroy_key(keys[i]);
8918 }
8919 psa_destroy_key(derived_key);
8920 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008921}
8922/* END_CASE */
8923
8924/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008925void derive_full(int alg_arg,
8926 data_t *key_data,
8927 data_t *input1,
8928 data_t *input2,
8929 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008930{
Ronald Cron5425a212020-08-04 14:58:35 +02008931 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008932 psa_algorithm_t alg = alg_arg;
8933 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008934 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008935 unsigned char output_buffer[16];
8936 size_t expected_capacity = requested_capacity;
8937 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008938 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008939
Gilles Peskine449bd832023-01-11 14:50:10 +01008940 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008941
Gilles Peskine449bd832023-01-11 14:50:10 +01008942 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8943 psa_set_key_algorithm(&attributes, alg);
8944 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008945
Gilles Peskine449bd832023-01-11 14:50:10 +01008946 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8947 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008948
Gilles Peskine449bd832023-01-11 14:50:10 +01008949 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8950 input1->x, input1->len,
8951 input2->x, input2->len,
8952 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008953 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008954 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008955
Gilles Peskine449bd832023-01-11 14:50:10 +01008956 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8957 &current_capacity));
8958 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008959
8960 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008961 while (current_capacity > 0) {
8962 size_t read_size = sizeof(output_buffer);
8963 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008964 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008965 }
8966 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8967 output_buffer,
8968 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008969 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008970 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8971 &current_capacity));
8972 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008973 }
8974
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008975 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008976 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8977 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008978
Gilles Peskine449bd832023-01-11 14:50:10 +01008979 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008980
8981exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008982 psa_key_derivation_abort(&operation);
8983 psa_destroy_key(key);
8984 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008985}
8986/* END_CASE */
8987
Stephan Koch78109f52023-04-12 14:19:36 +02008988/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008989void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8990 int derivation_step,
8991 int capacity, int expected_capacity_status_arg,
8992 data_t *expected_output,
8993 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008994{
8995 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
8996 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04008997 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04008998 uint8_t *output_buffer = NULL;
8999 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04009000 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
9001 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
9002 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009003
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009004 TEST_CALLOC(output_buffer, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009005 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009006
Gilles Peskine449bd832023-01-11 14:50:10 +01009007 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9008 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
9009 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009010
Gilles Peskine449bd832023-01-11 14:50:10 +01009011 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
9012 step, input->x, input->len),
9013 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009014
Gilles Peskine449bd832023-01-11 14:50:10 +01009015 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009016 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009017 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009018
Gilles Peskine449bd832023-01-11 14:50:10 +01009019 status = psa_key_derivation_output_bytes(&operation, output_buffer,
9020 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009021
Gilles Peskine449bd832023-01-11 14:50:10 +01009022 TEST_EQUAL(status, expected_output_status);
9023 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009024 TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009025 expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009026 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009027
9028exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009029 mbedtls_free(output_buffer);
9030 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009031 PSA_DONE();
9032}
9033/* END_CASE */
9034
Janos Follathe60c9052019-07-03 13:51:30 +01009035/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009036void derive_key_exercise(int alg_arg,
9037 data_t *key_data,
9038 data_t *input1,
9039 data_t *input2,
9040 int derived_type_arg,
9041 int derived_bits_arg,
9042 int derived_usage_arg,
9043 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009044{
Ronald Cron5425a212020-08-04 14:58:35 +02009045 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9046 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009047 psa_algorithm_t alg = alg_arg;
9048 psa_key_type_t derived_type = derived_type_arg;
9049 size_t derived_bits = derived_bits_arg;
9050 psa_key_usage_t derived_usage = derived_usage_arg;
9051 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009052 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009053 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009054 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009055 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009056
Gilles Peskine449bd832023-01-11 14:50:10 +01009057 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009058
Gilles Peskine449bd832023-01-11 14:50:10 +01009059 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9060 psa_set_key_algorithm(&attributes, alg);
9061 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9062 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9063 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009064
9065 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009066 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9067 input1->x, input1->len,
9068 input2->x, input2->len,
9069 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009070 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009071 }
Janos Follathe60c9052019-07-03 13:51:30 +01009072
Gilles Peskine449bd832023-01-11 14:50:10 +01009073 psa_set_key_usage_flags(&attributes, derived_usage);
9074 psa_set_key_algorithm(&attributes, derived_alg);
9075 psa_set_key_type(&attributes, derived_type);
9076 psa_set_key_bits(&attributes, derived_bits);
9077 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9078 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009079
9080 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009081 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9082 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9083 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009084
9085 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009086 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009087 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009088 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009089
9090exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009091 /*
9092 * Key attributes may have been returned by psa_get_key_attributes()
9093 * thus reset them as required.
9094 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009095 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009096
Gilles Peskine449bd832023-01-11 14:50:10 +01009097 psa_key_derivation_abort(&operation);
9098 psa_destroy_key(base_key);
9099 psa_destroy_key(derived_key);
9100 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009101}
9102/* END_CASE */
9103
Janos Follath42fd8882019-07-03 14:17:09 +01009104/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009105void derive_key_export(int alg_arg,
9106 data_t *key_data,
9107 data_t *input1,
9108 data_t *input2,
9109 int bytes1_arg,
9110 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009111{
Ronald Cron5425a212020-08-04 14:58:35 +02009112 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9113 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009114 psa_algorithm_t alg = alg_arg;
9115 size_t bytes1 = bytes1_arg;
9116 size_t bytes2 = bytes2_arg;
9117 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009118 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009119 uint8_t *output_buffer = NULL;
9120 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009121 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9122 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009123 size_t length;
9124
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009125 TEST_CALLOC(output_buffer, capacity);
9126 TEST_CALLOC(export_buffer, capacity);
Gilles Peskine449bd832023-01-11 14:50:10 +01009127 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009128
Gilles Peskine449bd832023-01-11 14:50:10 +01009129 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9130 psa_set_key_algorithm(&base_attributes, alg);
9131 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9132 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9133 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009134
9135 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009136 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9137 input1->x, input1->len,
9138 input2->x, input2->len,
9139 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009140 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009141 }
Janos Follath42fd8882019-07-03 14:17:09 +01009142
Gilles Peskine449bd832023-01-11 14:50:10 +01009143 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9144 output_buffer,
9145 capacity));
9146 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009147
9148 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009149 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9150 input1->x, input1->len,
9151 input2->x, input2->len,
9152 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009153 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009154 }
Janos Follath42fd8882019-07-03 14:17:09 +01009155
Gilles Peskine449bd832023-01-11 14:50:10 +01009156 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9157 psa_set_key_algorithm(&derived_attributes, 0);
9158 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9159 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9160 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9161 &derived_key));
9162 PSA_ASSERT(psa_export_key(derived_key,
9163 export_buffer, bytes1,
9164 &length));
9165 TEST_EQUAL(length, bytes1);
9166 PSA_ASSERT(psa_destroy_key(derived_key));
9167 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9168 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9169 &derived_key));
9170 PSA_ASSERT(psa_export_key(derived_key,
9171 export_buffer + bytes1, bytes2,
9172 &length));
9173 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009174
9175 /* Compare the outputs from the two runs. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009176 TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009177 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009178
9179exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009180 mbedtls_free(output_buffer);
9181 mbedtls_free(export_buffer);
9182 psa_key_derivation_abort(&operation);
9183 psa_destroy_key(base_key);
9184 psa_destroy_key(derived_key);
9185 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009186}
9187/* END_CASE */
9188
9189/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009190void derive_key_type(int alg_arg,
9191 data_t *key_data,
9192 data_t *input1,
9193 data_t *input2,
9194 int key_type_arg, int bits_arg,
9195 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009196{
9197 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9198 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9199 const psa_algorithm_t alg = alg_arg;
9200 const psa_key_type_t key_type = key_type_arg;
9201 const size_t bits = bits_arg;
9202 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9203 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009204 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009205 uint8_t *export_buffer = NULL;
9206 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9207 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9208 size_t export_length;
9209
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009210 TEST_CALLOC(export_buffer, export_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01009211 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009212
Gilles Peskine449bd832023-01-11 14:50:10 +01009213 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9214 psa_set_key_algorithm(&base_attributes, alg);
9215 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9216 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9217 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009218
Gilles Peskine449bd832023-01-11 14:50:10 +01009219 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009220 &operation, base_key, alg,
9221 input1->x, input1->len,
9222 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009223 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009224 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009225 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009226
Gilles Peskine449bd832023-01-11 14:50:10 +01009227 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9228 psa_set_key_algorithm(&derived_attributes, 0);
9229 psa_set_key_type(&derived_attributes, key_type);
9230 psa_set_key_bits(&derived_attributes, bits);
9231 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9232 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009233
Gilles Peskine449bd832023-01-11 14:50:10 +01009234 PSA_ASSERT(psa_export_key(derived_key,
9235 export_buffer, export_buffer_size,
9236 &export_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009237 TEST_MEMORY_COMPARE(export_buffer, export_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009238 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009239
9240exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009241 mbedtls_free(export_buffer);
9242 psa_key_derivation_abort(&operation);
9243 psa_destroy_key(base_key);
9244 psa_destroy_key(derived_key);
9245 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009246}
9247/* END_CASE */
9248
9249/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009250void derive_key(int alg_arg,
9251 data_t *key_data, data_t *input1, data_t *input2,
9252 int type_arg, int bits_arg,
9253 int expected_status_arg,
9254 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009255{
Ronald Cron5425a212020-08-04 14:58:35 +02009256 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9257 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009258 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009259 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009260 size_t bits = bits_arg;
9261 psa_status_t expected_status = expected_status_arg;
9262 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9263 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9264 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9265
Gilles Peskine449bd832023-01-11 14:50:10 +01009266 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009267
Gilles Peskine449bd832023-01-11 14:50:10 +01009268 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9269 psa_set_key_algorithm(&base_attributes, alg);
9270 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9271 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9272 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009273
Gilles Peskine449bd832023-01-11 14:50:10 +01009274 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9275 input1->x, input1->len,
9276 input2->x, input2->len,
9277 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009278 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009279 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009280
Gilles Peskine449bd832023-01-11 14:50:10 +01009281 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9282 psa_set_key_algorithm(&derived_attributes, 0);
9283 psa_set_key_type(&derived_attributes, type);
9284 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009285
9286 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009287 psa_key_derivation_output_key(&derived_attributes,
9288 &operation,
9289 &derived_key);
9290 if (is_large_output > 0) {
9291 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9292 }
9293 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009294
9295exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009296 psa_key_derivation_abort(&operation);
9297 psa_destroy_key(base_key);
9298 psa_destroy_key(derived_key);
9299 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009300}
9301/* END_CASE */
9302
9303/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009304void key_agreement_setup(int alg_arg,
9305 int our_key_type_arg, int our_key_alg_arg,
9306 data_t *our_key_data, data_t *peer_key_data,
9307 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009308{
Ronald Cron5425a212020-08-04 14:58:35 +02009309 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009310 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009311 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009312 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009313 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009314 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009315 psa_status_t expected_status = expected_status_arg;
9316 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009317
Gilles Peskine449bd832023-01-11 14:50:10 +01009318 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009319
Gilles Peskine449bd832023-01-11 14:50:10 +01009320 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9321 psa_set_key_algorithm(&attributes, our_key_alg);
9322 psa_set_key_type(&attributes, our_key_type);
9323 PSA_ASSERT(psa_import_key(&attributes,
9324 our_key_data->x, our_key_data->len,
9325 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009326
Gilles Peskine77f40d82019-04-11 21:27:06 +02009327 /* The tests currently include inputs that should fail at either step.
9328 * Test cases that fail at the setup step should be changed to call
9329 * key_derivation_setup instead, and this function should be renamed
9330 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009331 status = psa_key_derivation_setup(&operation, alg);
9332 if (status == PSA_SUCCESS) {
9333 TEST_EQUAL(psa_key_derivation_key_agreement(
9334 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9335 our_key,
9336 peer_key_data->x, peer_key_data->len),
9337 expected_status);
9338 } else {
9339 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009340 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009341
9342exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009343 psa_key_derivation_abort(&operation);
9344 psa_destroy_key(our_key);
9345 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009346}
9347/* END_CASE */
9348
9349/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009350void raw_key_agreement(int alg_arg,
9351 int our_key_type_arg, data_t *our_key_data,
9352 data_t *peer_key_data,
9353 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009354{
Ronald Cron5425a212020-08-04 14:58:35 +02009355 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009356 psa_algorithm_t alg = alg_arg;
9357 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009358 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009359 unsigned char *output = NULL;
9360 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009361 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009362
Gilles Peskine449bd832023-01-11 14:50:10 +01009363 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009364
Gilles Peskine449bd832023-01-11 14:50:10 +01009365 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9366 psa_set_key_algorithm(&attributes, alg);
9367 psa_set_key_type(&attributes, our_key_type);
9368 PSA_ASSERT(psa_import_key(&attributes,
9369 our_key_data->x, our_key_data->len,
9370 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009371
Gilles Peskine449bd832023-01-11 14:50:10 +01009372 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9373 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009374
Gilles Peskine992bee82022-04-13 23:25:52 +02009375 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009376 TEST_LE_U(expected_output->len,
9377 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9378 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9379 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009380
9381 /* Good case with exact output size */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009382 TEST_CALLOC(output, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009383 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9384 peer_key_data->x, peer_key_data->len,
9385 output, expected_output->len,
9386 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009387 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009388 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009389 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009390 output = NULL;
9391 output_length = ~0;
9392
9393 /* Larger buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009394 TEST_CALLOC(output, expected_output->len + 1);
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 + 1,
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 /* Buffer too small */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009406 TEST_CALLOC(output, expected_output->len - 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009407 TEST_EQUAL(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),
9411 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009412 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009413 TEST_LE_U(output_length, expected_output->len - 1);
9414 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009415 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009416
9417exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009418 mbedtls_free(output);
9419 psa_destroy_key(our_key);
9420 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009421}
9422/* END_CASE */
9423
9424/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009425void key_agreement_capacity(int alg_arg,
9426 int our_key_type_arg, data_t *our_key_data,
9427 data_t *peer_key_data,
9428 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009429{
Ronald Cron5425a212020-08-04 14:58:35 +02009430 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009431 psa_algorithm_t alg = alg_arg;
9432 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009433 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009434 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009435 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009436 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009437
Gilles Peskine449bd832023-01-11 14:50:10 +01009438 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009439
Gilles Peskine449bd832023-01-11 14:50:10 +01009440 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9441 psa_set_key_algorithm(&attributes, alg);
9442 psa_set_key_type(&attributes, our_key_type);
9443 PSA_ASSERT(psa_import_key(&attributes,
9444 our_key_data->x, our_key_data->len,
9445 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009446
Gilles Peskine449bd832023-01-11 14:50:10 +01009447 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9448 PSA_ASSERT(psa_key_derivation_key_agreement(
9449 &operation,
9450 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9451 peer_key_data->x, peer_key_data->len));
9452 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009453 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009454 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9455 PSA_KEY_DERIVATION_INPUT_INFO,
9456 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009457 }
Gilles Peskine59685592018-09-18 12:11:34 +02009458
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009459 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009460 PSA_ASSERT(psa_key_derivation_get_capacity(
9461 &operation, &actual_capacity));
9462 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009463
Gilles Peskinebf491972018-10-25 22:36:12 +02009464 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009465 while (actual_capacity > sizeof(output)) {
9466 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9467 output, sizeof(output)));
9468 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009469 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009470 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9471 output, actual_capacity));
9472 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9473 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009474
Gilles Peskine59685592018-09-18 12:11:34 +02009475exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009476 psa_key_derivation_abort(&operation);
9477 psa_destroy_key(our_key);
9478 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009479}
9480/* END_CASE */
9481
9482/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009483void key_agreement_output(int alg_arg,
9484 int our_key_type_arg, data_t *our_key_data,
9485 data_t *peer_key_data,
9486 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009487{
Ronald Cron5425a212020-08-04 14:58:35 +02009488 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009489 psa_algorithm_t alg = alg_arg;
9490 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009491 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009492 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009493 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009494
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009495 TEST_CALLOC(actual_output, MAX(expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009496 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009497
Gilles Peskine449bd832023-01-11 14:50:10 +01009498 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009499
Gilles Peskine449bd832023-01-11 14:50:10 +01009500 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9501 psa_set_key_algorithm(&attributes, alg);
9502 psa_set_key_type(&attributes, our_key_type);
9503 PSA_ASSERT(psa_import_key(&attributes,
9504 our_key_data->x, our_key_data->len,
9505 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009506
Gilles Peskine449bd832023-01-11 14:50:10 +01009507 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9508 PSA_ASSERT(psa_key_derivation_key_agreement(
9509 &operation,
9510 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9511 peer_key_data->x, peer_key_data->len));
9512 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009513 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009514 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9515 PSA_KEY_DERIVATION_INPUT_INFO,
9516 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009517 }
Gilles Peskine59685592018-09-18 12:11:34 +02009518
Gilles Peskine449bd832023-01-11 14:50:10 +01009519 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9520 actual_output,
9521 expected_output1->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009522 TEST_MEMORY_COMPARE(actual_output, expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009523 expected_output1->x, expected_output1->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009524 if (expected_output2->len != 0) {
9525 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9526 actual_output,
9527 expected_output2->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009528 TEST_MEMORY_COMPARE(actual_output, expected_output2->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009529 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009530 }
Gilles Peskine59685592018-09-18 12:11:34 +02009531
9532exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009533 psa_key_derivation_abort(&operation);
9534 psa_destroy_key(our_key);
9535 PSA_DONE();
9536 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009537}
9538/* END_CASE */
9539
9540/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009541void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009542{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009543 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009544 unsigned char *output = NULL;
9545 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009546 size_t i;
9547 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009548
Gilles Peskine449bd832023-01-11 14:50:10 +01009549 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009550
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009551 TEST_CALLOC(output, bytes);
9552 TEST_CALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009553
Gilles Peskine449bd832023-01-11 14:50:10 +01009554 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009555
Gilles Peskinea50d7392018-06-21 10:22:13 +02009556 /* Run several times, to ensure that every output byte will be
9557 * nonzero at least once with overwhelming probability
9558 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009559 for (run = 0; run < 10; run++) {
9560 if (bytes != 0) {
9561 memset(output, 0, bytes);
9562 }
9563 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009564
Gilles Peskine449bd832023-01-11 14:50:10 +01009565 for (i = 0; i < bytes; i++) {
9566 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009567 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009568 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009569 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009570 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009571
9572 /* Check that every byte was changed to nonzero at least once. This
9573 * validates that psa_generate_random is overwriting every byte of
9574 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009575 for (i = 0; i < bytes; i++) {
9576 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009577 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009578
9579exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009580 PSA_DONE();
9581 mbedtls_free(output);
9582 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009583}
9584/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009585
9586/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009587void generate_key(int type_arg,
9588 int bits_arg,
9589 int usage_arg,
9590 int alg_arg,
9591 int expected_status_arg,
9592 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009593{
Ronald Cron5425a212020-08-04 14:58:35 +02009594 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009595 psa_key_type_t type = type_arg;
9596 psa_key_usage_t usage = usage_arg;
9597 size_t bits = bits_arg;
9598 psa_algorithm_t alg = alg_arg;
9599 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009600 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009601 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009602
Gilles Peskine449bd832023-01-11 14:50:10 +01009603 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009604
Gilles Peskine449bd832023-01-11 14:50:10 +01009605 psa_set_key_usage_flags(&attributes, usage);
9606 psa_set_key_algorithm(&attributes, alg);
9607 psa_set_key_type(&attributes, type);
9608 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009609
9610 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009611 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009612
Gilles Peskine449bd832023-01-11 14:50:10 +01009613 if (is_large_key > 0) {
9614 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9615 }
9616 TEST_EQUAL(status, expected_status);
9617 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009618 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009619 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009620
9621 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009622 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9623 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9624 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009625
Gilles Peskine818ca122018-06-20 18:16:48 +02009626 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009627 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009628 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009629 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009630
9631exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009632 /*
9633 * Key attributes may have been returned by psa_get_key_attributes()
9634 * thus reset them as required.
9635 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009636 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009637
Gilles Peskine449bd832023-01-11 14:50:10 +01009638 psa_destroy_key(key);
9639 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009640}
9641/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009642
Valerio Setti19fec542023-07-25 12:31:50 +02009643/* 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 +01009644void generate_key_rsa(int bits_arg,
9645 data_t *e_arg,
9646 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009647{
Ronald Cron5425a212020-08-04 14:58:35 +02009648 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009649 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009650 size_t bits = bits_arg;
9651 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9652 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9653 psa_status_t expected_status = expected_status_arg;
9654 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9655 uint8_t *exported = NULL;
9656 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009657 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009658 size_t exported_length = SIZE_MAX;
9659 uint8_t *e_read_buffer = NULL;
9660 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009661 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009662 size_t e_read_length = SIZE_MAX;
9663
Gilles Peskine449bd832023-01-11 14:50:10 +01009664 if (e_arg->len == 0 ||
9665 (e_arg->len == 3 &&
9666 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009667 is_default_public_exponent = 1;
9668 e_read_size = 0;
9669 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009670 TEST_CALLOC(e_read_buffer, e_read_size);
9671 TEST_CALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009672
Gilles Peskine449bd832023-01-11 14:50:10 +01009673 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009674
Gilles Peskine449bd832023-01-11 14:50:10 +01009675 psa_set_key_usage_flags(&attributes, usage);
9676 psa_set_key_algorithm(&attributes, alg);
9677 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9678 e_arg->x, e_arg->len));
9679 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009680
9681 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009682 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9683 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009684 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009685 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009686
9687 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009688 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9689 TEST_EQUAL(psa_get_key_type(&attributes), type);
9690 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9691 PSA_ASSERT(psa_get_key_domain_parameters(&attributes,
9692 e_read_buffer, e_read_size,
9693 &e_read_length));
9694 if (is_default_public_exponent) {
9695 TEST_EQUAL(e_read_length, 0);
9696 } else {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009697 TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009698 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009699
9700 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009701 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009702 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009703 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009704
9705 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009706 PSA_ASSERT(psa_export_public_key(key,
9707 exported, exported_size,
9708 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009709 {
9710 uint8_t *p = exported;
9711 uint8_t *end = exported + exported_length;
9712 size_t len;
9713 /* RSAPublicKey ::= SEQUENCE {
9714 * modulus INTEGER, -- n
9715 * publicExponent INTEGER } -- e
9716 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009717 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9718 MBEDTLS_ASN1_SEQUENCE |
9719 MBEDTLS_ASN1_CONSTRUCTED));
9720 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9721 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9722 MBEDTLS_ASN1_INTEGER));
9723 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009724 ++p;
9725 --len;
9726 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009727 if (e_arg->len == 0) {
9728 TEST_EQUAL(len, 3);
9729 TEST_EQUAL(p[0], 1);
9730 TEST_EQUAL(p[1], 0);
9731 TEST_EQUAL(p[2], 1);
9732 } else {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009733 TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009734 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009735 }
9736
9737exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009738 /*
9739 * Key attributes may have been returned by psa_get_key_attributes() or
9740 * set by psa_set_key_domain_parameters() thus reset them as required.
9741 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009742 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009743
Gilles Peskine449bd832023-01-11 14:50:10 +01009744 psa_destroy_key(key);
9745 PSA_DONE();
9746 mbedtls_free(e_read_buffer);
9747 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009748}
9749/* END_CASE */
9750
Darryl Greend49a4992018-06-18 17:27:26 +01009751/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009752void persistent_key_load_key_from_storage(data_t *data,
9753 int type_arg, int bits_arg,
9754 int usage_flags_arg, int alg_arg,
9755 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009756{
Gilles Peskine449bd832023-01-11 14:50:10 +01009757 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009758 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009759 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9760 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009761 psa_key_type_t type = type_arg;
9762 size_t bits = bits_arg;
9763 psa_key_usage_t usage_flags = usage_flags_arg;
9764 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009765 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009766 unsigned char *first_export = NULL;
9767 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009768 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Sergeybef1f632023-03-06 15:25:06 -07009769 size_t first_exported_length = 0;
Darryl Greend49a4992018-06-18 17:27:26 +01009770 size_t second_exported_length;
9771
Gilles Peskine449bd832023-01-11 14:50:10 +01009772 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009773 TEST_CALLOC(first_export, export_size);
9774 TEST_CALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009775 }
Darryl Greend49a4992018-06-18 17:27:26 +01009776
Gilles Peskine449bd832023-01-11 14:50:10 +01009777 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009778
Gilles Peskine449bd832023-01-11 14:50:10 +01009779 psa_set_key_id(&attributes, key_id);
9780 psa_set_key_usage_flags(&attributes, usage_flags);
9781 psa_set_key_algorithm(&attributes, alg);
9782 psa_set_key_type(&attributes, type);
9783 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009784
Gilles Peskine449bd832023-01-11 14:50:10 +01009785 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009786 case IMPORT_KEY:
9787 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009788 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9789 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009790 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009791
Darryl Green0c6575a2018-11-07 16:05:30 +00009792 case GENERATE_KEY:
9793 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009794 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009795 break;
9796
9797 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009798#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009799 {
9800 /* Create base key */
9801 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9802 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9803 psa_set_key_usage_flags(&base_attributes,
9804 PSA_KEY_USAGE_DERIVE);
9805 psa_set_key_algorithm(&base_attributes, derive_alg);
9806 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9807 PSA_ASSERT(psa_import_key(&base_attributes,
9808 data->x, data->len,
9809 &base_key));
9810 /* Derive a key. */
9811 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9812 PSA_ASSERT(psa_key_derivation_input_key(
9813 &operation,
9814 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9815 PSA_ASSERT(psa_key_derivation_input_bytes(
9816 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9817 NULL, 0));
9818 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9819 &operation,
9820 &key));
9821 PSA_ASSERT(psa_key_derivation_abort(&operation));
9822 PSA_ASSERT(psa_destroy_key(base_key));
9823 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9824 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009825#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009826 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009827#endif
9828 break;
9829
9830 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01009831 TEST_FAIL("generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009832 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009833 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009834 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009835
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009836 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009837 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9838 PSA_ASSERT(psa_export_key(key,
9839 first_export, export_size,
9840 &first_exported_length));
9841 if (generation_method == IMPORT_KEY) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009842 TEST_MEMORY_COMPARE(data->x, data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009843 first_export, first_exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01009844 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009845 }
Darryl Greend49a4992018-06-18 17:27:26 +01009846
9847 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009848 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009849 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009850 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009851
Darryl Greend49a4992018-06-18 17:27:26 +01009852 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009853 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9854 TEST_ASSERT(mbedtls_svc_key_id_equal(
9855 psa_get_key_id(&attributes), key_id));
9856 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9857 PSA_KEY_LIFETIME_PERSISTENT);
9858 TEST_EQUAL(psa_get_key_type(&attributes), type);
9859 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9860 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9861 mbedtls_test_update_key_usage_flags(usage_flags));
9862 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009863
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009864 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009865 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9866 PSA_ASSERT(psa_export_key(key,
9867 second_export, export_size,
9868 &second_exported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009869 TEST_MEMORY_COMPARE(first_export, first_exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009870 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009871 }
9872
9873 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009874 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009875 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009876 }
Darryl Greend49a4992018-06-18 17:27:26 +01009877
9878exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009879 /*
9880 * Key attributes may have been returned by psa_get_key_attributes()
9881 * thus reset them as required.
9882 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009883 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009884
Gilles Peskine449bd832023-01-11 14:50:10 +01009885 mbedtls_free(first_export);
9886 mbedtls_free(second_export);
9887 psa_key_derivation_abort(&operation);
9888 psa_destroy_key(base_key);
9889 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009890 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009891}
9892/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009893
Neil Armstronga557cb82022-06-10 08:58:32 +02009894/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009895void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9896 int primitive_arg, int hash_arg, int role_arg,
9897 int test_input, data_t *pw_data,
9898 int inj_err_type_arg,
9899 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009900{
9901 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9902 psa_pake_operation_t operation = psa_pake_operation_init();
9903 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009904 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009905 psa_key_type_t key_type_pw = key_type_pw_arg;
9906 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009907 psa_algorithm_t hash_alg = hash_arg;
9908 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009909 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9910 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009911 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9912 psa_status_t expected_error = expected_error_arg;
9913 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009914 unsigned char *output_buffer = NULL;
9915 size_t output_len = 0;
9916
Gilles Peskine449bd832023-01-11 14:50:10 +01009917 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009918
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009919 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009920 PSA_PAKE_STEP_KEY_SHARE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009921 TEST_CALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009922
Gilles Peskine449bd832023-01-11 14:50:10 +01009923 if (pw_data->len > 0) {
9924 psa_set_key_usage_flags(&attributes, key_usage_pw);
9925 psa_set_key_algorithm(&attributes, alg);
9926 psa_set_key_type(&attributes, key_type_pw);
9927 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9928 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009929 }
9930
Gilles Peskine449bd832023-01-11 14:50:10 +01009931 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9932 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9933 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009934
Gilles Peskine449bd832023-01-11 14:50:10 +01009935 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009936
Gilles Peskine449bd832023-01-11 14:50:10 +01009937 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9938 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9939 expected_error);
9940 PSA_ASSERT(psa_pake_abort(&operation));
9941 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9942 expected_error);
9943 PSA_ASSERT(psa_pake_abort(&operation));
9944 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
9945 expected_error);
9946 PSA_ASSERT(psa_pake_abort(&operation));
9947 TEST_EQUAL(psa_pake_set_role(&operation, role),
9948 expected_error);
9949 PSA_ASSERT(psa_pake_abort(&operation));
9950 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
9951 NULL, 0, NULL),
9952 expected_error);
9953 PSA_ASSERT(psa_pake_abort(&operation));
9954 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
9955 expected_error);
9956 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009957 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009958 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009959
Gilles Peskine449bd832023-01-11 14:50:10 +01009960 status = psa_pake_setup(&operation, &cipher_suite);
9961 if (status != PSA_SUCCESS) {
9962 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009963 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009964 }
9965
Gilles Peskine449bd832023-01-11 14:50:10 +01009966 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
9967 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
9968 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009969 goto exit;
9970 }
9971
Gilles Peskine449bd832023-01-11 14:50:10 +01009972 status = psa_pake_set_role(&operation, role);
9973 if (status != PSA_SUCCESS) {
9974 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +01009975 goto exit;
9976 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009977
Gilles Peskine449bd832023-01-11 14:50:10 +01009978 if (pw_data->len > 0) {
9979 status = psa_pake_set_password_key(&operation, key);
9980 if (status != PSA_SUCCESS) {
9981 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009982 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01009983 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009984 }
9985
Gilles Peskine449bd832023-01-11 14:50:10 +01009986 if (inj_err_type == INJECT_ERR_INVALID_USER) {
9987 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9988 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009989 goto exit;
9990 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009991
Gilles Peskine449bd832023-01-11 14:50:10 +01009992 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
9993 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9994 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +01009995 goto exit;
9996 }
Neil Armstrong707d9572022-06-08 17:31:49 +02009997
Gilles Peskine449bd832023-01-11 14:50:10 +01009998 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +01009999 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010000 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
10001 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010002 goto exit;
10003 }
10004
Gilles Peskine449bd832023-01-11 14:50:10 +010010005 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010006 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010007 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
10008 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010009 goto exit;
10010 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010011
Gilles Peskine449bd832023-01-11 14:50:10 +010010012 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
10013 PSA_PAKE_STEP_KEY_SHARE);
10014 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
10015 PSA_PAKE_STEP_ZK_PUBLIC);
10016 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
10017 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010018
Gilles Peskine449bd832023-01-11 14:50:10 +010010019 if (test_input) {
10020 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10021 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
10022 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010023 goto exit;
10024 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010025
Gilles Peskine449bd832023-01-11 14:50:10 +010010026 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10027 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10028 output_buffer, size_zk_proof),
10029 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010030 goto exit;
10031 }
10032
Gilles Peskine449bd832023-01-11 14:50:10 +010010033 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10034 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
10035 output_buffer, size_zk_proof),
10036 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010037 goto exit;
10038 }
10039
Gilles Peskine449bd832023-01-11 14:50:10 +010010040 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
10041 output_buffer, size_key_share);
10042 if (status != PSA_SUCCESS) {
10043 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010044 goto exit;
10045 }
10046
Gilles Peskine449bd832023-01-11 14:50:10 +010010047 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10048 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10049 output_buffer, size_zk_public + 1),
10050 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010051 goto exit;
10052 }
10053
Gilles Peskine449bd832023-01-11 14:50:10 +010010054 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010055 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010056 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10057 output_buffer, size_zk_public + 1);
10058 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10059 output_buffer, size_zk_public),
10060 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010061 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010062 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010063 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010064 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10065 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10066 NULL, 0, NULL),
10067 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010068 goto exit;
10069 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010070
Gilles Peskine449bd832023-01-11 14:50:10 +010010071 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10072 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10073 output_buffer, buf_size, &output_len),
10074 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010075 goto exit;
10076 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010077
Gilles Peskine449bd832023-01-11 14:50:10 +010010078 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10079 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10080 output_buffer, buf_size, &output_len),
10081 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010082 goto exit;
10083 }
10084
Gilles Peskine449bd832023-01-11 14:50:10 +010010085 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10086 output_buffer, buf_size, &output_len);
10087 if (status != PSA_SUCCESS) {
10088 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010089 goto exit;
10090 }
10091
Gilles Peskine449bd832023-01-11 14:50:10 +010010092 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010093
Gilles Peskine449bd832023-01-11 14:50:10 +010010094 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10095 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10096 output_buffer, size_zk_public - 1, &output_len),
10097 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010098 goto exit;
10099 }
10100
Gilles Peskine449bd832023-01-11 14:50:10 +010010101 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010102 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010103 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10104 output_buffer, size_zk_public - 1, &output_len);
10105 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10106 output_buffer, buf_size, &output_len),
10107 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010108 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010109 }
10110 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010111
10112exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010113 PSA_ASSERT(psa_destroy_key(key));
10114 PSA_ASSERT(psa_pake_abort(&operation));
10115 mbedtls_free(output_buffer);
10116 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010117}
10118/* END_CASE */
10119
Neil Armstronga557cb82022-06-10 08:58:32 +020010120/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010121void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10122 int client_input_first, int inject_error,
10123 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010124{
10125 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10126 psa_pake_operation_t server = psa_pake_operation_init();
10127 psa_pake_operation_t client = psa_pake_operation_init();
10128 psa_algorithm_t alg = alg_arg;
10129 psa_algorithm_t hash_alg = hash_arg;
10130 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10131 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10132
Gilles Peskine449bd832023-01-11 14:50:10 +010010133 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010134
Gilles Peskine449bd832023-01-11 14:50:10 +010010135 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10136 psa_set_key_algorithm(&attributes, alg);
10137 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10138 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10139 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010140
Gilles Peskine449bd832023-01-11 14:50:10 +010010141 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10142 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10143 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010144
10145
Gilles Peskine449bd832023-01-11 14:50:10 +010010146 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10147 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010148
Gilles Peskine449bd832023-01-11 14:50:10 +010010149 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10150 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010151
Gilles Peskine449bd832023-01-11 14:50:10 +010010152 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10153 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010154
Gilles Peskine449bd832023-01-11 14:50:10 +010010155 ecjpake_do_round(alg, primitive_arg, &server, &client,
10156 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010157
Gilles Peskine449bd832023-01-11 14:50:10 +010010158 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010159 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010160 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010161
Gilles Peskine449bd832023-01-11 14:50:10 +010010162 ecjpake_do_round(alg, primitive_arg, &server, &client,
10163 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010164
10165exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010166 psa_destroy_key(key);
10167 psa_pake_abort(&server);
10168 psa_pake_abort(&client);
10169 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010170}
10171/* END_CASE */
10172
10173/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010174void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10175 int derive_alg_arg, data_t *pw_data,
10176 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010177{
10178 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10179 psa_pake_operation_t server = psa_pake_operation_init();
10180 psa_pake_operation_t client = psa_pake_operation_init();
10181 psa_algorithm_t alg = alg_arg;
10182 psa_algorithm_t hash_alg = hash_arg;
10183 psa_algorithm_t derive_alg = derive_alg_arg;
10184 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10185 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10186 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010187 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010188 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010189 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010190 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010191
Gilles Peskine449bd832023-01-11 14:50:10 +010010192 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010193
Gilles Peskine449bd832023-01-11 14:50:10 +010010194 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10195 psa_set_key_algorithm(&attributes, alg);
10196 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10197 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10198 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010199
Gilles Peskine449bd832023-01-11 14:50:10 +010010200 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10201 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10202 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010203
Neil Armstrong1e855602022-06-15 11:32:11 +020010204 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010205 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10206 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010207
Gilles Peskine449bd832023-01-11 14:50:10 +010010208 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10209 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10210 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10211 PSA_KEY_DERIVATION_INPUT_SEED,
10212 (const uint8_t *) "", 0));
10213 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10214 PSA_KEY_DERIVATION_INPUT_SEED,
10215 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010216 }
10217
Gilles Peskine449bd832023-01-11 14:50:10 +010010218 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10219 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010220
Gilles Peskine449bd832023-01-11 14:50:10 +010010221 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10222 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010223
Gilles Peskine449bd832023-01-11 14:50:10 +010010224 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10225 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010226
Gilles Peskine449bd832023-01-11 14:50:10 +010010227 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10228 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10229 PSA_ERROR_BAD_STATE);
10230 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10231 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010232 goto exit;
10233 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010234
Neil Armstrongf983caf2022-06-15 15:27:48 +020010235 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010236 ecjpake_do_round(alg, primitive_arg, &server, &client,
10237 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010238
Gilles Peskine449bd832023-01-11 14:50:10 +010010239 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10240 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10241 PSA_ERROR_BAD_STATE);
10242 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10243 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010244 goto exit;
10245 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010246
Neil Armstrongf983caf2022-06-15 15:27:48 +020010247 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010248 ecjpake_do_round(alg, primitive_arg, &server, &client,
10249 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010250
Gilles Peskine449bd832023-01-11 14:50:10 +010010251 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10252 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010253
10254exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010255 psa_key_derivation_abort(&server_derive);
10256 psa_key_derivation_abort(&client_derive);
10257 psa_destroy_key(key);
10258 psa_pake_abort(&server);
10259 psa_pake_abort(&client);
10260 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010261}
10262/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010263
10264/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010265void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010266{
10267 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10268 const size_t bits = 256;
10269 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010270 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010271 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010272 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010273
10274 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10275 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010276 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10277 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10278 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10279 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010280 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010281 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10282 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010283
10284 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010285 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10286 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10287 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10288 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10289 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10290 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010291
10292 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010293 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10294 PSA_PAKE_OUTPUT_MAX_SIZE);
10295 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10296 PSA_PAKE_OUTPUT_MAX_SIZE);
10297 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10298 PSA_PAKE_OUTPUT_MAX_SIZE);
10299 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10300 PSA_PAKE_INPUT_MAX_SIZE);
10301 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10302 PSA_PAKE_INPUT_MAX_SIZE);
10303 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10304 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010305}
10306/* END_CASE */