blob: 05b49bd80798dc0d8417c85112432701d52e9556 [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
Valerio Settibf999cb2023-12-28 17:48:13 +01009#include "mbedtls/psa_util.h"
10
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020011/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
12 * uses mbedtls_ctr_drbg internally. */
13#include "mbedtls/ctr_drbg.h"
14
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020015#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020016#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020017
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +010018/* For psa_can_do_hash() */
19#include "psa_crypto_core.h"
20
Gilles Peskine8e94efe2021-02-13 00:25:53 +010021#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010022#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010023#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053024#if defined(PSA_CRYPTO_DRIVER_TEST)
25#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053026#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
27#else
28#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053029#endif
Ronald Cron28a45ed2021-02-09 20:35:42 +010030
Gilles Peskine4023c012021-05-27 13:21:20 +020031/* If this comes up, it's a bug in the test code or in the test data. */
32#define UNUSED 0xdeadbeef
33
Dave Rodgman647791d2021-06-23 12:49:59 +010034/* Assert that an operation is (not) active.
35 * This serves as a proxy for checking if the operation is aborted. */
Gilles Peskine449bd832023-01-11 14:50:10 +010036#define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
37#define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010038
Przemek Stekiel7c795482022-11-15 22:26:12 +010039#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +010040int ecjpake_operation_setup(psa_pake_operation_t *operation,
41 psa_pake_cipher_suite_t *cipher_suite,
42 psa_pake_role_t role,
43 mbedtls_svc_key_id_t key,
44 size_t key_available)
Przemek Stekiel7c795482022-11-15 22:26:12 +010045{
Gilles Peskine449bd832023-01-11 14:50:10 +010046 PSA_ASSERT(psa_pake_abort(operation));
Przemek Stekiel7c795482022-11-15 22:26:12 +010047
Gilles Peskine449bd832023-01-11 14:50:10 +010048 PSA_ASSERT(psa_pake_setup(operation, cipher_suite));
Przemek Stekiel7c795482022-11-15 22:26:12 +010049
Gilles Peskine449bd832023-01-11 14:50:10 +010050 PSA_ASSERT(psa_pake_set_role(operation, role));
Przemek Stekiel7c795482022-11-15 22:26:12 +010051
Gilles Peskine449bd832023-01-11 14:50:10 +010052 if (key_available) {
53 PSA_ASSERT(psa_pake_set_password_key(operation, key));
54 }
Przemek Stekielf82effa2022-11-21 15:10:32 +010055 return 0;
Przemek Stekiel7c795482022-11-15 22:26:12 +010056exit:
Przemek Stekielf82effa2022-11-21 15:10:32 +010057 return 1;
Przemek Stekiel7c795482022-11-15 22:26:12 +010058}
59#endif
60
Jaeden Amerof24c7f82018-06-27 17:20:43 +010061/** An invalid export length that will never be set by psa_export_key(). */
62static const size_t INVALID_EXPORT_LENGTH = ~0U;
63
Gilles Peskinea7aa4422018-08-14 15:17:54 +020064/** Test if a buffer contains a constant byte value.
65 *
66 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020067 *
68 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020069 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020070 * \param size Size of the buffer in bytes.
71 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020072 * \return 1 if the buffer is all-bits-zero.
73 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020074 */
Gilles Peskine449bd832023-01-11 14:50:10 +010075static int mem_is_char(void *buffer, unsigned char c, size_t size)
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020076{
77 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010078 for (i = 0; i < size; i++) {
79 if (((unsigned char *) buffer)[i] != c) {
80 return 0;
81 }
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020082 }
Gilles Peskine449bd832023-01-11 14:50:10 +010083 return 1;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020084}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010085#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020086/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
Gilles Peskine449bd832023-01-11 14:50:10 +010087static int asn1_write_10x(unsigned char **p,
88 unsigned char *start,
89 size_t bits,
90 unsigned char x)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020091{
92 int ret;
93 int len = bits / 8 + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010094 if (bits == 0) {
95 return MBEDTLS_ERR_ASN1_INVALID_DATA;
96 }
97 if (bits <= 8 && x >= 1 << (bits - 1)) {
98 return MBEDTLS_ERR_ASN1_INVALID_DATA;
99 }
100 if (*p < start || *p - start < (ptrdiff_t) len) {
101 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
102 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200103 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 (*p)[len-1] = x;
105 if (bits % 8 == 0) {
106 (*p)[1] |= 1;
107 } else {
108 (*p)[0] |= 1 << (bits % 8);
109 }
110 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
111 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
112 MBEDTLS_ASN1_INTEGER));
113 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200114}
115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116static int construct_fake_rsa_key(unsigned char *buffer,
117 size_t buffer_size,
118 unsigned char **p,
119 size_t bits,
120 int keypair)
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200121{
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 size_t half_bits = (bits + 1) / 2;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200123 int ret;
124 int len = 0;
125 /* Construct something that looks like a DER encoding of
126 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
127 * RSAPrivateKey ::= SEQUENCE {
128 * version Version,
129 * modulus INTEGER, -- n
130 * publicExponent INTEGER, -- e
131 * privateExponent INTEGER, -- d
132 * prime1 INTEGER, -- p
133 * prime2 INTEGER, -- q
134 * exponent1 INTEGER, -- d mod (p-1)
135 * exponent2 INTEGER, -- d mod (q-1)
136 * coefficient INTEGER, -- (inverse of q) mod p
137 * otherPrimeInfos OtherPrimeInfos OPTIONAL
138 * }
139 * Or, for a public key, the same structure with only
140 * version, modulus and publicExponent.
141 */
142 *p = buffer + buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 if (keypair) {
144 MBEDTLS_ASN1_CHK_ADD(len, /* pq */
145 asn1_write_10x(p, buffer, half_bits, 1));
146 MBEDTLS_ASN1_CHK_ADD(len, /* dq */
147 asn1_write_10x(p, buffer, half_bits, 1));
148 MBEDTLS_ASN1_CHK_ADD(len, /* dp */
149 asn1_write_10x(p, buffer, half_bits, 1));
150 MBEDTLS_ASN1_CHK_ADD(len, /* q */
151 asn1_write_10x(p, buffer, half_bits, 1));
152 MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
153 asn1_write_10x(p, buffer, half_bits, 3));
154 MBEDTLS_ASN1_CHK_ADD(len, /* d */
155 asn1_write_10x(p, buffer, bits, 1));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200156 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
158 asn1_write_10x(p, buffer, 17, 1));
159 MBEDTLS_ASN1_CHK_ADD(len, /* n */
160 asn1_write_10x(p, buffer, bits, 1));
161 if (keypair) {
162 MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
163 mbedtls_asn1_write_int(p, buffer, 0));
164 }
165 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200166 {
167 const unsigned char tag =
168 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200170 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200172}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100173#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175int exercise_mac_setup(psa_key_type_t key_type,
176 const unsigned char *key_bytes,
177 size_t key_length,
178 psa_algorithm_t alg,
179 psa_mac_operation_t *operation,
180 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100181{
Ronald Cron5425a212020-08-04 14:58:35 +0200182 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
186 psa_set_key_algorithm(&attributes, alg);
187 psa_set_key_type(&attributes, key_type);
188 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 *status = psa_mac_sign_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100191 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 PSA_ASSERT(psa_mac_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100193 /* If setup failed, reproduce the failure, so that the caller can
194 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 if (*status != PSA_SUCCESS) {
196 TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197 }
198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 psa_destroy_key(key);
200 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100201
202exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 psa_destroy_key(key);
204 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100205}
206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207int exercise_cipher_setup(psa_key_type_t key_type,
208 const unsigned char *key_bytes,
209 size_t key_length,
210 psa_algorithm_t alg,
211 psa_cipher_operation_t *operation,
212 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100213{
Ronald Cron5425a212020-08-04 14:58:35 +0200214 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200215 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
218 psa_set_key_algorithm(&attributes, alg);
219 psa_set_key_type(&attributes, key_type);
220 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 *status = psa_cipher_encrypt_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100223 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 PSA_ASSERT(psa_cipher_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100225 /* If setup failed, reproduce the failure, so that the caller can
226 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 if (*status != PSA_SUCCESS) {
228 TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
229 *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100230 }
231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 psa_destroy_key(key);
233 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100234
235exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 psa_destroy_key(key);
237 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100238}
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200241{
242 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200244 uint8_t buffer[1];
245 size_t length;
246 int ok = 0;
247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 psa_set_key_id(&attributes, key_id);
249 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
250 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
251 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
252 TEST_EQUAL(psa_get_key_attributes(key, &attributes),
253 PSA_ERROR_INVALID_HANDLE);
Ronald Cronecfb2372020-07-23 17:13:42 +0200254 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
Ronald Cronecfb2372020-07-23 17:13:42 +0200256 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
258 TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
259 TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
260 TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
261 TEST_EQUAL(psa_get_key_type(&attributes), 0);
262 TEST_EQUAL(psa_get_key_bits(&attributes), 0);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
265 PSA_ERROR_INVALID_HANDLE);
266 TEST_EQUAL(psa_export_public_key(key,
267 buffer, sizeof(buffer), &length),
268 PSA_ERROR_INVALID_HANDLE);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200269
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200270 ok = 1;
271
272exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100273 /*
274 * Key attributes may have been returned by psa_get_key_attributes()
275 * thus reset them as required.
276 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return ok;
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200280}
281
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200282/* Assert that a key isn't reported as having a slot number. */
283#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100284#define ASSERT_NO_SLOT_NUMBER(attributes) \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200285 do \
286 { \
287 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 TEST_EQUAL(psa_get_key_slot_number( \
289 attributes, \
290 &ASSERT_NO_SLOT_NUMBER_slot_number), \
291 PSA_ERROR_INVALID_ARGUMENT); \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200292 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 while (0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200294#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100295#define ASSERT_NO_SLOT_NUMBER(attributes) \
296 ((void) 0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200297#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
298
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +0530299#define INPUT_INTEGER 0x10000 /* Out of range of psa_key_type_t */
300
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100301/* An overapproximation of the amount of storage needed for a key of the
302 * given type and with the given content. The API doesn't make it easy
303 * to find a good value for the size. The current implementation doesn't
304 * care about the value anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100305#define KEY_BITS_FROM_DATA(type, data) \
306 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100307
Darryl Green0c6575a2018-11-07 16:05:30 +0000308typedef enum {
309 IMPORT_KEY = 0,
310 GENERATE_KEY = 1,
311 DERIVE_KEY = 2
312} generate_method;
313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314typedef enum {
Paul Elliott33746aa2021-09-15 16:40:40 +0100315 DO_NOT_SET_LENGTHS = 0,
316 SET_LENGTHS_BEFORE_NONCE = 1,
317 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100318} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320typedef enum {
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100321 USE_NULL_TAG = 0,
322 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100323} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100324
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +0530325
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100326/*!
327 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100328 * \param key_type_arg Type of key passed in
329 * \param key_data The encryption / decryption key data
330 * \param alg_arg The type of algorithm used
331 * \param nonce Nonce data
332 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100333 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100334 * feed additional data in to be encrypted /
335 * decrypted. If -1, no chunking.
336 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100337 * \param data_part_len_arg If not -1, the length of chunks to feed
338 * the data in to be encrypted / decrypted. If
339 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100340 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100341 * expected here, this controls whether or not
342 * to set lengths, and in what order with
343 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100344 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100345 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100346 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100347 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100348 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100349 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350static int aead_multipart_internal_func(int key_type_arg, data_t *key_data,
351 int alg_arg,
352 data_t *nonce,
353 data_t *additional_data,
354 int ad_part_len_arg,
355 data_t *input_data,
356 int data_part_len_arg,
357 set_lengths_method_t set_lengths_method,
358 data_t *expected_output,
359 int is_encrypt,
360 int do_zero_parts)
Paul Elliottd3f82412021-06-16 16:52:21 +0100361{
362 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
363 psa_key_type_t key_type = key_type_arg;
364 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100365 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100366 unsigned char *output_data = NULL;
367 unsigned char *part_data = NULL;
368 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100369 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100370 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100371 size_t output_size = 0;
372 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100373 size_t output_length = 0;
374 size_t key_bits = 0;
375 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100376 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100377 size_t part_length = 0;
378 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100379 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100380 size_t ad_part_len = 0;
381 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100382 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100383 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
384 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
385
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100386 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100387 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 PSA_ASSERT(psa_crypto_init());
Paul Elliottd3f82412021-06-16 16:52:21 +0100390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (is_encrypt) {
392 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
393 } else {
394 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100395 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100396
397 psa_set_key_algorithm(&attributes, alg);
398 psa_set_key_type(&attributes, key_type);
399
400 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
401 &key));
402
403 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
404 key_bits = psa_get_key_bits(&attributes);
405
406 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
407
408 if (is_encrypt) {
409 /* Tag gets written at end of buffer. */
410 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
411 (input_data->len +
412 tag_length));
413 data_true_size = input_data->len;
414 } else {
415 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
416 (input_data->len -
417 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100418
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100419 /* Do not want to attempt to decrypt tag. */
420 data_true_size = input_data->len - tag_length;
421 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100422
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100423 TEST_CALLOC(output_data, output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (is_encrypt) {
426 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
427 TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
428 } else {
429 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
430 TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100431 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100432
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100433 TEST_CALLOC(final_data, final_output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if (is_encrypt) {
436 status = psa_aead_encrypt_setup(&operation, key, alg);
437 } else {
438 status = psa_aead_decrypt_setup(&operation, key, alg);
439 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100440
441 /* If the operation is not supported, just skip and not fail in case the
442 * encryption involves a common limitation of cryptography hardwares and
443 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if (status == PSA_ERROR_NOT_SUPPORTED) {
445 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
446 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100447 }
448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 PSA_ASSERT(status);
Paul Elliottd3f82412021-06-16 16:52:21 +0100450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if (set_lengths_method == DO_NOT_SET_LENGTHS) {
452 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
453 } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
454 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
455 data_true_size));
456 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
457 } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
458 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottebf91632021-07-22 17:54:42 +0100459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
461 data_true_size));
Paul Elliottd3f82412021-06-16 16:52:21 +0100462 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if (ad_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100465 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100466 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100469 part_offset < additional_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 part_offset += part_length, part_count++) {
471 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100472 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 } else if (additional_data->len - part_offset < ad_part_len) {
Paul Elliotte49fe452021-09-15 16:52:11 +0100474 part_length = additional_data->len - part_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100476 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100477 }
478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 PSA_ASSERT(psa_aead_update_ad(&operation,
480 additional_data->x + part_offset,
481 part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100482
Paul Elliottd3f82412021-06-16 16:52:21 +0100483 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 } else {
Paul Elliottd3f82412021-06-16 16:52:21 +0100485 /* Pass additional data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
487 additional_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 }
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if (data_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100491 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 data_part_len = (size_t) data_part_len_arg;
493 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
494 (size_t) data_part_len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100495
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100496 TEST_CALLOC(part_data, part_data_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100499 part_offset < data_true_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 part_offset += part_length, part_count++) {
501 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100502 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 } else if ((data_true_size - part_offset) < data_part_len) {
504 part_length = (data_true_size - part_offset);
505 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100506 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100507 }
508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 PSA_ASSERT(psa_aead_update(&operation,
510 (input_data->x + part_offset),
511 part_length, part_data,
512 part_data_size,
513 &output_part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 if (output_data && output_part_length) {
516 memcpy((output_data + output_length), part_data,
517 output_part_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100518 }
519
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 output_length += output_part_length;
521 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 } else {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100523 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
525 data_true_size, output_data,
526 output_size, &output_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100527 }
528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 if (is_encrypt) {
530 PSA_ASSERT(psa_aead_finish(&operation, final_data,
531 final_output_size,
532 &output_part_length,
533 tag_buffer, tag_length,
534 &tag_size));
535 } else {
536 PSA_ASSERT(psa_aead_verify(&operation, final_data,
537 final_output_size,
538 &output_part_length,
539 (input_data->x + data_true_size),
540 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100541 }
542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 if (output_data && output_part_length) {
544 memcpy((output_data + output_length), final_data,
545 output_part_length);
546 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100547
548 output_length += output_part_length;
549
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100550
551 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
552 * should be exact.*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 if (is_encrypt) {
554 TEST_EQUAL(tag_length, tag_size);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (output_data && tag_length) {
557 memcpy((output_data + output_length), tag_buffer,
558 tag_length);
559 }
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100560
561 output_length += tag_length;
562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 TEST_EQUAL(output_length,
564 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
565 input_data->len));
566 TEST_LE_U(output_length,
567 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
568 } else {
569 TEST_EQUAL(output_length,
570 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
571 input_data->len));
572 TEST_LE_U(output_length,
573 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100574 }
575
Paul Elliottd3f82412021-06-16 16:52:21 +0100576
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100577 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100578 output_data, output_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100579
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100581 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100582
583exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 psa_destroy_key(key);
585 psa_aead_abort(&operation);
586 mbedtls_free(output_data);
587 mbedtls_free(part_data);
588 mbedtls_free(final_data);
589 PSA_DONE();
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 return test_ok;
Paul Elliottd3f82412021-06-16 16:52:21 +0100592}
593
Neil Armstrong4766f992022-02-28 16:23:59 +0100594/*!
595 * \brief Internal Function for MAC multipart tests.
596 * \param key_type_arg Type of key passed in
597 * \param key_data The encryption / decryption key data
598 * \param alg_arg The type of algorithm used
599 * \param input_data Data to encrypt / decrypt
600 * \param data_part_len_arg If not -1, the length of chunks to feed
601 * the data in to be encrypted / decrypted. If
602 * -1, no chunking
603 * \param expected_output Expected output
Tom Cosgrove1797b052022-12-04 17:19:59 +0000604 * \param is_verify If non-zero this is a verify operation.
Neil Armstrong4766f992022-02-28 16:23:59 +0100605 * \param do_zero_parts If non-zero, interleave zero length chunks
606 * with normal length chunks.
607 * \return int Zero on failure, non-zero on success.
608 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100609static int mac_multipart_internal_func(int key_type_arg, data_t *key_data,
610 int alg_arg,
611 data_t *input_data,
612 int data_part_len_arg,
613 data_t *expected_output,
614 int is_verify,
615 int do_zero_parts)
Neil Armstrong4766f992022-02-28 16:23:59 +0100616{
617 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
618 psa_key_type_t key_type = key_type_arg;
619 psa_algorithm_t alg = alg_arg;
620 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
621 unsigned char mac[PSA_MAC_MAX_SIZE];
622 size_t part_offset = 0;
623 size_t part_length = 0;
624 size_t data_part_len = 0;
625 size_t mac_len = 0;
626 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
627 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
628
629 int test_ok = 0;
630 size_t part_count = 0;
631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 PSA_INIT();
Neil Armstrong4766f992022-02-28 16:23:59 +0100633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (is_verify) {
635 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
636 } else {
637 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
638 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100639
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 psa_set_key_algorithm(&attributes, alg);
641 psa_set_key_type(&attributes, key_type);
Neil Armstrong4766f992022-02-28 16:23:59 +0100642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
644 &key));
Neil Armstrong4766f992022-02-28 16:23:59 +0100645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if (is_verify) {
647 status = psa_mac_verify_setup(&operation, key, alg);
648 } else {
649 status = psa_mac_sign_setup(&operation, key, alg);
650 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 PSA_ASSERT(status);
Neil Armstrong4766f992022-02-28 16:23:59 +0100653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (data_part_len_arg != -1) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100655 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 data_part_len = (size_t) data_part_len_arg;
Neil Armstrong4766f992022-02-28 16:23:59 +0100657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 for (part_offset = 0, part_count = 0;
Neil Armstrong4766f992022-02-28 16:23:59 +0100659 part_offset < input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 part_offset += part_length, part_count++) {
661 if (do_zero_parts && (part_count & 0x01)) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100662 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 } else if ((input_data->len - part_offset) < data_part_len) {
664 part_length = (input_data->len - part_offset);
665 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100666 part_length = data_part_len;
667 }
668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 PSA_ASSERT(psa_mac_update(&operation,
670 (input_data->x + part_offset),
671 part_length));
Neil Armstrong4766f992022-02-28 16:23:59 +0100672 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100674 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 PSA_ASSERT(psa_mac_update(&operation, input_data->x,
676 input_data->len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100677 }
678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (is_verify) {
680 PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x,
681 expected_output->len));
682 } else {
683 PSA_ASSERT(psa_mac_sign_finish(&operation, mac,
684 PSA_MAC_MAX_SIZE, &mac_len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100685
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100686 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100687 mac, mac_len);
Neil Armstrong4766f992022-02-28 16:23:59 +0100688 }
689
690 test_ok = 1;
691
692exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 psa_destroy_key(key);
694 psa_mac_abort(&operation);
695 PSA_DONE();
Neil Armstrong4766f992022-02-28 16:23:59 +0100696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 return test_ok;
Neil Armstrong4766f992022-02-28 16:23:59 +0100698}
699
Neil Armstrong75673ab2022-06-15 17:39:01 +0200700#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100701static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
702 psa_pake_operation_t *server,
703 psa_pake_operation_t *client,
704 int client_input_first,
705 int round, int inject_error)
Neil Armstrongf983caf2022-06-15 15:27:48 +0200706{
707 unsigned char *buffer0 = NULL, *buffer1 = NULL;
708 size_t buffer_length = (
709 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
710 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
711 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200712 /* The output should be exactly this size according to the spec */
713 const size_t expected_size_key_share =
714 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
715 /* The output should be exactly this size according to the spec */
716 const size_t expected_size_zk_public =
717 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
718 /* The output can be smaller: the spec allows stripping leading zeroes */
719 const size_t max_expected_size_zk_proof =
720 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200721 size_t buffer0_off = 0;
722 size_t buffer1_off = 0;
723 size_t s_g1_len, s_g2_len, s_a_len;
724 size_t s_g1_off, s_g2_off, s_a_off;
725 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
726 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
727 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
728 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
729 size_t c_g1_len, c_g2_len, c_a_len;
730 size_t c_g1_off, c_g2_off, c_a_off;
731 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
732 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
733 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
734 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
735 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200736 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200737
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100738 TEST_CALLOC(buffer0, buffer_length);
739 TEST_CALLOC(buffer1, buffer_length);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 switch (round) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200742 case 1:
743 /* Server first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
745 buffer0 + buffer0_off,
746 512 - buffer0_off, &s_g1_len));
747 TEST_EQUAL(s_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200748 s_g1_off = buffer0_off;
749 buffer0_off += s_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
751 buffer0 + buffer0_off,
752 512 - buffer0_off, &s_x1_pk_len));
753 TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200754 s_x1_pk_off = buffer0_off;
755 buffer0_off += s_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
757 buffer0 + buffer0_off,
758 512 - buffer0_off, &s_x1_pr_len));
759 TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200760 s_x1_pr_off = buffer0_off;
761 buffer0_off += s_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
763 buffer0 + buffer0_off,
764 512 - buffer0_off, &s_g2_len));
765 TEST_EQUAL(s_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200766 s_g2_off = buffer0_off;
767 buffer0_off += s_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
769 buffer0 + buffer0_off,
770 512 - buffer0_off, &s_x2_pk_len));
771 TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200772 s_x2_pk_off = buffer0_off;
773 buffer0_off += s_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
775 buffer0 + buffer0_off,
776 512 - buffer0_off, &s_x2_pr_len));
777 TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200778 s_x2_pr_off = buffer0_off;
779 buffer0_off += s_x2_pr_len;
780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if (inject_error == 1) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500782 buffer0[s_x1_pr_off + 8] ^= 1;
783 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200784 expected_status = PSA_ERROR_DATA_INVALID;
785 }
786
Neil Armstrong51009d72022-09-05 17:59:54 +0200787 /*
788 * When injecting errors in inputs, the implementation is
789 * free to detect it right away of with a delay.
790 * This permits delaying the error until the end of the input
791 * sequence, if no error appears then, this will be treated
792 * as an error.
793 */
794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200796 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
798 buffer0 + s_g1_off, s_g1_len);
799 if (inject_error == 1 && status != PSA_SUCCESS) {
800 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200801 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 } else {
803 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200804 }
805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
807 buffer0 + s_x1_pk_off,
808 s_x1_pk_len);
809 if (inject_error == 1 && status != PSA_SUCCESS) {
810 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200811 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 } else {
813 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200814 }
815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
817 buffer0 + s_x1_pr_off,
818 s_x1_pr_len);
819 if (inject_error == 1 && status != PSA_SUCCESS) {
820 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200821 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 } else {
823 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200824 }
825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
827 buffer0 + s_g2_off,
828 s_g2_len);
829 if (inject_error == 1 && status != PSA_SUCCESS) {
830 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200831 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 } else {
833 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200834 }
835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
837 buffer0 + s_x2_pk_off,
838 s_x2_pk_len);
839 if (inject_error == 1 && status != PSA_SUCCESS) {
840 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200841 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 } else {
843 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200844 }
845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
847 buffer0 + s_x2_pr_off,
848 s_x2_pr_len);
849 if (inject_error == 1 && status != PSA_SUCCESS) {
850 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200851 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 } else {
853 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200854 }
855
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200856 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 if (inject_error == 1) {
858 TEST_ASSERT(
859 !"One of the last psa_pake_input() calls should have returned the expected error.");
860 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200861 }
862
863 /* Client first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
865 buffer1 + buffer1_off,
866 512 - buffer1_off, &c_g1_len));
867 TEST_EQUAL(c_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200868 c_g1_off = buffer1_off;
869 buffer1_off += c_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
871 buffer1 + buffer1_off,
872 512 - buffer1_off, &c_x1_pk_len));
873 TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200874 c_x1_pk_off = buffer1_off;
875 buffer1_off += c_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
877 buffer1 + buffer1_off,
878 512 - buffer1_off, &c_x1_pr_len));
879 TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200880 c_x1_pr_off = buffer1_off;
881 buffer1_off += c_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
883 buffer1 + buffer1_off,
884 512 - buffer1_off, &c_g2_len));
885 TEST_EQUAL(c_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200886 c_g2_off = buffer1_off;
887 buffer1_off += c_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
889 buffer1 + buffer1_off,
890 512 - buffer1_off, &c_x2_pk_len));
891 TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200892 c_x2_pk_off = buffer1_off;
893 buffer1_off += c_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
895 buffer1 + buffer1_off,
896 512 - buffer1_off, &c_x2_pr_len));
897 TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200898 c_x2_pr_off = buffer1_off;
899 buffer1_off += c_x2_pr_len;
900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200902 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
904 buffer0 + s_g1_off, s_g1_len);
905 if (inject_error == 1 && status != PSA_SUCCESS) {
906 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200907 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 } else {
909 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200910 }
911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
913 buffer0 + s_x1_pk_off,
914 s_x1_pk_len);
915 if (inject_error == 1 && status != PSA_SUCCESS) {
916 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200917 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 } else {
919 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200920 }
921
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
923 buffer0 + s_x1_pr_off,
924 s_x1_pr_len);
925 if (inject_error == 1 && status != PSA_SUCCESS) {
926 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200927 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 } else {
929 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200930 }
931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
933 buffer0 + s_g2_off,
934 s_g2_len);
935 if (inject_error == 1 && status != PSA_SUCCESS) {
936 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200937 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 } else {
939 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200940 }
941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
943 buffer0 + s_x2_pk_off,
944 s_x2_pk_len);
945 if (inject_error == 1 && status != PSA_SUCCESS) {
946 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200947 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 } else {
949 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200950 }
951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
953 buffer0 + s_x2_pr_off,
954 s_x2_pr_len);
955 if (inject_error == 1 && status != PSA_SUCCESS) {
956 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200957 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 } else {
959 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200960 }
961
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200962 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 if (inject_error == 1) {
964 TEST_ASSERT(
965 !"One of the last psa_pake_input() calls should have returned the expected error.");
966 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200967 }
968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if (inject_error == 2) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500970 buffer1[c_x1_pr_off + 12] ^= 1;
971 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200972 expected_status = PSA_ERROR_DATA_INVALID;
973 }
974
975 /* Server first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
977 buffer1 + c_g1_off, c_g1_len);
978 if (inject_error == 2 && status != PSA_SUCCESS) {
979 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200980 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 } else {
982 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200983 }
984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
986 buffer1 + c_x1_pk_off, c_x1_pk_len);
987 if (inject_error == 2 && status != PSA_SUCCESS) {
988 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200989 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 } else {
991 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200992 }
993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
995 buffer1 + c_x1_pr_off, c_x1_pr_len);
996 if (inject_error == 2 && status != PSA_SUCCESS) {
997 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200998 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 } else {
1000 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001001 }
1002
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1004 buffer1 + c_g2_off, c_g2_len);
1005 if (inject_error == 2 && status != PSA_SUCCESS) {
1006 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001007 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 } else {
1009 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001010 }
1011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1013 buffer1 + c_x2_pk_off, c_x2_pk_len);
1014 if (inject_error == 2 && status != PSA_SUCCESS) {
1015 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001016 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 } else {
1018 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001019 }
1020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1022 buffer1 + c_x2_pr_off, c_x2_pr_len);
1023 if (inject_error == 2 && status != PSA_SUCCESS) {
1024 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001025 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 } else {
1027 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001028 }
1029
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001030 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 if (inject_error == 2) {
1032 TEST_ASSERT(
1033 !"One of the last psa_pake_input() calls should have returned the expected error.");
1034 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001035
1036 break;
1037
1038 case 2:
1039 /* Server second round Output */
1040 buffer0_off = 0;
1041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
1043 buffer0 + buffer0_off,
1044 512 - buffer0_off, &s_a_len));
1045 TEST_EQUAL(s_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001046 s_a_off = buffer0_off;
1047 buffer0_off += s_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
1049 buffer0 + buffer0_off,
1050 512 - buffer0_off, &s_x2s_pk_len));
1051 TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001052 s_x2s_pk_off = buffer0_off;
1053 buffer0_off += s_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
1055 buffer0 + buffer0_off,
1056 512 - buffer0_off, &s_x2s_pr_len));
1057 TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001058 s_x2s_pr_off = buffer0_off;
1059 buffer0_off += s_x2s_pr_len;
1060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if (inject_error == 3) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001062 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001063 expected_status = PSA_ERROR_DATA_INVALID;
1064 }
1065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001067 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1069 buffer0 + s_a_off, s_a_len);
1070 if (inject_error == 3 && status != PSA_SUCCESS) {
1071 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001072 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 } else {
1074 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001075 }
1076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1078 buffer0 + s_x2s_pk_off,
1079 s_x2s_pk_len);
1080 if (inject_error == 3 && status != PSA_SUCCESS) {
1081 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001082 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 } else {
1084 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001085 }
1086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1088 buffer0 + s_x2s_pr_off,
1089 s_x2s_pr_len);
1090 if (inject_error == 3 && status != PSA_SUCCESS) {
1091 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001092 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 } else {
1094 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001095 }
1096
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001097 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 if (inject_error == 3) {
1099 TEST_ASSERT(
1100 !"One of the last psa_pake_input() calls should have returned the expected error.");
1101 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001102 }
1103
1104 /* Client second round Output */
1105 buffer1_off = 0;
1106
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
1108 buffer1 + buffer1_off,
1109 512 - buffer1_off, &c_a_len));
1110 TEST_EQUAL(c_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001111 c_a_off = buffer1_off;
1112 buffer1_off += c_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
1114 buffer1 + buffer1_off,
1115 512 - buffer1_off, &c_x2s_pk_len));
1116 TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001117 c_x2s_pk_off = buffer1_off;
1118 buffer1_off += c_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
1120 buffer1 + buffer1_off,
1121 512 - buffer1_off, &c_x2s_pr_len));
1122 TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001123 c_x2s_pr_off = buffer1_off;
1124 buffer1_off += c_x2s_pr_len;
1125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001127 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1129 buffer0 + s_a_off, s_a_len);
1130 if (inject_error == 3 && status != PSA_SUCCESS) {
1131 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001132 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 } else {
1134 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001135 }
1136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1138 buffer0 + s_x2s_pk_off,
1139 s_x2s_pk_len);
1140 if (inject_error == 3 && status != PSA_SUCCESS) {
1141 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001142 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 } else {
1144 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001145 }
1146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1148 buffer0 + s_x2s_pr_off,
1149 s_x2s_pr_len);
1150 if (inject_error == 3 && status != PSA_SUCCESS) {
1151 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001152 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 } else {
1154 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001155 }
1156
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001157 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 if (inject_error == 3) {
1159 TEST_ASSERT(
1160 !"One of the last psa_pake_input() calls should have returned the expected error.");
1161 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001162 }
1163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 if (inject_error == 4) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001165 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001166 expected_status = PSA_ERROR_DATA_INVALID;
1167 }
1168
1169 /* Server second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1171 buffer1 + c_a_off, c_a_len);
1172 if (inject_error == 4 && status != PSA_SUCCESS) {
1173 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001174 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 } else {
1176 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001177 }
1178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1180 buffer1 + c_x2s_pk_off, c_x2s_pk_len);
1181 if (inject_error == 4 && status != PSA_SUCCESS) {
1182 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001183 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 } else {
1185 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001186 }
1187
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1189 buffer1 + c_x2s_pr_off, c_x2s_pr_len);
1190 if (inject_error == 4 && status != PSA_SUCCESS) {
1191 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001192 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 } else {
1194 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001195 }
1196
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001197 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 if (inject_error == 4) {
1199 TEST_ASSERT(
1200 !"One of the last psa_pake_input() calls should have returned the expected error.");
1201 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001202
1203 break;
1204
1205 }
1206
Neil Armstrongf983caf2022-06-15 15:27:48 +02001207exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 mbedtls_free(buffer0);
1209 mbedtls_free(buffer1);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001210}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001211#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213typedef enum {
Valerio Setti1070aed2022-11-11 19:37:31 +01001214 INJECT_ERR_NONE = 0,
1215 INJECT_ERR_UNINITIALIZED_ACCESS,
1216 INJECT_ERR_DUPLICATE_SETUP,
1217 INJECT_ERR_INVALID_USER,
1218 INJECT_ERR_INVALID_PEER,
1219 INJECT_ERR_SET_USER,
1220 INJECT_ERR_SET_PEER,
1221 INJECT_EMPTY_IO_BUFFER,
1222 INJECT_UNKNOWN_STEP,
1223 INJECT_INVALID_FIRST_STEP,
1224 INJECT_WRONG_BUFFER_SIZE,
1225 INJECT_VALID_OPERATION_AFTER_FAILURE,
1226 INJECT_ANTICIPATE_KEY_DERIVATION_1,
1227 INJECT_ANTICIPATE_KEY_DERIVATION_2,
1228} ecjpake_injected_failure_t;
1229
Paul Elliott01885fa2023-02-09 12:07:30 +00001230#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott1243f932023-02-07 11:21:10 +00001231
Paul Elliott6f600372023-02-06 18:41:05 +00001232static void interruptible_signverify_get_minmax_completes(uint32_t max_ops,
1233 psa_status_t expected_status,
1234 size_t *min_completes,
1235 size_t *max_completes)
1236{
1237
1238 /* This is slightly contrived, but we only really know that with a minimum
1239 value of max_ops that a successful operation should take more than one op
1240 to complete, and likewise that with a max_ops of
1241 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */
1242 if (max_ops == 0 || max_ops == 1) {
Paul Elliottc86d45e2023-02-15 17:38:05 +00001243
Paul Elliott6f600372023-02-06 18:41:05 +00001244 if (expected_status == PSA_SUCCESS) {
1245 *min_completes = 2;
1246 } else {
1247 *min_completes = 1;
1248 }
1249
1250 *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
1251 } else {
1252 *min_completes = 1;
1253 *max_completes = 1;
1254 }
1255}
Paul Elliott01885fa2023-02-09 12:07:30 +00001256#endif /* MBEDTLS_ECP_RESTARTABLE */
Paul Elliott6f600372023-02-06 18:41:05 +00001257
Gilles Peskine1d25a0a2024-02-12 16:40:04 +01001258#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
1259static int rsa_test_e(mbedtls_svc_key_id_t key,
1260 size_t bits,
1261 const data_t *e_arg)
1262{
1263 uint8_t *exported = NULL;
1264 size_t exported_size =
1265 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
1266 size_t exported_length = SIZE_MAX;
1267 int ok = 0;
1268
1269 TEST_CALLOC(exported, exported_size);
1270 PSA_ASSERT(psa_export_public_key(key,
1271 exported, exported_size,
1272 &exported_length));
1273 uint8_t *p = exported;
1274 uint8_t *end = exported + exported_length;
1275 size_t len;
1276 /* RSAPublicKey ::= SEQUENCE {
1277 * modulus INTEGER, -- n
1278 * publicExponent INTEGER } -- e
1279 */
1280 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
1281 MBEDTLS_ASN1_SEQUENCE |
1282 MBEDTLS_ASN1_CONSTRUCTED));
1283 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
1284 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
1285 MBEDTLS_ASN1_INTEGER));
1286 if (len >= 1 && p[0] == 0) {
1287 ++p;
1288 --len;
1289 }
1290 if (e_arg->len == 0) {
1291 TEST_EQUAL(len, 3);
1292 TEST_EQUAL(p[0], 1);
1293 TEST_EQUAL(p[1], 0);
1294 TEST_EQUAL(p[2], 1);
1295 } else {
1296 TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len);
1297 }
1298 ok = 1;
1299
1300exit:
1301 mbedtls_free(exported);
1302 return ok;
1303}
1304#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE */
1305
Gilles Peskinef0765fa2024-02-12 16:46:16 +01001306static int setup_key_generation_method(psa_key_generation_method_t **method,
1307 size_t *method_length,
1308 int64_t flags_arg,
1309 const data_t *method_data)
1310{
1311 if (flags_arg >= 0) {
1312 *method_length = sizeof(**method) + method_data->len;
1313 *method = mbedtls_calloc(1, *method_length);
1314 TEST_ASSERT(*method != NULL);
1315 (*method)->flags = (uint32_t) flags_arg;
1316 memcpy((*method)->data, method_data->x, method_data->len);
1317 } else if (sizeof(**method) + flags_arg > 0) {
1318 *method_length = sizeof(**method) + flags_arg;
1319 *method = mbedtls_calloc(1, *method_length);
1320 TEST_ASSERT(*method != NULL);
1321 }
1322 return 1;
1323exit:
1324 return 0;
1325}
1326
Gilles Peskinee59236f2018-01-27 23:32:46 +01001327/* END_HEADER */
1328
1329/* BEGIN_DEPENDENCIES
1330 * depends_on:MBEDTLS_PSA_CRYPTO_C
1331 * END_DEPENDENCIES
1332 */
1333
1334/* BEGIN_CASE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01001335void psa_can_do_hash()
1336{
1337 /* We can't test that this is specific to drivers until partial init has
1338 * been implemented, but we can at least test before/after full init. */
1339 TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE));
1340 PSA_INIT();
1341 TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE));
1342 PSA_DONE();
1343}
1344/* END_CASE */
1345
1346/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001347void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001348{
1349 size_t max_truncated_mac_size =
1350 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1351
1352 /* Check that the length for a truncated MAC always fits in the algorithm
1353 * encoding. The shifted mask is the maximum truncated value. The
1354 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001356}
1357/* END_CASE */
1358
1359/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001360void import_with_policy(int type_arg,
1361 int usage_arg, int alg_arg,
1362 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001363{
1364 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1365 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001366 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001367 psa_key_type_t type = type_arg;
1368 psa_key_usage_t usage = usage_arg;
1369 psa_algorithm_t alg = alg_arg;
1370 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001372 psa_status_t status;
1373
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001375
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 psa_set_key_type(&attributes, type);
1377 psa_set_key_usage_flags(&attributes, usage);
1378 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001379
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 status = psa_import_key(&attributes,
1381 key_material, sizeof(key_material),
1382 &key);
1383 TEST_EQUAL(status, expected_status);
1384 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001385 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1389 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1390 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1391 mbedtls_test_update_key_usage_flags(usage));
1392 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1393 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001394
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 PSA_ASSERT(psa_destroy_key(key));
1396 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001397
1398exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001399 /*
1400 * Key attributes may have been returned by psa_get_key_attributes()
1401 * thus reset them as required.
1402 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 psa_destroy_key(key);
1406 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001407}
1408/* END_CASE */
1409
1410/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001411void import_with_data(data_t *data, int type_arg,
1412 int attr_bits_arg,
1413 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001414{
1415 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1416 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001417 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001418 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001419 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001420 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001421 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 psa_set_key_type(&attributes, type);
1426 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001427
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 status = psa_import_key(&attributes, data->x, data->len, &key);
Manuel Pégourié-Gonnard0509b582023-08-08 12:47:56 +02001429 /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED.
1430 *
1431 * This can happen with a type supported only by a driver:
1432 * - the driver sees the invalid data (for example wrong size) and thinks
1433 * "well perhaps this is a key size I don't support" so it returns
1434 * NOT_SUPPORTED which is correct at this point;
1435 * - we fallback to built-ins, which don't support this type, so return
1436 * NOT_SUPPORTED which again is correct at this point.
1437 */
1438 if (expected_status == PSA_ERROR_INVALID_ARGUMENT &&
1439 status == PSA_ERROR_NOT_SUPPORTED) {
1440 ; // OK
1441 } else {
1442 TEST_EQUAL(status, expected_status);
1443 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001445 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001447
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1449 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1450 if (attr_bits != 0) {
1451 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1452 }
1453 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 PSA_ASSERT(psa_destroy_key(key));
1456 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001457
1458exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001459 /*
1460 * Key attributes may have been returned by psa_get_key_attributes()
1461 * thus reset them as required.
1462 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001464
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 psa_destroy_key(key);
1466 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001467}
1468/* END_CASE */
1469
1470/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001471/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001472void import_large_key(int type_arg, int byte_size_arg,
1473 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001474{
1475 psa_key_type_t type = type_arg;
1476 size_t byte_size = byte_size_arg;
1477 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1478 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001479 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001480 psa_status_t status;
1481 uint8_t *buffer = NULL;
1482 size_t buffer_size = byte_size + 1;
1483 size_t n;
1484
Steven Cooreman69967ce2021-01-18 18:01:08 +01001485 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001486 * accommodate large keys due to heap size constraints */
Tom Cosgrove412a8132023-07-20 16:55:14 +01001487 TEST_CALLOC_OR_SKIP(buffer, buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001491
1492 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1494 psa_set_key_type(&attributes, type);
1495 status = psa_import_key(&attributes, buffer, byte_size, &key);
1496 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1497 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001498
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 if (status == PSA_SUCCESS) {
1500 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1501 TEST_EQUAL(psa_get_key_type(&attributes), type);
1502 TEST_EQUAL(psa_get_key_bits(&attributes),
1503 PSA_BYTES_TO_BITS(byte_size));
1504 ASSERT_NO_SLOT_NUMBER(&attributes);
1505 memset(buffer, 0, byte_size + 1);
1506 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1507 for (n = 0; n < byte_size; n++) {
1508 TEST_EQUAL(buffer[n], 'K');
1509 }
1510 for (n = byte_size; n < buffer_size; n++) {
1511 TEST_EQUAL(buffer[n], 0);
1512 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001513 }
1514
1515exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001516 /*
1517 * Key attributes may have been returned by psa_get_key_attributes()
1518 * thus reset them as required.
1519 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 psa_destroy_key(key);
1523 PSA_DONE();
1524 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001525}
1526/* END_CASE */
1527
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001528/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001529/* Import an RSA key with a valid structure (but not valid numbers
1530 * inside, beyond having sensible size and parity). This is expected to
1531 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001532void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001533{
Ronald Cron5425a212020-08-04 14:58:35 +02001534 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001535 size_t bits = bits_arg;
1536 psa_status_t expected_status = expected_status_arg;
1537 psa_status_t status;
1538 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001539 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001540 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001542 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001543 unsigned char *p;
1544 int ret;
1545 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001546 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 PSA_ASSERT(psa_crypto_init());
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001549 TEST_CALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001550
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1552 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001553 length = ret;
1554
1555 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 psa_set_key_type(&attributes, type);
1557 status = psa_import_key(&attributes, p, length, &key);
1558 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 if (status == PSA_SUCCESS) {
1561 PSA_ASSERT(psa_destroy_key(key));
1562 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001563
1564exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 mbedtls_free(buffer);
1566 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001567}
1568/* END_CASE */
1569
1570/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001571void import_export(data_t *data,
1572 int type_arg,
1573 int usage_arg, int alg_arg,
1574 int lifetime_arg,
1575 int expected_bits,
1576 int export_size_delta,
1577 int expected_export_status_arg,
1578 /*whether reexport must give the original input exactly*/
1579 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001580{
Ronald Cron5425a212020-08-04 14:58:35 +02001581 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001582 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001583 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001584 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001585 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301586 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001587 unsigned char *exported = NULL;
1588 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001589 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001590 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001591 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001592 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001593 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001594
Moran Pekercb088e72018-07-17 17:36:59 +03001595 export_size = (ptrdiff_t) data->len + export_size_delta;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001596 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 if (!canonical_input) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001598 TEST_CALLOC(reexported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 }
1600 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001601
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 psa_set_key_lifetime(&attributes, lifetime);
1603 psa_set_key_usage_flags(&attributes, usage_arg);
1604 psa_set_key_algorithm(&attributes, alg);
1605 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001606
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001607 if (PSA_KEY_TYPE_IS_DH(type) &&
1608 expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) {
Przemek Stekiel654bef02022-12-15 13:28:02 +01001609 /* Simulate that buffer is too small, by decreasing its size by 1 byte. */
1610 export_size -= 1;
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001611 }
1612
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001613 /* Import the key */
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001614 TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key),
Przemek Stekiel2e7c33d2023-04-27 12:29:45 +02001615 PSA_SUCCESS);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001616
1617 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1619 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1620 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1621 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001622
1623 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 status = psa_export_key(key, exported, export_size, &exported_length);
1625 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001626
1627 /* The exported length must be set by psa_export_key() to a value between 0
1628 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1630 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1631 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1634 export_size - exported_length));
1635 if (status != PSA_SUCCESS) {
1636 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001637 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001638 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001639
Gilles Peskineea38a922021-02-13 00:05:16 +01001640 /* Run sanity checks on the exported key. For non-canonical inputs,
1641 * this validates the canonical representations. For canonical inputs,
1642 * this doesn't directly validate the implementation, but it still helps
1643 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 if (!psa_key_lifetime_is_external(lifetime)) {
1645 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301646 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 }
Archana4d7ae1d2021-07-07 02:50:22 +05301648 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001649
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 if (canonical_input) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001651 TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001653 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1655 &key2));
1656 PSA_ASSERT(psa_export_key(key2,
1657 reexported,
1658 export_size,
1659 &reexported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001660 TEST_MEMORY_COMPARE(exported, exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001661 reexported, reexported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001663 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 TEST_LE_U(exported_length,
1665 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1666 psa_get_key_bits(&got_attributes)));
Valerio Setti1eacae82023-07-28 16:07:03 +02001667 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
1668 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
1669 } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
1670 TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1671 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001672
1673destroy:
1674 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 PSA_ASSERT(psa_destroy_key(key));
1676 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001677
1678exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001679 /*
1680 * Key attributes may have been returned by psa_get_key_attributes()
1681 * thus reset them as required.
1682 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 psa_reset_key_attributes(&got_attributes);
1684 psa_destroy_key(key);
1685 mbedtls_free(exported);
1686 mbedtls_free(reexported);
1687 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001688}
1689/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001690
Moran Pekerf709f4a2018-06-06 17:26:04 +03001691/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001692void import_export_public_key(data_t *data,
1693 int type_arg, // key pair or public key
1694 int alg_arg,
1695 int lifetime_arg,
1696 int export_size_delta,
1697 int expected_export_status_arg,
1698 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001699{
Ronald Cron5425a212020-08-04 14:58:35 +02001700 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001701 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001702 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001703 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001704 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301705 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001706 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001707 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001708 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001709 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001710
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001712
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 psa_set_key_lifetime(&attributes, lifetime);
1714 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1715 psa_set_key_algorithm(&attributes, alg);
1716 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001717
1718 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001720
Gilles Peskine49c25912018-10-29 15:15:31 +01001721 /* Export the public key */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001722 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 status = psa_export_public_key(key,
1724 exported, export_size,
1725 &exported_length);
1726 TEST_EQUAL(status, expected_export_status);
1727 if (status == PSA_SUCCESS) {
1728 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001729 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1731 bits = psa_get_key_bits(&attributes);
1732 TEST_LE_U(expected_public_key->len,
1733 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1734 TEST_LE_U(expected_public_key->len,
1735 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1736 TEST_LE_U(expected_public_key->len,
1737 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001738 TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001739 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001740 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001741exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001742 /*
1743 * Key attributes may have been returned by psa_get_key_attributes()
1744 * thus reset them as required.
1745 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 mbedtls_free(exported);
1749 psa_destroy_key(key);
1750 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001751}
1752/* END_CASE */
1753
Gilles Peskine20035e32018-02-03 22:44:14 +01001754/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001755void import_and_exercise_key(data_t *data,
1756 int type_arg,
1757 int bits_arg,
1758 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001759{
Ronald Cron5425a212020-08-04 14:58:35 +02001760 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001761 psa_key_type_t type = type_arg;
1762 size_t bits = bits_arg;
1763 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001765 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001766 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001767
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001769
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 psa_set_key_usage_flags(&attributes, usage);
1771 psa_set_key_algorithm(&attributes, alg);
1772 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001773
1774 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001776
1777 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1779 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1780 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001781
1782 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001784 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001786
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 PSA_ASSERT(psa_destroy_key(key));
1788 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001789
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001790exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001791 /*
1792 * Key attributes may have been returned by psa_get_key_attributes()
1793 * thus reset them as required.
1794 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001796
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 psa_reset_key_attributes(&attributes);
1798 psa_destroy_key(key);
1799 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001800}
1801/* END_CASE */
1802
1803/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001804void effective_key_attributes(int type_arg, int expected_type_arg,
1805 int bits_arg, int expected_bits_arg,
1806 int usage_arg, int expected_usage_arg,
1807 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001808{
Ronald Cron5425a212020-08-04 14:58:35 +02001809 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001810 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001811 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001812 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001813 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001814 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001815 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001816 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001817 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001818 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001819
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001821
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 psa_set_key_usage_flags(&attributes, usage);
1823 psa_set_key_algorithm(&attributes, alg);
1824 psa_set_key_type(&attributes, key_type);
1825 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001826
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 PSA_ASSERT(psa_generate_key(&attributes, &key));
1828 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1831 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1832 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1833 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1834 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001835
1836exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001837 /*
1838 * Key attributes may have been returned by psa_get_key_attributes()
1839 * thus reset them as required.
1840 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001842
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 psa_destroy_key(key);
1844 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001845}
1846/* END_CASE */
1847
1848/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001849void check_key_policy(int type_arg, int bits_arg,
1850 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001851{
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1853 usage_arg,
1854 mbedtls_test_update_key_usage_flags(usage_arg),
1855 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001856 goto exit;
1857}
1858/* END_CASE */
1859
1860/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001861void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001862{
1863 /* Test each valid way of initializing the object, except for `= {0}`, as
1864 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1865 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001866 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001868 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1869 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001870
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001872
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1874 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1875 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001876
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 TEST_EQUAL(psa_get_key_type(&func), 0);
1878 TEST_EQUAL(psa_get_key_type(&init), 0);
1879 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001880
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 TEST_EQUAL(psa_get_key_bits(&func), 0);
1882 TEST_EQUAL(psa_get_key_bits(&init), 0);
1883 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1886 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1887 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001888
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1890 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1891 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001892}
1893/* END_CASE */
1894
1895/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001896void mac_key_policy(int policy_usage_arg,
1897 int policy_alg_arg,
1898 int key_type_arg,
1899 data_t *key_data,
1900 int exercise_alg_arg,
1901 int expected_status_sign_arg,
1902 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001903{
Ronald Cron5425a212020-08-04 14:58:35 +02001904 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001905 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001906 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001907 psa_key_type_t key_type = key_type_arg;
1908 psa_algorithm_t policy_alg = policy_alg_arg;
1909 psa_algorithm_t exercise_alg = exercise_alg_arg;
1910 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001911 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001912 psa_status_t expected_status_sign = expected_status_sign_arg;
1913 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001914 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001915
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 psa_set_key_usage_flags(&attributes, policy_usage);
1919 psa_set_key_algorithm(&attributes, policy_alg);
1920 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001921
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1923 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001924
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1926 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001927
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1929 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001930
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001931 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001933 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1935 input, 128,
1936 mac, PSA_MAC_MAX_SIZE, &mac_len),
1937 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001938
Neil Armstrong3af9b972022-02-07 12:20:21 +01001939 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 PSA_ASSERT(psa_mac_abort(&operation));
1941 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1942 if (status == PSA_SUCCESS) {
1943 status = psa_mac_update(&operation, input, 128);
1944 if (status == PSA_SUCCESS) {
1945 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1946 &mac_len),
1947 expected_status_sign);
1948 } else {
1949 TEST_EQUAL(status, expected_status_sign);
1950 }
1951 } else {
1952 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001953 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001955
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001956 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 status = psa_mac_verify(key, exercise_alg, input, 128,
1958 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1961 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1962 } else {
1963 TEST_EQUAL(status, expected_status_verify);
1964 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001965
Neil Armstrong3af9b972022-02-07 12:20:21 +01001966 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1968 if (status == PSA_SUCCESS) {
1969 status = psa_mac_update(&operation, input, 128);
1970 if (status == PSA_SUCCESS) {
1971 status = psa_mac_verify_finish(&operation, mac, mac_len);
1972 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1973 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1974 } else {
1975 TEST_EQUAL(status, expected_status_verify);
1976 }
1977 } else {
1978 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001979 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 } else {
1981 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001982 }
1983
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001985
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 memset(mac, 0, sizeof(mac));
1987 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1988 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001989
1990exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 psa_mac_abort(&operation);
1992 psa_destroy_key(key);
1993 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001994}
1995/* END_CASE */
1996
1997/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001998void cipher_key_policy(int policy_usage_arg,
1999 int policy_alg,
2000 int key_type,
2001 data_t *key_data,
2002 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002003{
Ronald Cron5425a212020-08-04 14:58:35 +02002004 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002005 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002006 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002007 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002008 size_t output_buffer_size = 0;
2009 size_t input_buffer_size = 0;
2010 size_t output_length = 0;
2011 uint8_t *output = NULL;
2012 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002013 psa_status_t status;
2014
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
2016 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
2017 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002018
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002019 TEST_CALLOC(input, input_buffer_size);
2020 TEST_CALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002021
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 psa_set_key_usage_flags(&attributes, policy_usage);
2025 psa_set_key_algorithm(&attributes, policy_alg);
2026 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002027
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2029 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002030
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002031 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 TEST_EQUAL(policy_usage,
2033 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002034
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002035 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
2037 output, output_buffer_size,
2038 &output_length);
2039 if (policy_alg == exercise_alg &&
2040 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2041 PSA_ASSERT(status);
2042 } else {
2043 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2044 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002045
2046 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
2048 if (policy_alg == exercise_alg &&
2049 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2050 PSA_ASSERT(status);
2051 } else {
2052 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2053 }
2054 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002055
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002056 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
2058 input, input_buffer_size,
2059 &output_length);
2060 if (policy_alg == exercise_alg &&
2061 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2062 PSA_ASSERT(status);
2063 } else {
2064 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2065 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002066
2067 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
2069 if (policy_alg == exercise_alg &&
2070 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2071 PSA_ASSERT(status);
2072 } else {
2073 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2074 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002075
2076exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 psa_cipher_abort(&operation);
2078 mbedtls_free(input);
2079 mbedtls_free(output);
2080 psa_destroy_key(key);
2081 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002082}
2083/* END_CASE */
2084
2085/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002086void aead_key_policy(int policy_usage_arg,
2087 int policy_alg,
2088 int key_type,
2089 data_t *key_data,
2090 int nonce_length_arg,
2091 int tag_length_arg,
2092 int exercise_alg,
2093 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002094{
Ronald Cron5425a212020-08-04 14:58:35 +02002095 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002096 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002097 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002098 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002099 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002100 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002102 size_t nonce_length = nonce_length_arg;
2103 unsigned char tag[16];
2104 size_t tag_length = tag_length_arg;
2105 size_t output_length;
2106
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 TEST_LE_U(nonce_length, sizeof(nonce));
2108 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 psa_set_key_usage_flags(&attributes, policy_usage);
2113 psa_set_key_algorithm(&attributes, policy_alg);
2114 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2117 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002118
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002119 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 TEST_EQUAL(policy_usage,
2121 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002122
Neil Armstrong752d8112022-02-07 14:51:11 +01002123 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 status = psa_aead_encrypt(key, exercise_alg,
2125 nonce, nonce_length,
2126 NULL, 0,
2127 NULL, 0,
2128 tag, tag_length,
2129 &output_length);
2130 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2131 TEST_EQUAL(status, expected_status);
2132 } else {
2133 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2134 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002135
Neil Armstrong752d8112022-02-07 14:51:11 +01002136 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2138 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2139 TEST_EQUAL(status, expected_status);
2140 } else {
2141 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2142 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002143
2144 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 memset(tag, 0, sizeof(tag));
2146 status = psa_aead_decrypt(key, exercise_alg,
2147 nonce, nonce_length,
2148 NULL, 0,
2149 tag, tag_length,
2150 NULL, 0,
2151 &output_length);
2152 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2153 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2154 } else if (expected_status == PSA_SUCCESS) {
2155 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2156 } else {
2157 TEST_EQUAL(status, expected_status);
2158 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002159
Neil Armstrong752d8112022-02-07 14:51:11 +01002160 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 PSA_ASSERT(psa_aead_abort(&operation));
2162 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2163 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2164 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2165 } else {
2166 TEST_EQUAL(status, expected_status);
2167 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002168
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002169exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 PSA_ASSERT(psa_aead_abort(&operation));
2171 psa_destroy_key(key);
2172 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002173}
2174/* END_CASE */
2175
2176/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002177void asymmetric_encryption_key_policy(int policy_usage_arg,
2178 int policy_alg,
2179 int key_type,
2180 data_t *key_data,
Valerio Settif202c292024-01-15 10:42:37 +01002181 int exercise_alg,
2182 int use_opaque_key)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002183{
Ronald Cron5425a212020-08-04 14:58:35 +02002184 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002185 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002186 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002187 psa_status_t status;
2188 size_t key_bits;
2189 size_t buffer_length;
2190 unsigned char *buffer = NULL;
2191 size_t output_length;
2192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002194
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 psa_set_key_usage_flags(&attributes, policy_usage);
2196 psa_set_key_algorithm(&attributes, policy_alg);
2197 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002198
Valerio Settif202c292024-01-15 10:42:37 +01002199 if (use_opaque_key) {
2200 psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
2201 PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION));
2202 }
2203
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2205 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002206
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002207 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 TEST_EQUAL(policy_usage,
2209 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2212 key_bits = psa_get_key_bits(&attributes);
2213 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2214 exercise_alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002215 TEST_CALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 status = psa_asymmetric_encrypt(key, exercise_alg,
2218 NULL, 0,
2219 NULL, 0,
2220 buffer, buffer_length,
2221 &output_length);
2222 if (policy_alg == exercise_alg &&
2223 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 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 if (buffer_length != 0) {
2230 memset(buffer, 0, buffer_length);
2231 }
2232 status = psa_asymmetric_decrypt(key, exercise_alg,
2233 buffer, buffer_length,
2234 NULL, 0,
2235 buffer, buffer_length,
2236 &output_length);
2237 if (policy_alg == exercise_alg &&
2238 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2239 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2240 } else {
2241 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2242 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002243
2244exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002245 /*
2246 * Key attributes may have been returned by psa_get_key_attributes()
2247 * thus reset them as required.
2248 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 psa_destroy_key(key);
2252 PSA_DONE();
2253 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002254}
2255/* END_CASE */
2256
2257/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002258void asymmetric_signature_key_policy(int policy_usage_arg,
2259 int policy_alg,
2260 int key_type,
2261 data_t *key_data,
2262 int exercise_alg,
2263 int payload_length_arg,
2264 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002265{
Ronald Cron5425a212020-08-04 14:58:35 +02002266 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002267 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002268 psa_key_usage_t policy_usage = policy_usage_arg;
2269 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002270 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002272 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2273 * compatible with the policy and `payload_length_arg` is supposed to be
2274 * a valid input length to sign. If `payload_length_arg <= 0`,
2275 * `exercise_alg` is supposed to be forbidden by the policy. */
2276 int compatible_alg = payload_length_arg > 0;
2277 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002279 size_t signature_length;
2280
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002281 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002282 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 TEST_EQUAL(expected_usage,
2284 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002287
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 psa_set_key_usage_flags(&attributes, policy_usage);
2289 psa_set_key_algorithm(&attributes, policy_alg);
2290 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2293 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002294
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 status = psa_sign_hash(key, exercise_alg,
2298 payload, payload_length,
2299 signature, sizeof(signature),
2300 &signature_length);
2301 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2302 PSA_ASSERT(status);
2303 } else {
2304 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2305 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 memset(signature, 0, sizeof(signature));
2308 status = psa_verify_hash(key, exercise_alg,
2309 payload, payload_length,
2310 signature, sizeof(signature));
2311 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2312 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2313 } else {
2314 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2315 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002316
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2318 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2319 status = psa_sign_message(key, exercise_alg,
2320 payload, payload_length,
2321 signature, sizeof(signature),
2322 &signature_length);
2323 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2324 PSA_ASSERT(status);
2325 } else {
2326 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2327 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 memset(signature, 0, sizeof(signature));
2330 status = psa_verify_message(key, exercise_alg,
2331 payload, payload_length,
2332 signature, sizeof(signature));
2333 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2334 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2335 } else {
2336 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2337 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002338 }
2339
Gilles Peskined5b33222018-06-18 22:20:03 +02002340exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 psa_destroy_key(key);
2342 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002343}
2344/* END_CASE */
2345
Janos Follathba3fab92019-06-11 14:50:16 +01002346/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002347void derive_key_policy(int policy_usage,
2348 int policy_alg,
2349 int key_type,
2350 data_t *key_data,
2351 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002352{
Ronald Cron5425a212020-08-04 14:58:35 +02002353 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002354 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002355 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002356 psa_status_t status;
2357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002359
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 psa_set_key_usage_flags(&attributes, policy_usage);
2361 psa_set_key_algorithm(&attributes, policy_alg);
2362 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2365 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002366
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2370 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2371 PSA_ASSERT(psa_key_derivation_input_bytes(
2372 &operation,
2373 PSA_KEY_DERIVATION_INPUT_SEED,
2374 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002375 }
Janos Follathba3fab92019-06-11 14:50:16 +01002376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 status = psa_key_derivation_input_key(&operation,
2378 PSA_KEY_DERIVATION_INPUT_SECRET,
2379 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 if (policy_alg == exercise_alg &&
2382 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2383 PSA_ASSERT(status);
2384 } else {
2385 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2386 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002387
2388exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 psa_key_derivation_abort(&operation);
2390 psa_destroy_key(key);
2391 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002392}
2393/* END_CASE */
2394
2395/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002396void agreement_key_policy(int policy_usage,
2397 int policy_alg,
2398 int key_type_arg,
2399 data_t *key_data,
2400 int exercise_alg,
2401 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002402{
Ronald Cron5425a212020-08-04 14:58:35 +02002403 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002404 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002405 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002406 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002407 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002408 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002409
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002411
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 psa_set_key_usage_flags(&attributes, policy_usage);
2413 psa_set_key_algorithm(&attributes, policy_alg);
2414 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002415
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2417 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2420 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002421
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002423
2424exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 psa_key_derivation_abort(&operation);
2426 psa_destroy_key(key);
2427 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002428}
2429/* END_CASE */
2430
2431/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002432void key_policy_alg2(int key_type_arg, data_t *key_data,
2433 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002434{
Ronald Cron5425a212020-08-04 14:58:35 +02002435 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002436 psa_key_type_t key_type = key_type_arg;
2437 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2438 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2439 psa_key_usage_t usage = usage_arg;
2440 psa_algorithm_t alg = alg_arg;
2441 psa_algorithm_t alg2 = alg2_arg;
2442
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 psa_set_key_usage_flags(&attributes, usage);
2446 psa_set_key_algorithm(&attributes, alg);
2447 psa_set_key_enrollment_algorithm(&attributes, alg2);
2448 psa_set_key_type(&attributes, key_type);
2449 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2450 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002451
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002452 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 usage = mbedtls_test_update_key_usage_flags(usage);
2454 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2455 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2456 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2457 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002460 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 }
2462 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002463 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002465
2466exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002467 /*
2468 * Key attributes may have been returned by psa_get_key_attributes()
2469 * thus reset them as required.
2470 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 psa_destroy_key(key);
2474 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002475}
2476/* END_CASE */
2477
2478/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002479void raw_agreement_key_policy(int policy_usage,
2480 int policy_alg,
2481 int key_type_arg,
2482 data_t *key_data,
2483 int exercise_alg,
2484 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002485{
Ronald Cron5425a212020-08-04 14:58:35 +02002486 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002487 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002488 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002489 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002490 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002491 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002492
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 psa_set_key_usage_flags(&attributes, policy_usage);
2496 psa_set_key_algorithm(&attributes, policy_alg);
2497 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002498
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2500 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002501
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002503
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002505
2506exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 psa_key_derivation_abort(&operation);
2508 psa_destroy_key(key);
2509 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002510}
2511/* END_CASE */
2512
2513/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002514void copy_success(int source_usage_arg,
2515 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002516 int source_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 int type_arg, data_t *material,
2518 int copy_attributes,
2519 int target_usage_arg,
2520 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002521 int target_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 int expected_usage_arg,
2523 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002524{
Gilles Peskineca25db92019-04-19 11:43:08 +02002525 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2526 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002527 psa_key_usage_t expected_usage = expected_usage_arg;
2528 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002529 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302530 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2531 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002532 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2533 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002534 uint8_t *export_buffer = NULL;
2535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002537
Gilles Peskineca25db92019-04-19 11:43:08 +02002538 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2540 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2541 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2542 psa_set_key_type(&source_attributes, type_arg);
2543 psa_set_key_lifetime(&source_attributes, source_lifetime);
2544 PSA_ASSERT(psa_import_key(&source_attributes,
2545 material->x, material->len,
2546 &source_key));
2547 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002548
Gilles Peskineca25db92019-04-19 11:43:08 +02002549 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002551 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002552 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 if (target_usage_arg != -1) {
2556 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2557 }
2558 if (target_alg_arg != -1) {
2559 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2560 }
2561 if (target_alg2_arg != -1) {
2562 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2563 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002564
Archana8a180362021-07-05 02:18:48 +05302565
Gilles Peskine57ab7212019-01-28 13:03:09 +01002566 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 PSA_ASSERT(psa_copy_key(source_key,
2568 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002569
2570 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002572
2573 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2575 TEST_EQUAL(psa_get_key_type(&source_attributes),
2576 psa_get_key_type(&target_attributes));
2577 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2578 psa_get_key_bits(&target_attributes));
2579 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2580 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2581 TEST_EQUAL(expected_alg2,
2582 psa_get_key_enrollment_algorithm(&target_attributes));
2583 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002584 size_t length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002585 TEST_CALLOC(export_buffer, material->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2587 material->len, &length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002588 TEST_MEMORY_COMPARE(material->x, material->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002589 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002590 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 if (!psa_key_lifetime_is_external(target_lifetime)) {
2593 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302594 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 }
2596 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302597 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 }
Archana8a180362021-07-05 02:18:48 +05302599 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002600
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002602
2603exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002604 /*
2605 * Source and target key attributes may have been returned by
2606 * psa_get_key_attributes() thus reset them as required.
2607 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 psa_reset_key_attributes(&source_attributes);
2609 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 PSA_DONE();
2612 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002613}
2614/* END_CASE */
2615
2616/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002617void copy_fail(int source_usage_arg,
2618 int source_alg_arg, int source_alg2_arg,
2619 int source_lifetime_arg,
2620 int type_arg, data_t *material,
2621 int target_type_arg, int target_bits_arg,
2622 int target_usage_arg,
2623 int target_alg_arg, int target_alg2_arg,
2624 int target_id_arg, int target_lifetime_arg,
2625 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002626{
2627 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2628 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002629 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2630 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002632
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002634
2635 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2637 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2638 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2639 psa_set_key_type(&source_attributes, type_arg);
2640 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2641 PSA_ASSERT(psa_import_key(&source_attributes,
2642 material->x, material->len,
2643 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002644
2645 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 psa_set_key_id(&target_attributes, key_id);
2647 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2648 psa_set_key_type(&target_attributes, target_type_arg);
2649 psa_set_key_bits(&target_attributes, target_bits_arg);
2650 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2651 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2652 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002653
2654 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 TEST_EQUAL(psa_copy_key(source_key,
2656 &target_attributes, &target_key),
2657 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002658
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002660
Gilles Peskine4a644642019-05-03 17:14:08 +02002661exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 psa_reset_key_attributes(&source_attributes);
2663 psa_reset_key_attributes(&target_attributes);
2664 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002665}
2666/* END_CASE */
2667
2668/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002669void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002670{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002671 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002672 /* Test each valid way of initializing the object, except for `= {0}`, as
2673 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2674 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002675 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002677 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2678 psa_hash_operation_t zero;
2679
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002681
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002682 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2684 PSA_ERROR_BAD_STATE);
2685 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2686 PSA_ERROR_BAD_STATE);
2687 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2688 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002689
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002690 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 PSA_ASSERT(psa_hash_abort(&func));
2692 PSA_ASSERT(psa_hash_abort(&init));
2693 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002694}
2695/* END_CASE */
2696
2697/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002698void hash_setup(int alg_arg,
2699 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002700{
2701 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002702 uint8_t *output = NULL;
2703 size_t output_size = 0;
2704 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002705 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002706 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002707 psa_status_t status;
2708
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002710
Neil Armstrongedb20862022-02-07 15:47:44 +01002711 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 output_size = PSA_HASH_LENGTH(alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002713 TEST_CALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002714
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 status = psa_hash_compute(alg, NULL, 0,
2716 output, output_size, &output_length);
2717 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002718
2719 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 status = psa_hash_setup(&operation, alg);
2721 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002722
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002723 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002725
2726 /* If setup failed, reproduce the failure, so as to
2727 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 if (status != PSA_SUCCESS) {
2729 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2730 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002731
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002732 /* Now the operation object should be reusable. */
2733#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2735 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002736#endif
2737
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002738exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 mbedtls_free(output);
2740 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002741}
2742/* END_CASE */
2743
2744/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002745void hash_compute_fail(int alg_arg, data_t *input,
2746 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002747{
2748 psa_algorithm_t alg = alg_arg;
2749 uint8_t *output = NULL;
2750 size_t output_size = output_size_arg;
2751 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002752 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002753 psa_status_t expected_status = expected_status_arg;
2754 psa_status_t status;
2755
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002756 TEST_CALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002757
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002759
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002760 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 status = psa_hash_compute(alg, input->x, input->len,
2762 output, output_size, &output_length);
2763 TEST_EQUAL(status, expected_status);
2764 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002765
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002766 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 status = psa_hash_setup(&operation, alg);
2768 if (status == PSA_SUCCESS) {
2769 status = psa_hash_update(&operation, input->x, input->len);
2770 if (status == PSA_SUCCESS) {
2771 status = psa_hash_finish(&operation, output, output_size,
2772 &output_length);
2773 if (status == PSA_SUCCESS) {
2774 TEST_LE_U(output_length, output_size);
2775 } else {
2776 TEST_EQUAL(status, expected_status);
2777 }
2778 } else {
2779 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002780 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 } else {
2782 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002783 }
2784
Gilles Peskine0a749c82019-11-28 19:33:58 +01002785exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 PSA_ASSERT(psa_hash_abort(&operation));
2787 mbedtls_free(output);
2788 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002789}
2790/* END_CASE */
2791
2792/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002793void hash_compare_fail(int alg_arg, data_t *input,
2794 data_t *reference_hash,
2795 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002796{
2797 psa_algorithm_t alg = alg_arg;
2798 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002799 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002800 psa_status_t status;
2801
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002803
Neil Armstrong55a1be12022-02-07 11:23:20 +01002804 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 status = psa_hash_compare(alg, input->x, input->len,
2806 reference_hash->x, reference_hash->len);
2807 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002808
Neil Armstrong55a1be12022-02-07 11:23:20 +01002809 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 status = psa_hash_setup(&operation, alg);
2811 if (status == PSA_SUCCESS) {
2812 status = psa_hash_update(&operation, input->x, input->len);
2813 if (status == PSA_SUCCESS) {
2814 status = psa_hash_verify(&operation, reference_hash->x,
2815 reference_hash->len);
2816 TEST_EQUAL(status, expected_status);
2817 } else {
2818 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002819 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 } else {
2821 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002822 }
2823
Gilles Peskine88e08462020-01-28 20:43:00 +01002824exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 PSA_ASSERT(psa_hash_abort(&operation));
2826 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002827}
2828/* END_CASE */
2829
2830/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002831void hash_compute_compare(int alg_arg, data_t *input,
2832 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002833{
2834 psa_algorithm_t alg = alg_arg;
2835 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2836 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002837 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002838 size_t i;
2839
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002841
Neil Armstrongca30a002022-02-07 11:40:23 +01002842 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2844 output, PSA_HASH_LENGTH(alg),
2845 &output_length));
2846 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002847 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002848 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002849
Neil Armstrongca30a002022-02-07 11:40:23 +01002850 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 PSA_ASSERT(psa_hash_setup(&operation, alg));
2852 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2853 PSA_ASSERT(psa_hash_finish(&operation, output,
2854 PSA_HASH_LENGTH(alg),
2855 &output_length));
2856 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002857 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002858 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002859
2860 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2862 output, sizeof(output),
2863 &output_length));
2864 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002865 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002866 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002867
Neil Armstrongca30a002022-02-07 11:40:23 +01002868 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 PSA_ASSERT(psa_hash_setup(&operation, alg));
2870 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2871 PSA_ASSERT(psa_hash_finish(&operation, output,
2872 sizeof(output), &output_length));
2873 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002874 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002875 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002876
2877 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2879 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002880
Neil Armstrongca30a002022-02-07 11:40:23 +01002881 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 PSA_ASSERT(psa_hash_setup(&operation, alg));
2883 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2884 PSA_ASSERT(psa_hash_verify(&operation, output,
2885 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002886
2887 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2889 output, output_length + 1),
2890 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002891
Neil Armstrongca30a002022-02-07 11:40:23 +01002892 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 PSA_ASSERT(psa_hash_setup(&operation, alg));
2894 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2895 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2896 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002897
2898 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2900 output, output_length - 1),
2901 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002902
Neil Armstrongca30a002022-02-07 11:40:23 +01002903 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 PSA_ASSERT(psa_hash_setup(&operation, alg));
2905 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2906 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2907 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002908
Gilles Peskine0a749c82019-11-28 19:33:58 +01002909 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 for (i = 0; i < output_length; i++) {
2911 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002912 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002913
2914 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2916 output, output_length),
2917 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002918
2919 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 PSA_ASSERT(psa_hash_setup(&operation, alg));
2921 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2922 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2923 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002924
Gilles Peskine0a749c82019-11-28 19:33:58 +01002925 output[i] ^= 1;
2926 }
2927
2928exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 PSA_ASSERT(psa_hash_abort(&operation));
2930 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002931}
2932/* END_CASE */
2933
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002934/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002935void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002936{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002937 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002938 unsigned char input[] = "";
2939 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002940 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002941 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2942 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2944 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002945 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002946 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002947 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002948
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002950
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002951 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 PSA_ASSERT(psa_hash_setup(&operation, alg));
2953 ASSERT_OPERATION_IS_ACTIVE(operation);
2954 TEST_EQUAL(psa_hash_setup(&operation, alg),
2955 PSA_ERROR_BAD_STATE);
2956 ASSERT_OPERATION_IS_INACTIVE(operation);
2957 PSA_ASSERT(psa_hash_abort(&operation));
2958 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002959
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002960 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2962 PSA_ERROR_BAD_STATE);
2963 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002964
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002965 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002967 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 ASSERT_OPERATION_IS_ACTIVE(operation);
2969 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2970 PSA_ERROR_BAD_STATE);
2971 ASSERT_OPERATION_IS_INACTIVE(operation);
2972 PSA_ASSERT(psa_hash_abort(&operation));
2973 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002974
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002975 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 PSA_ASSERT(psa_hash_setup(&operation, alg));
2977 PSA_ASSERT(psa_hash_finish(&operation,
2978 hash, sizeof(hash), &hash_len));
2979 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2980 PSA_ERROR_BAD_STATE);
2981 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002982
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002983 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002984 TEST_EQUAL(psa_hash_verify(&operation,
2985 valid_hash, sizeof(valid_hash)),
2986 PSA_ERROR_BAD_STATE);
2987 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002988
2989 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 PSA_ASSERT(psa_hash_setup(&operation, alg));
2991 PSA_ASSERT(psa_hash_finish(&operation,
2992 hash, sizeof(hash), &hash_len));
2993 TEST_EQUAL(psa_hash_verify(&operation,
2994 valid_hash, sizeof(valid_hash)),
2995 PSA_ERROR_BAD_STATE);
2996 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002997
2998 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 PSA_ASSERT(psa_hash_setup(&operation, alg));
3000 ASSERT_OPERATION_IS_ACTIVE(operation);
3001 PSA_ASSERT(psa_hash_verify(&operation,
3002 valid_hash, sizeof(valid_hash)));
3003 ASSERT_OPERATION_IS_INACTIVE(operation);
3004 TEST_EQUAL(psa_hash_verify(&operation,
3005 valid_hash, sizeof(valid_hash)),
3006 PSA_ERROR_BAD_STATE);
3007 ASSERT_OPERATION_IS_INACTIVE(operation);
3008 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003009
3010 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 TEST_EQUAL(psa_hash_finish(&operation,
3012 hash, sizeof(hash), &hash_len),
3013 PSA_ERROR_BAD_STATE);
3014 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003015
3016 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 PSA_ASSERT(psa_hash_setup(&operation, alg));
3018 PSA_ASSERT(psa_hash_finish(&operation,
3019 hash, sizeof(hash), &hash_len));
3020 TEST_EQUAL(psa_hash_finish(&operation,
3021 hash, sizeof(hash), &hash_len),
3022 PSA_ERROR_BAD_STATE);
3023 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003024
3025 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 PSA_ASSERT(psa_hash_setup(&operation, alg));
3027 PSA_ASSERT(psa_hash_verify(&operation,
3028 valid_hash, sizeof(valid_hash)));
3029 TEST_EQUAL(psa_hash_finish(&operation,
3030 hash, sizeof(hash), &hash_len),
3031 PSA_ERROR_BAD_STATE);
3032 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02003033
3034exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02003036}
3037/* END_CASE */
3038
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003039/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003040void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03003041{
3042 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02003043 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
3044 * appended to it */
3045 unsigned char hash[] = {
3046 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
3047 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
3049 };
3050 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003051 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03003052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03003054
itayzafrir27e69452018-11-01 14:26:34 +02003055 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 PSA_ASSERT(psa_hash_setup(&operation, alg));
3057 ASSERT_OPERATION_IS_ACTIVE(operation);
3058 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
3059 PSA_ERROR_INVALID_SIGNATURE);
3060 ASSERT_OPERATION_IS_INACTIVE(operation);
3061 PSA_ASSERT(psa_hash_abort(&operation));
3062 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03003063
itayzafrir27e69452018-11-01 14:26:34 +02003064 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 PSA_ASSERT(psa_hash_setup(&operation, alg));
3066 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
3067 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03003068
itayzafrir27e69452018-11-01 14:26:34 +02003069 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 PSA_ASSERT(psa_hash_setup(&operation, alg));
3071 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
3072 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03003073
itayzafrirec93d302018-10-18 18:01:10 +03003074exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03003076}
3077/* END_CASE */
3078
Ronald Cronee414c72021-03-18 18:50:08 +01003079/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003080void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03003081{
3082 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003083 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003085 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003086 size_t hash_len;
3087
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03003089
itayzafrir58028322018-10-25 10:22:01 +03003090 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 PSA_ASSERT(psa_hash_setup(&operation, alg));
3092 TEST_EQUAL(psa_hash_finish(&operation,
3093 hash, expected_size - 1, &hash_len),
3094 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003095
3096exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003098}
3099/* END_CASE */
3100
Ronald Cronee414c72021-03-18 18:50:08 +01003101/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003102void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003103{
3104 psa_algorithm_t alg = PSA_ALG_SHA_256;
3105 unsigned char hash[PSA_HASH_MAX_SIZE];
3106 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3107 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3108 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3109 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3110 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3111 size_t hash_len;
3112
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 PSA_ASSERT(psa_crypto_init());
3114 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003115
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3117 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3118 PSA_ASSERT(psa_hash_finish(&op_finished,
3119 hash, sizeof(hash), &hash_len));
3120 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3121 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003122
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3124 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003125
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3127 PSA_ASSERT(psa_hash_finish(&op_init,
3128 hash, sizeof(hash), &hash_len));
3129 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3130 PSA_ASSERT(psa_hash_finish(&op_finished,
3131 hash, sizeof(hash), &hash_len));
3132 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3133 PSA_ASSERT(psa_hash_finish(&op_aborted,
3134 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003135
3136exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 psa_hash_abort(&op_source);
3138 psa_hash_abort(&op_init);
3139 psa_hash_abort(&op_setup);
3140 psa_hash_abort(&op_finished);
3141 psa_hash_abort(&op_aborted);
3142 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003143}
3144/* END_CASE */
3145
Ronald Cronee414c72021-03-18 18:50:08 +01003146/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003147void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003148{
3149 psa_algorithm_t alg = PSA_ALG_SHA_256;
3150 unsigned char hash[PSA_HASH_MAX_SIZE];
3151 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3152 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3153 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3154 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3155 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3156 size_t hash_len;
3157
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003159
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3161 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3162 PSA_ASSERT(psa_hash_finish(&op_finished,
3163 hash, sizeof(hash), &hash_len));
3164 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3165 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003166
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3168 PSA_ASSERT(psa_hash_finish(&op_target,
3169 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003170
Gilles Peskine449bd832023-01-11 14:50:10 +01003171 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3172 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3173 PSA_ERROR_BAD_STATE);
3174 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3175 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003176
3177exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 psa_hash_abort(&op_target);
3179 psa_hash_abort(&op_init);
3180 psa_hash_abort(&op_setup);
3181 psa_hash_abort(&op_finished);
3182 psa_hash_abort(&op_aborted);
3183 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003184}
3185/* END_CASE */
3186
itayzafrir58028322018-10-25 10:22:01 +03003187/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003188void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003189{
Jaeden Amero252ef282019-02-15 14:05:35 +00003190 const uint8_t input[1] = { 0 };
3191
Jaeden Amero769ce272019-01-04 11:48:03 +00003192 /* Test each valid way of initializing the object, except for `= {0}`, as
3193 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3194 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003195 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003197 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3198 psa_mac_operation_t zero;
3199
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003201
Jaeden Amero252ef282019-02-15 14:05:35 +00003202 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 TEST_EQUAL(psa_mac_update(&func,
3204 input, sizeof(input)),
3205 PSA_ERROR_BAD_STATE);
3206 TEST_EQUAL(psa_mac_update(&init,
3207 input, sizeof(input)),
3208 PSA_ERROR_BAD_STATE);
3209 TEST_EQUAL(psa_mac_update(&zero,
3210 input, sizeof(input)),
3211 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003212
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003213 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 PSA_ASSERT(psa_mac_abort(&func));
3215 PSA_ASSERT(psa_mac_abort(&init));
3216 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003217}
3218/* END_CASE */
3219
3220/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003221void mac_setup(int key_type_arg,
3222 data_t *key,
3223 int alg_arg,
3224 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003225{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003226 psa_key_type_t key_type = key_type_arg;
3227 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003228 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003229 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003230 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3231#if defined(KNOWN_SUPPORTED_MAC_ALG)
3232 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3233#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003234
Gilles Peskine449bd832023-01-11 14:50:10 +01003235 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003236
Gilles Peskine449bd832023-01-11 14:50:10 +01003237 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3238 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003239 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003240 }
3241 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003242
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003243 /* The operation object should be reusable. */
3244#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003245 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3246 smoke_test_key_data,
3247 sizeof(smoke_test_key_data),
3248 KNOWN_SUPPORTED_MAC_ALG,
3249 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003250 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003251 }
3252 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003253#endif
3254
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003255exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003257}
3258/* END_CASE */
3259
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003260/* 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 +01003261void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003262{
Ronald Cron5425a212020-08-04 14:58:35 +02003263 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003264 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3265 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003266 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003267 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3268 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3270 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003271 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003272 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3273 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3274 size_t sign_mac_length = 0;
3275 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3276 const uint8_t verify_mac[] = {
3277 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3278 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3280 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003281
Gilles Peskine449bd832023-01-11 14:50:10 +01003282 PSA_ASSERT(psa_crypto_init());
3283 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3284 psa_set_key_algorithm(&attributes, alg);
3285 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003286
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3288 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003289
Jaeden Amero252ef282019-02-15 14:05:35 +00003290 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003291 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3292 PSA_ERROR_BAD_STATE);
3293 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003294
3295 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3297 &sign_mac_length),
3298 PSA_ERROR_BAD_STATE);
3299 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003300
3301 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003302 TEST_EQUAL(psa_mac_verify_finish(&operation,
3303 verify_mac, sizeof(verify_mac)),
3304 PSA_ERROR_BAD_STATE);
3305 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003306
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003307 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3309 ASSERT_OPERATION_IS_ACTIVE(operation);
3310 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3311 PSA_ERROR_BAD_STATE);
3312 ASSERT_OPERATION_IS_INACTIVE(operation);
3313 PSA_ASSERT(psa_mac_abort(&operation));
3314 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003315
Jaeden Amero252ef282019-02-15 14:05:35 +00003316 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3318 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3319 PSA_ASSERT(psa_mac_sign_finish(&operation,
3320 sign_mac, sizeof(sign_mac),
3321 &sign_mac_length));
3322 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3323 PSA_ERROR_BAD_STATE);
3324 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003325
3326 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3328 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3329 PSA_ASSERT(psa_mac_verify_finish(&operation,
3330 verify_mac, sizeof(verify_mac)));
3331 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3332 PSA_ERROR_BAD_STATE);
3333 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003334
3335 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3337 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3338 PSA_ASSERT(psa_mac_sign_finish(&operation,
3339 sign_mac, sizeof(sign_mac),
3340 &sign_mac_length));
3341 TEST_EQUAL(psa_mac_sign_finish(&operation,
3342 sign_mac, sizeof(sign_mac),
3343 &sign_mac_length),
3344 PSA_ERROR_BAD_STATE);
3345 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003346
3347 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003348 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3349 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3350 PSA_ASSERT(psa_mac_verify_finish(&operation,
3351 verify_mac, sizeof(verify_mac)));
3352 TEST_EQUAL(psa_mac_verify_finish(&operation,
3353 verify_mac, sizeof(verify_mac)),
3354 PSA_ERROR_BAD_STATE);
3355 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003356
3357 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003358 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3359 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3360 ASSERT_OPERATION_IS_ACTIVE(operation);
3361 TEST_EQUAL(psa_mac_verify_finish(&operation,
3362 verify_mac, sizeof(verify_mac)),
3363 PSA_ERROR_BAD_STATE);
3364 ASSERT_OPERATION_IS_INACTIVE(operation);
3365 PSA_ASSERT(psa_mac_abort(&operation));
3366 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003367
3368 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3370 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3371 ASSERT_OPERATION_IS_ACTIVE(operation);
3372 TEST_EQUAL(psa_mac_sign_finish(&operation,
3373 sign_mac, sizeof(sign_mac),
3374 &sign_mac_length),
3375 PSA_ERROR_BAD_STATE);
3376 ASSERT_OPERATION_IS_INACTIVE(operation);
3377 PSA_ASSERT(psa_mac_abort(&operation));
3378 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003379
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003381
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003382exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003383 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003384}
3385/* END_CASE */
3386
3387/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003388void mac_sign_verify_multi(int key_type_arg,
3389 data_t *key_data,
3390 int alg_arg,
3391 data_t *input,
3392 int is_verify,
3393 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003394{
3395 size_t data_part_len = 0;
3396
Gilles Peskine449bd832023-01-11 14:50:10 +01003397 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003398 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003399 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003400
Gilles Peskine449bd832023-01-11 14:50:10 +01003401 if (mac_multipart_internal_func(key_type_arg, key_data,
3402 alg_arg,
3403 input, data_part_len,
3404 expected_mac,
3405 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003406 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003407 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003408
3409 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003410 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003411
Gilles Peskine449bd832023-01-11 14:50:10 +01003412 if (mac_multipart_internal_func(key_type_arg, key_data,
3413 alg_arg,
3414 input, data_part_len,
3415 expected_mac,
3416 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003417 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003418 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003419 }
3420
3421 /* Goto is required to silence warnings about unused labels, as we
3422 * don't actually do any test assertions in this function. */
3423 goto exit;
3424}
3425/* END_CASE */
3426
3427/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003428void mac_sign(int key_type_arg,
3429 data_t *key_data,
3430 int alg_arg,
3431 data_t *input,
3432 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003433{
Ronald Cron5425a212020-08-04 14:58:35 +02003434 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003435 psa_key_type_t key_type = key_type_arg;
3436 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003437 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003438 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003439 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003440 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003441 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003442 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003443 const size_t output_sizes_to_test[] = {
3444 0,
3445 1,
3446 expected_mac->len - 1,
3447 expected_mac->len,
3448 expected_mac->len + 1,
3449 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003450
Gilles Peskine449bd832023-01-11 14:50:10 +01003451 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003452 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003453 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003454
Gilles Peskine449bd832023-01-11 14:50:10 +01003455 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003456
Gilles Peskine449bd832023-01-11 14:50:10 +01003457 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3458 psa_set_key_algorithm(&attributes, alg);
3459 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003460
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3462 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003463
Gilles Peskine449bd832023-01-11 14:50:10 +01003464 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003465 const size_t output_size = output_sizes_to_test[i];
3466 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003467 (output_size >= expected_mac->len ? PSA_SUCCESS :
3468 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003469
Gilles Peskine449bd832023-01-11 14:50:10 +01003470 mbedtls_test_set_step(output_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003471 TEST_CALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003472
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003473 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003474 TEST_EQUAL(psa_mac_compute(key, alg,
3475 input->x, input->len,
3476 actual_mac, output_size, &mac_length),
3477 expected_status);
3478 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003479 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003480 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003481 }
3482
Gilles Peskine449bd832023-01-11 14:50:10 +01003483 if (output_size > 0) {
3484 memset(actual_mac, 0, output_size);
3485 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003486
3487 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003488 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3489 PSA_ASSERT(psa_mac_update(&operation,
3490 input->x, input->len));
3491 TEST_EQUAL(psa_mac_sign_finish(&operation,
3492 actual_mac, output_size,
3493 &mac_length),
3494 expected_status);
3495 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003496
Gilles Peskine449bd832023-01-11 14:50:10 +01003497 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003498 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003499 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003500 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003501 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003502 actual_mac = NULL;
3503 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003504
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003505exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003506 psa_mac_abort(&operation);
3507 psa_destroy_key(key);
3508 PSA_DONE();
3509 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003510}
3511/* END_CASE */
3512
3513/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003514void mac_verify(int key_type_arg,
3515 data_t *key_data,
3516 int alg_arg,
3517 data_t *input,
3518 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003519{
Ronald Cron5425a212020-08-04 14:58:35 +02003520 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003521 psa_key_type_t key_type = key_type_arg;
3522 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003523 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003524 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003525 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003526
Gilles Peskine449bd832023-01-11 14:50:10 +01003527 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003528
Gilles Peskine449bd832023-01-11 14:50:10 +01003529 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003530
Gilles Peskine449bd832023-01-11 14:50:10 +01003531 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3532 psa_set_key_algorithm(&attributes, alg);
3533 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003534
Gilles Peskine449bd832023-01-11 14:50:10 +01003535 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3536 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003537
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003538 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003539 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3540 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003541
3542 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003543 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3544 PSA_ASSERT(psa_mac_update(&operation,
3545 input->x, input->len));
3546 PSA_ASSERT(psa_mac_verify_finish(&operation,
3547 expected_mac->x,
3548 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003549
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003550 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003551 TEST_EQUAL(psa_mac_verify(key, alg,
3552 input->x, input->len,
3553 expected_mac->x,
3554 expected_mac->len - 1),
3555 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003556
3557 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003558 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3559 PSA_ASSERT(psa_mac_update(&operation,
3560 input->x, input->len));
3561 TEST_EQUAL(psa_mac_verify_finish(&operation,
3562 expected_mac->x,
3563 expected_mac->len - 1),
3564 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003565
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003566 /* Test a MAC that's too long, one-shot case. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003567 TEST_CALLOC(perturbed_mac, expected_mac->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01003568 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3569 TEST_EQUAL(psa_mac_verify(key, alg,
3570 input->x, input->len,
3571 perturbed_mac, expected_mac->len + 1),
3572 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003573
3574 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003575 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3576 PSA_ASSERT(psa_mac_update(&operation,
3577 input->x, input->len));
3578 TEST_EQUAL(psa_mac_verify_finish(&operation,
3579 perturbed_mac,
3580 expected_mac->len + 1),
3581 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003582
3583 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003584 for (size_t i = 0; i < expected_mac->len; i++) {
3585 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003586 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003587
Gilles Peskine449bd832023-01-11 14:50:10 +01003588 TEST_EQUAL(psa_mac_verify(key, alg,
3589 input->x, input->len,
3590 perturbed_mac, expected_mac->len),
3591 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003592
Gilles Peskine449bd832023-01-11 14:50:10 +01003593 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3594 PSA_ASSERT(psa_mac_update(&operation,
3595 input->x, input->len));
3596 TEST_EQUAL(psa_mac_verify_finish(&operation,
3597 perturbed_mac,
3598 expected_mac->len),
3599 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003600 perturbed_mac[i] ^= 1;
3601 }
3602
Gilles Peskine8c9def32018-02-08 10:02:12 +01003603exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003604 psa_mac_abort(&operation);
3605 psa_destroy_key(key);
3606 PSA_DONE();
3607 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003608}
3609/* END_CASE */
3610
3611/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003612void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003613{
Jaeden Ameroab439972019-02-15 14:12:05 +00003614 const uint8_t input[1] = { 0 };
3615 unsigned char output[1] = { 0 };
3616 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003617 /* Test each valid way of initializing the object, except for `= {0}`, as
3618 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3619 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003620 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003621 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003622 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3623 psa_cipher_operation_t zero;
3624
Gilles Peskine449bd832023-01-11 14:50:10 +01003625 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003626
Jaeden Ameroab439972019-02-15 14:12:05 +00003627 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003628 TEST_EQUAL(psa_cipher_update(&func,
3629 input, sizeof(input),
3630 output, sizeof(output),
3631 &output_length),
3632 PSA_ERROR_BAD_STATE);
3633 TEST_EQUAL(psa_cipher_update(&init,
3634 input, sizeof(input),
3635 output, sizeof(output),
3636 &output_length),
3637 PSA_ERROR_BAD_STATE);
3638 TEST_EQUAL(psa_cipher_update(&zero,
3639 input, sizeof(input),
3640 output, sizeof(output),
3641 &output_length),
3642 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003643
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003644 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003645 PSA_ASSERT(psa_cipher_abort(&func));
3646 PSA_ASSERT(psa_cipher_abort(&init));
3647 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003648}
3649/* END_CASE */
3650
3651/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003652void cipher_setup(int key_type_arg,
3653 data_t *key,
3654 int alg_arg,
3655 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003656{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003657 psa_key_type_t key_type = key_type_arg;
3658 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003659 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003660 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003661 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003662#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003663 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3664#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003665
Gilles Peskine449bd832023-01-11 14:50:10 +01003666 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003667
Gilles Peskine449bd832023-01-11 14:50:10 +01003668 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3669 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003670 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003671 }
3672 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003673
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003674 /* The operation object should be reusable. */
3675#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003676 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3677 smoke_test_key_data,
3678 sizeof(smoke_test_key_data),
3679 KNOWN_SUPPORTED_CIPHER_ALG,
3680 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003681 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003682 }
3683 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003684#endif
3685
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003686exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003687 psa_cipher_abort(&operation);
3688 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003689}
3690/* END_CASE */
3691
Ronald Cronee414c72021-03-18 18:50:08 +01003692/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003693void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003694{
Ronald Cron5425a212020-08-04 14:58:35 +02003695 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003696 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3697 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003698 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003699 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003700 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003701 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003702 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003703 0xaa, 0xaa, 0xaa, 0xaa
3704 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003705 const uint8_t text[] = {
3706 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003707 0xbb, 0xbb, 0xbb, 0xbb
3708 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003709 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003710 size_t length = 0;
3711
Gilles Peskine449bd832023-01-11 14:50:10 +01003712 PSA_ASSERT(psa_crypto_init());
3713 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3714 psa_set_key_algorithm(&attributes, alg);
3715 psa_set_key_type(&attributes, key_type);
3716 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3717 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003718
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003719 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003720 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3721 ASSERT_OPERATION_IS_ACTIVE(operation);
3722 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3723 PSA_ERROR_BAD_STATE);
3724 ASSERT_OPERATION_IS_INACTIVE(operation);
3725 PSA_ASSERT(psa_cipher_abort(&operation));
3726 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003727
3728 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003729 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3730 ASSERT_OPERATION_IS_ACTIVE(operation);
3731 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3732 PSA_ERROR_BAD_STATE);
3733 ASSERT_OPERATION_IS_INACTIVE(operation);
3734 PSA_ASSERT(psa_cipher_abort(&operation));
3735 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003736
Jaeden Ameroab439972019-02-15 14:12:05 +00003737 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003738 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3739 buffer, sizeof(buffer),
3740 &length),
3741 PSA_ERROR_BAD_STATE);
3742 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003743
3744 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003745 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3746 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3747 buffer, sizeof(buffer),
3748 &length));
3749 ASSERT_OPERATION_IS_ACTIVE(operation);
3750 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3751 buffer, sizeof(buffer),
3752 &length),
3753 PSA_ERROR_BAD_STATE);
3754 ASSERT_OPERATION_IS_INACTIVE(operation);
3755 PSA_ASSERT(psa_cipher_abort(&operation));
3756 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003757
3758 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003759 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3760 PSA_ASSERT(psa_cipher_set_iv(&operation,
3761 iv, sizeof(iv)));
3762 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3763 buffer, sizeof(buffer),
3764 &length),
3765 PSA_ERROR_BAD_STATE);
3766 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003767
3768 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003769 TEST_EQUAL(psa_cipher_set_iv(&operation,
3770 iv, sizeof(iv)),
3771 PSA_ERROR_BAD_STATE);
3772 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003773
3774 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003775 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3776 PSA_ASSERT(psa_cipher_set_iv(&operation,
3777 iv, sizeof(iv)));
3778 ASSERT_OPERATION_IS_ACTIVE(operation);
3779 TEST_EQUAL(psa_cipher_set_iv(&operation,
3780 iv, sizeof(iv)),
3781 PSA_ERROR_BAD_STATE);
3782 ASSERT_OPERATION_IS_INACTIVE(operation);
3783 PSA_ASSERT(psa_cipher_abort(&operation));
3784 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003785
3786 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003787 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3788 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3789 buffer, sizeof(buffer),
3790 &length));
3791 TEST_EQUAL(psa_cipher_set_iv(&operation,
3792 iv, sizeof(iv)),
3793 PSA_ERROR_BAD_STATE);
3794 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003795
3796 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003797 TEST_EQUAL(psa_cipher_update(&operation,
3798 text, sizeof(text),
3799 buffer, sizeof(buffer),
3800 &length),
3801 PSA_ERROR_BAD_STATE);
3802 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003803
3804 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003805 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3806 ASSERT_OPERATION_IS_ACTIVE(operation);
3807 TEST_EQUAL(psa_cipher_update(&operation,
3808 text, sizeof(text),
3809 buffer, sizeof(buffer),
3810 &length),
3811 PSA_ERROR_BAD_STATE);
3812 ASSERT_OPERATION_IS_INACTIVE(operation);
3813 PSA_ASSERT(psa_cipher_abort(&operation));
3814 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003815
3816 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003817 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3818 PSA_ASSERT(psa_cipher_set_iv(&operation,
3819 iv, sizeof(iv)));
3820 PSA_ASSERT(psa_cipher_finish(&operation,
3821 buffer, sizeof(buffer), &length));
3822 TEST_EQUAL(psa_cipher_update(&operation,
3823 text, sizeof(text),
3824 buffer, sizeof(buffer),
3825 &length),
3826 PSA_ERROR_BAD_STATE);
3827 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003828
3829 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003830 TEST_EQUAL(psa_cipher_finish(&operation,
3831 buffer, sizeof(buffer), &length),
3832 PSA_ERROR_BAD_STATE);
3833 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003834
3835 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003836 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003837 /* Not calling update means we are encrypting an empty buffer, which is OK
3838 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003839 ASSERT_OPERATION_IS_ACTIVE(operation);
3840 TEST_EQUAL(psa_cipher_finish(&operation,
3841 buffer, sizeof(buffer), &length),
3842 PSA_ERROR_BAD_STATE);
3843 ASSERT_OPERATION_IS_INACTIVE(operation);
3844 PSA_ASSERT(psa_cipher_abort(&operation));
3845 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003846
3847 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003848 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3849 PSA_ASSERT(psa_cipher_set_iv(&operation,
3850 iv, sizeof(iv)));
3851 PSA_ASSERT(psa_cipher_finish(&operation,
3852 buffer, sizeof(buffer), &length));
3853 TEST_EQUAL(psa_cipher_finish(&operation,
3854 buffer, sizeof(buffer), &length),
3855 PSA_ERROR_BAD_STATE);
3856 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003857
Gilles Peskine449bd832023-01-11 14:50:10 +01003858 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003859
Jaeden Ameroab439972019-02-15 14:12:05 +00003860exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003861 psa_cipher_abort(&operation);
3862 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003863}
3864/* END_CASE */
3865
3866/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003867void cipher_encrypt_fail(int alg_arg,
3868 int key_type_arg,
3869 data_t *key_data,
3870 data_t *input,
3871 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003872{
Ronald Cron5425a212020-08-04 14:58:35 +02003873 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003874 psa_status_t status;
3875 psa_key_type_t key_type = key_type_arg;
3876 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003877 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003878 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003879 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3880 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003881 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003882 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003883 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003884 size_t function_output_length;
3885 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003886 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3887
Gilles Peskine449bd832023-01-11 14:50:10 +01003888 if (PSA_ERROR_BAD_STATE != expected_status) {
3889 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003890
Gilles Peskine449bd832023-01-11 14:50:10 +01003891 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3892 psa_set_key_algorithm(&attributes, alg);
3893 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003894
Gilles Peskine449bd832023-01-11 14:50:10 +01003895 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3896 input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003897 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003898
Gilles Peskine449bd832023-01-11 14:50:10 +01003899 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3900 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003901 }
3902
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003903 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003904 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3905 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003906
Gilles Peskine449bd832023-01-11 14:50:10 +01003907 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003908
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003909 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003910 status = psa_cipher_encrypt_setup(&operation, key, alg);
3911 if (status == PSA_SUCCESS) {
3912 if (alg != PSA_ALG_ECB_NO_PADDING) {
3913 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3914 iv, iv_size,
3915 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003916 }
3917
Gilles Peskine449bd832023-01-11 14:50:10 +01003918 status = psa_cipher_update(&operation, input->x, input->len,
3919 output, output_buffer_size,
3920 &function_output_length);
3921 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003922 output_length += function_output_length;
3923
Gilles Peskine449bd832023-01-11 14:50:10 +01003924 status = psa_cipher_finish(&operation, output + output_length,
3925 output_buffer_size - output_length,
3926 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003927
Gilles Peskine449bd832023-01-11 14:50:10 +01003928 TEST_EQUAL(status, expected_status);
3929 } else {
3930 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003931 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003932 } else {
3933 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003934 }
3935
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003936exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003937 psa_cipher_abort(&operation);
3938 mbedtls_free(output);
3939 psa_destroy_key(key);
3940 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003941}
3942/* END_CASE */
3943
3944/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003945void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3946 data_t *input, int iv_length,
3947 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003948{
3949 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3950 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3951 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3952 size_t output_buffer_size = 0;
3953 unsigned char *output = NULL;
3954
Gilles Peskine449bd832023-01-11 14:50:10 +01003955 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003956 TEST_CALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003957
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003959
Gilles Peskine449bd832023-01-11 14:50:10 +01003960 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3961 psa_set_key_algorithm(&attributes, alg);
3962 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003963
Gilles Peskine449bd832023-01-11 14:50:10 +01003964 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3965 &key));
3966 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3967 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3968 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003969
3970exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003971 psa_cipher_abort(&operation);
3972 mbedtls_free(output);
3973 psa_destroy_key(key);
3974 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003975}
3976/* END_CASE */
3977
3978/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003979void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3980 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003981{
3982 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3983 psa_key_type_t key_type = key_type_arg;
3984 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003985 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3986 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003987 unsigned char *output = NULL;
3988 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003989 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003990 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3991
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003993
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003994 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003995 TEST_LE_U(ciphertext->len,
3996 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3997 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3998 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3999 TEST_LE_U(plaintext->len,
4000 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
4001 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
4002 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004003
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004004
4005 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01004006 psa_set_key_usage_flags(&attributes,
4007 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4008 psa_set_key_algorithm(&attributes, alg);
4009 psa_set_key_type(&attributes, key_type);
4010 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4011 &key));
4012 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
4013 plaintext->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004014 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004015
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004016 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01004017 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4018 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
4019 PSA_ERROR_BAD_STATE);
4020 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
4021 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
4022 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02004023
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004024 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01004025 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4026 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
4027 &length),
4028 PSA_ERROR_BAD_STATE);
4029 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
4030 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
4031 &length),
4032 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004033
Gilles Peskine286c3142022-04-20 17:09:38 +02004034 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02004036 output_length = 0;
4037 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004038 PSA_ASSERT(psa_cipher_update(&operation,
4039 plaintext->x, plaintext->len,
4040 output, output_buffer_size,
4041 &length));
4042 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02004043 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004044 PSA_ASSERT(psa_cipher_finish(&operation,
4045 mbedtls_buffer_offset(output, output_length),
4046 output_buffer_size - output_length,
4047 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02004048 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004049 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004050 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004051
Gilles Peskine286c3142022-04-20 17:09:38 +02004052 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01004053 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02004054 output_length = 0;
4055 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004056 PSA_ASSERT(psa_cipher_update(&operation,
4057 ciphertext->x, ciphertext->len,
4058 output, output_buffer_size,
4059 &length));
4060 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02004061 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004062 PSA_ASSERT(psa_cipher_finish(&operation,
4063 mbedtls_buffer_offset(output, output_length),
4064 output_buffer_size - output_length,
4065 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02004066 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004067 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004068 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004069
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004070 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004071 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004072 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
4073 output, output_buffer_size,
4074 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004075 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004076 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004077
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004078 /* One-shot decryption */
4079 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004080 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
4081 output, output_buffer_size,
4082 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004083 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004084 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004085
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004086exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004087 PSA_ASSERT(psa_cipher_abort(&operation));
4088 mbedtls_free(output);
4089 psa_cipher_abort(&operation);
4090 psa_destroy_key(key);
4091 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004092}
4093/* END_CASE */
4094
4095/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004096void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004097{
4098 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4099 psa_algorithm_t alg = alg_arg;
4100 psa_key_type_t key_type = key_type_arg;
4101 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4102 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4103 psa_status_t status;
4104
Gilles Peskine449bd832023-01-11 14:50:10 +01004105 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004106
Gilles Peskine449bd832023-01-11 14:50:10 +01004107 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4108 psa_set_key_algorithm(&attributes, alg);
4109 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004110
4111 /* Usage of either of these two size macros would cause divide by zero
4112 * with incorrect key types previously. Input length should be irrelevant
4113 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4115 0);
4116 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004117
4118
Gilles Peskine449bd832023-01-11 14:50:10 +01004119 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4120 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004121
4122 /* Should fail due to invalid alg type (to support invalid key type).
4123 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004125
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004127
4128exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004129 psa_cipher_abort(&operation);
4130 psa_destroy_key(key);
4131 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004132}
4133/* END_CASE */
4134
4135/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004136void cipher_encrypt_validation(int alg_arg,
4137 int key_type_arg,
4138 data_t *key_data,
4139 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004140{
4141 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4142 psa_key_type_t key_type = key_type_arg;
4143 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004144 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004145 unsigned char *output1 = NULL;
4146 size_t output1_buffer_size = 0;
4147 size_t output1_length = 0;
4148 unsigned char *output2 = NULL;
4149 size_t output2_buffer_size = 0;
4150 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004151 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004152 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004153 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004154
Gilles Peskine449bd832023-01-11 14:50:10 +01004155 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004156
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4158 psa_set_key_algorithm(&attributes, alg);
4159 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004160
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4162 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4163 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004164 TEST_CALLOC(output1, output1_buffer_size);
4165 TEST_CALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004166
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
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004170 /* The one-shot cipher encryption uses generated iv so validating
4171 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004172 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4173 output1_buffer_size, &output1_length));
4174 TEST_LE_U(output1_length,
4175 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4176 TEST_LE_U(output1_length,
4177 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004178
Gilles Peskine449bd832023-01-11 14:50:10 +01004179 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4180 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004181
Gilles Peskine449bd832023-01-11 14:50:10 +01004182 PSA_ASSERT(psa_cipher_update(&operation,
4183 input->x, input->len,
4184 output2, output2_buffer_size,
4185 &function_output_length));
4186 TEST_LE_U(function_output_length,
4187 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4188 TEST_LE_U(function_output_length,
4189 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004190 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004191
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 PSA_ASSERT(psa_cipher_finish(&operation,
4193 output2 + output2_length,
4194 output2_buffer_size - output2_length,
4195 &function_output_length));
4196 TEST_LE_U(function_output_length,
4197 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4198 TEST_LE_U(function_output_length,
4199 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004200 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004201
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 PSA_ASSERT(psa_cipher_abort(&operation));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004203 TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004204 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004205
Gilles Peskine50e586b2018-06-08 14:28:46 +02004206exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 psa_cipher_abort(&operation);
4208 mbedtls_free(output1);
4209 mbedtls_free(output2);
4210 psa_destroy_key(key);
4211 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004212}
4213/* END_CASE */
4214
4215/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004216void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4217 data_t *key_data, data_t *iv,
4218 data_t *input,
4219 int first_part_size_arg,
4220 int output1_length_arg, int output2_length_arg,
4221 data_t *expected_output,
4222 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004223{
Ronald Cron5425a212020-08-04 14:58:35 +02004224 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004225 psa_key_type_t key_type = key_type_arg;
4226 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004227 psa_status_t status;
4228 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004229 size_t first_part_size = first_part_size_arg;
4230 size_t output1_length = output1_length_arg;
4231 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004232 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004233 size_t output_buffer_size = 0;
4234 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004235 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004236 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004237 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004238
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004240
Gilles Peskine449bd832023-01-11 14:50:10 +01004241 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4242 psa_set_key_algorithm(&attributes, alg);
4243 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004244
Gilles Peskine449bd832023-01-11 14:50:10 +01004245 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4246 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004247
Gilles Peskine449bd832023-01-11 14:50:10 +01004248 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004249
Gilles Peskine449bd832023-01-11 14:50:10 +01004250 if (iv->len > 0) {
4251 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004252 }
4253
Gilles Peskine449bd832023-01-11 14:50:10 +01004254 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4255 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004256 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004257
Gilles Peskine449bd832023-01-11 14:50:10 +01004258 TEST_LE_U(first_part_size, input->len);
4259 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4260 output, output_buffer_size,
4261 &function_output_length));
4262 TEST_ASSERT(function_output_length == output1_length);
4263 TEST_LE_U(function_output_length,
4264 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4265 TEST_LE_U(function_output_length,
4266 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004267 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004268
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 if (first_part_size < input->len) {
4270 PSA_ASSERT(psa_cipher_update(&operation,
4271 input->x + first_part_size,
4272 input->len - first_part_size,
4273 (output_buffer_size == 0 ? NULL :
4274 output + total_output_length),
4275 output_buffer_size - total_output_length,
4276 &function_output_length));
4277 TEST_ASSERT(function_output_length == output2_length);
4278 TEST_LE_U(function_output_length,
4279 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4280 alg,
4281 input->len - first_part_size));
4282 TEST_LE_U(function_output_length,
4283 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004284 total_output_length += function_output_length;
4285 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004286
Gilles Peskine449bd832023-01-11 14:50:10 +01004287 status = psa_cipher_finish(&operation,
4288 (output_buffer_size == 0 ? NULL :
4289 output + total_output_length),
4290 output_buffer_size - total_output_length,
4291 &function_output_length);
4292 TEST_LE_U(function_output_length,
4293 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4294 TEST_LE_U(function_output_length,
4295 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004296 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004298
Gilles Peskine449bd832023-01-11 14:50:10 +01004299 if (expected_status == PSA_SUCCESS) {
4300 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004301
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004302 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004303 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004304 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004305
4306exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 psa_cipher_abort(&operation);
4308 mbedtls_free(output);
4309 psa_destroy_key(key);
4310 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004311}
4312/* END_CASE */
4313
4314/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004315void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4316 data_t *key_data, data_t *iv,
4317 data_t *input,
4318 int first_part_size_arg,
4319 int output1_length_arg, int output2_length_arg,
4320 data_t *expected_output,
4321 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004322{
Ronald Cron5425a212020-08-04 14:58:35 +02004323 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004324 psa_key_type_t key_type = key_type_arg;
4325 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004326 psa_status_t status;
4327 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004328 size_t first_part_size = first_part_size_arg;
4329 size_t output1_length = output1_length_arg;
4330 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004331 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004332 size_t output_buffer_size = 0;
4333 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004334 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004335 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004336 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004337
Gilles Peskine449bd832023-01-11 14:50:10 +01004338 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004339
Gilles Peskine449bd832023-01-11 14:50:10 +01004340 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4341 psa_set_key_algorithm(&attributes, alg);
4342 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004343
Gilles Peskine449bd832023-01-11 14:50:10 +01004344 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4345 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004346
Gilles Peskine449bd832023-01-11 14:50:10 +01004347 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004348
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 if (iv->len > 0) {
4350 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004351 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004352
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4354 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004355 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004356
Gilles Peskine449bd832023-01-11 14:50:10 +01004357 TEST_LE_U(first_part_size, input->len);
4358 PSA_ASSERT(psa_cipher_update(&operation,
4359 input->x, first_part_size,
4360 output, output_buffer_size,
4361 &function_output_length));
4362 TEST_ASSERT(function_output_length == output1_length);
4363 TEST_LE_U(function_output_length,
4364 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4365 TEST_LE_U(function_output_length,
4366 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004367 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004368
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 if (first_part_size < input->len) {
4370 PSA_ASSERT(psa_cipher_update(&operation,
4371 input->x + first_part_size,
4372 input->len - first_part_size,
4373 (output_buffer_size == 0 ? NULL :
4374 output + total_output_length),
4375 output_buffer_size - total_output_length,
4376 &function_output_length));
4377 TEST_ASSERT(function_output_length == output2_length);
4378 TEST_LE_U(function_output_length,
4379 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4380 alg,
4381 input->len - first_part_size));
4382 TEST_LE_U(function_output_length,
4383 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004384 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004385 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004386
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 status = psa_cipher_finish(&operation,
4388 (output_buffer_size == 0 ? NULL :
4389 output + total_output_length),
4390 output_buffer_size - total_output_length,
4391 &function_output_length);
4392 TEST_LE_U(function_output_length,
4393 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4394 TEST_LE_U(function_output_length,
4395 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004396 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004398
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 if (expected_status == PSA_SUCCESS) {
4400 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004401
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004402 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004403 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004404 }
4405
Gilles Peskine50e586b2018-06-08 14:28:46 +02004406exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004407 psa_cipher_abort(&operation);
4408 mbedtls_free(output);
4409 psa_destroy_key(key);
4410 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004411}
4412/* END_CASE */
4413
Gilles Peskine50e586b2018-06-08 14:28:46 +02004414/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004415void cipher_decrypt_fail(int alg_arg,
4416 int key_type_arg,
4417 data_t *key_data,
4418 data_t *iv,
4419 data_t *input_arg,
4420 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004421{
4422 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4423 psa_status_t status;
4424 psa_key_type_t key_type = key_type_arg;
4425 psa_algorithm_t alg = alg_arg;
4426 psa_status_t expected_status = expected_status_arg;
4427 unsigned char *input = NULL;
4428 size_t input_buffer_size = 0;
4429 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004430 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004431 size_t output_buffer_size = 0;
4432 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004433 size_t function_output_length;
4434 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004435 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4436
Gilles Peskine449bd832023-01-11 14:50:10 +01004437 if (PSA_ERROR_BAD_STATE != expected_status) {
4438 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004439
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4441 psa_set_key_algorithm(&attributes, alg);
4442 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004443
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4445 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004446 }
4447
4448 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004449 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4450 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004451 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004452 memcpy(input, iv->x, iv->len);
4453 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004454 }
4455
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004457 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004458
Neil Armstrong66a479f2022-02-07 15:41:19 +01004459 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4461 output_buffer_size, &output_length);
4462 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004463
Neil Armstrong66a479f2022-02-07 15:41:19 +01004464 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 status = psa_cipher_decrypt_setup(&operation, key, alg);
4466 if (status == PSA_SUCCESS) {
4467 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4468 input_arg->len) +
4469 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004470 TEST_CALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004471
Gilles Peskine449bd832023-01-11 14:50:10 +01004472 if (iv->len > 0) {
4473 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004474
Gilles Peskine449bd832023-01-11 14:50:10 +01004475 if (status != PSA_SUCCESS) {
4476 TEST_EQUAL(status, expected_status);
4477 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004478 }
4479
Gilles Peskine449bd832023-01-11 14:50:10 +01004480 if (status == PSA_SUCCESS) {
4481 status = psa_cipher_update(&operation,
4482 input_arg->x, input_arg->len,
4483 output_multi, output_buffer_size,
4484 &function_output_length);
4485 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004486 output_length = function_output_length;
4487
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 status = psa_cipher_finish(&operation,
4489 output_multi + output_length,
4490 output_buffer_size - output_length,
4491 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004492
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 TEST_EQUAL(status, expected_status);
4494 } else {
4495 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004496 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 } else {
4498 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004499 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 } else {
4501 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004502 }
4503
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004504exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 psa_cipher_abort(&operation);
4506 mbedtls_free(input);
4507 mbedtls_free(output);
4508 mbedtls_free(output_multi);
4509 psa_destroy_key(key);
4510 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004511}
4512/* END_CASE */
4513
4514/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004515void cipher_decrypt(int alg_arg,
4516 int key_type_arg,
4517 data_t *key_data,
4518 data_t *iv,
4519 data_t *input_arg,
4520 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004521{
4522 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4523 psa_key_type_t key_type = key_type_arg;
4524 psa_algorithm_t alg = alg_arg;
4525 unsigned char *input = NULL;
4526 size_t input_buffer_size = 0;
4527 unsigned char *output = NULL;
4528 size_t output_buffer_size = 0;
4529 size_t output_length = 0;
4530 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4531
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004533
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4535 psa_set_key_algorithm(&attributes, alg);
4536 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004537
4538 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4540 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004541 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 memcpy(input, iv->x, iv->len);
4543 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004544 }
4545
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004547 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004548
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4550 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004551
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4553 output_buffer_size, &output_length));
4554 TEST_LE_U(output_length,
4555 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4556 TEST_LE_U(output_length,
4557 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004558
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004559 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004560 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004561exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 mbedtls_free(input);
4563 mbedtls_free(output);
4564 psa_destroy_key(key);
4565 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004566}
4567/* END_CASE */
4568
4569/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004570void cipher_verify_output(int alg_arg,
4571 int key_type_arg,
4572 data_t *key_data,
4573 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004574{
Ronald Cron5425a212020-08-04 14:58:35 +02004575 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004576 psa_key_type_t key_type = key_type_arg;
4577 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004578 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004579 size_t output1_size = 0;
4580 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004581 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004582 size_t output2_size = 0;
4583 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004584 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004585
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004587
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4589 psa_set_key_algorithm(&attributes, alg);
4590 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004591
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4593 &key));
4594 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004595 TEST_CALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004596
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4598 output1, output1_size,
4599 &output1_length));
4600 TEST_LE_U(output1_length,
4601 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4602 TEST_LE_U(output1_length,
4603 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004604
4605 output2_size = output1_length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004606 TEST_CALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004607
Gilles Peskine449bd832023-01-11 14:50:10 +01004608 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4609 output2, output2_size,
4610 &output2_length));
4611 TEST_LE_U(output2_length,
4612 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4613 TEST_LE_U(output2_length,
4614 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004615
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004616 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004617
4618exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 mbedtls_free(output1);
4620 mbedtls_free(output2);
4621 psa_destroy_key(key);
4622 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004623}
4624/* END_CASE */
4625
4626/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004627void cipher_verify_output_multipart(int alg_arg,
4628 int key_type_arg,
4629 data_t *key_data,
4630 data_t *input,
4631 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004632{
Ronald Cron5425a212020-08-04 14:58:35 +02004633 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004634 psa_key_type_t key_type = key_type_arg;
4635 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004636 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004638 size_t iv_size = 16;
4639 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004640 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004641 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004642 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004643 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004644 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004645 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004646 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004647 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4648 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004649 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004650
Gilles Peskine449bd832023-01-11 14:50:10 +01004651 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004652
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4654 psa_set_key_algorithm(&attributes, alg);
4655 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004656
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4658 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004659
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4661 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004662
Gilles Peskine449bd832023-01-11 14:50:10 +01004663 if (alg != PSA_ALG_ECB_NO_PADDING) {
4664 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4665 iv, iv_size,
4666 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004667 }
4668
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4670 TEST_LE_U(output1_buffer_size,
4671 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004672 TEST_CALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004673
Gilles Peskine449bd832023-01-11 14:50:10 +01004674 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004675
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4677 output1, output1_buffer_size,
4678 &function_output_length));
4679 TEST_LE_U(function_output_length,
4680 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4681 TEST_LE_U(function_output_length,
4682 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004683 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004684
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 PSA_ASSERT(psa_cipher_update(&operation1,
4686 input->x + first_part_size,
4687 input->len - first_part_size,
4688 output1, output1_buffer_size,
4689 &function_output_length));
4690 TEST_LE_U(function_output_length,
4691 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4692 alg,
4693 input->len - first_part_size));
4694 TEST_LE_U(function_output_length,
4695 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004696 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004697
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 PSA_ASSERT(psa_cipher_finish(&operation1,
4699 output1 + output1_length,
4700 output1_buffer_size - output1_length,
4701 &function_output_length));
4702 TEST_LE_U(function_output_length,
4703 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4704 TEST_LE_U(function_output_length,
4705 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004706 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004707
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004709
Gilles Peskine048b7f02018-06-08 14:20:49 +02004710 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 TEST_LE_U(output2_buffer_size,
4712 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4713 TEST_LE_U(output2_buffer_size,
4714 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004715 TEST_CALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004716
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 if (iv_length > 0) {
4718 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4719 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004720 }
Moran Pekerded84402018-06-06 16:36:50 +03004721
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4723 output2, output2_buffer_size,
4724 &function_output_length));
4725 TEST_LE_U(function_output_length,
4726 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4727 TEST_LE_U(function_output_length,
4728 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004729 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004730
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 PSA_ASSERT(psa_cipher_update(&operation2,
4732 output1 + first_part_size,
4733 output1_length - first_part_size,
4734 output2, output2_buffer_size,
4735 &function_output_length));
4736 TEST_LE_U(function_output_length,
4737 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4738 alg,
4739 output1_length - first_part_size));
4740 TEST_LE_U(function_output_length,
4741 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004742 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004743
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 PSA_ASSERT(psa_cipher_finish(&operation2,
4745 output2 + output2_length,
4746 output2_buffer_size - output2_length,
4747 &function_output_length));
4748 TEST_LE_U(function_output_length,
4749 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4750 TEST_LE_U(function_output_length,
4751 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004752 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004753
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004755
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004756 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004757
4758exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 psa_cipher_abort(&operation1);
4760 psa_cipher_abort(&operation2);
4761 mbedtls_free(output1);
4762 mbedtls_free(output2);
4763 psa_destroy_key(key);
4764 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004765}
4766/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004767
Gilles Peskine20035e32018-02-03 22:44:14 +01004768/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004769void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4770 int alg_arg,
4771 data_t *nonce,
4772 data_t *additional_data,
4773 data_t *input_data,
4774 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004775{
Ronald Cron5425a212020-08-04 14:58:35 +02004776 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004777 psa_key_type_t key_type = key_type_arg;
4778 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004779 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004780 unsigned char *output_data = NULL;
4781 size_t output_size = 0;
4782 size_t output_length = 0;
4783 unsigned char *output_data2 = NULL;
4784 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004785 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004786 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004787 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004788
Gilles Peskine449bd832023-01-11 14:50:10 +01004789 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004790
Gilles Peskine449bd832023-01-11 14:50:10 +01004791 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4792 psa_set_key_algorithm(&attributes, alg);
4793 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004794
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4796 &key));
4797 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4798 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004799
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4801 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004802 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4803 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4805 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4806 TEST_EQUAL(output_size,
4807 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4808 TEST_LE_U(output_size,
4809 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004810 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004811 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004812
Gilles Peskine449bd832023-01-11 14:50:10 +01004813 status = psa_aead_encrypt(key, alg,
4814 nonce->x, nonce->len,
4815 additional_data->x,
4816 additional_data->len,
4817 input_data->x, input_data->len,
4818 output_data, output_size,
4819 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004820
4821 /* If the operation is not supported, just skip and not fail in case the
4822 * encryption involves a common limitation of cryptography hardwares and
4823 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 if (status == PSA_ERROR_NOT_SUPPORTED) {
4825 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4826 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004827 }
4828
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004830
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 if (PSA_SUCCESS == expected_result) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004832 TEST_CALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004833
Gilles Peskine003a4a92019-05-14 16:09:40 +02004834 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4835 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 TEST_EQUAL(input_data->len,
4837 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004838
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 TEST_LE_U(input_data->len,
4840 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004841
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 TEST_EQUAL(psa_aead_decrypt(key, alg,
4843 nonce->x, nonce->len,
4844 additional_data->x,
4845 additional_data->len,
4846 output_data, output_length,
4847 output_data2, output_length,
4848 &output_length2),
4849 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004850
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004851 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004852 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004853 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004854
Gilles Peskinea1cac842018-06-11 19:33:02 +02004855exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004856 psa_destroy_key(key);
4857 mbedtls_free(output_data);
4858 mbedtls_free(output_data2);
4859 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004860}
4861/* END_CASE */
4862
4863/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004864void aead_encrypt(int key_type_arg, data_t *key_data,
4865 int alg_arg,
4866 data_t *nonce,
4867 data_t *additional_data,
4868 data_t *input_data,
4869 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004870{
Ronald Cron5425a212020-08-04 14:58:35 +02004871 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004872 psa_key_type_t key_type = key_type_arg;
4873 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004874 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004875 unsigned char *output_data = NULL;
4876 size_t output_size = 0;
4877 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004878 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004879 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004880
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004882
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4884 psa_set_key_algorithm(&attributes, alg);
4885 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004886
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4888 &key));
4889 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4890 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004891
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4893 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004894 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4895 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 TEST_EQUAL(output_size,
4897 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4898 TEST_LE_U(output_size,
4899 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004900 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004901
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 status = psa_aead_encrypt(key, alg,
4903 nonce->x, nonce->len,
4904 additional_data->x, additional_data->len,
4905 input_data->x, input_data->len,
4906 output_data, output_size,
4907 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004908
Ronald Cron28a45ed2021-02-09 20:35:42 +01004909 /* If the operation is not supported, just skip and not fail in case the
4910 * encryption involves a common limitation of cryptography hardwares and
4911 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 if (status == PSA_ERROR_NOT_SUPPORTED) {
4913 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4914 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004915 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004916
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 PSA_ASSERT(status);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004918 TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004919 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004920
Gilles Peskinea1cac842018-06-11 19:33:02 +02004921exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004922 psa_destroy_key(key);
4923 mbedtls_free(output_data);
4924 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004925}
4926/* END_CASE */
4927
4928/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004929void aead_decrypt(int key_type_arg, data_t *key_data,
4930 int alg_arg,
4931 data_t *nonce,
4932 data_t *additional_data,
4933 data_t *input_data,
4934 data_t *expected_data,
4935 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004936{
Ronald Cron5425a212020-08-04 14:58:35 +02004937 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004938 psa_key_type_t key_type = key_type_arg;
4939 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004940 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004941 unsigned char *output_data = NULL;
4942 size_t output_size = 0;
4943 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004944 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004945 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004946 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004947
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004949
Gilles Peskine449bd832023-01-11 14:50:10 +01004950 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4951 psa_set_key_algorithm(&attributes, alg);
4952 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004953
Gilles Peskine449bd832023-01-11 14:50:10 +01004954 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4955 &key));
4956 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4957 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004958
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4960 alg);
4961 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4962 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004963 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4964 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 TEST_EQUAL(output_size,
4966 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4967 TEST_LE_U(output_size,
4968 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004969 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004970 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004971
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 status = psa_aead_decrypt(key, alg,
4973 nonce->x, nonce->len,
4974 additional_data->x,
4975 additional_data->len,
4976 input_data->x, input_data->len,
4977 output_data, output_size,
4978 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004979
Ronald Cron28a45ed2021-02-09 20:35:42 +01004980 /* If the operation is not supported, just skip and not fail in case the
4981 * decryption involves a common limitation of cryptography hardwares and
4982 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004983 if (status == PSA_ERROR_NOT_SUPPORTED) {
4984 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4985 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004986 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004987
Gilles Peskine449bd832023-01-11 14:50:10 +01004988 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004989
Gilles Peskine449bd832023-01-11 14:50:10 +01004990 if (expected_result == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004991 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004992 output_data, output_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004993 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004994
Gilles Peskinea1cac842018-06-11 19:33:02 +02004995exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 psa_destroy_key(key);
4997 mbedtls_free(output_data);
4998 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004999}
5000/* END_CASE */
5001
5002/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005003void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
5004 int alg_arg,
5005 data_t *nonce,
5006 data_t *additional_data,
5007 data_t *input_data,
5008 int do_set_lengths,
5009 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005010{
Paul Elliottd3f82412021-06-16 16:52:21 +01005011 size_t ad_part_len = 0;
5012 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005013 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005014
Gilles Peskine449bd832023-01-11 14:50:10 +01005015 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
5016 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005017
Gilles Peskine449bd832023-01-11 14:50:10 +01005018 if (do_set_lengths) {
5019 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005020 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005022 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005023 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005024 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005025
5026 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005027 if (!aead_multipart_internal_func(key_type_arg, key_data,
5028 alg_arg, nonce,
5029 additional_data,
5030 ad_part_len,
5031 input_data, -1,
5032 set_lengths_method,
5033 expected_output,
5034 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005035 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005036 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005037
Gilles Peskine449bd832023-01-11 14:50:10 +01005038 /* length(0) part, length(ad_part_len) part, length(0) part... */
5039 mbedtls_test_set_step(1000 + ad_part_len);
5040
5041 if (!aead_multipart_internal_func(key_type_arg, key_data,
5042 alg_arg, nonce,
5043 additional_data,
5044 ad_part_len,
5045 input_data, -1,
5046 set_lengths_method,
5047 expected_output,
5048 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005049 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 }
5051 }
5052
5053 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
5054 /* Split data into length(data_part_len) parts. */
5055 mbedtls_test_set_step(2000 + data_part_len);
5056
5057 if (do_set_lengths) {
5058 if (data_part_len & 0x01) {
5059 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5060 } else {
5061 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
5062 }
5063 }
5064
5065 if (!aead_multipart_internal_func(key_type_arg, key_data,
5066 alg_arg, nonce,
5067 additional_data, -1,
5068 input_data, data_part_len,
5069 set_lengths_method,
5070 expected_output,
5071 1, 0)) {
5072 break;
5073 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005074
5075 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005077
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 if (!aead_multipart_internal_func(key_type_arg, key_data,
5079 alg_arg, nonce,
5080 additional_data, -1,
5081 input_data, data_part_len,
5082 set_lengths_method,
5083 expected_output,
5084 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005085 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005087 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005088
Paul Elliott8fc45162021-06-23 16:06:01 +01005089 /* Goto is required to silence warnings about unused labels, as we
5090 * don't actually do any test assertions in this function. */
5091 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005092}
5093/* END_CASE */
5094
5095/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005096void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5097 int alg_arg,
5098 data_t *nonce,
5099 data_t *additional_data,
5100 data_t *input_data,
5101 int do_set_lengths,
5102 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005103{
Paul Elliottd3f82412021-06-16 16:52:21 +01005104 size_t ad_part_len = 0;
5105 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005106 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005107
Gilles Peskine449bd832023-01-11 14:50:10 +01005108 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005109 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005110 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005111
Gilles Peskine449bd832023-01-11 14:50:10 +01005112 if (do_set_lengths) {
5113 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005114 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005116 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005118 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005119
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 if (!aead_multipart_internal_func(key_type_arg, key_data,
5121 alg_arg, nonce,
5122 additional_data,
5123 ad_part_len,
5124 input_data, -1,
5125 set_lengths_method,
5126 expected_output,
5127 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005128 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005130
5131 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005132 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005133
Gilles Peskine449bd832023-01-11 14:50:10 +01005134 if (!aead_multipart_internal_func(key_type_arg, key_data,
5135 alg_arg, nonce,
5136 additional_data,
5137 ad_part_len,
5138 input_data, -1,
5139 set_lengths_method,
5140 expected_output,
5141 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005142 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005143 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005144 }
5145
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005147 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 if (do_set_lengths) {
5151 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005152 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005154 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005156 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005157
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 if (!aead_multipart_internal_func(key_type_arg, key_data,
5159 alg_arg, nonce,
5160 additional_data, -1,
5161 input_data, data_part_len,
5162 set_lengths_method,
5163 expected_output,
5164 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005165 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005167
5168 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005170
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 if (!aead_multipart_internal_func(key_type_arg, key_data,
5172 alg_arg, nonce,
5173 additional_data, -1,
5174 input_data, data_part_len,
5175 set_lengths_method,
5176 expected_output,
5177 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005178 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005180 }
5181
Paul Elliott8fc45162021-06-23 16:06:01 +01005182 /* Goto is required to silence warnings about unused labels, as we
5183 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005184 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005185}
5186/* END_CASE */
5187
5188/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005189void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5190 int alg_arg,
5191 int nonce_length,
5192 int expected_nonce_length_arg,
5193 data_t *additional_data,
5194 data_t *input_data,
5195 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005196{
5197
5198 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5199 psa_key_type_t key_type = key_type_arg;
5200 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005201 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005202 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5203 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5204 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005205 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005206 size_t actual_nonce_length = 0;
5207 size_t expected_nonce_length = expected_nonce_length_arg;
5208 unsigned char *output = NULL;
5209 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005210 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005211 size_t ciphertext_size = 0;
5212 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005213 size_t tag_length = 0;
5214 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005215
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005217
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5219 psa_set_key_algorithm(&attributes, alg);
5220 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005221
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5223 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005224
Gilles Peskine449bd832023-01-11 14:50:10 +01005225 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005226
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005228
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005229 TEST_CALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005230
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005232
Gilles Peskine449bd832023-01-11 14:50:10 +01005233 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005234
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005235 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005236
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005238
5239 /* If the operation is not supported, just skip and not fail in case the
5240 * encryption involves a common limitation of cryptography hardwares and
5241 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 if (status == PSA_ERROR_NOT_SUPPORTED) {
5243 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5244 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005245 }
5246
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005248
Gilles Peskine449bd832023-01-11 14:50:10 +01005249 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5250 nonce_length,
5251 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005252
Gilles Peskine449bd832023-01-11 14:50:10 +01005253 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005254
Gilles Peskine449bd832023-01-11 14:50:10 +01005255 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005256
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 if (expected_status == PSA_SUCCESS) {
5258 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5259 alg));
5260 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005261
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005263
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005265 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5267 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005268
Gilles Peskine449bd832023-01-11 14:50:10 +01005269 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5270 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005271
Gilles Peskine449bd832023-01-11 14:50:10 +01005272 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5273 output, output_size,
5274 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005275
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5277 &ciphertext_length, tag_buffer,
5278 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005279 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005280
5281exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 psa_destroy_key(key);
5283 mbedtls_free(output);
5284 mbedtls_free(ciphertext);
5285 psa_aead_abort(&operation);
5286 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005287}
5288/* END_CASE */
5289
5290/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005291void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5292 int alg_arg,
5293 int nonce_length_arg,
5294 int set_lengths_method_arg,
5295 data_t *additional_data,
5296 data_t *input_data,
5297 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005298{
5299
5300 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5301 psa_key_type_t key_type = key_type_arg;
5302 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005303 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005304 uint8_t *nonce_buffer = NULL;
5305 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5306 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5307 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005308 unsigned char *output = NULL;
5309 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005310 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005311 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005312 size_t ciphertext_size = 0;
5313 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005314 size_t tag_length = 0;
5315 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005316 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005317 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005318
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005320
Gilles Peskine449bd832023-01-11 14:50:10 +01005321 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5322 psa_set_key_algorithm(&attributes, alg);
5323 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005324
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5326 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005327
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005329
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005331
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005332 TEST_CALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005333
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005335
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005337
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005338 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005339
Gilles Peskine449bd832023-01-11 14:50:10 +01005340 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005341
5342 /* If the operation is not supported, just skip and not fail in case the
5343 * encryption involves a common limitation of cryptography hardwares and
5344 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 if (status == PSA_ERROR_NOT_SUPPORTED) {
5346 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5347 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005348 }
5349
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005351
Paul Elliott4023ffd2021-09-10 16:21:22 +01005352 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 if (nonce_length_arg == -1) {
5354 /* Arbitrary size buffer, to test zero length valid buffer. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005355 TEST_CALLOC(nonce_buffer, 4);
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 nonce_length = 0;
5357 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005358 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005359 nonce_length = (size_t) nonce_length_arg;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005360 TEST_CALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005361
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 if (nonce_buffer) {
5363 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005364 nonce_buffer[index] = 'a' + index;
5365 }
Paul Elliott66696b52021-08-16 18:42:41 +01005366 }
Paul Elliott863864a2021-07-23 17:28:31 +01005367 }
5368
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5370 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5371 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005372 }
5373
Gilles Peskine449bd832023-01-11 14:50:10 +01005374 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005375
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005377
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 if (expected_status == PSA_SUCCESS) {
5379 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5380 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5381 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005382 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005384 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 }
Paul Elliott863864a2021-07-23 17:28:31 +01005386
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005387 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5389 additional_data->len),
5390 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005391
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5393 output, output_size,
5394 &ciphertext_length),
5395 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005396
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5398 &ciphertext_length, tag_buffer,
5399 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5400 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005401 }
5402
5403exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 psa_destroy_key(key);
5405 mbedtls_free(output);
5406 mbedtls_free(ciphertext);
5407 mbedtls_free(nonce_buffer);
5408 psa_aead_abort(&operation);
5409 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005410}
5411/* END_CASE */
5412
5413/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005414void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005415 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005416 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005417 data_t *nonce,
5418 data_t *additional_data,
5419 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005420 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005421{
5422
5423 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5424 psa_key_type_t key_type = key_type_arg;
5425 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005426 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005427 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5428 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5429 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005430 unsigned char *output = NULL;
5431 unsigned char *ciphertext = NULL;
5432 size_t output_size = output_size_arg;
5433 size_t ciphertext_size = 0;
5434 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005435 size_t tag_length = 0;
5436 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5437
Gilles Peskine449bd832023-01-11 14:50:10 +01005438 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005439
Gilles Peskine449bd832023-01-11 14:50:10 +01005440 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5441 psa_set_key_algorithm(&attributes, alg);
5442 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005443
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5445 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005446
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005448
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005449 TEST_CALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005450
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005452
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005453 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005454
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005456
5457 /* If the operation is not supported, just skip and not fail in case the
5458 * encryption involves a common limitation of cryptography hardwares and
5459 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 if (status == PSA_ERROR_NOT_SUPPORTED) {
5461 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5462 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005463 }
5464
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005466
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5468 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005469
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005471
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5473 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005474
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 status = psa_aead_update(&operation, input_data->x, input_data->len,
5476 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005477
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005479
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005481 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5483 &ciphertext_length, tag_buffer,
5484 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005485 }
5486
5487exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 psa_destroy_key(key);
5489 mbedtls_free(output);
5490 mbedtls_free(ciphertext);
5491 psa_aead_abort(&operation);
5492 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005493}
5494/* END_CASE */
5495
Paul Elliott91b021e2021-07-23 18:52:31 +01005496/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005497void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5498 int alg_arg,
5499 int finish_ciphertext_size_arg,
5500 int tag_size_arg,
5501 data_t *nonce,
5502 data_t *additional_data,
5503 data_t *input_data,
5504 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005505{
5506
5507 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5508 psa_key_type_t key_type = key_type_arg;
5509 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005510 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005511 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5512 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5513 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005514 unsigned char *ciphertext = NULL;
5515 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005516 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005517 size_t ciphertext_size = 0;
5518 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5520 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005521 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005522
Gilles Peskine449bd832023-01-11 14:50:10 +01005523 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005524
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5526 psa_set_key_algorithm(&attributes, alg);
5527 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005528
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5530 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005531
Gilles Peskine449bd832023-01-11 14:50:10 +01005532 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005533
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005535
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005536 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005537
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005538 TEST_CALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005539
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005540 TEST_CALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005541
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005543
5544 /* If the operation is not supported, just skip and not fail in case the
5545 * encryption involves a common limitation of cryptography hardwares and
5546 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 if (status == PSA_ERROR_NOT_SUPPORTED) {
5548 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5549 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005550 }
5551
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005553
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005555
Gilles Peskine449bd832023-01-11 14:50:10 +01005556 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5557 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005558
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5560 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005561
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5563 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005564
5565 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 status = psa_aead_finish(&operation, finish_ciphertext,
5567 finish_ciphertext_size,
5568 &ciphertext_length, tag_buffer,
5569 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005570
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005572
5573exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 psa_destroy_key(key);
5575 mbedtls_free(ciphertext);
5576 mbedtls_free(finish_ciphertext);
5577 mbedtls_free(tag_buffer);
5578 psa_aead_abort(&operation);
5579 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005580}
5581/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005582
5583/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005584void aead_multipart_verify(int key_type_arg, data_t *key_data,
5585 int alg_arg,
5586 data_t *nonce,
5587 data_t *additional_data,
5588 data_t *input_data,
5589 data_t *tag,
5590 int tag_usage_arg,
5591 int expected_setup_status_arg,
5592 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005593{
5594 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5595 psa_key_type_t key_type = key_type_arg;
5596 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005597 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005598 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5599 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5600 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005601 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005602 unsigned char *plaintext = NULL;
5603 unsigned char *finish_plaintext = NULL;
5604 size_t plaintext_size = 0;
5605 size_t plaintext_length = 0;
5606 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005607 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005608 unsigned char *tag_buffer = NULL;
5609 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005610
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005612
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5614 psa_set_key_algorithm(&attributes, alg);
5615 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005616
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5618 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005619
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005621
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5623 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005624
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005625 TEST_CALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005626
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005628
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005629 TEST_CALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005630
Gilles Peskine449bd832023-01-11 14:50:10 +01005631 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005632
5633 /* If the operation is not supported, just skip and not fail in case the
5634 * encryption involves a common limitation of cryptography hardwares and
5635 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 if (status == PSA_ERROR_NOT_SUPPORTED) {
5637 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5638 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005639 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005640 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005641
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005643 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005644 }
Paul Elliott9961a662021-09-17 19:19:02 +01005645
Gilles Peskine449bd832023-01-11 14:50:10 +01005646 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005647
Gilles Peskine449bd832023-01-11 14:50:10 +01005648 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005649
Gilles Peskine449bd832023-01-11 14:50:10 +01005650 status = psa_aead_set_lengths(&operation, additional_data->len,
5651 input_data->len);
5652 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005653
Gilles Peskine449bd832023-01-11 14:50:10 +01005654 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5655 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005656
Gilles Peskine449bd832023-01-11 14:50:10 +01005657 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5658 input_data->len,
5659 plaintext, plaintext_size,
5660 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005661
Gilles Peskine449bd832023-01-11 14:50:10 +01005662 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005663 tag_buffer = tag->x;
5664 tag_size = tag->len;
5665 }
5666
Gilles Peskine449bd832023-01-11 14:50:10 +01005667 status = psa_aead_verify(&operation, finish_plaintext,
5668 verify_plaintext_size,
5669 &plaintext_length,
5670 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005671
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005673
5674exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005675 psa_destroy_key(key);
5676 mbedtls_free(plaintext);
5677 mbedtls_free(finish_plaintext);
5678 psa_aead_abort(&operation);
5679 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005680}
5681/* END_CASE */
5682
Paul Elliott9961a662021-09-17 19:19:02 +01005683/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005684void aead_multipart_setup(int key_type_arg, data_t *key_data,
5685 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005686{
5687 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5688 psa_key_type_t key_type = key_type_arg;
5689 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005690 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005691 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5692 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5693 psa_status_t expected_status = expected_status_arg;
5694
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005696
Gilles Peskine449bd832023-01-11 14:50:10 +01005697 psa_set_key_usage_flags(&attributes,
5698 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5699 psa_set_key_algorithm(&attributes, alg);
5700 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005701
Gilles Peskine449bd832023-01-11 14:50:10 +01005702 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5703 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005704
Gilles Peskine449bd832023-01-11 14:50:10 +01005705 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005706
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005708
Gilles Peskine449bd832023-01-11 14:50:10 +01005709 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005710
Gilles Peskine449bd832023-01-11 14:50:10 +01005711 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005712
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005714
5715exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 psa_destroy_key(key);
5717 psa_aead_abort(&operation);
5718 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005719}
5720/* END_CASE */
5721
5722/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005723void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5724 int alg_arg,
5725 data_t *nonce,
5726 data_t *additional_data,
5727 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005728{
5729 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5730 psa_key_type_t key_type = key_type_arg;
5731 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005732 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005733 unsigned char *output_data = NULL;
5734 unsigned char *final_data = NULL;
5735 size_t output_size = 0;
5736 size_t finish_output_size = 0;
5737 size_t output_length = 0;
5738 size_t key_bits = 0;
5739 size_t tag_length = 0;
5740 size_t tag_size = 0;
5741 size_t nonce_length = 0;
5742 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5743 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5744 size_t output_part_length = 0;
5745 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5746
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005748
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 psa_set_key_usage_flags(&attributes,
5750 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5751 psa_set_key_algorithm(&attributes, alg);
5752 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005753
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5755 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005756
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5758 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005759
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005761
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005763
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005765
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005766 TEST_CALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005767
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005769
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005771
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005772 TEST_CALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005773
5774 /* Test all operations error without calling setup first. */
5775
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5777 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005778
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005780
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5782 PSA_AEAD_NONCE_MAX_SIZE,
5783 &nonce_length),
5784 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005785
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005787
Paul Elliott481be342021-07-16 17:38:47 +01005788 /* ------------------------------------------------------- */
5789
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5791 input_data->len),
5792 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005793
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005795
Paul Elliott481be342021-07-16 17:38:47 +01005796 /* ------------------------------------------------------- */
5797
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5799 additional_data->len),
5800 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005801
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005803
Paul Elliott481be342021-07-16 17:38:47 +01005804 /* ------------------------------------------------------- */
5805
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5807 input_data->len, output_data,
5808 output_size, &output_length),
5809 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005810
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005812
Paul Elliott481be342021-07-16 17:38:47 +01005813 /* ------------------------------------------------------- */
5814
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5816 finish_output_size,
5817 &output_part_length,
5818 tag_buffer, tag_length,
5819 &tag_size),
5820 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005821
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005823
Paul Elliott481be342021-07-16 17:38:47 +01005824 /* ------------------------------------------------------- */
5825
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5827 finish_output_size,
5828 &output_part_length,
5829 tag_buffer,
5830 tag_length),
5831 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005832
Gilles Peskine449bd832023-01-11 14:50:10 +01005833 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005834
5835 /* Test for double setups. */
5836
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005838
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5840 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005841
Gilles Peskine449bd832023-01-11 14:50:10 +01005842 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005843
Paul Elliott481be342021-07-16 17:38:47 +01005844 /* ------------------------------------------------------- */
5845
Gilles Peskine449bd832023-01-11 14:50:10 +01005846 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005847
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5849 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005850
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005852
Paul Elliott374a2be2021-07-16 17:53:40 +01005853 /* ------------------------------------------------------- */
5854
Gilles Peskine449bd832023-01-11 14:50:10 +01005855 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005856
Gilles Peskine449bd832023-01-11 14:50:10 +01005857 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5858 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005859
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005861
5862 /* ------------------------------------------------------- */
5863
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005865
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5867 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005868
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005870
Paul Elliottc23a9a02021-06-21 18:32:46 +01005871 /* Test for not setting a nonce. */
5872
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005874
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5876 additional_data->len),
5877 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005878
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005880
Paul Elliott7f628422021-09-01 12:08:29 +01005881 /* ------------------------------------------------------- */
5882
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005884
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5886 input_data->len, output_data,
5887 output_size, &output_length),
5888 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005889
Gilles Peskine449bd832023-01-11 14:50:10 +01005890 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005891
Paul Elliottbdc2c682021-09-21 18:37:10 +01005892 /* ------------------------------------------------------- */
5893
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005895
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5897 finish_output_size,
5898 &output_part_length,
5899 tag_buffer, tag_length,
5900 &tag_size),
5901 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005902
Gilles Peskine449bd832023-01-11 14:50:10 +01005903 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005904
5905 /* ------------------------------------------------------- */
5906
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005908
Gilles Peskine449bd832023-01-11 14:50:10 +01005909 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5910 finish_output_size,
5911 &output_part_length,
5912 tag_buffer,
5913 tag_length),
5914 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005915
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005917
Paul Elliottc23a9a02021-06-21 18:32:46 +01005918 /* Test for double setting nonce. */
5919
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005921
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005923
Gilles Peskine449bd832023-01-11 14:50:10 +01005924 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5925 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005926
Gilles Peskine449bd832023-01-11 14:50:10 +01005927 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005928
Paul Elliott374a2be2021-07-16 17:53:40 +01005929 /* Test for double generating nonce. */
5930
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005932
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5934 PSA_AEAD_NONCE_MAX_SIZE,
5935 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005936
Gilles Peskine449bd832023-01-11 14:50:10 +01005937 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5938 PSA_AEAD_NONCE_MAX_SIZE,
5939 &nonce_length),
5940 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005941
5942
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005944
5945 /* Test for generate nonce then set and vice versa */
5946
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005948
Gilles Peskine449bd832023-01-11 14:50:10 +01005949 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5950 PSA_AEAD_NONCE_MAX_SIZE,
5951 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005952
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5954 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005955
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005957
Andrzej Kurekad837522021-12-15 15:28:49 +01005958 /* Test for generating nonce after calling set lengths */
5959
Gilles Peskine449bd832023-01-11 14:50:10 +01005960 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005961
Gilles Peskine449bd832023-01-11 14:50:10 +01005962 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5963 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005964
Gilles Peskine449bd832023-01-11 14:50:10 +01005965 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5966 PSA_AEAD_NONCE_MAX_SIZE,
5967 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005968
Gilles Peskine449bd832023-01-11 14:50:10 +01005969 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005970
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005971 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005972
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005974
Gilles Peskine449bd832023-01-11 14:50:10 +01005975 if (operation.alg == PSA_ALG_CCM) {
5976 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5977 input_data->len),
5978 PSA_ERROR_INVALID_ARGUMENT);
5979 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5980 PSA_AEAD_NONCE_MAX_SIZE,
5981 &nonce_length),
5982 PSA_ERROR_BAD_STATE);
5983 } else {
5984 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5985 input_data->len));
5986 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5987 PSA_AEAD_NONCE_MAX_SIZE,
5988 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005989 }
5990
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005992
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005993 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005994#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005996
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5998 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5999 input_data->len),
6000 PSA_ERROR_INVALID_ARGUMENT);
6001 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6002 PSA_AEAD_NONCE_MAX_SIZE,
6003 &nonce_length),
6004 PSA_ERROR_BAD_STATE);
6005 } else {
6006 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6007 input_data->len));
6008 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6009 PSA_AEAD_NONCE_MAX_SIZE,
6010 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006011 }
6012
Gilles Peskine449bd832023-01-11 14:50:10 +01006013 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00006014#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01006015
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006016 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01006017
Gilles Peskine449bd832023-01-11 14:50:10 +01006018 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006019
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6021 PSA_AEAD_NONCE_MAX_SIZE,
6022 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006023
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 if (operation.alg == PSA_ALG_CCM) {
6025 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6026 input_data->len),
6027 PSA_ERROR_INVALID_ARGUMENT);
6028 } else {
6029 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6030 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006031 }
6032
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006034
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006035 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006036 /* Test for setting nonce after calling set lengths */
6037
Gilles Peskine449bd832023-01-11 14:50:10 +01006038 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006039
Gilles Peskine449bd832023-01-11 14:50:10 +01006040 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6041 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006042
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006044
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006046
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006047 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006048
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006050
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 if (operation.alg == PSA_ALG_CCM) {
6052 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6053 input_data->len),
6054 PSA_ERROR_INVALID_ARGUMENT);
6055 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6056 PSA_ERROR_BAD_STATE);
6057 } else {
6058 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6059 input_data->len));
6060 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006061 }
6062
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006064
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006065 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00006066#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006068
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
6070 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
6071 input_data->len),
6072 PSA_ERROR_INVALID_ARGUMENT);
6073 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6074 PSA_ERROR_BAD_STATE);
6075 } else {
6076 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6077 input_data->len));
6078 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006079 }
6080
Gilles Peskine449bd832023-01-11 14:50:10 +01006081 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00006082#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006083
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006084 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006085
Gilles Peskine449bd832023-01-11 14:50:10 +01006086 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006087
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006089
Gilles Peskine449bd832023-01-11 14:50:10 +01006090 if (operation.alg == PSA_ALG_CCM) {
6091 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6092 input_data->len),
6093 PSA_ERROR_INVALID_ARGUMENT);
6094 } else {
6095 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6096 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006097 }
6098
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006100
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006101 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006102#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006104
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 if (operation.alg == PSA_ALG_GCM) {
6106 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6107 SIZE_MAX),
6108 PSA_ERROR_INVALID_ARGUMENT);
6109 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6110 PSA_ERROR_BAD_STATE);
6111 } else if (operation.alg != PSA_ALG_CCM) {
6112 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6113 SIZE_MAX));
6114 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006115 }
6116
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006118#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006119
Tom Cosgrove1797b052022-12-04 17:19:59 +00006120 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006121#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006123
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006125
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 if (operation.alg == PSA_ALG_GCM) {
6127 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6128 SIZE_MAX),
6129 PSA_ERROR_INVALID_ARGUMENT);
6130 } else if (operation.alg != PSA_ALG_CCM) {
6131 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6132 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006133 }
6134
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006136#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006137
6138 /* ------------------------------------------------------- */
6139
Gilles Peskine449bd832023-01-11 14:50:10 +01006140 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006141
Gilles Peskine449bd832023-01-11 14:50:10 +01006142 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006143
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6145 PSA_AEAD_NONCE_MAX_SIZE,
6146 &nonce_length),
6147 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006148
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006150
Paul Elliott7220cae2021-06-22 17:25:57 +01006151 /* Test for generating nonce in decrypt setup. */
6152
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006154
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6156 PSA_AEAD_NONCE_MAX_SIZE,
6157 &nonce_length),
6158 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006159
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006161
Paul Elliottc23a9a02021-06-21 18:32:46 +01006162 /* Test for setting lengths twice. */
6163
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006165
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006167
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6169 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006170
Gilles Peskine449bd832023-01-11 14:50:10 +01006171 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6172 input_data->len),
6173 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006174
Gilles Peskine449bd832023-01-11 14:50:10 +01006175 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006176
Andrzej Kurekad837522021-12-15 15:28:49 +01006177 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006178
Gilles Peskine449bd832023-01-11 14:50:10 +01006179 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006180
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006182
Gilles Peskine449bd832023-01-11 14:50:10 +01006183 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006184
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6186 additional_data->len),
6187 PSA_ERROR_BAD_STATE);
6188 } else {
6189 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6190 additional_data->len));
6191
6192 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6193 input_data->len),
6194 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006195 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006196 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006197
6198 /* ------------------------------------------------------- */
6199
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006201
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006203
Gilles Peskine449bd832023-01-11 14:50:10 +01006204 if (operation.alg == PSA_ALG_CCM) {
6205 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6206 input_data->len, output_data,
6207 output_size, &output_length),
6208 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006209
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 } else {
6211 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6212 input_data->len, output_data,
6213 output_size, &output_length));
6214
6215 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6216 input_data->len),
6217 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006218 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006220
6221 /* ------------------------------------------------------- */
6222
Gilles Peskine449bd832023-01-11 14:50:10 +01006223 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006224
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006226
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 if (operation.alg == PSA_ALG_CCM) {
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));
6233 } else {
6234 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6235 finish_output_size,
6236 &output_part_length,
6237 tag_buffer, tag_length,
6238 &tag_size));
6239
6240 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6241 input_data->len),
6242 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006243 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006245
6246 /* Test for setting lengths after generating nonce + already starting data. */
6247
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006249
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6251 PSA_AEAD_NONCE_MAX_SIZE,
6252 &nonce_length));
6253 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006254
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6256 additional_data->len),
6257 PSA_ERROR_BAD_STATE);
6258 } else {
6259 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6260 additional_data->len));
6261
6262 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6263 input_data->len),
6264 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006265 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006267
6268 /* ------------------------------------------------------- */
6269
Gilles Peskine449bd832023-01-11 14:50:10 +01006270 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006271
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6273 PSA_AEAD_NONCE_MAX_SIZE,
6274 &nonce_length));
6275 if (operation.alg == PSA_ALG_CCM) {
6276 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6277 input_data->len, output_data,
6278 output_size, &output_length),
6279 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006280
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 } else {
6282 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6283 input_data->len, output_data,
6284 output_size, &output_length));
6285
6286 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6287 input_data->len),
6288 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006289 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006290 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006291
6292 /* ------------------------------------------------------- */
6293
Gilles Peskine449bd832023-01-11 14:50:10 +01006294 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006295
Gilles Peskine449bd832023-01-11 14:50:10 +01006296 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6297 PSA_AEAD_NONCE_MAX_SIZE,
6298 &nonce_length));
6299 if (operation.alg == PSA_ALG_CCM) {
6300 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6301 finish_output_size,
6302 &output_part_length,
6303 tag_buffer, tag_length,
6304 &tag_size));
6305 } else {
6306 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6307 finish_output_size,
6308 &output_part_length,
6309 tag_buffer, tag_length,
6310 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006311
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6313 input_data->len),
6314 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006315 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006317
Paul Elliott243080c2021-07-21 19:01:17 +01006318 /* Test for not sending any additional data or data after setting non zero
6319 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006320
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006322
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006324
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6326 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006327
Gilles Peskine449bd832023-01-11 14:50:10 +01006328 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6329 finish_output_size,
6330 &output_part_length,
6331 tag_buffer, tag_length,
6332 &tag_size),
6333 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006334
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006336
Paul Elliott243080c2021-07-21 19:01:17 +01006337 /* Test for not sending any additional data or data after setting non-zero
6338 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006339
Gilles Peskine449bd832023-01-11 14:50:10 +01006340 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006341
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006343
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6345 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006346
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6348 finish_output_size,
6349 &output_part_length,
6350 tag_buffer,
6351 tag_length),
6352 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006353
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006355
Paul Elliott243080c2021-07-21 19:01:17 +01006356 /* Test for not sending any additional data after setting a non-zero length
6357 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006358
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006360
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006362
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6364 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006365
Gilles Peskine449bd832023-01-11 14:50:10 +01006366 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6367 input_data->len, output_data,
6368 output_size, &output_length),
6369 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006370
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006372
Paul Elliottf94bd992021-09-19 18:15:59 +01006373 /* Test for not sending any data after setting a non-zero length for it.*/
6374
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006376
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006378
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6380 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006381
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6383 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006384
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6386 finish_output_size,
6387 &output_part_length,
6388 tag_buffer, tag_length,
6389 &tag_size),
6390 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006391
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006393
Paul Elliottb0450fe2021-09-01 15:06:26 +01006394 /* Test for sending too much additional data after setting lengths. */
6395
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006397
Gilles Peskine449bd832023-01-11 14:50:10 +01006398 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006399
Gilles Peskine449bd832023-01-11 14:50:10 +01006400 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006401
6402
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6404 additional_data->len),
6405 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006406
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006408
Paul Elliotta2a09b02021-09-22 14:56:40 +01006409 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006410
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006412
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006414
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6416 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006417
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6419 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006420
Gilles Peskine449bd832023-01-11 14:50:10 +01006421 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6422 1),
6423 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006424
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006426
Paul Elliottb0450fe2021-09-01 15:06:26 +01006427 /* Test for sending too much data after setting lengths. */
6428
Gilles Peskine449bd832023-01-11 14:50:10 +01006429 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006430
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006432
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006434
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6436 input_data->len, output_data,
6437 output_size, &output_length),
6438 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006439
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006441
Paul Elliotta2a09b02021-09-22 14:56:40 +01006442 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006443
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006445
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006447
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6449 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006450
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6452 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006453
Gilles Peskine449bd832023-01-11 14:50:10 +01006454 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6455 input_data->len, output_data,
6456 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006457
Gilles Peskine449bd832023-01-11 14:50:10 +01006458 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6459 1, output_data,
6460 output_size, &output_length),
6461 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006462
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006464
Paul Elliottc23a9a02021-06-21 18:32:46 +01006465 /* Test sending additional data after data. */
6466
Gilles Peskine449bd832023-01-11 14:50:10 +01006467 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006468
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006470
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 if (operation.alg != PSA_ALG_CCM) {
6472 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6473 input_data->len, output_data,
6474 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006475
Gilles Peskine449bd832023-01-11 14:50:10 +01006476 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6477 additional_data->len),
6478 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006479 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006481
Paul Elliott534d0b42021-06-22 19:15:20 +01006482 /* Test calling finish on decryption. */
6483
Gilles Peskine449bd832023-01-11 14:50:10 +01006484 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006485
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006487
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6489 finish_output_size,
6490 &output_part_length,
6491 tag_buffer, tag_length,
6492 &tag_size),
6493 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006494
Gilles Peskine449bd832023-01-11 14:50:10 +01006495 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006496
6497 /* Test calling verify on encryption. */
6498
Gilles Peskine449bd832023-01-11 14:50:10 +01006499 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006500
Gilles Peskine449bd832023-01-11 14:50:10 +01006501 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006502
Gilles Peskine449bd832023-01-11 14:50:10 +01006503 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6504 finish_output_size,
6505 &output_part_length,
6506 tag_buffer,
6507 tag_length),
6508 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006509
Gilles Peskine449bd832023-01-11 14:50:10 +01006510 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006511
6512
Paul Elliottc23a9a02021-06-21 18:32:46 +01006513exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006514 psa_destroy_key(key);
6515 psa_aead_abort(&operation);
6516 mbedtls_free(output_data);
6517 mbedtls_free(final_data);
6518 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006519}
6520/* END_CASE */
6521
6522/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006523void signature_size(int type_arg,
6524 int bits,
6525 int alg_arg,
6526 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006527{
6528 psa_key_type_t type = type_arg;
6529 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006530 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006531
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006533
Gilles Peskinee59236f2018-01-27 23:32:46 +01006534exit:
6535 ;
6536}
6537/* END_CASE */
6538
6539/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006540void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6541 int alg_arg, data_t *input_data,
6542 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006543{
Ronald Cron5425a212020-08-04 14:58:35 +02006544 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006545 psa_key_type_t key_type = key_type_arg;
6546 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006547 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006548 unsigned char *signature = NULL;
6549 size_t signature_size;
6550 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006551 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006552
Gilles Peskine449bd832023-01-11 14:50:10 +01006553 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006554
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6556 psa_set_key_algorithm(&attributes, alg);
6557 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006558
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6560 &key));
6561 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6562 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006563
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006564 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006565 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006566 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6567 key_bits, alg);
6568 TEST_ASSERT(signature_size != 0);
6569 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006570 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006571
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006572 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006573 PSA_ASSERT(psa_sign_hash(key, alg,
6574 input_data->x, input_data->len,
6575 signature, signature_size,
6576 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006577 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006578 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006579 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006580
6581exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006582 /*
6583 * Key attributes may have been returned by psa_get_key_attributes()
6584 * thus reset them as required.
6585 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006587
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 psa_destroy_key(key);
6589 mbedtls_free(signature);
6590 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006591}
6592/* END_CASE */
6593
Paul Elliott712d5122022-12-07 14:03:10 +00006594/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006595/**
6596 * sign_hash_interruptible() test intentions:
6597 *
6598 * Note: This test can currently only handle ECDSA.
6599 *
6600 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006601 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006602 *
6603 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6604 * expected for different max_ops values.
6605 *
6606 * 3. Test that the number of ops done prior to start and after abort is zero
6607 * and that each successful stage completes some ops (this is not mandated by
6608 * the PSA specification, but is currently the case).
6609 *
6610 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6611 * complete() calls does not alter the number of ops returned.
6612 */
Paul Elliott712d5122022-12-07 14:03:10 +00006613void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6614 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006615 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006616{
6617 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6618 psa_key_type_t key_type = key_type_arg;
6619 psa_algorithm_t alg = alg_arg;
6620 size_t key_bits;
6621 unsigned char *signature = NULL;
6622 size_t signature_size;
6623 size_t signature_length = 0xdeadbeef;
6624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6625 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006626 uint32_t num_ops = 0;
6627 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006628 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006629 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006630 size_t min_completes = 0;
6631 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006632
Paul Elliott712d5122022-12-07 14:03:10 +00006633 psa_sign_hash_interruptible_operation_t operation =
6634 psa_sign_hash_interruptible_operation_init();
6635
6636 PSA_ASSERT(psa_crypto_init());
6637
6638 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6639 psa_set_key_algorithm(&attributes, alg);
6640 psa_set_key_type(&attributes, key_type);
6641
6642 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6643 &key));
6644 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6645 key_bits = psa_get_key_bits(&attributes);
6646
6647 /* Allocate a buffer which has the size advertised by the
6648 * library. */
6649 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6650 key_bits, alg);
6651 TEST_ASSERT(signature_size != 0);
6652 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006653 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006654
Paul Elliott0c683352022-12-16 19:16:56 +00006655 psa_interruptible_set_max_ops(max_ops);
6656
Paul Elliott6f600372023-02-06 18:41:05 +00006657 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6658 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006659
Paul Elliott712d5122022-12-07 14:03:10 +00006660 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6661 TEST_ASSERT(num_ops_prior == 0);
6662
6663 /* Start performing the signature. */
6664 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6665 input_data->x, input_data->len));
6666
6667 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6668 TEST_ASSERT(num_ops_prior == 0);
6669
6670 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006671 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006672 status = psa_sign_hash_complete(&operation, signature, signature_size,
6673 &signature_length);
6674
Paul Elliott0c683352022-12-16 19:16:56 +00006675 num_completes++;
6676
Paul Elliott712d5122022-12-07 14:03:10 +00006677 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6678 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006679 /* We are asserting here that every complete makes progress
6680 * (completes some ops), which is true of the internal
6681 * implementation and probably any implementation, however this is
6682 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006683 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006684
Paul Elliott712d5122022-12-07 14:03:10 +00006685 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006686
6687 /* Ensure calling get_num_ops() twice still returns the same
6688 * number of ops as previously reported. */
6689 num_ops = psa_sign_hash_get_num_ops(&operation);
6690
6691 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006692 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006693 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006694
6695 TEST_ASSERT(status == PSA_SUCCESS);
6696
Paul Elliott0c683352022-12-16 19:16:56 +00006697 TEST_LE_U(min_completes, num_completes);
6698 TEST_LE_U(num_completes, max_completes);
6699
Paul Elliott712d5122022-12-07 14:03:10 +00006700 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006701 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006702 signature, signature_length);
Paul Elliott712d5122022-12-07 14:03:10 +00006703
6704 PSA_ASSERT(psa_sign_hash_abort(&operation));
6705
Paul Elliott59ad9452022-12-18 15:09:02 +00006706 num_ops = psa_sign_hash_get_num_ops(&operation);
6707 TEST_ASSERT(num_ops == 0);
6708
Paul Elliott712d5122022-12-07 14:03:10 +00006709exit:
6710
6711 /*
6712 * Key attributes may have been returned by psa_get_key_attributes()
6713 * thus reset them as required.
6714 */
6715 psa_reset_key_attributes(&attributes);
6716
6717 psa_destroy_key(key);
6718 mbedtls_free(signature);
6719 PSA_DONE();
6720}
6721/* END_CASE */
6722
Gilles Peskine20035e32018-02-03 22:44:14 +01006723/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006724void sign_hash_fail(int key_type_arg, data_t *key_data,
6725 int alg_arg, data_t *input_data,
6726 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006727{
Ronald Cron5425a212020-08-04 14:58:35 +02006728 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006729 psa_key_type_t key_type = key_type_arg;
6730 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006731 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006732 psa_status_t actual_status;
6733 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006734 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006735 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006736 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006737
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006738 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006739
Gilles Peskine449bd832023-01-11 14:50:10 +01006740 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006741
Gilles Peskine449bd832023-01-11 14:50:10 +01006742 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6743 psa_set_key_algorithm(&attributes, alg);
6744 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006745
Gilles Peskine449bd832023-01-11 14:50:10 +01006746 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6747 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006748
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 actual_status = psa_sign_hash(key, alg,
6750 input_data->x, input_data->len,
6751 signature, signature_size,
6752 &signature_length);
6753 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006754 /* The value of *signature_length is unspecified on error, but
6755 * whatever it is, it should be less than signature_size, so that
6756 * if the caller tries to read *signature_length bytes without
6757 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006758 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006759
6760exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006761 psa_reset_key_attributes(&attributes);
6762 psa_destroy_key(key);
6763 mbedtls_free(signature);
6764 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006765}
6766/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006767
Paul Elliott91007972022-12-16 12:21:24 +00006768/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006769/**
6770 * sign_hash_fail_interruptible() test intentions:
6771 *
6772 * Note: This test can currently only handle ECDSA.
6773 *
6774 * 1. Test that various failure cases for interruptible sign hash fail with the
6775 * correct error codes, and at the correct point (at start or during
6776 * complete).
6777 *
6778 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6779 * expected for different max_ops values.
6780 *
6781 * 3. Test that the number of ops done prior to start and after abort is zero
6782 * and that each successful stage completes some ops (this is not mandated by
6783 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006784 *
6785 * 4. Check that calling complete() when start() fails and complete()
6786 * after completion results in a BAD_STATE error.
6787 *
6788 * 5. Check that calling start() again after start fails results in a BAD_STATE
6789 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006790 */
Paul Elliott91007972022-12-16 12:21:24 +00006791void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6792 int alg_arg, data_t *input_data,
6793 int signature_size_arg,
6794 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006795 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006796 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006797{
6798 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6799 psa_key_type_t key_type = key_type_arg;
6800 psa_algorithm_t alg = alg_arg;
6801 size_t signature_size = signature_size_arg;
6802 psa_status_t actual_status;
6803 psa_status_t expected_start_status = expected_start_status_arg;
6804 psa_status_t expected_complete_status = expected_complete_status_arg;
6805 unsigned char *signature = NULL;
6806 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006807 uint32_t num_ops = 0;
6808 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006809 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006810 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006811 size_t min_completes = 0;
6812 size_t max_completes = 0;
6813
Paul Elliott91007972022-12-16 12:21:24 +00006814 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6815 psa_sign_hash_interruptible_operation_t operation =
6816 psa_sign_hash_interruptible_operation_init();
6817
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006818 TEST_CALLOC(signature, signature_size);
Paul Elliott91007972022-12-16 12:21:24 +00006819
6820 PSA_ASSERT(psa_crypto_init());
6821
6822 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6823 psa_set_key_algorithm(&attributes, alg);
6824 psa_set_key_type(&attributes, key_type);
6825
6826 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6827 &key));
6828
Paul Elliott0c683352022-12-16 19:16:56 +00006829 psa_interruptible_set_max_ops(max_ops);
6830
Paul Elliott6f600372023-02-06 18:41:05 +00006831 interruptible_signverify_get_minmax_completes(max_ops,
6832 expected_complete_status,
6833 &min_completes,
6834 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006835
Paul Elliott91007972022-12-16 12:21:24 +00006836 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6837 TEST_ASSERT(num_ops_prior == 0);
6838
6839 /* Start performing the signature. */
6840 actual_status = psa_sign_hash_start(&operation, key, alg,
6841 input_data->x, input_data->len);
6842
6843 TEST_EQUAL(actual_status, expected_start_status);
6844
Paul Elliottc9774412023-02-06 15:14:07 +00006845 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006846 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006847 * start failed. */
6848 actual_status = psa_sign_hash_complete(&operation, signature,
6849 signature_size,
6850 &signature_length);
6851
6852 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6853
6854 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006855 actual_status = psa_sign_hash_start(&operation, key, alg,
6856 input_data->x, input_data->len);
6857
6858 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6859 }
6860
Paul Elliott91007972022-12-16 12:21:24 +00006861 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6862 TEST_ASSERT(num_ops_prior == 0);
6863
Paul Elliott91007972022-12-16 12:21:24 +00006864 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006865 do {
Paul Elliott91007972022-12-16 12:21:24 +00006866 actual_status = psa_sign_hash_complete(&operation, signature,
6867 signature_size,
6868 &signature_length);
6869
Paul Elliott0c683352022-12-16 19:16:56 +00006870 num_completes++;
6871
Paul Elliott334d7262023-01-20 17:29:41 +00006872 if (actual_status == PSA_SUCCESS ||
6873 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006874 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006875 /* We are asserting here that every complete makes progress
6876 * (completes some ops), which is true of the internal
6877 * implementation and probably any implementation, however this is
6878 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006879 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006880
Paul Elliott91007972022-12-16 12:21:24 +00006881 num_ops_prior = num_ops;
6882 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006883 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006884
Paul Elliottc9774412023-02-06 15:14:07 +00006885 TEST_EQUAL(actual_status, expected_complete_status);
6886
Paul Elliottefebad02023-02-15 16:56:45 +00006887 /* Check that another complete returns BAD_STATE. */
6888 actual_status = psa_sign_hash_complete(&operation, signature,
6889 signature_size,
6890 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006891
Paul Elliottefebad02023-02-15 16:56:45 +00006892 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006893
Paul Elliott91007972022-12-16 12:21:24 +00006894 PSA_ASSERT(psa_sign_hash_abort(&operation));
6895
Paul Elliott59ad9452022-12-18 15:09:02 +00006896 num_ops = psa_sign_hash_get_num_ops(&operation);
6897 TEST_ASSERT(num_ops == 0);
6898
Paul Elliott91007972022-12-16 12:21:24 +00006899 /* The value of *signature_length is unspecified on error, but
6900 * whatever it is, it should be less than signature_size, so that
6901 * if the caller tries to read *signature_length bytes without
6902 * checking the error code then they don't overflow a buffer. */
6903 TEST_LE_U(signature_length, signature_size);
6904
Paul Elliott0c683352022-12-16 19:16:56 +00006905 TEST_LE_U(min_completes, num_completes);
6906 TEST_LE_U(num_completes, max_completes);
6907
Paul Elliott91007972022-12-16 12:21:24 +00006908exit:
6909 psa_reset_key_attributes(&attributes);
6910 psa_destroy_key(key);
6911 mbedtls_free(signature);
6912 PSA_DONE();
6913}
6914/* END_CASE */
6915
mohammad16038cc1cee2018-03-28 01:21:33 +03006916/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006917void sign_verify_hash(int key_type_arg, data_t *key_data,
6918 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006919{
Ronald Cron5425a212020-08-04 14:58:35 +02006920 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006921 psa_key_type_t key_type = key_type_arg;
6922 psa_algorithm_t alg = alg_arg;
6923 size_t key_bits;
6924 unsigned char *signature = NULL;
6925 size_t signature_size;
6926 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006927 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006928
Gilles Peskine449bd832023-01-11 14:50:10 +01006929 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006930
Gilles Peskine449bd832023-01-11 14:50:10 +01006931 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6932 psa_set_key_algorithm(&attributes, alg);
6933 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006934
Gilles Peskine449bd832023-01-11 14:50:10 +01006935 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6936 &key));
6937 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6938 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006939
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006940 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006941 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006942 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6943 key_bits, alg);
6944 TEST_ASSERT(signature_size != 0);
6945 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006946 TEST_CALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006947
6948 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006949 PSA_ASSERT(psa_sign_hash(key, alg,
6950 input_data->x, input_data->len,
6951 signature, signature_size,
6952 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006953 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006954 TEST_LE_U(signature_length, signature_size);
6955 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006956
6957 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006958 PSA_ASSERT(psa_verify_hash(key, alg,
6959 input_data->x, input_data->len,
6960 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006961
Gilles Peskine449bd832023-01-11 14:50:10 +01006962 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006963 /* Flip a bit in the input and verify that the signature is now
6964 * detected as invalid. Flip a bit at the beginning, not at the end,
6965 * because ECDSA may ignore the last few bits of the input. */
6966 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006967 TEST_EQUAL(psa_verify_hash(key, alg,
6968 input_data->x, input_data->len,
6969 signature, signature_length),
6970 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006971 }
6972
6973exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006974 /*
6975 * Key attributes may have been returned by psa_get_key_attributes()
6976 * thus reset them as required.
6977 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006978 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006979
Gilles Peskine449bd832023-01-11 14:50:10 +01006980 psa_destroy_key(key);
6981 mbedtls_free(signature);
6982 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006983}
6984/* END_CASE */
6985
Paul Elliott712d5122022-12-07 14:03:10 +00006986/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006987/**
6988 * sign_verify_hash_interruptible() test intentions:
6989 *
6990 * Note: This test can currently only handle ECDSA.
6991 *
Paul Elliott8c092052023-03-06 17:49:14 +00006992 * 1. Test that we can sign an input hash with the given keypair and then
6993 * afterwards verify that signature. This is currently the only way to test
6994 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00006995 *
6996 * 2. Test that after corrupting the hash, the verification detects an invalid
6997 * signature.
6998 *
6999 * 3. Test the number of calls to psa_sign_hash_complete() required are as
7000 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00007001 *
7002 * 4. Test that the number of ops done prior to starting signing and after abort
7003 * is zero and that each successful signing stage completes some ops (this is
7004 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00007005 */
Paul Elliott712d5122022-12-07 14:03:10 +00007006void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00007007 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007008 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007009{
7010 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7011 psa_key_type_t key_type = key_type_arg;
7012 psa_algorithm_t alg = alg_arg;
7013 size_t key_bits;
7014 unsigned char *signature = NULL;
7015 size_t signature_size;
7016 size_t signature_length = 0xdeadbeef;
7017 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7018 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007019 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00007020 uint32_t num_ops = 0;
7021 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007022 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007023 size_t min_completes = 0;
7024 size_t max_completes = 0;
7025
Paul Elliott712d5122022-12-07 14:03:10 +00007026 psa_sign_hash_interruptible_operation_t sign_operation =
7027 psa_sign_hash_interruptible_operation_init();
7028 psa_verify_hash_interruptible_operation_t verify_operation =
7029 psa_verify_hash_interruptible_operation_init();
7030
7031 PSA_ASSERT(psa_crypto_init());
7032
Paul Elliott0c683352022-12-16 19:16:56 +00007033 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7034 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00007035 psa_set_key_algorithm(&attributes, alg);
7036 psa_set_key_type(&attributes, key_type);
7037
7038 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7039 &key));
7040 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7041 key_bits = psa_get_key_bits(&attributes);
7042
7043 /* Allocate a buffer which has the size advertised by the
7044 * library. */
7045 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7046 key_bits, alg);
7047 TEST_ASSERT(signature_size != 0);
7048 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007049 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00007050
Paul Elliott0c683352022-12-16 19:16:56 +00007051 psa_interruptible_set_max_ops(max_ops);
7052
Paul Elliott6f600372023-02-06 18:41:05 +00007053 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7054 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007055
Paul Elliott7c173082023-02-26 18:44:45 +00007056 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
7057 TEST_ASSERT(num_ops_prior == 0);
7058
Paul Elliott712d5122022-12-07 14:03:10 +00007059 /* Start performing the signature. */
7060 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7061 input_data->x, input_data->len));
7062
Paul Elliott7c173082023-02-26 18:44:45 +00007063 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
7064 TEST_ASSERT(num_ops_prior == 0);
7065
Paul Elliott712d5122022-12-07 14:03:10 +00007066 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007067 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007068
Paul Elliott0c683352022-12-16 19:16:56 +00007069 status = psa_sign_hash_complete(&sign_operation, signature,
7070 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00007071 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00007072
7073 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00007074
7075 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7076 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7077 /* We are asserting here that every complete makes progress
7078 * (completes some ops), which is true of the internal
7079 * implementation and probably any implementation, however this is
7080 * not mandated by the PSA specification. */
7081 TEST_ASSERT(num_ops > num_ops_prior);
7082
7083 num_ops_prior = num_ops;
7084 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007085 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007086
7087 TEST_ASSERT(status == PSA_SUCCESS);
7088
Paul Elliott0c683352022-12-16 19:16:56 +00007089 TEST_LE_U(min_completes, num_completes);
7090 TEST_LE_U(num_completes, max_completes);
7091
Paul Elliott712d5122022-12-07 14:03:10 +00007092 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7093
Paul Elliott7c173082023-02-26 18:44:45 +00007094 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7095 TEST_ASSERT(num_ops == 0);
7096
Paul Elliott712d5122022-12-07 14:03:10 +00007097 /* Check that the signature length looks sensible. */
7098 TEST_LE_U(signature_length, signature_size);
7099 TEST_ASSERT(signature_length > 0);
7100
Paul Elliott0c683352022-12-16 19:16:56 +00007101 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007102
7103 /* Start verification. */
7104 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7105 input_data->x, input_data->len,
7106 signature, signature_length));
7107
7108 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007109 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007110 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007111
7112 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007113 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007114
7115 TEST_ASSERT(status == PSA_SUCCESS);
7116
Paul Elliott0c683352022-12-16 19:16:56 +00007117 TEST_LE_U(min_completes, num_completes);
7118 TEST_LE_U(num_completes, max_completes);
7119
Paul Elliott712d5122022-12-07 14:03:10 +00007120 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7121
7122 verify_operation = psa_verify_hash_interruptible_operation_init();
7123
7124 if (input_data->len != 0) {
7125 /* Flip a bit in the input and verify that the signature is now
7126 * detected as invalid. Flip a bit at the beginning, not at the end,
7127 * because ECDSA may ignore the last few bits of the input. */
7128 input_data->x[0] ^= 1;
7129
Paul Elliott712d5122022-12-07 14:03:10 +00007130 /* Start verification. */
7131 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7132 input_data->x, input_data->len,
7133 signature, signature_length));
7134
7135 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007136 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007137 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007138 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007139
7140 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7141 }
7142
7143 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7144
7145exit:
7146 /*
7147 * Key attributes may have been returned by psa_get_key_attributes()
7148 * thus reset them as required.
7149 */
7150 psa_reset_key_attributes(&attributes);
7151
7152 psa_destroy_key(key);
7153 mbedtls_free(signature);
7154 PSA_DONE();
7155}
7156/* END_CASE */
7157
Gilles Peskine9911b022018-06-29 17:30:48 +02007158/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007159void verify_hash(int key_type_arg, data_t *key_data,
7160 int alg_arg, data_t *hash_data,
7161 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007162{
Ronald Cron5425a212020-08-04 14:58:35 +02007163 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007164 psa_key_type_t key_type = key_type_arg;
7165 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007166 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007167
Gilles Peskine449bd832023-01-11 14:50:10 +01007168 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007169
Gilles Peskine449bd832023-01-11 14:50:10 +01007170 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007171
Gilles Peskine449bd832023-01-11 14:50:10 +01007172 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7173 psa_set_key_algorithm(&attributes, alg);
7174 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007175
Gilles Peskine449bd832023-01-11 14:50:10 +01007176 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7177 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007178
Gilles Peskine449bd832023-01-11 14:50:10 +01007179 PSA_ASSERT(psa_verify_hash(key, alg,
7180 hash_data->x, hash_data->len,
7181 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007182
itayzafrir5c753392018-05-08 11:18:38 +03007183exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007184 psa_reset_key_attributes(&attributes);
7185 psa_destroy_key(key);
7186 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007187}
7188/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007189
Paul Elliott712d5122022-12-07 14:03:10 +00007190/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007191/**
7192 * verify_hash_interruptible() test intentions:
7193 *
7194 * Note: This test can currently only handle ECDSA.
7195 *
7196 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007197 * only). Given this test only does verification it can accept public keys as
7198 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007199 *
7200 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7201 * expected for different max_ops values.
7202 *
7203 * 3. Test that the number of ops done prior to start and after abort is zero
7204 * and that each successful stage completes some ops (this is not mandated by
7205 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007206 *
7207 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7208 * complete() calls does not alter the number of ops returned.
7209 *
7210 * 5. Test that after corrupting the hash, the verification detects an invalid
7211 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007212 */
Paul Elliott712d5122022-12-07 14:03:10 +00007213void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7214 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007215 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007216{
7217 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7218 psa_key_type_t key_type = key_type_arg;
7219 psa_algorithm_t alg = alg_arg;
7220 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7221 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007222 uint32_t num_ops = 0;
7223 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007224 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007225 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007226 size_t min_completes = 0;
7227 size_t max_completes = 0;
7228
Paul Elliott712d5122022-12-07 14:03:10 +00007229 psa_verify_hash_interruptible_operation_t operation =
7230 psa_verify_hash_interruptible_operation_init();
7231
7232 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7233
7234 PSA_ASSERT(psa_crypto_init());
7235
7236 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7237 psa_set_key_algorithm(&attributes, alg);
7238 psa_set_key_type(&attributes, key_type);
7239
7240 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7241 &key));
7242
Paul Elliott0c683352022-12-16 19:16:56 +00007243 psa_interruptible_set_max_ops(max_ops);
7244
Paul Elliott6f600372023-02-06 18:41:05 +00007245 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7246 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007247
Paul Elliott712d5122022-12-07 14:03:10 +00007248 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7249
7250 TEST_ASSERT(num_ops_prior == 0);
7251
7252 /* Start verification. */
7253 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7254 hash_data->x, hash_data->len,
7255 signature_data->x, signature_data->len)
7256 );
7257
7258 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7259
7260 TEST_ASSERT(num_ops_prior == 0);
7261
7262 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007263 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007264 status = psa_verify_hash_complete(&operation);
7265
Paul Elliott0c683352022-12-16 19:16:56 +00007266 num_completes++;
7267
Paul Elliott712d5122022-12-07 14:03:10 +00007268 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7269 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007270 /* We are asserting here that every complete makes progress
7271 * (completes some ops), which is true of the internal
7272 * implementation and probably any implementation, however this is
7273 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007274 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007275
Paul Elliott712d5122022-12-07 14:03:10 +00007276 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007277
7278 /* Ensure calling get_num_ops() twice still returns the same
7279 * number of ops as previously reported. */
7280 num_ops = psa_verify_hash_get_num_ops(&operation);
7281
7282 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007283 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007284 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007285
7286 TEST_ASSERT(status == PSA_SUCCESS);
7287
Paul Elliott0c683352022-12-16 19:16:56 +00007288 TEST_LE_U(min_completes, num_completes);
7289 TEST_LE_U(num_completes, max_completes);
7290
Paul Elliott712d5122022-12-07 14:03:10 +00007291 PSA_ASSERT(psa_verify_hash_abort(&operation));
7292
Paul Elliott59ad9452022-12-18 15:09:02 +00007293 num_ops = psa_verify_hash_get_num_ops(&operation);
7294 TEST_ASSERT(num_ops == 0);
7295
Paul Elliott8359c142023-02-24 18:40:10 +00007296 if (hash_data->len != 0) {
7297 /* Flip a bit in the hash and verify that the signature is now detected
7298 * as invalid. Flip a bit at the beginning, not at the end, because
7299 * ECDSA may ignore the last few bits of the input. */
7300 hash_data->x[0] ^= 1;
7301
7302 /* Start verification. */
7303 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7304 hash_data->x, hash_data->len,
7305 signature_data->x, signature_data->len));
7306
7307 /* Continue performing the signature until complete. */
7308 do {
7309 status = psa_verify_hash_complete(&operation);
7310 } while (status == PSA_OPERATION_INCOMPLETE);
7311
7312 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7313 }
7314
Paul Elliott712d5122022-12-07 14:03:10 +00007315exit:
7316 psa_reset_key_attributes(&attributes);
7317 psa_destroy_key(key);
7318 PSA_DONE();
7319}
7320/* END_CASE */
7321
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007322/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007323void verify_hash_fail(int key_type_arg, data_t *key_data,
7324 int alg_arg, data_t *hash_data,
7325 data_t *signature_data,
7326 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007327{
Ronald Cron5425a212020-08-04 14:58:35 +02007328 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007329 psa_key_type_t key_type = key_type_arg;
7330 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007331 psa_status_t actual_status;
7332 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007333 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007334
Gilles Peskine449bd832023-01-11 14:50:10 +01007335 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007336
Gilles Peskine449bd832023-01-11 14:50:10 +01007337 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7338 psa_set_key_algorithm(&attributes, alg);
7339 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007340
Gilles Peskine449bd832023-01-11 14:50:10 +01007341 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7342 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007343
Gilles Peskine449bd832023-01-11 14:50:10 +01007344 actual_status = psa_verify_hash(key, alg,
7345 hash_data->x, hash_data->len,
7346 signature_data->x, signature_data->len);
7347 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007348
7349exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007350 psa_reset_key_attributes(&attributes);
7351 psa_destroy_key(key);
7352 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007353}
7354/* END_CASE */
7355
Paul Elliott91007972022-12-16 12:21:24 +00007356/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007357/**
7358 * verify_hash_fail_interruptible() test intentions:
7359 *
7360 * Note: This test can currently only handle ECDSA.
7361 *
7362 * 1. Test that various failure cases for interruptible verify hash fail with
7363 * the correct error codes, and at the correct point (at start or during
7364 * complete).
7365 *
7366 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7367 * expected for different max_ops values.
7368 *
7369 * 3. Test that the number of ops done prior to start and after abort is zero
7370 * and that each successful stage completes some ops (this is not mandated by
7371 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007372 *
7373 * 4. Check that calling complete() when start() fails and complete()
7374 * after completion results in a BAD_STATE error.
7375 *
7376 * 5. Check that calling start() again after start fails results in a BAD_STATE
7377 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007378 */
Paul Elliott91007972022-12-16 12:21:24 +00007379void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7380 int alg_arg, data_t *hash_data,
7381 data_t *signature_data,
7382 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007383 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007384 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007385{
7386 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7387 psa_key_type_t key_type = key_type_arg;
7388 psa_algorithm_t alg = alg_arg;
7389 psa_status_t actual_status;
7390 psa_status_t expected_start_status = expected_start_status_arg;
7391 psa_status_t expected_complete_status = expected_complete_status_arg;
7392 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007393 uint32_t num_ops = 0;
7394 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007395 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007396 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007397 size_t min_completes = 0;
7398 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007399 psa_verify_hash_interruptible_operation_t operation =
7400 psa_verify_hash_interruptible_operation_init();
7401
7402 PSA_ASSERT(psa_crypto_init());
7403
7404 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7405 psa_set_key_algorithm(&attributes, alg);
7406 psa_set_key_type(&attributes, key_type);
7407
7408 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7409 &key));
7410
Paul Elliott0c683352022-12-16 19:16:56 +00007411 psa_interruptible_set_max_ops(max_ops);
7412
Paul Elliott6f600372023-02-06 18:41:05 +00007413 interruptible_signverify_get_minmax_completes(max_ops,
7414 expected_complete_status,
7415 &min_completes,
7416 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007417
Paul Elliott91007972022-12-16 12:21:24 +00007418 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7419 TEST_ASSERT(num_ops_prior == 0);
7420
7421 /* Start verification. */
7422 actual_status = psa_verify_hash_start(&operation, key, alg,
7423 hash_data->x, hash_data->len,
7424 signature_data->x,
7425 signature_data->len);
7426
7427 TEST_EQUAL(actual_status, expected_start_status);
7428
Paul Elliottc9774412023-02-06 15:14:07 +00007429 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007430 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007431 * start failed. */
7432 actual_status = psa_verify_hash_complete(&operation);
7433
7434 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7435
7436 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007437 actual_status = psa_verify_hash_start(&operation, key, alg,
7438 hash_data->x, hash_data->len,
7439 signature_data->x,
7440 signature_data->len);
7441
7442 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7443 }
7444
Paul Elliott91007972022-12-16 12:21:24 +00007445 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7446 TEST_ASSERT(num_ops_prior == 0);
7447
Paul Elliott91007972022-12-16 12:21:24 +00007448 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007449 do {
Paul Elliott91007972022-12-16 12:21:24 +00007450 actual_status = psa_verify_hash_complete(&operation);
7451
Paul Elliott0c683352022-12-16 19:16:56 +00007452 num_completes++;
7453
Paul Elliott334d7262023-01-20 17:29:41 +00007454 if (actual_status == PSA_SUCCESS ||
7455 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007456 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007457 /* We are asserting here that every complete makes progress
7458 * (completes some ops), which is true of the internal
7459 * implementation and probably any implementation, however this is
7460 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007461 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007462
Paul Elliott91007972022-12-16 12:21:24 +00007463 num_ops_prior = num_ops;
7464 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007465 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007466
Paul Elliottc9774412023-02-06 15:14:07 +00007467 TEST_EQUAL(actual_status, expected_complete_status);
7468
Paul Elliottefebad02023-02-15 16:56:45 +00007469 /* Check that another complete returns BAD_STATE. */
7470 actual_status = psa_verify_hash_complete(&operation);
7471 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007472
Paul Elliott0c683352022-12-16 19:16:56 +00007473 TEST_LE_U(min_completes, num_completes);
7474 TEST_LE_U(num_completes, max_completes);
7475
Paul Elliott91007972022-12-16 12:21:24 +00007476 PSA_ASSERT(psa_verify_hash_abort(&operation));
7477
Paul Elliott59ad9452022-12-18 15:09:02 +00007478 num_ops = psa_verify_hash_get_num_ops(&operation);
7479 TEST_ASSERT(num_ops == 0);
7480
Paul Elliott91007972022-12-16 12:21:24 +00007481exit:
7482 psa_reset_key_attributes(&attributes);
7483 psa_destroy_key(key);
7484 PSA_DONE();
7485}
7486/* END_CASE */
7487
Paul Elliott20a36062022-12-18 13:21:25 +00007488/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007489/**
7490 * interruptible_signverify_hash_state_test() test intentions:
7491 *
7492 * Note: This test can currently only handle ECDSA.
7493 *
7494 * 1. Test that calling the various interruptible sign and verify hash functions
7495 * in incorrect orders returns BAD_STATE errors.
7496 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007497void interruptible_signverify_hash_state_test(int key_type_arg,
7498 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007499{
7500 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7501 psa_key_type_t key_type = key_type_arg;
7502 psa_algorithm_t alg = alg_arg;
7503 size_t key_bits;
7504 unsigned char *signature = NULL;
7505 size_t signature_size;
7506 size_t signature_length = 0xdeadbeef;
7507 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7508 psa_sign_hash_interruptible_operation_t sign_operation =
7509 psa_sign_hash_interruptible_operation_init();
7510 psa_verify_hash_interruptible_operation_t verify_operation =
7511 psa_verify_hash_interruptible_operation_init();
7512
7513 PSA_ASSERT(psa_crypto_init());
7514
7515 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7516 PSA_KEY_USAGE_VERIFY_HASH);
7517 psa_set_key_algorithm(&attributes, alg);
7518 psa_set_key_type(&attributes, key_type);
7519
7520 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7521 &key));
7522 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7523 key_bits = psa_get_key_bits(&attributes);
7524
7525 /* Allocate a buffer which has the size advertised by the
7526 * library. */
7527 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7528 key_bits, alg);
7529 TEST_ASSERT(signature_size != 0);
7530 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007531 TEST_CALLOC(signature, signature_size);
Paul Elliott20a36062022-12-18 13:21:25 +00007532
7533 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7534
7535 /* --- Attempt completes prior to starts --- */
7536 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7537 signature_size,
7538 &signature_length),
7539 PSA_ERROR_BAD_STATE);
7540
7541 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7542
7543 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7544 PSA_ERROR_BAD_STATE);
7545
7546 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7547
7548 /* --- Aborts in all other places. --- */
7549 psa_sign_hash_abort(&sign_operation);
7550
7551 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7552 input_data->x, input_data->len));
7553
7554 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7555
7556 psa_interruptible_set_max_ops(1);
7557
7558 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7559 input_data->x, input_data->len));
7560
7561 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7562 signature_size,
7563 &signature_length),
7564 PSA_OPERATION_INCOMPLETE);
7565
7566 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7567
7568 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7569
7570 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7571 input_data->x, input_data->len));
7572
7573 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7574 signature_size,
7575 &signature_length));
7576
7577 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7578
7579 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7580
7581 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7582 input_data->x, input_data->len,
7583 signature, signature_length));
7584
7585 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7586
7587 psa_interruptible_set_max_ops(1);
7588
7589 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7590 input_data->x, input_data->len,
7591 signature, signature_length));
7592
7593 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7594 PSA_OPERATION_INCOMPLETE);
7595
7596 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7597
7598 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7599
7600 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7601 input_data->x, input_data->len,
7602 signature, signature_length));
7603
7604 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7605
7606 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7607
7608 /* --- Attempt double starts. --- */
7609
7610 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7611 input_data->x, input_data->len));
7612
7613 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7614 input_data->x, input_data->len),
7615 PSA_ERROR_BAD_STATE);
7616
7617 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7618
7619 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7620 input_data->x, input_data->len,
7621 signature, signature_length));
7622
7623 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7624 input_data->x, input_data->len,
7625 signature, signature_length),
7626 PSA_ERROR_BAD_STATE);
7627
7628 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7629
Paul Elliott76d671a2023-02-07 17:45:18 +00007630exit:
7631 /*
7632 * Key attributes may have been returned by psa_get_key_attributes()
7633 * thus reset them as required.
7634 */
7635 psa_reset_key_attributes(&attributes);
7636
7637 psa_destroy_key(key);
7638 mbedtls_free(signature);
7639 PSA_DONE();
7640}
7641/* END_CASE */
7642
7643/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007644/**
Paul Elliottc2033502023-02-26 17:09:14 +00007645 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007646 *
7647 * Note: This test can currently only handle ECDSA.
7648 *
7649 * 1. Test various edge cases in the interruptible sign and verify hash
7650 * interfaces.
7651 */
Paul Elliottc2033502023-02-26 17:09:14 +00007652void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007653 data_t *key_data, int alg_arg, data_t *input_data)
7654{
7655 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7656 psa_key_type_t key_type = key_type_arg;
7657 psa_algorithm_t alg = alg_arg;
7658 size_t key_bits;
7659 unsigned char *signature = NULL;
7660 size_t signature_size;
7661 size_t signature_length = 0xdeadbeef;
7662 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7663 uint8_t *input_buffer = NULL;
7664 psa_sign_hash_interruptible_operation_t sign_operation =
7665 psa_sign_hash_interruptible_operation_init();
7666 psa_verify_hash_interruptible_operation_t verify_operation =
7667 psa_verify_hash_interruptible_operation_init();
7668
7669 PSA_ASSERT(psa_crypto_init());
7670
7671 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7672 PSA_KEY_USAGE_VERIFY_HASH);
7673 psa_set_key_algorithm(&attributes, alg);
7674 psa_set_key_type(&attributes, key_type);
7675
7676 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7677 &key));
7678 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7679 key_bits = psa_get_key_bits(&attributes);
7680
7681 /* Allocate a buffer which has the size advertised by the
7682 * library. */
7683 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7684 key_bits, alg);
7685 TEST_ASSERT(signature_size != 0);
7686 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007687 TEST_CALLOC(signature, signature_size);
Paul Elliott76d671a2023-02-07 17:45:18 +00007688
Paul Elliott20a36062022-12-18 13:21:25 +00007689 /* --- Change function inputs mid run, to cause an error (sign only,
7690 * verify passes all inputs to start. --- */
7691
7692 psa_interruptible_set_max_ops(1);
7693
7694 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7695 input_data->x, input_data->len));
7696
7697 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7698 signature_size,
7699 &signature_length),
7700 PSA_OPERATION_INCOMPLETE);
7701
7702 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7703 0,
7704 &signature_length),
7705 PSA_ERROR_BUFFER_TOO_SMALL);
7706
Paul Elliottc9774412023-02-06 15:14:07 +00007707 /* And test that this invalidates the operation. */
7708 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7709 0,
7710 &signature_length),
7711 PSA_ERROR_BAD_STATE);
7712
Paul Elliott20a36062022-12-18 13:21:25 +00007713 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7714
Paul Elliottf9c91a72023-02-05 18:06:38 +00007715 /* Trash the hash buffer in between start and complete, to ensure
7716 * no reliance on external buffers. */
7717 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7718
Paul Elliott6c68df42023-10-23 15:33:37 +01007719 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007720
7721 memcpy(input_buffer, input_data->x, input_data->len);
7722
7723 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7724 input_buffer, input_data->len));
7725
7726 memset(input_buffer, '!', input_data->len);
7727 mbedtls_free(input_buffer);
7728 input_buffer = NULL;
7729
7730 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7731 signature_size,
7732 &signature_length));
7733
7734 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7735
Paul Elliott6c68df42023-10-23 15:33:37 +01007736 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007737
7738 memcpy(input_buffer, input_data->x, input_data->len);
7739
7740 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7741 input_buffer, input_data->len,
7742 signature, signature_length));
7743
7744 memset(input_buffer, '!', input_data->len);
7745 mbedtls_free(input_buffer);
7746 input_buffer = NULL;
7747
7748 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7749
7750 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7751
Paul Elliott20a36062022-12-18 13:21:25 +00007752exit:
7753 /*
7754 * Key attributes may have been returned by psa_get_key_attributes()
7755 * thus reset them as required.
7756 */
7757 psa_reset_key_attributes(&attributes);
7758
7759 psa_destroy_key(key);
7760 mbedtls_free(signature);
Paul Elliott6c68df42023-10-23 15:33:37 +01007761 mbedtls_free(input_buffer);
Paul Elliott20a36062022-12-18 13:21:25 +00007762 PSA_DONE();
7763}
7764/* END_CASE */
7765
Paul Elliotta4cb9092023-02-07 18:01:55 +00007766/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007767/**
Paul Elliott57702242023-02-26 20:36:10 +00007768 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007769 *
7770 * Note: This test can currently only handle ECDSA.
7771 *
7772 * 1. Test that setting max ops is reflected in both interruptible sign and
7773 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007774 * 2. Test that changing the value of max_ops to unlimited during an operation
7775 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007776 *
7777 * 3. Test that calling get_num_ops() between complete calls gives the same
7778 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007779 */
Paul Elliott57702242023-02-26 20:36:10 +00007780void interruptible_signverify_hash_ops_tests(int key_type_arg,
7781 data_t *key_data, int alg_arg,
7782 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007783{
7784 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7785 psa_key_type_t key_type = key_type_arg;
7786 psa_algorithm_t alg = alg_arg;
7787 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007788 size_t key_bits;
7789 unsigned char *signature = NULL;
7790 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007791 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007792 uint32_t num_ops = 0;
7793 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7794
Paul Elliotta4cb9092023-02-07 18:01:55 +00007795 psa_sign_hash_interruptible_operation_t sign_operation =
7796 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007797 psa_verify_hash_interruptible_operation_t verify_operation =
7798 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007799
7800 PSA_ASSERT(psa_crypto_init());
7801
7802 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7803 PSA_KEY_USAGE_VERIFY_HASH);
7804 psa_set_key_algorithm(&attributes, alg);
7805 psa_set_key_type(&attributes, key_type);
7806
Paul Elliottf1743e22023-02-15 18:44:16 +00007807 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7808 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7809 key_bits = psa_get_key_bits(&attributes);
7810
7811 /* Allocate a buffer which has the size advertised by the
7812 * library. */
7813 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7814
7815 TEST_ASSERT(signature_size != 0);
7816 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007817 TEST_CALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007818
7819 /* Check that default max ops gets set if we don't set it. */
7820 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7821 input_data->x, input_data->len));
7822
7823 TEST_EQUAL(psa_interruptible_get_max_ops(),
7824 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7825
7826 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7827
Paul Elliottf1743e22023-02-15 18:44:16 +00007828 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7829 input_data->x, input_data->len,
7830 signature, signature_size));
7831
7832 TEST_EQUAL(psa_interruptible_get_max_ops(),
7833 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7834
7835 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7836
Paul Elliotta4cb9092023-02-07 18:01:55 +00007837 /* Check that max ops gets set properly. */
7838
7839 psa_interruptible_set_max_ops(0xbeef);
7840
Paul Elliottf1743e22023-02-15 18:44:16 +00007841 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007842
Paul Elliott9e8819f2023-02-26 19:01:35 +00007843 /* --- Ensure changing the max ops mid operation works (operation should
7844 * complete successfully after setting max ops to unlimited --- */
7845 psa_interruptible_set_max_ops(1);
7846
7847 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7848 input_data->x, input_data->len));
7849
7850 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7851 signature_size,
7852 &signature_length),
7853 PSA_OPERATION_INCOMPLETE);
7854
7855 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7856
7857 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7858 signature_size,
7859 &signature_length));
7860
7861 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7862
7863 psa_interruptible_set_max_ops(1);
7864
7865 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7866 input_data->x, input_data->len,
7867 signature, signature_length));
7868
7869 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7870 PSA_OPERATION_INCOMPLETE);
7871
7872 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7873
7874 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7875
7876 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7877
Paul Elliottc1e04002023-02-26 20:27:23 +00007878 /* --- Test that not calling get_num_ops inbetween complete calls does not
7879 * result in lost ops. ---*/
7880
7881 psa_interruptible_set_max_ops(1);
7882
7883 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7884 input_data->x, input_data->len));
7885
7886 /* Continue performing the signature until complete. */
7887 do {
7888 status = psa_sign_hash_complete(&sign_operation, signature,
7889 signature_size,
7890 &signature_length);
7891
7892 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7893
7894 } while (status == PSA_OPERATION_INCOMPLETE);
7895
7896 PSA_ASSERT(status);
7897
7898 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7899
7900 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7901 input_data->x, input_data->len));
7902
7903 /* Continue performing the signature until complete. */
7904 do {
7905 status = psa_sign_hash_complete(&sign_operation, signature,
7906 signature_size,
7907 &signature_length);
7908 } while (status == PSA_OPERATION_INCOMPLETE);
7909
7910 PSA_ASSERT(status);
7911
7912 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7913
7914 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7915
7916 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7917 input_data->x, input_data->len,
7918 signature, signature_length));
7919
7920 /* Continue performing the verification until complete. */
7921 do {
7922 status = psa_verify_hash_complete(&verify_operation);
7923
7924 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7925
7926 } while (status == PSA_OPERATION_INCOMPLETE);
7927
7928 PSA_ASSERT(status);
7929
7930 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7931
7932 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7933 input_data->x, input_data->len,
7934 signature, signature_length));
7935
7936 /* Continue performing the verification until complete. */
7937 do {
7938 status = psa_verify_hash_complete(&verify_operation);
7939
7940 } while (status == PSA_OPERATION_INCOMPLETE);
7941
7942 PSA_ASSERT(status);
7943
7944 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7945
7946 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7947
Paul Elliotta4cb9092023-02-07 18:01:55 +00007948exit:
7949 /*
7950 * Key attributes may have been returned by psa_get_key_attributes()
7951 * thus reset them as required.
7952 */
7953 psa_reset_key_attributes(&attributes);
7954
7955 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007956 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007957 PSA_DONE();
7958}
7959/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007960
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007961/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007962void sign_message_deterministic(int key_type_arg,
7963 data_t *key_data,
7964 int alg_arg,
7965 data_t *input_data,
7966 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007967{
7968 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7969 psa_key_type_t key_type = key_type_arg;
7970 psa_algorithm_t alg = alg_arg;
7971 size_t key_bits;
7972 unsigned char *signature = NULL;
7973 size_t signature_size;
7974 size_t signature_length = 0xdeadbeef;
7975 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7976
Gilles Peskine449bd832023-01-11 14:50:10 +01007977 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007978
Gilles Peskine449bd832023-01-11 14:50:10 +01007979 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7980 psa_set_key_algorithm(&attributes, alg);
7981 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007982
Gilles Peskine449bd832023-01-11 14:50:10 +01007983 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7984 &key));
7985 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7986 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007987
Gilles Peskine449bd832023-01-11 14:50:10 +01007988 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7989 TEST_ASSERT(signature_size != 0);
7990 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007991 TEST_CALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007992
Gilles Peskine449bd832023-01-11 14:50:10 +01007993 PSA_ASSERT(psa_sign_message(key, alg,
7994 input_data->x, input_data->len,
7995 signature, signature_size,
7996 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007997
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01007998 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01007999 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008000
8001exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008002 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008003
Gilles Peskine449bd832023-01-11 14:50:10 +01008004 psa_destroy_key(key);
8005 mbedtls_free(signature);
8006 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02008007
8008}
8009/* END_CASE */
8010
8011/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008012void sign_message_fail(int key_type_arg,
8013 data_t *key_data,
8014 int alg_arg,
8015 data_t *input_data,
8016 int signature_size_arg,
8017 int expected_status_arg)
8018{
8019 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8020 psa_key_type_t key_type = key_type_arg;
8021 psa_algorithm_t alg = alg_arg;
8022 size_t signature_size = signature_size_arg;
8023 psa_status_t actual_status;
8024 psa_status_t expected_status = expected_status_arg;
8025 unsigned char *signature = NULL;
8026 size_t signature_length = 0xdeadbeef;
8027 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8028
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008029 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008030
8031 PSA_ASSERT(psa_crypto_init());
8032
8033 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8034 psa_set_key_algorithm(&attributes, alg);
8035 psa_set_key_type(&attributes, key_type);
8036
8037 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8038 &key));
8039
8040 actual_status = psa_sign_message(key, alg,
8041 input_data->x, input_data->len,
8042 signature, signature_size,
8043 &signature_length);
8044 TEST_EQUAL(actual_status, expected_status);
8045 /* The value of *signature_length is unspecified on error, but
8046 * whatever it is, it should be less than signature_size, so that
8047 * if the caller tries to read *signature_length bytes without
8048 * checking the error code then they don't overflow a buffer. */
8049 TEST_LE_U(signature_length, signature_size);
8050
8051exit:
8052 psa_reset_key_attributes(&attributes);
8053 psa_destroy_key(key);
8054 mbedtls_free(signature);
8055 PSA_DONE();
8056}
8057/* END_CASE */
8058
8059/* BEGIN_CASE */
8060void sign_verify_message(int key_type_arg,
8061 data_t *key_data,
8062 int alg_arg,
8063 data_t *input_data)
8064{
8065 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8066 psa_key_type_t key_type = key_type_arg;
8067 psa_algorithm_t alg = alg_arg;
8068 size_t key_bits;
8069 unsigned char *signature = NULL;
8070 size_t signature_size;
8071 size_t signature_length = 0xdeadbeef;
8072 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8073
8074 PSA_ASSERT(psa_crypto_init());
8075
8076 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
8077 PSA_KEY_USAGE_VERIFY_MESSAGE);
8078 psa_set_key_algorithm(&attributes, alg);
8079 psa_set_key_type(&attributes, key_type);
8080
8081 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8082 &key));
8083 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8084 key_bits = psa_get_key_bits(&attributes);
8085
8086 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8087 TEST_ASSERT(signature_size != 0);
8088 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008089 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008090
8091 PSA_ASSERT(psa_sign_message(key, alg,
8092 input_data->x, input_data->len,
8093 signature, signature_size,
8094 &signature_length));
8095 TEST_LE_U(signature_length, signature_size);
8096 TEST_ASSERT(signature_length > 0);
8097
8098 PSA_ASSERT(psa_verify_message(key, alg,
8099 input_data->x, input_data->len,
8100 signature, signature_length));
8101
8102 if (input_data->len != 0) {
8103 /* Flip a bit in the input and verify that the signature is now
8104 * detected as invalid. Flip a bit at the beginning, not at the end,
8105 * because ECDSA may ignore the last few bits of the input. */
8106 input_data->x[0] ^= 1;
8107 TEST_EQUAL(psa_verify_message(key, alg,
8108 input_data->x, input_data->len,
8109 signature, signature_length),
8110 PSA_ERROR_INVALID_SIGNATURE);
8111 }
8112
8113exit:
8114 psa_reset_key_attributes(&attributes);
8115
8116 psa_destroy_key(key);
8117 mbedtls_free(signature);
8118 PSA_DONE();
8119}
8120/* END_CASE */
8121
8122/* BEGIN_CASE */
8123void verify_message(int key_type_arg,
8124 data_t *key_data,
8125 int alg_arg,
8126 data_t *input_data,
8127 data_t *signature_data)
8128{
8129 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8130 psa_key_type_t key_type = key_type_arg;
8131 psa_algorithm_t alg = alg_arg;
8132 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8133
8134 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8135
8136 PSA_ASSERT(psa_crypto_init());
8137
8138 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8139 psa_set_key_algorithm(&attributes, alg);
8140 psa_set_key_type(&attributes, key_type);
8141
8142 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8143 &key));
8144
8145 PSA_ASSERT(psa_verify_message(key, alg,
8146 input_data->x, input_data->len,
8147 signature_data->x, signature_data->len));
8148
8149exit:
8150 psa_reset_key_attributes(&attributes);
8151 psa_destroy_key(key);
8152 PSA_DONE();
8153}
8154/* END_CASE */
8155
8156/* BEGIN_CASE */
8157void verify_message_fail(int key_type_arg,
8158 data_t *key_data,
8159 int alg_arg,
8160 data_t *hash_data,
8161 data_t *signature_data,
8162 int expected_status_arg)
8163{
8164 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8165 psa_key_type_t key_type = key_type_arg;
8166 psa_algorithm_t alg = alg_arg;
8167 psa_status_t actual_status;
8168 psa_status_t expected_status = expected_status_arg;
8169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8170
8171 PSA_ASSERT(psa_crypto_init());
8172
8173 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8174 psa_set_key_algorithm(&attributes, alg);
8175 psa_set_key_type(&attributes, key_type);
8176
8177 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8178 &key));
8179
8180 actual_status = psa_verify_message(key, alg,
8181 hash_data->x, hash_data->len,
8182 signature_data->x,
8183 signature_data->len);
8184 TEST_EQUAL(actual_status, expected_status);
8185
8186exit:
8187 psa_reset_key_attributes(&attributes);
8188 psa_destroy_key(key);
8189 PSA_DONE();
8190}
8191/* END_CASE */
8192
8193/* BEGIN_CASE */
8194void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008195 data_t *key_data,
8196 int alg_arg,
8197 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008198 data_t *label,
8199 int expected_output_length_arg,
8200 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008201{
Ronald Cron5425a212020-08-04 14:58:35 +02008202 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008203 psa_key_type_t key_type = key_type_arg;
8204 psa_algorithm_t alg = alg_arg;
8205 size_t expected_output_length = expected_output_length_arg;
8206 size_t key_bits;
8207 unsigned char *output = NULL;
8208 size_t output_size;
8209 size_t output_length = ~0;
8210 psa_status_t actual_status;
8211 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008213
Gilles Peskine449bd832023-01-11 14:50:10 +01008214 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008215
Gilles Peskine656896e2018-06-29 19:12:28 +02008216 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008217 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8218 psa_set_key_algorithm(&attributes, alg);
8219 psa_set_key_type(&attributes, key_type);
8220 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8221 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008222
8223 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008224 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8225 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008226
Gilles Peskine449bd832023-01-11 14:50:10 +01008227 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8228 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008229 TEST_CALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008230
8231 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008232 actual_status = psa_asymmetric_encrypt(key, alg,
8233 input_data->x, input_data->len,
8234 label->x, label->len,
8235 output, output_size,
8236 &output_length);
8237 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008238 if (actual_status == PSA_SUCCESS) {
8239 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008240 } else {
8241 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008242 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008243
Gilles Peskine68428122018-06-30 18:42:41 +02008244 /* If the label is empty, the test framework puts a non-null pointer
8245 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008246 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008247 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008248 if (output_size != 0) {
8249 memset(output, 0, output_size);
8250 }
8251 actual_status = psa_asymmetric_encrypt(key, alg,
8252 input_data->x, input_data->len,
8253 NULL, label->len,
8254 output, output_size,
8255 &output_length);
8256 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008257 if (actual_status == PSA_SUCCESS) {
8258 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008259 } else {
8260 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008261 }
Gilles Peskine68428122018-06-30 18:42:41 +02008262 }
8263
Gilles Peskine656896e2018-06-29 19:12:28 +02008264exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008265 /*
8266 * Key attributes may have been returned by psa_get_key_attributes()
8267 * thus reset them as required.
8268 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008269 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008270
Gilles Peskine449bd832023-01-11 14:50:10 +01008271 psa_destroy_key(key);
8272 mbedtls_free(output);
8273 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008274}
8275/* END_CASE */
8276
8277/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008278void asymmetric_encrypt_decrypt(int key_type_arg,
8279 data_t *key_data,
8280 int alg_arg,
8281 data_t *input_data,
8282 data_t *label)
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;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008287 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008288 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008289 size_t output_size;
8290 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008291 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008292 size_t output2_size;
8293 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008294 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008295
Gilles Peskine449bd832023-01-11 14:50:10 +01008296 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008297
Gilles Peskine449bd832023-01-11 14:50:10 +01008298 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8299 psa_set_key_algorithm(&attributes, alg);
8300 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008301
Gilles Peskine449bd832023-01-11 14:50:10 +01008302 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8303 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008304
8305 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008306 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8307 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008308
Gilles Peskine449bd832023-01-11 14:50:10 +01008309 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8310 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008311 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008312
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008313 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008314 TEST_LE_U(output2_size,
8315 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8316 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008317 TEST_CALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008318
Gilles Peskineeebd7382018-06-08 18:11:54 +02008319 /* We test encryption by checking that encrypt-then-decrypt gives back
8320 * the original plaintext because of the non-optional random
8321 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008322 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8323 input_data->x, input_data->len,
8324 label->x, label->len,
8325 output, output_size,
8326 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008327 /* We don't know what ciphertext length to expect, but check that
8328 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008329 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008330
Gilles Peskine449bd832023-01-11 14:50:10 +01008331 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8332 output, output_length,
8333 label->x, label->len,
8334 output2, output2_size,
8335 &output2_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008336 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008337 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008338
8339exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008340 /*
8341 * Key attributes may have been returned by psa_get_key_attributes()
8342 * thus reset them as required.
8343 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008344 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008345
Gilles Peskine449bd832023-01-11 14:50:10 +01008346 psa_destroy_key(key);
8347 mbedtls_free(output);
8348 mbedtls_free(output2);
8349 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008350}
8351/* END_CASE */
8352
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008353/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008354void asymmetric_decrypt(int key_type_arg,
8355 data_t *key_data,
8356 int alg_arg,
8357 data_t *input_data,
8358 data_t *label,
8359 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008360{
Ronald Cron5425a212020-08-04 14:58:35 +02008361 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008362 psa_key_type_t key_type = key_type_arg;
8363 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008364 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008365 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008366 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008367 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008368 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008369
Gilles Peskine449bd832023-01-11 14:50:10 +01008370 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008371
Gilles Peskine449bd832023-01-11 14:50:10 +01008372 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8373 psa_set_key_algorithm(&attributes, alg);
8374 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008375
Gilles Peskine449bd832023-01-11 14:50:10 +01008376 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8377 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008378
Gilles Peskine449bd832023-01-11 14:50:10 +01008379 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8380 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008381
8382 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008383 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8384 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008385 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008386
Gilles Peskine449bd832023-01-11 14:50:10 +01008387 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8388 input_data->x, input_data->len,
8389 label->x, label->len,
8390 output,
8391 output_size,
8392 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008393 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008394 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008395
Gilles Peskine68428122018-06-30 18:42:41 +02008396 /* If the label is empty, the test framework puts a non-null pointer
8397 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008398 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008399 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008400 if (output_size != 0) {
8401 memset(output, 0, output_size);
8402 }
8403 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8404 input_data->x, input_data->len,
8405 NULL, label->len,
8406 output,
8407 output_size,
8408 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008409 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008410 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008411 }
8412
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008413exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008414 psa_reset_key_attributes(&attributes);
8415 psa_destroy_key(key);
8416 mbedtls_free(output);
8417 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008418}
8419/* END_CASE */
8420
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008421/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008422void asymmetric_decrypt_fail(int key_type_arg,
8423 data_t *key_data,
8424 int alg_arg,
8425 data_t *input_data,
8426 data_t *label,
8427 int output_size_arg,
8428 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008429{
Ronald Cron5425a212020-08-04 14:58:35 +02008430 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008431 psa_key_type_t key_type = key_type_arg;
8432 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008433 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008434 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008435 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008436 psa_status_t actual_status;
8437 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008438 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008439
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008440 TEST_CALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008441
Gilles Peskine449bd832023-01-11 14:50:10 +01008442 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008443
Gilles Peskine449bd832023-01-11 14:50:10 +01008444 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8445 psa_set_key_algorithm(&attributes, alg);
8446 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008447
Gilles Peskine449bd832023-01-11 14:50:10 +01008448 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8449 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008450
Gilles Peskine449bd832023-01-11 14:50:10 +01008451 actual_status = psa_asymmetric_decrypt(key, alg,
8452 input_data->x, input_data->len,
8453 label->x, label->len,
8454 output, output_size,
8455 &output_length);
8456 TEST_EQUAL(actual_status, expected_status);
8457 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008458
Gilles Peskine68428122018-06-30 18:42:41 +02008459 /* If the label is empty, the test framework puts a non-null pointer
8460 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008461 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008462 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008463 if (output_size != 0) {
8464 memset(output, 0, output_size);
8465 }
8466 actual_status = psa_asymmetric_decrypt(key, alg,
8467 input_data->x, input_data->len,
8468 NULL, label->len,
8469 output, output_size,
8470 &output_length);
8471 TEST_EQUAL(actual_status, expected_status);
8472 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008473 }
8474
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008475exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008476 psa_reset_key_attributes(&attributes);
8477 psa_destroy_key(key);
8478 mbedtls_free(output);
8479 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008480}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008481/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008482
8483/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008484void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008485{
8486 /* Test each valid way of initializing the object, except for `= {0}`, as
8487 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8488 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008489 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008490 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008491 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008492 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8493 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008494
Gilles Peskine449bd832023-01-11 14:50:10 +01008495 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008496
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008497 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008498 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8499 PSA_ERROR_BAD_STATE);
8500 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8501 PSA_ERROR_BAD_STATE);
8502 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8503 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008504
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008505 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008506 PSA_ASSERT(psa_key_derivation_abort(&func));
8507 PSA_ASSERT(psa_key_derivation_abort(&init));
8508 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008509}
8510/* END_CASE */
8511
Janos Follath16de4a42019-06-13 16:32:24 +01008512/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008513void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008514{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008515 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008516 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008517 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008518
Gilles Peskine449bd832023-01-11 14:50:10 +01008519 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008520
Gilles Peskine449bd832023-01-11 14:50:10 +01008521 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8522 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008523
8524exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008525 psa_key_derivation_abort(&operation);
8526 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008527}
8528/* END_CASE */
8529
Janos Follathaf3c2a02019-06-12 12:34:34 +01008530/* BEGIN_CASE */
Kusumit Ghoderao9ffd3972023-12-01 16:40:13 +05308531void derive_set_capacity(int alg_arg, int64_t capacity_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01008532 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008533{
8534 psa_algorithm_t alg = alg_arg;
8535 size_t capacity = capacity_arg;
8536 psa_status_t expected_status = expected_status_arg;
8537 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8538
Gilles Peskine449bd832023-01-11 14:50:10 +01008539 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008540
Gilles Peskine449bd832023-01-11 14:50:10 +01008541 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008542
Gilles Peskine449bd832023-01-11 14:50:10 +01008543 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8544 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008545
8546exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008547 psa_key_derivation_abort(&operation);
8548 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008549}
8550/* END_CASE */
8551
8552/* BEGIN_CASE */
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308553void parse_binary_string_test(data_t *input, int output)
8554{
8555 uint64_t value;
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308556 value = mbedtls_test_parse_binary_string(input);
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308557 TEST_EQUAL(value, output);
8558}
8559/* END_CASE */
8560
8561/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008562void derive_input(int alg_arg,
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308563 int step_arg1, int key_type_arg1, data_t *input1,
8564 int expected_status_arg1,
8565 int step_arg2, int key_type_arg2, data_t *input2,
8566 int expected_status_arg2,
8567 int step_arg3, int key_type_arg3, data_t *input3,
8568 int expected_status_arg3,
Gilles Peskine449bd832023-01-11 14:50:10 +01008569 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008570{
8571 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008572 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308573 uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
Gilles Peskine449bd832023-01-11 14:50:10 +01008574 psa_status_t expected_statuses[] = { expected_status_arg1,
8575 expected_status_arg2,
8576 expected_status_arg3 };
8577 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008578 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8579 MBEDTLS_SVC_KEY_ID_INIT,
8580 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008581 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8582 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8583 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008584 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008585 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008586 psa_status_t expected_output_status = expected_output_status_arg;
8587 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008588
Gilles Peskine449bd832023-01-11 14:50:10 +01008589 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008590
Gilles Peskine449bd832023-01-11 14:50:10 +01008591 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8592 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008593
Gilles Peskine449bd832023-01-11 14:50:10 +01008594 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008595
Gilles Peskine449bd832023-01-11 14:50:10 +01008596 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8597 mbedtls_test_set_step(i);
8598 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008599 /* Skip this step */
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308600 } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE &&
8601 key_types[i] != INPUT_INTEGER) {
8602 psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i]));
Gilles Peskine449bd832023-01-11 14:50:10 +01008603 PSA_ASSERT(psa_import_key(&attributes,
8604 inputs[i]->x, inputs[i]->len,
8605 &keys[i]));
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308606 if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008607 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008608 // When taking a private key as secret input, use key agreement
8609 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008610 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8611 &operation, keys[i]),
8612 expected_statuses[i]);
8613 } else {
8614 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8615 keys[i]),
8616 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008617 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008618 } else {
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308619 if (key_types[i] == INPUT_INTEGER) {
8620 TEST_EQUAL(psa_key_derivation_input_integer(
8621 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308622 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308623 expected_statuses[i]);
8624 } else {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308625 TEST_EQUAL(psa_key_derivation_input_bytes(
8626 &operation, steps[i],
8627 inputs[i]->x, inputs[i]->len),
8628 expected_statuses[i]);
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308629 }
Janos Follathaf3c2a02019-06-12 12:34:34 +01008630 }
8631 }
8632
Gilles Peskine449bd832023-01-11 14:50:10 +01008633 if (output_key_type != PSA_KEY_TYPE_NONE) {
8634 psa_reset_key_attributes(&attributes);
8635 psa_set_key_type(&attributes, output_key_type);
8636 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008637 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008638 psa_key_derivation_output_key(&attributes, &operation,
8639 &output_key);
8640 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008641 uint8_t buffer[1];
8642 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008643 psa_key_derivation_output_bytes(&operation,
8644 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008645 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008646 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008647
Janos Follathaf3c2a02019-06-12 12:34:34 +01008648exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008649 psa_key_derivation_abort(&operation);
8650 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8651 psa_destroy_key(keys[i]);
8652 }
8653 psa_destroy_key(output_key);
8654 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008655}
8656/* END_CASE */
8657
Kusumit Ghoderao42b02b92023-06-06 16:48:46 +05308658/* BEGIN_CASE*/
8659void derive_input_invalid_cost(int alg_arg, int64_t cost)
8660{
8661 psa_algorithm_t alg = alg_arg;
8662 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8663
8664 PSA_ASSERT(psa_crypto_init());
8665 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8666
8667 TEST_EQUAL(psa_key_derivation_input_integer(&operation,
8668 PSA_KEY_DERIVATION_INPUT_COST,
8669 cost),
8670 PSA_ERROR_NOT_SUPPORTED);
8671
8672exit:
8673 psa_key_derivation_abort(&operation);
8674 PSA_DONE();
8675}
8676/* END_CASE*/
8677
Janos Follathd958bb72019-07-03 15:02:16 +01008678/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008679void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008680{
Janos Follathd958bb72019-07-03 15:02:16 +01008681 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008682 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008683 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008684 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008685 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008686 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008687 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008688 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008689 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008690 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008691 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8692 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008693 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008694 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008695
Gilles Peskine449bd832023-01-11 14:50:10 +01008696 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008697
Gilles Peskine449bd832023-01-11 14:50:10 +01008698 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8699 psa_set_key_algorithm(&attributes, alg);
8700 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008701
Gilles Peskine449bd832023-01-11 14:50:10 +01008702 PSA_ASSERT(psa_import_key(&attributes,
8703 key_data, sizeof(key_data),
8704 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008705
8706 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008707 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8708 input1, input1_length,
8709 input2, input2_length,
8710 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008711 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008712 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008713
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008714 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008715 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8716 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008717
Gilles Peskine449bd832023-01-11 14:50:10 +01008718 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008719
Gilles Peskine449bd832023-01-11 14:50:10 +01008720 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8721 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008722
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008723exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008724 psa_key_derivation_abort(&operation);
8725 psa_destroy_key(key);
8726 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008727}
8728/* END_CASE */
8729
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008730/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008731void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008732{
8733 uint8_t output_buffer[16];
8734 size_t buffer_size = 16;
8735 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008736 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008737
Gilles Peskine449bd832023-01-11 14:50:10 +01008738 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8739 output_buffer, buffer_size)
8740 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008741
Gilles Peskine449bd832023-01-11 14:50:10 +01008742 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8743 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008744
Gilles Peskine449bd832023-01-11 14:50:10 +01008745 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008746
Gilles Peskine449bd832023-01-11 14:50:10 +01008747 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8748 output_buffer, buffer_size)
8749 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008750
Gilles Peskine449bd832023-01-11 14:50:10 +01008751 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8752 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008753
8754exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008755 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008756}
8757/* END_CASE */
8758
8759/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008760void derive_output(int alg_arg,
8761 int step1_arg, data_t *input1, int expected_status_arg1,
8762 int step2_arg, data_t *input2, int expected_status_arg2,
8763 int step3_arg, data_t *input3, int expected_status_arg3,
8764 int step4_arg, data_t *input4, int expected_status_arg4,
8765 data_t *key_agreement_peer_key,
8766 int requested_capacity_arg,
8767 data_t *expected_output1,
8768 data_t *expected_output2,
8769 int other_key_input_type,
8770 int key_input_type,
8771 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008772{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008773 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008774 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8775 data_t *inputs[] = { input1, input2, input3, input4 };
8776 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8777 MBEDTLS_SVC_KEY_ID_INIT,
8778 MBEDTLS_SVC_KEY_ID_INIT,
8779 MBEDTLS_SVC_KEY_ID_INIT };
8780 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8781 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008782 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008783 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008784 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008785 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008786 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008787 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008788 size_t output_buffer_size = 0;
8789 uint8_t *output_buffer = NULL;
8790 size_t expected_capacity;
8791 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008792 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8793 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8794 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8795 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008796 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008797 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008798 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008799
Gilles Peskine449bd832023-01-11 14:50:10 +01008800 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8801 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008802 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008803 }
8804 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008805 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008806 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008807 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008808 TEST_CALLOC(output_buffer, output_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008809 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008810
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008811 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008812 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8813 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8814 requested_capacity));
8815 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8816 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008817 case 0:
8818 break;
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308819 case PSA_KEY_DERIVATION_INPUT_COST:
8820 TEST_EQUAL(psa_key_derivation_input_integer(
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308821 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308822 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308823 statuses[i]);
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308824 if (statuses[i] != PSA_SUCCESS) {
8825 goto exit;
8826 }
8827 break;
8828 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Gilles Peskine1468da72019-05-29 17:35:49 +02008829 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008830 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008831 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008832 TEST_EQUAL(psa_key_derivation_input_bytes(
8833 &operation, steps[i],
8834 inputs[i]->x, inputs[i]->len),
8835 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008836
Gilles Peskine449bd832023-01-11 14:50:10 +01008837 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008838 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008839 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008840 break;
8841 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008842 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8843 psa_set_key_algorithm(&attributes1, alg);
8844 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008845
Gilles Peskine449bd832023-01-11 14:50:10 +01008846 PSA_ASSERT(psa_import_key(&attributes1,
8847 inputs[i]->x, inputs[i]->len,
8848 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008849
Gilles Peskine449bd832023-01-11 14:50:10 +01008850 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8851 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8852 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8853 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008854 }
8855
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308856 TEST_EQUAL(psa_key_derivation_input_key(&operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01008857 steps[i],
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308858 keys[i]),
8859 statuses[i]);
8860
8861 if (statuses[i] != PSA_SUCCESS) {
8862 goto exit;
8863 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008864 break;
8865 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008866 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008867 break;
8868 }
8869 break;
8870 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008871 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008872 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008873 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8874 steps[i],
8875 inputs[i]->x,
8876 inputs[i]->len),
8877 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008878 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008879 case 1: // input key, type DERIVE
8880 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008881 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8882 psa_set_key_algorithm(&attributes2, alg);
8883 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008884
8885 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008886 if (other_key_input_type == 11) {
8887 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8888 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008889
Gilles Peskine449bd832023-01-11 14:50:10 +01008890 PSA_ASSERT(psa_import_key(&attributes2,
8891 inputs[i]->x, inputs[i]->len,
8892 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008893
Gilles Peskine449bd832023-01-11 14:50:10 +01008894 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8895 steps[i],
8896 keys[i]),
8897 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008898 break;
8899 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008900 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8901 psa_set_key_algorithm(&attributes3, alg);
8902 psa_set_key_type(&attributes3,
8903 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008904
Gilles Peskine449bd832023-01-11 14:50:10 +01008905 PSA_ASSERT(psa_import_key(&attributes3,
8906 inputs[i]->x, inputs[i]->len,
8907 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008908
Gilles Peskine449bd832023-01-11 14:50:10 +01008909 TEST_EQUAL(psa_key_derivation_key_agreement(
8910 &operation,
8911 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8912 keys[i], key_agreement_peer_key->x,
8913 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008914 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008915 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008916 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008917 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008918 }
8919
Gilles Peskine449bd832023-01-11 14:50:10 +01008920 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008921 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008922 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008923 break;
8924 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008925 TEST_EQUAL(psa_key_derivation_input_bytes(
8926 &operation, steps[i],
8927 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008928
Gilles Peskine449bd832023-01-11 14:50:10 +01008929 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008930 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008931 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008932 break;
8933 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008934 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008935
Gilles Peskine449bd832023-01-11 14:50:10 +01008936 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8937 &current_capacity));
8938 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008939 expected_capacity = requested_capacity;
8940
Gilles Peskine449bd832023-01-11 14:50:10 +01008941 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008942 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8943
8944 /* For output key derivation secret must be provided using
8945 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008946 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008947 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008948 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008949
Gilles Peskine449bd832023-01-11 14:50:10 +01008950 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8951 psa_set_key_algorithm(&attributes4, alg);
8952 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8953 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008954
Gilles Peskine449bd832023-01-11 14:50:10 +01008955 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8956 &derived_key), expected_status);
8957 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008958 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008959 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008960 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008961 status = psa_key_derivation_output_bytes(&operation,
8962 output_buffer, output_sizes[i]);
8963 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008964 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008965 TEST_ASSERT(status == PSA_SUCCESS ||
8966 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008967 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008968 } else if (expected_capacity == 0 ||
8969 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008970 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008971 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008972 expected_capacity = 0;
8973 continue;
8974 }
8975 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008976 PSA_ASSERT(status);
8977 if (output_sizes[i] != 0) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008978 TEST_MEMORY_COMPARE(output_buffer, output_sizes[i],
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008979 expected_outputs[i], output_sizes[i]);
Gilles Peskine449bd832023-01-11 14:50:10 +01008980 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008981 /* Check the operation status. */
8982 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008983 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8984 &current_capacity));
8985 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008986 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008987 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008988 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008989
8990exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008991 mbedtls_free(output_buffer);
8992 psa_key_derivation_abort(&operation);
8993 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8994 psa_destroy_key(keys[i]);
8995 }
8996 psa_destroy_key(derived_key);
8997 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008998}
8999/* END_CASE */
9000
9001/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009002void derive_full(int alg_arg,
9003 data_t *key_data,
9004 data_t *input1,
9005 data_t *input2,
9006 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02009007{
Ronald Cron5425a212020-08-04 14:58:35 +02009008 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02009009 psa_algorithm_t alg = alg_arg;
9010 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009011 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Kusumit Ghoderao9ffd3972023-12-01 16:40:13 +05309012 unsigned char output_buffer[32];
Gilles Peskined54931c2018-07-17 21:06:59 +02009013 size_t expected_capacity = requested_capacity;
9014 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009015 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02009016
Gilles Peskine449bd832023-01-11 14:50:10 +01009017 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02009018
Gilles Peskine449bd832023-01-11 14:50:10 +01009019 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9020 psa_set_key_algorithm(&attributes, alg);
9021 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02009022
Gilles Peskine449bd832023-01-11 14:50:10 +01009023 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9024 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02009025
Gilles Peskine449bd832023-01-11 14:50:10 +01009026 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
9027 input1->x, input1->len,
9028 input2->x, input2->len,
9029 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01009030 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009031 }
Janos Follath47f27ed2019-06-25 13:24:52 +01009032
Gilles Peskine449bd832023-01-11 14:50:10 +01009033 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9034 &current_capacity));
9035 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02009036
9037 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009038 while (current_capacity > 0) {
9039 size_t read_size = sizeof(output_buffer);
9040 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02009041 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01009042 }
9043 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9044 output_buffer,
9045 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02009046 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01009047 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9048 &current_capacity));
9049 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02009050 }
9051
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009052 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009053 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
9054 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02009055
Gilles Peskine449bd832023-01-11 14:50:10 +01009056 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02009057
9058exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009059 psa_key_derivation_abort(&operation);
9060 psa_destroy_key(key);
9061 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02009062}
9063/* END_CASE */
9064
Stephan Koch78109f52023-04-12 14:19:36 +02009065/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01009066void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
9067 int derivation_step,
9068 int capacity, int expected_capacity_status_arg,
9069 data_t *expected_output,
9070 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009071{
9072 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
9073 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04009074 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009075 uint8_t *output_buffer = NULL;
9076 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04009077 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
9078 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
9079 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009080
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009081 TEST_CALLOC(output_buffer, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009082 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009083
Gilles Peskine449bd832023-01-11 14:50:10 +01009084 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9085 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
9086 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009087
Gilles Peskine449bd832023-01-11 14:50:10 +01009088 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
9089 step, input->x, input->len),
9090 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009091
Gilles Peskine449bd832023-01-11 14:50:10 +01009092 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009093 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009094 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009095
Gilles Peskine449bd832023-01-11 14:50:10 +01009096 status = psa_key_derivation_output_bytes(&operation, output_buffer,
9097 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009098
Gilles Peskine449bd832023-01-11 14:50:10 +01009099 TEST_EQUAL(status, expected_output_status);
9100 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009101 TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009102 expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009103 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009104
9105exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009106 mbedtls_free(output_buffer);
9107 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009108 PSA_DONE();
9109}
9110/* END_CASE */
9111
Janos Follathe60c9052019-07-03 13:51:30 +01009112/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009113void derive_key_exercise(int alg_arg,
9114 data_t *key_data,
9115 data_t *input1,
9116 data_t *input2,
9117 int derived_type_arg,
9118 int derived_bits_arg,
9119 int derived_usage_arg,
9120 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009121{
Ronald Cron5425a212020-08-04 14:58:35 +02009122 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9123 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009124 psa_algorithm_t alg = alg_arg;
9125 psa_key_type_t derived_type = derived_type_arg;
9126 size_t derived_bits = derived_bits_arg;
9127 psa_key_usage_t derived_usage = derived_usage_arg;
9128 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009129 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009130 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009131 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009132 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009133
Gilles Peskine449bd832023-01-11 14:50:10 +01009134 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009135
Gilles Peskine449bd832023-01-11 14:50:10 +01009136 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9137 psa_set_key_algorithm(&attributes, alg);
9138 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9139 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9140 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009141
9142 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009143 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9144 input1->x, input1->len,
9145 input2->x, input2->len,
9146 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009147 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009148 }
Janos Follathe60c9052019-07-03 13:51:30 +01009149
Gilles Peskine449bd832023-01-11 14:50:10 +01009150 psa_set_key_usage_flags(&attributes, derived_usage);
9151 psa_set_key_algorithm(&attributes, derived_alg);
9152 psa_set_key_type(&attributes, derived_type);
9153 psa_set_key_bits(&attributes, derived_bits);
9154 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9155 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009156
9157 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009158 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9159 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9160 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009161
9162 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009163 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009164 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009165 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009166
9167exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009168 /*
9169 * Key attributes may have been returned by psa_get_key_attributes()
9170 * thus reset them as required.
9171 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009172 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009173
Gilles Peskine449bd832023-01-11 14:50:10 +01009174 psa_key_derivation_abort(&operation);
9175 psa_destroy_key(base_key);
9176 psa_destroy_key(derived_key);
9177 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009178}
9179/* END_CASE */
9180
Janos Follath42fd8882019-07-03 14:17:09 +01009181/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009182void derive_key_export(int alg_arg,
9183 data_t *key_data,
9184 data_t *input1,
9185 data_t *input2,
9186 int bytes1_arg,
9187 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009188{
Ronald Cron5425a212020-08-04 14:58:35 +02009189 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9190 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009191 psa_algorithm_t alg = alg_arg;
9192 size_t bytes1 = bytes1_arg;
9193 size_t bytes2 = bytes2_arg;
9194 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009195 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009196 uint8_t *output_buffer = NULL;
9197 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009198 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9199 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009200 size_t length;
9201
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009202 TEST_CALLOC(output_buffer, capacity);
9203 TEST_CALLOC(export_buffer, capacity);
Gilles Peskine449bd832023-01-11 14:50:10 +01009204 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009205
Gilles Peskine449bd832023-01-11 14:50:10 +01009206 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9207 psa_set_key_algorithm(&base_attributes, alg);
9208 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9209 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9210 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009211
9212 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009213 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9214 input1->x, input1->len,
9215 input2->x, input2->len,
9216 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009217 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009218 }
Janos Follath42fd8882019-07-03 14:17:09 +01009219
Gilles Peskine449bd832023-01-11 14:50:10 +01009220 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9221 output_buffer,
9222 capacity));
9223 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009224
9225 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009226 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9227 input1->x, input1->len,
9228 input2->x, input2->len,
9229 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009230 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009231 }
Janos Follath42fd8882019-07-03 14:17:09 +01009232
Gilles Peskine449bd832023-01-11 14:50:10 +01009233 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9234 psa_set_key_algorithm(&derived_attributes, 0);
9235 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9236 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9237 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9238 &derived_key));
9239 PSA_ASSERT(psa_export_key(derived_key,
9240 export_buffer, bytes1,
9241 &length));
9242 TEST_EQUAL(length, bytes1);
9243 PSA_ASSERT(psa_destroy_key(derived_key));
9244 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9245 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9246 &derived_key));
9247 PSA_ASSERT(psa_export_key(derived_key,
9248 export_buffer + bytes1, bytes2,
9249 &length));
9250 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009251
9252 /* Compare the outputs from the two runs. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009253 TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009254 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009255
9256exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009257 mbedtls_free(output_buffer);
9258 mbedtls_free(export_buffer);
9259 psa_key_derivation_abort(&operation);
9260 psa_destroy_key(base_key);
9261 psa_destroy_key(derived_key);
9262 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009263}
9264/* END_CASE */
9265
9266/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009267void derive_key_type(int alg_arg,
9268 data_t *key_data,
9269 data_t *input1,
9270 data_t *input2,
9271 int key_type_arg, int bits_arg,
9272 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009273{
9274 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9275 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9276 const psa_algorithm_t alg = alg_arg;
9277 const psa_key_type_t key_type = key_type_arg;
9278 const size_t bits = bits_arg;
9279 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9280 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009281 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009282 uint8_t *export_buffer = NULL;
9283 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9284 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9285 size_t export_length;
9286
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009287 TEST_CALLOC(export_buffer, export_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01009288 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009289
Gilles Peskine449bd832023-01-11 14:50:10 +01009290 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9291 psa_set_key_algorithm(&base_attributes, alg);
9292 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9293 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9294 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009295
Gilles Peskine449bd832023-01-11 14:50:10 +01009296 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009297 &operation, base_key, alg,
9298 input1->x, input1->len,
9299 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009300 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009301 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009302 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009303
Gilles Peskine449bd832023-01-11 14:50:10 +01009304 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9305 psa_set_key_algorithm(&derived_attributes, 0);
9306 psa_set_key_type(&derived_attributes, key_type);
9307 psa_set_key_bits(&derived_attributes, bits);
9308 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9309 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009310
Gilles Peskine449bd832023-01-11 14:50:10 +01009311 PSA_ASSERT(psa_export_key(derived_key,
9312 export_buffer, export_buffer_size,
9313 &export_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009314 TEST_MEMORY_COMPARE(export_buffer, export_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009315 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009316
9317exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009318 mbedtls_free(export_buffer);
9319 psa_key_derivation_abort(&operation);
9320 psa_destroy_key(base_key);
9321 psa_destroy_key(derived_key);
9322 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009323}
9324/* END_CASE */
9325
9326/* BEGIN_CASE */
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009327void derive_key_ext(int alg_arg,
9328 data_t *key_data,
9329 data_t *input1,
9330 data_t *input2,
9331 int key_type_arg, int bits_arg,
9332 int64_t flags_arg, /*negative for truncated method*/
9333 data_t *method_data,
9334 psa_status_t expected_status,
9335 data_t *expected_export)
9336{
9337 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9338 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9339 const psa_algorithm_t alg = alg_arg;
9340 const psa_key_type_t key_type = key_type_arg;
9341 const size_t bits = bits_arg;
9342 psa_key_generation_method_t *method = NULL;
9343 size_t method_length = 0;
9344 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9345 const size_t export_buffer_size =
9346 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
9347 uint8_t *export_buffer = NULL;
9348 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9349 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9350 size_t export_length;
9351
9352 TEST_CALLOC(export_buffer, export_buffer_size);
9353 PSA_ASSERT(psa_crypto_init());
9354
9355 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9356 psa_set_key_algorithm(&base_attributes, alg);
9357 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9358 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9359 &base_key));
9360
9361 if (mbedtls_test_psa_setup_key_derivation_wrap(
9362 &operation, base_key, alg,
9363 input1->x, input1->len,
9364 input2->x, input2->len,
9365 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
9366 goto exit;
9367 }
9368
9369 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9370 psa_set_key_algorithm(&derived_attributes, 0);
9371 psa_set_key_type(&derived_attributes, key_type);
9372 psa_set_key_bits(&derived_attributes, bits);
9373 if (!setup_key_generation_method(&method, &method_length,
9374 flags_arg, method_data)) {
9375 goto exit;
9376 }
9377
9378 TEST_EQUAL(psa_key_derivation_output_key_ext(&derived_attributes, &operation,
9379 method, method_length,
9380 &derived_key),
9381 expected_status);
9382
9383 if (expected_status == PSA_SUCCESS) {
9384 PSA_ASSERT(psa_export_key(derived_key,
9385 export_buffer, export_buffer_size,
9386 &export_length));
9387 TEST_MEMORY_COMPARE(export_buffer, export_length,
9388 expected_export->x, expected_export->len);
9389 }
9390
9391exit:
9392 mbedtls_free(export_buffer);
9393 mbedtls_free(method);
9394 psa_key_derivation_abort(&operation);
9395 psa_destroy_key(base_key);
9396 psa_destroy_key(derived_key);
9397 PSA_DONE();
9398}
9399/* END_CASE */
9400
9401/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009402void derive_key(int alg_arg,
9403 data_t *key_data, data_t *input1, data_t *input2,
9404 int type_arg, int bits_arg,
9405 int expected_status_arg,
9406 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009407{
Ronald Cron5425a212020-08-04 14:58:35 +02009408 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9409 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009410 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009411 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009412 size_t bits = bits_arg;
9413 psa_status_t expected_status = expected_status_arg;
9414 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9415 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9416 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9417
Gilles Peskine449bd832023-01-11 14:50:10 +01009418 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009419
Gilles Peskine449bd832023-01-11 14:50:10 +01009420 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9421 psa_set_key_algorithm(&base_attributes, alg);
9422 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9423 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9424 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009425
Gilles Peskine449bd832023-01-11 14:50:10 +01009426 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9427 input1->x, input1->len,
9428 input2->x, input2->len,
9429 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009430 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009431 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009432
Gilles Peskine449bd832023-01-11 14:50:10 +01009433 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9434 psa_set_key_algorithm(&derived_attributes, 0);
9435 psa_set_key_type(&derived_attributes, type);
9436 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009437
9438 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009439 psa_key_derivation_output_key(&derived_attributes,
9440 &operation,
9441 &derived_key);
9442 if (is_large_output > 0) {
9443 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9444 }
9445 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009446
9447exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009448 psa_key_derivation_abort(&operation);
9449 psa_destroy_key(base_key);
9450 psa_destroy_key(derived_key);
9451 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009452}
9453/* END_CASE */
9454
9455/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009456void key_agreement_setup(int alg_arg,
9457 int our_key_type_arg, int our_key_alg_arg,
9458 data_t *our_key_data, data_t *peer_key_data,
9459 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009460{
Ronald Cron5425a212020-08-04 14:58:35 +02009461 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009462 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009463 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009464 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009465 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009466 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009467 psa_status_t expected_status = expected_status_arg;
9468 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009469
Gilles Peskine449bd832023-01-11 14:50:10 +01009470 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009471
Gilles Peskine449bd832023-01-11 14:50:10 +01009472 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9473 psa_set_key_algorithm(&attributes, our_key_alg);
9474 psa_set_key_type(&attributes, our_key_type);
9475 PSA_ASSERT(psa_import_key(&attributes,
9476 our_key_data->x, our_key_data->len,
9477 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009478
Gilles Peskine77f40d82019-04-11 21:27:06 +02009479 /* The tests currently include inputs that should fail at either step.
9480 * Test cases that fail at the setup step should be changed to call
9481 * key_derivation_setup instead, and this function should be renamed
9482 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009483 status = psa_key_derivation_setup(&operation, alg);
9484 if (status == PSA_SUCCESS) {
9485 TEST_EQUAL(psa_key_derivation_key_agreement(
9486 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9487 our_key,
9488 peer_key_data->x, peer_key_data->len),
9489 expected_status);
9490 } else {
9491 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009492 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009493
9494exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009495 psa_key_derivation_abort(&operation);
9496 psa_destroy_key(our_key);
9497 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009498}
9499/* END_CASE */
9500
9501/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009502void raw_key_agreement(int alg_arg,
9503 int our_key_type_arg, data_t *our_key_data,
9504 data_t *peer_key_data,
9505 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009506{
Ronald Cron5425a212020-08-04 14:58:35 +02009507 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009508 psa_algorithm_t alg = alg_arg;
9509 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009510 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009511 unsigned char *output = NULL;
9512 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009513 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009514
Gilles Peskine449bd832023-01-11 14:50:10 +01009515 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009516
Gilles Peskine449bd832023-01-11 14:50:10 +01009517 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9518 psa_set_key_algorithm(&attributes, alg);
9519 psa_set_key_type(&attributes, our_key_type);
9520 PSA_ASSERT(psa_import_key(&attributes,
9521 our_key_data->x, our_key_data->len,
9522 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009523
Gilles Peskine449bd832023-01-11 14:50:10 +01009524 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9525 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009526
Gilles Peskine992bee82022-04-13 23:25:52 +02009527 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009528 TEST_LE_U(expected_output->len,
9529 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9530 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9531 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009532
9533 /* Good case with exact output size */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009534 TEST_CALLOC(output, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009535 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9536 peer_key_data->x, peer_key_data->len,
9537 output, expected_output->len,
9538 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009539 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009540 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009541 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009542 output = NULL;
9543 output_length = ~0;
9544
9545 /* Larger buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009546 TEST_CALLOC(output, expected_output->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009547 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9548 peer_key_data->x, peer_key_data->len,
9549 output, expected_output->len + 1,
9550 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009551 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009552 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009553 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009554 output = NULL;
9555 output_length = ~0;
9556
9557 /* Buffer too small */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009558 TEST_CALLOC(output, expected_output->len - 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009559 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9560 peer_key_data->x, peer_key_data->len,
9561 output, expected_output->len - 1,
9562 &output_length),
9563 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009564 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009565 TEST_LE_U(output_length, expected_output->len - 1);
9566 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009567 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009568
9569exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009570 mbedtls_free(output);
9571 psa_destroy_key(our_key);
9572 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009573}
9574/* END_CASE */
9575
9576/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009577void key_agreement_capacity(int alg_arg,
9578 int our_key_type_arg, data_t *our_key_data,
9579 data_t *peer_key_data,
9580 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009581{
Ronald Cron5425a212020-08-04 14:58:35 +02009582 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009583 psa_algorithm_t alg = alg_arg;
9584 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009585 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009586 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009587 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009588 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009589
Gilles Peskine449bd832023-01-11 14:50:10 +01009590 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009591
Gilles Peskine449bd832023-01-11 14:50:10 +01009592 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9593 psa_set_key_algorithm(&attributes, alg);
9594 psa_set_key_type(&attributes, our_key_type);
9595 PSA_ASSERT(psa_import_key(&attributes,
9596 our_key_data->x, our_key_data->len,
9597 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009598
Gilles Peskine449bd832023-01-11 14:50:10 +01009599 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9600 PSA_ASSERT(psa_key_derivation_key_agreement(
9601 &operation,
9602 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9603 peer_key_data->x, peer_key_data->len));
9604 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009605 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009606 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9607 PSA_KEY_DERIVATION_INPUT_INFO,
9608 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009609 }
Gilles Peskine59685592018-09-18 12:11:34 +02009610
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009611 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009612 PSA_ASSERT(psa_key_derivation_get_capacity(
9613 &operation, &actual_capacity));
9614 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009615
Gilles Peskinebf491972018-10-25 22:36:12 +02009616 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009617 while (actual_capacity > sizeof(output)) {
9618 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9619 output, sizeof(output)));
9620 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009621 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009622 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9623 output, actual_capacity));
9624 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9625 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009626
Gilles Peskine59685592018-09-18 12:11:34 +02009627exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009628 psa_key_derivation_abort(&operation);
9629 psa_destroy_key(our_key);
9630 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009631}
9632/* END_CASE */
9633
Valerio Settiad819672023-12-29 12:14:41 +01009634/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9635void ecc_conversion_functions(int grp_id_arg, int psa_family_arg, int bits_arg)
Valerio Settibf999cb2023-12-28 17:48:13 +01009636{
9637 mbedtls_ecp_group_id grp_id = grp_id_arg;
Valerio Settiad819672023-12-29 12:14:41 +01009638 psa_ecc_family_t ecc_family = psa_family_arg;
9639 size_t bits = bits_arg;
9640 size_t bits_tmp;
Valerio Settibf999cb2023-12-28 17:48:13 +01009641
Valerio Settiad819672023-12-29 12:14:41 +01009642 TEST_EQUAL(ecc_family, mbedtls_ecc_group_to_psa(grp_id, &bits_tmp));
9643 TEST_EQUAL(bits, bits_tmp);
Valerio Settiac739522024-01-04 10:22:01 +01009644 TEST_EQUAL(grp_id, mbedtls_ecc_group_from_psa(ecc_family, bits));
Valerio Settibf999cb2023-12-28 17:48:13 +01009645}
9646/* END_CASE */
9647
Valerio Settiac739522024-01-04 10:22:01 +01009648/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9649void ecc_conversion_functions_fail()
9650{
9651 size_t bits;
9652
Valerio Settidb6e0292024-01-05 10:15:45 +01009653 /* Invalid legacy curve identifiers. */
9654 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_MAX, &bits));
9655 TEST_EQUAL(0, bits);
Valerio Settiac739522024-01-04 10:22:01 +01009656 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_NONE, &bits));
9657 TEST_EQUAL(0, bits);
9658
9659 /* Invalid PSA EC family. */
9660 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(0, 192));
9661 /* Invalid bit-size for a valid EC family. */
9662 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_SECP_R1, 512));
9663
9664 /* Twisted-Edward curves are not supported yet. */
9665 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9666 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255));
9667 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9668 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 448));
9669}
9670/* END_CASE */
9671
9672
Valerio Settibf999cb2023-12-28 17:48:13 +01009673/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009674void key_agreement_output(int alg_arg,
9675 int our_key_type_arg, data_t *our_key_data,
9676 data_t *peer_key_data,
9677 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009678{
Ronald Cron5425a212020-08-04 14:58:35 +02009679 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009680 psa_algorithm_t alg = alg_arg;
9681 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009682 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009683 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009684 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009685
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009686 TEST_CALLOC(actual_output, MAX(expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009687 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009688
Gilles Peskine449bd832023-01-11 14:50:10 +01009689 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009690
Gilles Peskine449bd832023-01-11 14:50:10 +01009691 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9692 psa_set_key_algorithm(&attributes, alg);
9693 psa_set_key_type(&attributes, our_key_type);
9694 PSA_ASSERT(psa_import_key(&attributes,
9695 our_key_data->x, our_key_data->len,
9696 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009697
Gilles Peskine449bd832023-01-11 14:50:10 +01009698 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9699 PSA_ASSERT(psa_key_derivation_key_agreement(
9700 &operation,
9701 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9702 peer_key_data->x, peer_key_data->len));
9703 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009704 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009705 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9706 PSA_KEY_DERIVATION_INPUT_INFO,
9707 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009708 }
Gilles Peskine59685592018-09-18 12:11:34 +02009709
Gilles Peskine449bd832023-01-11 14:50:10 +01009710 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9711 actual_output,
9712 expected_output1->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009713 TEST_MEMORY_COMPARE(actual_output, expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009714 expected_output1->x, expected_output1->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009715 if (expected_output2->len != 0) {
9716 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9717 actual_output,
9718 expected_output2->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009719 TEST_MEMORY_COMPARE(actual_output, expected_output2->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009720 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009721 }
Gilles Peskine59685592018-09-18 12:11:34 +02009722
9723exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009724 psa_key_derivation_abort(&operation);
9725 psa_destroy_key(our_key);
9726 PSA_DONE();
9727 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009728}
9729/* END_CASE */
9730
9731/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009732void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009733{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009734 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009735 unsigned char *output = NULL;
9736 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009737 size_t i;
9738 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009739
Gilles Peskine449bd832023-01-11 14:50:10 +01009740 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009741
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009742 TEST_CALLOC(output, bytes);
9743 TEST_CALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009744
Gilles Peskine449bd832023-01-11 14:50:10 +01009745 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009746
Gilles Peskinea50d7392018-06-21 10:22:13 +02009747 /* Run several times, to ensure that every output byte will be
9748 * nonzero at least once with overwhelming probability
9749 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009750 for (run = 0; run < 10; run++) {
9751 if (bytes != 0) {
9752 memset(output, 0, bytes);
9753 }
9754 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009755
Gilles Peskine449bd832023-01-11 14:50:10 +01009756 for (i = 0; i < bytes; i++) {
9757 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009758 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009759 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009760 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009761 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009762
9763 /* Check that every byte was changed to nonzero at least once. This
9764 * validates that psa_generate_random is overwriting every byte of
9765 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009766 for (i = 0; i < bytes; i++) {
9767 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009768 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009769
9770exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009771 PSA_DONE();
9772 mbedtls_free(output);
9773 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009774}
9775/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009776
9777/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009778void generate_key(int type_arg,
9779 int bits_arg,
9780 int usage_arg,
9781 int alg_arg,
9782 int expected_status_arg,
9783 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009784{
Ronald Cron5425a212020-08-04 14:58:35 +02009785 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009786 psa_key_type_t type = type_arg;
9787 psa_key_usage_t usage = usage_arg;
9788 size_t bits = bits_arg;
9789 psa_algorithm_t alg = alg_arg;
9790 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009791 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009792 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009793
Gilles Peskine449bd832023-01-11 14:50:10 +01009794 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009795
Gilles Peskine449bd832023-01-11 14:50:10 +01009796 psa_set_key_usage_flags(&attributes, usage);
9797 psa_set_key_algorithm(&attributes, alg);
9798 psa_set_key_type(&attributes, type);
9799 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009800
9801 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009802 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009803
Gilles Peskine449bd832023-01-11 14:50:10 +01009804 if (is_large_key > 0) {
9805 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9806 }
9807 TEST_EQUAL(status, expected_status);
9808 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009809 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009810 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009811
9812 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009813 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9814 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9815 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009816
Gilles Peskine818ca122018-06-20 18:16:48 +02009817 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009818 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009819 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009820 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009821
9822exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009823 /*
9824 * Key attributes may have been returned by psa_get_key_attributes()
9825 * thus reset them as required.
9826 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009827 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009828
Gilles Peskine449bd832023-01-11 14:50:10 +01009829 psa_destroy_key(key);
9830 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009831}
9832/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009833
Valerio Setti19fec542023-07-25 12:31:50 +02009834/* 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 +01009835void generate_key_rsa(int bits_arg,
9836 data_t *e_arg,
9837 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009838{
Ronald Cron5425a212020-08-04 14:58:35 +02009839 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009840 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009841 size_t bits = bits_arg;
9842 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9843 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9844 psa_status_t expected_status = expected_status_arg;
9845 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009846 uint8_t *e_read_buffer = NULL;
9847 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009848 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009849 size_t e_read_length = SIZE_MAX;
9850
Gilles Peskine449bd832023-01-11 14:50:10 +01009851 if (e_arg->len == 0 ||
9852 (e_arg->len == 3 &&
9853 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009854 is_default_public_exponent = 1;
9855 e_read_size = 0;
9856 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009857 TEST_CALLOC(e_read_buffer, e_read_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009858
Gilles Peskine449bd832023-01-11 14:50:10 +01009859 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009860
Gilles Peskine449bd832023-01-11 14:50:10 +01009861 psa_set_key_usage_flags(&attributes, usage);
9862 psa_set_key_algorithm(&attributes, alg);
9863 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9864 e_arg->x, e_arg->len));
9865 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009866
9867 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009868 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9869 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009870 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009871 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009872
9873 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009874 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9875 TEST_EQUAL(psa_get_key_type(&attributes), type);
9876 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009877 psa_status_t status = psa_get_key_domain_parameters(&attributes,
9878 e_read_buffer, e_read_size,
9879 &e_read_length);
9880
9881
9882#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
9883 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
9884 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +01009885 if (is_default_public_exponent) {
9886 TEST_EQUAL(e_read_length, 0);
9887 } else {
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009888 TEST_EQUAL(status, PSA_SUCCESS);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009889 TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009890 }
Pengyu Lv9e976f32023-12-06 16:58:05 +08009891#else
Pengyu Lv9e976f32023-12-06 16:58:05 +08009892 (void) is_default_public_exponent;
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009893 TEST_EQUAL(status, PSA_ERROR_NOT_SUPPORTED);
Pengyu Lv9e976f32023-12-06 16:58:05 +08009894#endif
Gilles Peskinee56e8782019-04-26 17:34:02 +02009895
9896 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009897 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009898 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009899 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009900
Gilles Peskine1d25a0a2024-02-12 16:40:04 +01009901 TEST_ASSERT(rsa_test_e(key, bits, e_arg));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009902
9903exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009904 /*
9905 * Key attributes may have been returned by psa_get_key_attributes() or
9906 * set by psa_set_key_domain_parameters() thus reset them as required.
9907 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009908 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009909
Gilles Peskine449bd832023-01-11 14:50:10 +01009910 psa_destroy_key(key);
9911 PSA_DONE();
9912 mbedtls_free(e_read_buffer);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009913}
9914/* END_CASE */
9915
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009916/* BEGIN_CASE */
9917void generate_key_ext(int type_arg,
9918 int bits_arg,
9919 int usage_arg,
9920 int alg_arg,
9921 int64_t flags_arg, /*negative for truncated method*/
9922 data_t *method_data,
9923 int expected_status_arg)
9924{
9925 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9926 psa_key_type_t type = type_arg;
9927 psa_key_usage_t usage = usage_arg;
9928 size_t bits = bits_arg;
9929 psa_algorithm_t alg = alg_arg;
9930 psa_status_t expected_status = expected_status_arg;
9931 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9932 psa_key_generation_method_t *method = NULL;
9933 size_t method_length = 0;
9934 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
9935
9936 PSA_ASSERT(psa_crypto_init());
9937
9938 psa_set_key_usage_flags(&attributes, usage);
9939 psa_set_key_algorithm(&attributes, alg);
9940 psa_set_key_type(&attributes, type);
9941 psa_set_key_bits(&attributes, bits);
9942
9943 if (!setup_key_generation_method(&method, &method_length,
9944 flags_arg, method_data)) {
9945 goto exit;
9946 }
9947
9948 /* Generate a key */
9949 psa_status_t status = psa_generate_key_ext(&attributes,
9950 method, method_length,
9951 &key);
9952
9953 TEST_EQUAL(status, expected_status);
9954 if (expected_status != PSA_SUCCESS) {
9955 goto exit;
9956 }
9957
9958 /* Test the key information */
9959 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9960 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9961 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
9962
9963 /* Do something with the key according to its type and permitted usage. */
9964 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
9965 goto exit;
9966 }
9967
9968exit:
9969 /*
9970 * Key attributes may have been returned by psa_get_key_attributes()
9971 * thus reset them as required.
9972 */
9973 psa_reset_key_attributes(&got_attributes);
9974 mbedtls_free(method);
9975 psa_destroy_key(key);
9976 PSA_DONE();
9977}
9978/* END_CASE */
9979
9980/* BEGIN_CASE */
9981void key_generation_method_init()
9982{
9983 psa_key_generation_method_t func = psa_key_generation_method_init();
9984 psa_key_generation_method_t init = PSA_KEY_GENERATION_METHOD_INIT;
9985 psa_key_generation_method_t zero;
9986 memset(&zero, 0, sizeof(zero));
9987
9988 /* In order for sizeof(psa_key_generation_method_t) to mean
9989 * empty data, there must not be any padding in the structure:
9990 * the size of the structure must be the offset of the data field. */
9991 TEST_EQUAL(sizeof(zero), offsetof(psa_key_generation_method_t, data));
9992
9993 TEST_EQUAL(func.flags, 0);
9994 TEST_EQUAL(init.flags, 0);
9995 TEST_EQUAL(zero.flags, 0);
9996}
9997/* END_CASE */
9998
Darryl Greend49a4992018-06-18 17:27:26 +01009999/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010010000void persistent_key_load_key_from_storage(data_t *data,
10001 int type_arg, int bits_arg,
10002 int usage_flags_arg, int alg_arg,
10003 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +010010004{
Gilles Peskine449bd832023-01-11 14:50:10 +010010005 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010006 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +020010007 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10008 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010009 psa_key_type_t type = type_arg;
10010 size_t bits = bits_arg;
10011 psa_key_usage_t usage_flags = usage_flags_arg;
10012 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +020010013 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +010010014 unsigned char *first_export = NULL;
10015 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +010010016 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Sergeybef1f632023-03-06 15:25:06 -070010017 size_t first_exported_length = 0;
Darryl Greend49a4992018-06-18 17:27:26 +010010018 size_t second_exported_length;
10019
Gilles Peskine449bd832023-01-11 14:50:10 +010010020 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +010010021 TEST_CALLOC(first_export, export_size);
10022 TEST_CALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010023 }
Darryl Greend49a4992018-06-18 17:27:26 +010010024
Gilles Peskine449bd832023-01-11 14:50:10 +010010025 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +010010026
Gilles Peskine449bd832023-01-11 14:50:10 +010010027 psa_set_key_id(&attributes, key_id);
10028 psa_set_key_usage_flags(&attributes, usage_flags);
10029 psa_set_key_algorithm(&attributes, alg);
10030 psa_set_key_type(&attributes, type);
10031 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +010010032
Gilles Peskine449bd832023-01-11 14:50:10 +010010033 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +000010034 case IMPORT_KEY:
10035 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010036 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
10037 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +000010038 break;
Darryl Greend49a4992018-06-18 17:27:26 +010010039
Darryl Green0c6575a2018-11-07 16:05:30 +000010040 case GENERATE_KEY:
10041 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010042 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +000010043 break;
10044
10045 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +010010046#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +010010047 {
10048 /* Create base key */
10049 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
10050 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
10051 psa_set_key_usage_flags(&base_attributes,
10052 PSA_KEY_USAGE_DERIVE);
10053 psa_set_key_algorithm(&base_attributes, derive_alg);
10054 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
10055 PSA_ASSERT(psa_import_key(&base_attributes,
10056 data->x, data->len,
10057 &base_key));
10058 /* Derive a key. */
10059 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
10060 PSA_ASSERT(psa_key_derivation_input_key(
10061 &operation,
10062 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
10063 PSA_ASSERT(psa_key_derivation_input_bytes(
10064 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
10065 NULL, 0));
10066 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
10067 &operation,
10068 &key));
10069 PSA_ASSERT(psa_key_derivation_abort(&operation));
10070 PSA_ASSERT(psa_destroy_key(base_key));
10071 base_key = MBEDTLS_SVC_KEY_ID_INIT;
10072 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +010010073#else
Gilles Peskine449bd832023-01-11 14:50:10 +010010074 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +010010075#endif
10076 break;
10077
10078 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +010010079 TEST_FAIL("generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +010010080 break;
Darryl Green0c6575a2018-11-07 16:05:30 +000010081 }
Gilles Peskine449bd832023-01-11 14:50:10 +010010082 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +010010083
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010084 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +010010085 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
10086 PSA_ASSERT(psa_export_key(key,
10087 first_export, export_size,
10088 &first_exported_length));
10089 if (generation_method == IMPORT_KEY) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +010010090 TEST_MEMORY_COMPARE(data->x, data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +010010091 first_export, first_exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +010010092 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010093 }
Darryl Greend49a4992018-06-18 17:27:26 +010010094
10095 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +010010096 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +020010097 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +010010098 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +010010099
Darryl Greend49a4992018-06-18 17:27:26 +010010100 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +010010101 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
10102 TEST_ASSERT(mbedtls_svc_key_id_equal(
10103 psa_get_key_id(&attributes), key_id));
10104 TEST_EQUAL(psa_get_key_lifetime(&attributes),
10105 PSA_KEY_LIFETIME_PERSISTENT);
10106 TEST_EQUAL(psa_get_key_type(&attributes), type);
10107 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
10108 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
10109 mbedtls_test_update_key_usage_flags(usage_flags));
10110 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +010010111
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010112 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +010010113 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
10114 PSA_ASSERT(psa_export_key(key,
10115 second_export, export_size,
10116 &second_exported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +010010117 TEST_MEMORY_COMPARE(first_export, first_exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +010010118 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +000010119 }
10120
10121 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +010010122 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +000010123 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010124 }
Darryl Greend49a4992018-06-18 17:27:26 +010010125
10126exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010127 /*
10128 * Key attributes may have been returned by psa_get_key_attributes()
10129 * thus reset them as required.
10130 */
Gilles Peskine449bd832023-01-11 14:50:10 +010010131 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010132
Gilles Peskine449bd832023-01-11 14:50:10 +010010133 mbedtls_free(first_export);
10134 mbedtls_free(second_export);
10135 psa_key_derivation_abort(&operation);
10136 psa_destroy_key(base_key);
10137 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +020010138 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +010010139}
10140/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +020010141
Neil Armstronga557cb82022-06-10 08:58:32 +020010142/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010143void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
10144 int primitive_arg, int hash_arg, int role_arg,
10145 int test_input, data_t *pw_data,
10146 int inj_err_type_arg,
10147 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010148{
10149 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10150 psa_pake_operation_t operation = psa_pake_operation_init();
10151 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010152 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +020010153 psa_key_type_t key_type_pw = key_type_pw_arg;
10154 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010155 psa_algorithm_t hash_alg = hash_arg;
10156 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010157 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10158 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010159 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
10160 psa_status_t expected_error = expected_error_arg;
10161 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010162 unsigned char *output_buffer = NULL;
10163 size_t output_len = 0;
10164
Gilles Peskine449bd832023-01-11 14:50:10 +010010165 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010166
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010167 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +010010168 PSA_PAKE_STEP_KEY_SHARE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +010010169 TEST_CALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010170
Gilles Peskine449bd832023-01-11 14:50:10 +010010171 if (pw_data->len > 0) {
10172 psa_set_key_usage_flags(&attributes, key_usage_pw);
10173 psa_set_key_algorithm(&attributes, alg);
10174 psa_set_key_type(&attributes, key_type_pw);
10175 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10176 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010177 }
10178
Gilles Peskine449bd832023-01-11 14:50:10 +010010179 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10180 psa_pake_cs_set_primitive(&cipher_suite, primitive);
10181 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010182
Gilles Peskine449bd832023-01-11 14:50:10 +010010183 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +020010184
Gilles Peskine449bd832023-01-11 14:50:10 +010010185 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
10186 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10187 expected_error);
10188 PSA_ASSERT(psa_pake_abort(&operation));
10189 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10190 expected_error);
10191 PSA_ASSERT(psa_pake_abort(&operation));
10192 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
10193 expected_error);
10194 PSA_ASSERT(psa_pake_abort(&operation));
10195 TEST_EQUAL(psa_pake_set_role(&operation, role),
10196 expected_error);
10197 PSA_ASSERT(psa_pake_abort(&operation));
10198 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10199 NULL, 0, NULL),
10200 expected_error);
10201 PSA_ASSERT(psa_pake_abort(&operation));
10202 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
10203 expected_error);
10204 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010205 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010206 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010207
Gilles Peskine449bd832023-01-11 14:50:10 +010010208 status = psa_pake_setup(&operation, &cipher_suite);
10209 if (status != PSA_SUCCESS) {
10210 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010211 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010212 }
10213
Gilles Peskine449bd832023-01-11 14:50:10 +010010214 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
10215 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
10216 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010217 goto exit;
10218 }
10219
Gilles Peskine449bd832023-01-11 14:50:10 +010010220 status = psa_pake_set_role(&operation, role);
10221 if (status != PSA_SUCCESS) {
10222 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010223 goto exit;
10224 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010225
Gilles Peskine449bd832023-01-11 14:50:10 +010010226 if (pw_data->len > 0) {
10227 status = psa_pake_set_password_key(&operation, key);
10228 if (status != PSA_SUCCESS) {
10229 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010230 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010231 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010232 }
10233
Gilles Peskine449bd832023-01-11 14:50:10 +010010234 if (inj_err_type == INJECT_ERR_INVALID_USER) {
10235 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10236 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010237 goto exit;
10238 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010239
Gilles Peskine449bd832023-01-11 14:50:10 +010010240 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
10241 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10242 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010243 goto exit;
10244 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010245
Gilles Peskine449bd832023-01-11 14:50:10 +010010246 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010247 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010248 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
10249 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010250 goto exit;
10251 }
10252
Gilles Peskine449bd832023-01-11 14:50:10 +010010253 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010254 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010255 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
10256 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010257 goto exit;
10258 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010259
Gilles Peskine449bd832023-01-11 14:50:10 +010010260 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
10261 PSA_PAKE_STEP_KEY_SHARE);
10262 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
10263 PSA_PAKE_STEP_ZK_PUBLIC);
10264 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
10265 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010266
Gilles Peskine449bd832023-01-11 14:50:10 +010010267 if (test_input) {
10268 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10269 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
10270 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010271 goto exit;
10272 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010273
Gilles Peskine449bd832023-01-11 14:50:10 +010010274 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10275 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10276 output_buffer, size_zk_proof),
10277 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010278 goto exit;
10279 }
10280
Gilles Peskine449bd832023-01-11 14:50:10 +010010281 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10282 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
10283 output_buffer, size_zk_proof),
10284 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010285 goto exit;
10286 }
10287
Gilles Peskine449bd832023-01-11 14:50:10 +010010288 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
10289 output_buffer, size_key_share);
10290 if (status != PSA_SUCCESS) {
10291 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010292 goto exit;
10293 }
10294
Gilles Peskine449bd832023-01-11 14:50:10 +010010295 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10296 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10297 output_buffer, size_zk_public + 1),
10298 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010299 goto exit;
10300 }
10301
Gilles Peskine449bd832023-01-11 14:50:10 +010010302 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010303 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010304 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10305 output_buffer, size_zk_public + 1);
10306 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10307 output_buffer, size_zk_public),
10308 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010309 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010310 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010311 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010312 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10313 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10314 NULL, 0, NULL),
10315 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010316 goto exit;
10317 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010318
Gilles Peskine449bd832023-01-11 14:50:10 +010010319 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10320 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10321 output_buffer, buf_size, &output_len),
10322 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010323 goto exit;
10324 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010325
Gilles Peskine449bd832023-01-11 14:50:10 +010010326 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10327 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10328 output_buffer, buf_size, &output_len),
10329 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010330 goto exit;
10331 }
10332
Gilles Peskine449bd832023-01-11 14:50:10 +010010333 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10334 output_buffer, buf_size, &output_len);
10335 if (status != PSA_SUCCESS) {
10336 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010337 goto exit;
10338 }
10339
Gilles Peskine449bd832023-01-11 14:50:10 +010010340 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010341
Gilles Peskine449bd832023-01-11 14:50:10 +010010342 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10343 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10344 output_buffer, size_zk_public - 1, &output_len),
10345 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010346 goto exit;
10347 }
10348
Gilles Peskine449bd832023-01-11 14:50:10 +010010349 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010350 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010351 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10352 output_buffer, size_zk_public - 1, &output_len);
10353 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10354 output_buffer, buf_size, &output_len),
10355 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010356 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010357 }
10358 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010359
10360exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010361 PSA_ASSERT(psa_destroy_key(key));
10362 PSA_ASSERT(psa_pake_abort(&operation));
10363 mbedtls_free(output_buffer);
10364 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010365}
10366/* END_CASE */
10367
Neil Armstronga557cb82022-06-10 08:58:32 +020010368/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010369void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10370 int client_input_first, int inject_error,
10371 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010372{
10373 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10374 psa_pake_operation_t server = psa_pake_operation_init();
10375 psa_pake_operation_t client = psa_pake_operation_init();
10376 psa_algorithm_t alg = alg_arg;
10377 psa_algorithm_t hash_alg = hash_arg;
10378 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10379 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10380
Gilles Peskine449bd832023-01-11 14:50:10 +010010381 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010382
Gilles Peskine449bd832023-01-11 14:50:10 +010010383 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10384 psa_set_key_algorithm(&attributes, alg);
10385 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10386 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10387 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010388
Gilles Peskine449bd832023-01-11 14:50:10 +010010389 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10390 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10391 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010392
10393
Gilles Peskine449bd832023-01-11 14:50:10 +010010394 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10395 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010396
Gilles Peskine449bd832023-01-11 14:50:10 +010010397 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10398 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010399
Gilles Peskine449bd832023-01-11 14:50:10 +010010400 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10401 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010402
Gilles Peskine449bd832023-01-11 14:50:10 +010010403 ecjpake_do_round(alg, primitive_arg, &server, &client,
10404 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010405
Gilles Peskine449bd832023-01-11 14:50:10 +010010406 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010407 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010408 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010409
Gilles Peskine449bd832023-01-11 14:50:10 +010010410 ecjpake_do_round(alg, primitive_arg, &server, &client,
10411 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010412
10413exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010414 psa_destroy_key(key);
10415 psa_pake_abort(&server);
10416 psa_pake_abort(&client);
10417 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010418}
10419/* END_CASE */
10420
10421/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010422void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10423 int derive_alg_arg, data_t *pw_data,
10424 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010425{
10426 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10427 psa_pake_operation_t server = psa_pake_operation_init();
10428 psa_pake_operation_t client = psa_pake_operation_init();
10429 psa_algorithm_t alg = alg_arg;
10430 psa_algorithm_t hash_alg = hash_arg;
10431 psa_algorithm_t derive_alg = derive_alg_arg;
10432 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10433 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10434 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010435 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010436 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010437 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010438 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010439
Gilles Peskine449bd832023-01-11 14:50:10 +010010440 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010441
Gilles Peskine449bd832023-01-11 14:50:10 +010010442 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10443 psa_set_key_algorithm(&attributes, alg);
10444 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10445 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10446 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010447
Gilles Peskine449bd832023-01-11 14:50:10 +010010448 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10449 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10450 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010451
Neil Armstrong1e855602022-06-15 11:32:11 +020010452 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010453 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10454 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010455
Gilles Peskine449bd832023-01-11 14:50:10 +010010456 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10457 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10458 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10459 PSA_KEY_DERIVATION_INPUT_SEED,
10460 (const uint8_t *) "", 0));
10461 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10462 PSA_KEY_DERIVATION_INPUT_SEED,
10463 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010464 }
10465
Gilles Peskine449bd832023-01-11 14:50:10 +010010466 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10467 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010468
Gilles Peskine449bd832023-01-11 14:50:10 +010010469 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10470 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010471
Gilles Peskine449bd832023-01-11 14:50:10 +010010472 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10473 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010474
Gilles Peskine449bd832023-01-11 14:50:10 +010010475 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10476 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10477 PSA_ERROR_BAD_STATE);
10478 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10479 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010480 goto exit;
10481 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010482
Neil Armstrongf983caf2022-06-15 15:27:48 +020010483 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010484 ecjpake_do_round(alg, primitive_arg, &server, &client,
10485 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010486
Gilles Peskine449bd832023-01-11 14:50:10 +010010487 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10488 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10489 PSA_ERROR_BAD_STATE);
10490 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10491 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010492 goto exit;
10493 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010494
Neil Armstrongf983caf2022-06-15 15:27:48 +020010495 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010496 ecjpake_do_round(alg, primitive_arg, &server, &client,
10497 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010498
Gilles Peskine449bd832023-01-11 14:50:10 +010010499 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10500 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010501
10502exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010503 psa_key_derivation_abort(&server_derive);
10504 psa_key_derivation_abort(&client_derive);
10505 psa_destroy_key(key);
10506 psa_pake_abort(&server);
10507 psa_pake_abort(&client);
10508 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010509}
10510/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010511
10512/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010513void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010514{
10515 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10516 const size_t bits = 256;
10517 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010518 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010519 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010520 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010521
10522 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10523 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010524 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10525 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10526 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10527 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010528 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010529 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10530 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010531
10532 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010533 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10534 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10535 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10536 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10537 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10538 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010539
10540 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010541 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10542 PSA_PAKE_OUTPUT_MAX_SIZE);
10543 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10544 PSA_PAKE_OUTPUT_MAX_SIZE);
10545 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10546 PSA_PAKE_OUTPUT_MAX_SIZE);
10547 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10548 PSA_PAKE_INPUT_MAX_SIZE);
10549 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10550 PSA_PAKE_INPUT_MAX_SIZE);
10551 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10552 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010553}
10554/* END_CASE */