blob: e946f4e607f229bec4beff205d013b5baee4271b [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 {
Gilles Peskine7a18f962024-02-12 16:48:11 +01001296 const uint8_t *expected = e_arg->x;
1297 size_t expected_len = e_arg->len;
1298 while (expected_len > 0 && *expected == 0) {
1299 ++expected;
1300 --expected_len;
1301 }
1302 TEST_MEMORY_COMPARE(p, len, expected, expected_len);
Gilles Peskine1d25a0a2024-02-12 16:40:04 +01001303 }
1304 ok = 1;
1305
1306exit:
1307 mbedtls_free(exported);
1308 return ok;
1309}
1310#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE */
1311
Gilles Peskinef0765fa2024-02-12 16:46:16 +01001312static int setup_key_generation_method(psa_key_generation_method_t **method,
Gilles Peskinec81393b2024-02-14 20:51:28 +01001313 size_t *method_data_length,
1314 int flags_arg,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01001315 const data_t *method_data)
1316{
Gilles Peskinec81393b2024-02-14 20:51:28 +01001317 *method_data_length = method_data->len;
1318 /* If there are N bytes of padding at the end of
1319 * psa_key_generation_method_t, then it's enough to allocate
1320 * MIN(sizeof(psa_key_generation_method_t),
1321 * offsetof(psa_key_generation_method_t, data) + method_data_length).
1322 *
1323 * For simplicity, here, we allocate up to N more bytes than necessary.
1324 * In practice, the current layout of psa_key_generation_method_t
1325 * makes padding extremely unlikely, so we don't worry about testing
1326 * that the library code doesn't try to access these extra N bytes.
1327 */
1328 *method = mbedtls_calloc(1, sizeof(**method) + *method_data_length);
1329 TEST_ASSERT(*method != NULL);
1330 (*method)->flags = (uint32_t) flags_arg;
1331 memcpy((*method)->data, method_data->x, method_data->len);
Gilles Peskinef0765fa2024-02-12 16:46:16 +01001332 return 1;
1333exit:
1334 return 0;
1335}
1336
Gilles Peskinee59236f2018-01-27 23:32:46 +01001337/* END_HEADER */
1338
1339/* BEGIN_DEPENDENCIES
1340 * depends_on:MBEDTLS_PSA_CRYPTO_C
1341 * END_DEPENDENCIES
1342 */
1343
1344/* BEGIN_CASE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01001345void psa_can_do_hash()
1346{
1347 /* We can't test that this is specific to drivers until partial init has
1348 * been implemented, but we can at least test before/after full init. */
1349 TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE));
1350 PSA_INIT();
1351 TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE));
1352 PSA_DONE();
1353}
1354/* END_CASE */
1355
1356/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001357void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001358{
1359 size_t max_truncated_mac_size =
1360 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1361
1362 /* Check that the length for a truncated MAC always fits in the algorithm
1363 * encoding. The shifted mask is the maximum truncated value. The
1364 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001366}
1367/* END_CASE */
1368
1369/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001370void import_with_policy(int type_arg,
1371 int usage_arg, int alg_arg,
1372 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001373{
1374 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1375 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001376 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001377 psa_key_type_t type = type_arg;
1378 psa_key_usage_t usage = usage_arg;
1379 psa_algorithm_t alg = alg_arg;
1380 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001382 psa_status_t status;
1383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001385
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 psa_set_key_type(&attributes, type);
1387 psa_set_key_usage_flags(&attributes, usage);
1388 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 status = psa_import_key(&attributes,
1391 key_material, sizeof(key_material),
1392 &key);
1393 TEST_EQUAL(status, expected_status);
1394 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001395 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001397
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1399 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1400 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1401 mbedtls_test_update_key_usage_flags(usage));
1402 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1403 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 PSA_ASSERT(psa_destroy_key(key));
1406 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001407
1408exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001409 /*
1410 * Key attributes may have been returned by psa_get_key_attributes()
1411 * thus reset them as required.
1412 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 psa_destroy_key(key);
1416 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001417}
1418/* END_CASE */
1419
1420/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001421void import_with_data(data_t *data, int type_arg,
1422 int attr_bits_arg,
1423 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001424{
1425 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1426 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001427 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001428 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001429 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001430 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001431 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001434
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 psa_set_key_type(&attributes, type);
1436 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 status = psa_import_key(&attributes, data->x, data->len, &key);
Manuel Pégourié-Gonnard0509b582023-08-08 12:47:56 +02001439 /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED.
1440 *
1441 * This can happen with a type supported only by a driver:
1442 * - the driver sees the invalid data (for example wrong size) and thinks
1443 * "well perhaps this is a key size I don't support" so it returns
1444 * NOT_SUPPORTED which is correct at this point;
1445 * - we fallback to built-ins, which don't support this type, so return
1446 * NOT_SUPPORTED which again is correct at this point.
1447 */
1448 if (expected_status == PSA_ERROR_INVALID_ARGUMENT &&
1449 status == PSA_ERROR_NOT_SUPPORTED) {
1450 ; // OK
1451 } else {
1452 TEST_EQUAL(status, expected_status);
1453 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001455 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1459 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1460 if (attr_bits != 0) {
1461 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1462 }
1463 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001464
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 PSA_ASSERT(psa_destroy_key(key));
1466 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001467
1468exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001469 /*
1470 * Key attributes may have been returned by psa_get_key_attributes()
1471 * thus reset them as required.
1472 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 psa_destroy_key(key);
1476 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001477}
1478/* END_CASE */
1479
1480/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001481/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001482void import_large_key(int type_arg, int byte_size_arg,
1483 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001484{
1485 psa_key_type_t type = type_arg;
1486 size_t byte_size = byte_size_arg;
1487 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1488 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001489 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001490 psa_status_t status;
1491 uint8_t *buffer = NULL;
1492 size_t buffer_size = byte_size + 1;
1493 size_t n;
1494
Steven Cooreman69967ce2021-01-18 18:01:08 +01001495 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001496 * accommodate large keys due to heap size constraints */
Tom Cosgrove412a8132023-07-20 16:55:14 +01001497 TEST_CALLOC_OR_SKIP(buffer, buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001501
1502 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1504 psa_set_key_type(&attributes, type);
1505 status = psa_import_key(&attributes, buffer, byte_size, &key);
1506 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1507 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 if (status == PSA_SUCCESS) {
1510 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1511 TEST_EQUAL(psa_get_key_type(&attributes), type);
1512 TEST_EQUAL(psa_get_key_bits(&attributes),
1513 PSA_BYTES_TO_BITS(byte_size));
1514 ASSERT_NO_SLOT_NUMBER(&attributes);
1515 memset(buffer, 0, byte_size + 1);
1516 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1517 for (n = 0; n < byte_size; n++) {
1518 TEST_EQUAL(buffer[n], 'K');
1519 }
1520 for (n = byte_size; n < buffer_size; n++) {
1521 TEST_EQUAL(buffer[n], 0);
1522 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001523 }
1524
1525exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001526 /*
1527 * Key attributes may have been returned by psa_get_key_attributes()
1528 * thus reset them as required.
1529 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 psa_destroy_key(key);
1533 PSA_DONE();
1534 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001535}
1536/* END_CASE */
1537
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001538/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001539/* Import an RSA key with a valid structure (but not valid numbers
1540 * inside, beyond having sensible size and parity). This is expected to
1541 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001542void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001543{
Ronald Cron5425a212020-08-04 14:58:35 +02001544 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001545 size_t bits = bits_arg;
1546 psa_status_t expected_status = expected_status_arg;
1547 psa_status_t status;
1548 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001549 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001550 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001552 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001553 unsigned char *p;
1554 int ret;
1555 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001556 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 PSA_ASSERT(psa_crypto_init());
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001559 TEST_CALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1562 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001563 length = ret;
1564
1565 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 psa_set_key_type(&attributes, type);
1567 status = psa_import_key(&attributes, p, length, &key);
1568 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 if (status == PSA_SUCCESS) {
1571 PSA_ASSERT(psa_destroy_key(key));
1572 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001573
1574exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 mbedtls_free(buffer);
1576 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001577}
1578/* END_CASE */
1579
1580/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001581void import_export(data_t *data,
1582 int type_arg,
1583 int usage_arg, int alg_arg,
1584 int lifetime_arg,
1585 int expected_bits,
1586 int export_size_delta,
1587 int expected_export_status_arg,
1588 /*whether reexport must give the original input exactly*/
1589 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001590{
Ronald Cron5425a212020-08-04 14:58:35 +02001591 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001592 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001593 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001594 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001595 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301596 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001597 unsigned char *exported = NULL;
1598 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001599 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001600 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001601 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001602 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001603 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001604
Moran Pekercb088e72018-07-17 17:36:59 +03001605 export_size = (ptrdiff_t) data->len + export_size_delta;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001606 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 if (!canonical_input) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001608 TEST_CALLOC(reexported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 }
1610 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001611
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 psa_set_key_lifetime(&attributes, lifetime);
1613 psa_set_key_usage_flags(&attributes, usage_arg);
1614 psa_set_key_algorithm(&attributes, alg);
1615 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001616
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001617 if (PSA_KEY_TYPE_IS_DH(type) &&
1618 expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) {
Przemek Stekiel654bef02022-12-15 13:28:02 +01001619 /* Simulate that buffer is too small, by decreasing its size by 1 byte. */
1620 export_size -= 1;
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001621 }
1622
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001623 /* Import the key */
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001624 TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key),
Przemek Stekiel2e7c33d2023-04-27 12:29:45 +02001625 PSA_SUCCESS);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001626
1627 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1629 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1630 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1631 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001632
1633 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 status = psa_export_key(key, exported, export_size, &exported_length);
1635 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001636
1637 /* The exported length must be set by psa_export_key() to a value between 0
1638 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1640 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1641 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001642
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1644 export_size - exported_length));
1645 if (status != PSA_SUCCESS) {
1646 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001647 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001648 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001649
Gilles Peskineea38a922021-02-13 00:05:16 +01001650 /* Run sanity checks on the exported key. For non-canonical inputs,
1651 * this validates the canonical representations. For canonical inputs,
1652 * this doesn't directly validate the implementation, but it still helps
1653 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 if (!psa_key_lifetime_is_external(lifetime)) {
1655 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301656 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 }
Archana4d7ae1d2021-07-07 02:50:22 +05301658 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001659
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 if (canonical_input) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001661 TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001663 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1665 &key2));
1666 PSA_ASSERT(psa_export_key(key2,
1667 reexported,
1668 export_size,
1669 &reexported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001670 TEST_MEMORY_COMPARE(exported, exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001671 reexported, reexported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001673 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 TEST_LE_U(exported_length,
1675 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1676 psa_get_key_bits(&got_attributes)));
Valerio Setti1eacae82023-07-28 16:07:03 +02001677 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
1678 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
1679 } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
1680 TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1681 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001682
1683destroy:
1684 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 PSA_ASSERT(psa_destroy_key(key));
1686 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001687
1688exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001689 /*
1690 * Key attributes may have been returned by psa_get_key_attributes()
1691 * thus reset them as required.
1692 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 psa_reset_key_attributes(&got_attributes);
1694 psa_destroy_key(key);
1695 mbedtls_free(exported);
1696 mbedtls_free(reexported);
1697 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001698}
1699/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001700
Moran Pekerf709f4a2018-06-06 17:26:04 +03001701/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001702void import_export_public_key(data_t *data,
1703 int type_arg, // key pair or public key
1704 int alg_arg,
1705 int lifetime_arg,
1706 int export_size_delta,
1707 int expected_export_status_arg,
1708 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001709{
Ronald Cron5425a212020-08-04 14:58:35 +02001710 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001711 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001712 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001713 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001714 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301715 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001716 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001717 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001718 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001719 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001720
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001722
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 psa_set_key_lifetime(&attributes, lifetime);
1724 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1725 psa_set_key_algorithm(&attributes, alg);
1726 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001727
1728 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001730
Gilles Peskine49c25912018-10-29 15:15:31 +01001731 /* Export the public key */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001732 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 status = psa_export_public_key(key,
1734 exported, export_size,
1735 &exported_length);
1736 TEST_EQUAL(status, expected_export_status);
1737 if (status == PSA_SUCCESS) {
1738 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001739 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1741 bits = psa_get_key_bits(&attributes);
1742 TEST_LE_U(expected_public_key->len,
1743 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1744 TEST_LE_U(expected_public_key->len,
1745 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1746 TEST_LE_U(expected_public_key->len,
1747 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001748 TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001749 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001750 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001751exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001752 /*
1753 * Key attributes may have been returned by psa_get_key_attributes()
1754 * thus reset them as required.
1755 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 mbedtls_free(exported);
1759 psa_destroy_key(key);
1760 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001761}
1762/* END_CASE */
1763
Gilles Peskine20035e32018-02-03 22:44:14 +01001764/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001765void import_and_exercise_key(data_t *data,
1766 int type_arg,
1767 int bits_arg,
1768 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001769{
Ronald Cron5425a212020-08-04 14:58:35 +02001770 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001771 psa_key_type_t type = type_arg;
1772 size_t bits = bits_arg;
1773 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001775 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001776 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001777
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 psa_set_key_usage_flags(&attributes, usage);
1781 psa_set_key_algorithm(&attributes, alg);
1782 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001783
1784 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001786
1787 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1789 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1790 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001791
1792 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001794 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001796
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 PSA_ASSERT(psa_destroy_key(key));
1798 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001799
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001800exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001801 /*
1802 * Key attributes may have been returned by psa_get_key_attributes()
1803 * thus reset them as required.
1804 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001806
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 psa_reset_key_attributes(&attributes);
1808 psa_destroy_key(key);
1809 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001810}
1811/* END_CASE */
1812
1813/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001814void effective_key_attributes(int type_arg, int expected_type_arg,
1815 int bits_arg, int expected_bits_arg,
1816 int usage_arg, int expected_usage_arg,
1817 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001818{
Ronald Cron5425a212020-08-04 14:58:35 +02001819 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001820 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001821 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001822 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001823 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001824 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001825 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001826 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001827 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001828 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001831
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 psa_set_key_usage_flags(&attributes, usage);
1833 psa_set_key_algorithm(&attributes, alg);
1834 psa_set_key_type(&attributes, key_type);
1835 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001836
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 PSA_ASSERT(psa_generate_key(&attributes, &key));
1838 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001839
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1841 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1842 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1843 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1844 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001845
1846exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001847 /*
1848 * Key attributes may have been returned by psa_get_key_attributes()
1849 * thus reset them as required.
1850 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001852
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 psa_destroy_key(key);
1854 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001855}
1856/* END_CASE */
1857
1858/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001859void check_key_policy(int type_arg, int bits_arg,
1860 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001861{
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1863 usage_arg,
1864 mbedtls_test_update_key_usage_flags(usage_arg),
1865 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001866 goto exit;
1867}
1868/* END_CASE */
1869
1870/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001871void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001872{
1873 /* Test each valid way of initializing the object, except for `= {0}`, as
1874 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1875 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001876 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001878 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1879 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001880
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001882
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1884 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1885 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 TEST_EQUAL(psa_get_key_type(&func), 0);
1888 TEST_EQUAL(psa_get_key_type(&init), 0);
1889 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001890
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 TEST_EQUAL(psa_get_key_bits(&func), 0);
1892 TEST_EQUAL(psa_get_key_bits(&init), 0);
1893 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001894
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1896 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1897 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001898
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1900 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1901 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001902}
1903/* END_CASE */
1904
1905/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001906void mac_key_policy(int policy_usage_arg,
1907 int policy_alg_arg,
1908 int key_type_arg,
1909 data_t *key_data,
1910 int exercise_alg_arg,
1911 int expected_status_sign_arg,
1912 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001913{
Ronald Cron5425a212020-08-04 14:58:35 +02001914 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001915 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001916 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001917 psa_key_type_t key_type = key_type_arg;
1918 psa_algorithm_t policy_alg = policy_alg_arg;
1919 psa_algorithm_t exercise_alg = exercise_alg_arg;
1920 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001921 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001922 psa_status_t expected_status_sign = expected_status_sign_arg;
1923 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001924 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001927
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 psa_set_key_usage_flags(&attributes, policy_usage);
1929 psa_set_key_algorithm(&attributes, policy_alg);
1930 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001931
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1933 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001934
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1936 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001937
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1939 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001940
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001941 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001943 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1945 input, 128,
1946 mac, PSA_MAC_MAX_SIZE, &mac_len),
1947 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001948
Neil Armstrong3af9b972022-02-07 12:20:21 +01001949 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 PSA_ASSERT(psa_mac_abort(&operation));
1951 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1952 if (status == PSA_SUCCESS) {
1953 status = psa_mac_update(&operation, input, 128);
1954 if (status == PSA_SUCCESS) {
1955 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1956 &mac_len),
1957 expected_status_sign);
1958 } else {
1959 TEST_EQUAL(status, expected_status_sign);
1960 }
1961 } else {
1962 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001963 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001965
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001966 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 status = psa_mac_verify(key, exercise_alg, input, 128,
1968 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001969
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1971 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1972 } else {
1973 TEST_EQUAL(status, expected_status_verify);
1974 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001975
Neil Armstrong3af9b972022-02-07 12:20:21 +01001976 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1978 if (status == PSA_SUCCESS) {
1979 status = psa_mac_update(&operation, input, 128);
1980 if (status == PSA_SUCCESS) {
1981 status = psa_mac_verify_finish(&operation, mac, mac_len);
1982 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1983 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1984 } else {
1985 TEST_EQUAL(status, expected_status_verify);
1986 }
1987 } else {
1988 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001989 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 } else {
1991 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001992 }
1993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001995
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 memset(mac, 0, sizeof(mac));
1997 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1998 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001999
2000exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 psa_mac_abort(&operation);
2002 psa_destroy_key(key);
2003 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002004}
2005/* END_CASE */
2006
2007/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002008void cipher_key_policy(int policy_usage_arg,
2009 int policy_alg,
2010 int key_type,
2011 data_t *key_data,
2012 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002013{
Ronald Cron5425a212020-08-04 14:58:35 +02002014 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002015 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002016 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002017 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002018 size_t output_buffer_size = 0;
2019 size_t input_buffer_size = 0;
2020 size_t output_length = 0;
2021 uint8_t *output = NULL;
2022 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002023 psa_status_t status;
2024
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
2026 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
2027 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002028
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002029 TEST_CALLOC(input, input_buffer_size);
2030 TEST_CALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002031
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 psa_set_key_usage_flags(&attributes, policy_usage);
2035 psa_set_key_algorithm(&attributes, policy_alg);
2036 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2039 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002040
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002041 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 TEST_EQUAL(policy_usage,
2043 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002044
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002045 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
2047 output, output_buffer_size,
2048 &output_length);
2049 if (policy_alg == exercise_alg &&
2050 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2051 PSA_ASSERT(status);
2052 } else {
2053 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2054 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002055
2056 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
2058 if (policy_alg == exercise_alg &&
2059 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2060 PSA_ASSERT(status);
2061 } else {
2062 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2063 }
2064 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002065
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002066 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
2068 input, input_buffer_size,
2069 &output_length);
2070 if (policy_alg == exercise_alg &&
2071 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2072 PSA_ASSERT(status);
2073 } else {
2074 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2075 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002076
2077 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
2079 if (policy_alg == exercise_alg &&
2080 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2081 PSA_ASSERT(status);
2082 } else {
2083 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2084 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002085
2086exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 psa_cipher_abort(&operation);
2088 mbedtls_free(input);
2089 mbedtls_free(output);
2090 psa_destroy_key(key);
2091 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002092}
2093/* END_CASE */
2094
2095/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002096void aead_key_policy(int policy_usage_arg,
2097 int policy_alg,
2098 int key_type,
2099 data_t *key_data,
2100 int nonce_length_arg,
2101 int tag_length_arg,
2102 int exercise_alg,
2103 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002104{
Ronald Cron5425a212020-08-04 14:58:35 +02002105 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002106 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002107 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002108 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002109 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002110 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002112 size_t nonce_length = nonce_length_arg;
2113 unsigned char tag[16];
2114 size_t tag_length = tag_length_arg;
2115 size_t output_length;
2116
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 TEST_LE_U(nonce_length, sizeof(nonce));
2118 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002121
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 psa_set_key_usage_flags(&attributes, policy_usage);
2123 psa_set_key_algorithm(&attributes, policy_alg);
2124 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2127 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002128
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002129 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 TEST_EQUAL(policy_usage,
2131 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002132
Neil Armstrong752d8112022-02-07 14:51:11 +01002133 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 status = psa_aead_encrypt(key, exercise_alg,
2135 nonce, nonce_length,
2136 NULL, 0,
2137 NULL, 0,
2138 tag, tag_length,
2139 &output_length);
2140 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2141 TEST_EQUAL(status, expected_status);
2142 } else {
2143 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2144 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002145
Neil Armstrong752d8112022-02-07 14:51:11 +01002146 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2148 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2149 TEST_EQUAL(status, expected_status);
2150 } else {
2151 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2152 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002153
2154 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 memset(tag, 0, sizeof(tag));
2156 status = psa_aead_decrypt(key, exercise_alg,
2157 nonce, nonce_length,
2158 NULL, 0,
2159 tag, tag_length,
2160 NULL, 0,
2161 &output_length);
2162 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2163 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2164 } else if (expected_status == PSA_SUCCESS) {
2165 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2166 } else {
2167 TEST_EQUAL(status, expected_status);
2168 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002169
Neil Armstrong752d8112022-02-07 14:51:11 +01002170 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 PSA_ASSERT(psa_aead_abort(&operation));
2172 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2173 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2174 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2175 } else {
2176 TEST_EQUAL(status, expected_status);
2177 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002178
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002179exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 PSA_ASSERT(psa_aead_abort(&operation));
2181 psa_destroy_key(key);
2182 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002183}
2184/* END_CASE */
2185
2186/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002187void asymmetric_encryption_key_policy(int policy_usage_arg,
2188 int policy_alg,
2189 int key_type,
2190 data_t *key_data,
Valerio Settif202c292024-01-15 10:42:37 +01002191 int exercise_alg,
2192 int use_opaque_key)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002193{
Ronald Cron5425a212020-08-04 14:58:35 +02002194 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002195 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002196 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002197 psa_status_t status;
2198 size_t key_bits;
2199 size_t buffer_length;
2200 unsigned char *buffer = NULL;
2201 size_t output_length;
2202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002204
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 psa_set_key_usage_flags(&attributes, policy_usage);
2206 psa_set_key_algorithm(&attributes, policy_alg);
2207 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002208
Valerio Settif202c292024-01-15 10:42:37 +01002209 if (use_opaque_key) {
2210 psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
2211 PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION));
2212 }
2213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2215 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002216
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002217 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 TEST_EQUAL(policy_usage,
2219 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2222 key_bits = psa_get_key_bits(&attributes);
2223 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2224 exercise_alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002225 TEST_CALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002226
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 status = psa_asymmetric_encrypt(key, exercise_alg,
2228 NULL, 0,
2229 NULL, 0,
2230 buffer, buffer_length,
2231 &output_length);
2232 if (policy_alg == exercise_alg &&
2233 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2234 PSA_ASSERT(status);
2235 } else {
2236 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2237 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 if (buffer_length != 0) {
2240 memset(buffer, 0, buffer_length);
2241 }
2242 status = psa_asymmetric_decrypt(key, exercise_alg,
2243 buffer, buffer_length,
2244 NULL, 0,
2245 buffer, buffer_length,
2246 &output_length);
2247 if (policy_alg == exercise_alg &&
2248 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2249 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2250 } else {
2251 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2252 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002253
2254exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002255 /*
2256 * Key attributes may have been returned by psa_get_key_attributes()
2257 * thus reset them as required.
2258 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002260
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 psa_destroy_key(key);
2262 PSA_DONE();
2263 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002264}
2265/* END_CASE */
2266
2267/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002268void asymmetric_signature_key_policy(int policy_usage_arg,
2269 int policy_alg,
2270 int key_type,
2271 data_t *key_data,
2272 int exercise_alg,
2273 int payload_length_arg,
2274 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002275{
Ronald Cron5425a212020-08-04 14:58:35 +02002276 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002277 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002278 psa_key_usage_t policy_usage = policy_usage_arg;
2279 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002280 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002282 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2283 * compatible with the policy and `payload_length_arg` is supposed to be
2284 * a valid input length to sign. If `payload_length_arg <= 0`,
2285 * `exercise_alg` is supposed to be forbidden by the policy. */
2286 int compatible_alg = payload_length_arg > 0;
2287 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002289 size_t signature_length;
2290
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002291 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002292 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 TEST_EQUAL(expected_usage,
2294 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002295
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 psa_set_key_usage_flags(&attributes, policy_usage);
2299 psa_set_key_algorithm(&attributes, policy_alg);
2300 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002301
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2303 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002304
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 status = psa_sign_hash(key, exercise_alg,
2308 payload, payload_length,
2309 signature, sizeof(signature),
2310 &signature_length);
2311 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2312 PSA_ASSERT(status);
2313 } else {
2314 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2315 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002316
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 memset(signature, 0, sizeof(signature));
2318 status = psa_verify_hash(key, exercise_alg,
2319 payload, payload_length,
2320 signature, sizeof(signature));
2321 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2322 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2323 } else {
2324 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2325 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002326
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2328 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2329 status = psa_sign_message(key, exercise_alg,
2330 payload, payload_length,
2331 signature, sizeof(signature),
2332 &signature_length);
2333 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2334 PSA_ASSERT(status);
2335 } else {
2336 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2337 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 memset(signature, 0, sizeof(signature));
2340 status = psa_verify_message(key, exercise_alg,
2341 payload, payload_length,
2342 signature, sizeof(signature));
2343 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2344 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2345 } else {
2346 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2347 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002348 }
2349
Gilles Peskined5b33222018-06-18 22:20:03 +02002350exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 psa_destroy_key(key);
2352 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002353}
2354/* END_CASE */
2355
Janos Follathba3fab92019-06-11 14:50:16 +01002356/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002357void derive_key_policy(int policy_usage,
2358 int policy_alg,
2359 int key_type,
2360 data_t *key_data,
2361 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002362{
Ronald Cron5425a212020-08-04 14:58:35 +02002363 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002364 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002365 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002366 psa_status_t status;
2367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002369
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 psa_set_key_usage_flags(&attributes, policy_usage);
2371 psa_set_key_algorithm(&attributes, policy_alg);
2372 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002373
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2375 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002378
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2380 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2381 PSA_ASSERT(psa_key_derivation_input_bytes(
2382 &operation,
2383 PSA_KEY_DERIVATION_INPUT_SEED,
2384 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002385 }
Janos Follathba3fab92019-06-11 14:50:16 +01002386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 status = psa_key_derivation_input_key(&operation,
2388 PSA_KEY_DERIVATION_INPUT_SECRET,
2389 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002390
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 if (policy_alg == exercise_alg &&
2392 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2393 PSA_ASSERT(status);
2394 } else {
2395 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2396 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002397
2398exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 psa_key_derivation_abort(&operation);
2400 psa_destroy_key(key);
2401 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002402}
2403/* END_CASE */
2404
2405/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002406void agreement_key_policy(int policy_usage,
2407 int policy_alg,
2408 int key_type_arg,
2409 data_t *key_data,
2410 int exercise_alg,
2411 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002412{
Ronald Cron5425a212020-08-04 14:58:35 +02002413 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002414 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002415 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002416 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002417 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002418 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002421
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 psa_set_key_usage_flags(&attributes, policy_usage);
2423 psa_set_key_algorithm(&attributes, policy_alg);
2424 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002425
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2427 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2430 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002431
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002433
2434exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 psa_key_derivation_abort(&operation);
2436 psa_destroy_key(key);
2437 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002438}
2439/* END_CASE */
2440
2441/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002442void key_policy_alg2(int key_type_arg, data_t *key_data,
2443 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002444{
Ronald Cron5425a212020-08-04 14:58:35 +02002445 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002446 psa_key_type_t key_type = key_type_arg;
2447 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2448 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2449 psa_key_usage_t usage = usage_arg;
2450 psa_algorithm_t alg = alg_arg;
2451 psa_algorithm_t alg2 = alg2_arg;
2452
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002454
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 psa_set_key_usage_flags(&attributes, usage);
2456 psa_set_key_algorithm(&attributes, alg);
2457 psa_set_key_enrollment_algorithm(&attributes, alg2);
2458 psa_set_key_type(&attributes, key_type);
2459 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2460 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002461
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002462 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 usage = mbedtls_test_update_key_usage_flags(usage);
2464 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2465 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2466 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2467 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002470 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 }
2472 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002473 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002475
2476exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002477 /*
2478 * Key attributes may have been returned by psa_get_key_attributes()
2479 * thus reset them as required.
2480 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 psa_destroy_key(key);
2484 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002485}
2486/* END_CASE */
2487
2488/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002489void raw_agreement_key_policy(int policy_usage,
2490 int policy_alg,
2491 int key_type_arg,
2492 data_t *key_data,
2493 int exercise_alg,
2494 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002495{
Ronald Cron5425a212020-08-04 14:58:35 +02002496 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002497 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002498 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002499 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002500 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002501 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002504
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 psa_set_key_usage_flags(&attributes, policy_usage);
2506 psa_set_key_algorithm(&attributes, policy_alg);
2507 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002508
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2510 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002513
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002515
2516exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 psa_key_derivation_abort(&operation);
2518 psa_destroy_key(key);
2519 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002520}
2521/* END_CASE */
2522
2523/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002524void copy_success(int source_usage_arg,
2525 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002526 int source_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 int type_arg, data_t *material,
2528 int copy_attributes,
2529 int target_usage_arg,
2530 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002531 int target_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 int expected_usage_arg,
2533 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002534{
Gilles Peskineca25db92019-04-19 11:43:08 +02002535 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2536 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002537 psa_key_usage_t expected_usage = expected_usage_arg;
2538 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002539 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302540 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2541 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002542 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2543 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002544 uint8_t *export_buffer = NULL;
2545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002547
Gilles Peskineca25db92019-04-19 11:43:08 +02002548 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2550 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2551 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2552 psa_set_key_type(&source_attributes, type_arg);
2553 psa_set_key_lifetime(&source_attributes, source_lifetime);
2554 PSA_ASSERT(psa_import_key(&source_attributes,
2555 material->x, material->len,
2556 &source_key));
2557 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002558
Gilles Peskineca25db92019-04-19 11:43:08 +02002559 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002561 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002562 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 if (target_usage_arg != -1) {
2566 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2567 }
2568 if (target_alg_arg != -1) {
2569 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2570 }
2571 if (target_alg2_arg != -1) {
2572 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2573 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002574
Archana8a180362021-07-05 02:18:48 +05302575
Gilles Peskine57ab7212019-01-28 13:03:09 +01002576 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 PSA_ASSERT(psa_copy_key(source_key,
2578 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002579
2580 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002582
2583 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2585 TEST_EQUAL(psa_get_key_type(&source_attributes),
2586 psa_get_key_type(&target_attributes));
2587 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2588 psa_get_key_bits(&target_attributes));
2589 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2590 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2591 TEST_EQUAL(expected_alg2,
2592 psa_get_key_enrollment_algorithm(&target_attributes));
2593 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002594 size_t length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002595 TEST_CALLOC(export_buffer, material->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2597 material->len, &length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002598 TEST_MEMORY_COMPARE(material->x, material->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002599 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002600 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 if (!psa_key_lifetime_is_external(target_lifetime)) {
2603 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302604 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 }
2606 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302607 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 }
Archana8a180362021-07-05 02:18:48 +05302609 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002612
2613exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002614 /*
2615 * Source and target key attributes may have been returned by
2616 * psa_get_key_attributes() thus reset them as required.
2617 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 psa_reset_key_attributes(&source_attributes);
2619 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002620
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 PSA_DONE();
2622 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002623}
2624/* END_CASE */
2625
2626/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002627void copy_fail(int source_usage_arg,
2628 int source_alg_arg, int source_alg2_arg,
2629 int source_lifetime_arg,
2630 int type_arg, data_t *material,
2631 int target_type_arg, int target_bits_arg,
2632 int target_usage_arg,
2633 int target_alg_arg, int target_alg2_arg,
2634 int target_id_arg, int target_lifetime_arg,
2635 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002636{
2637 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2638 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002639 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2640 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002642
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002644
2645 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2647 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2648 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2649 psa_set_key_type(&source_attributes, type_arg);
2650 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2651 PSA_ASSERT(psa_import_key(&source_attributes,
2652 material->x, material->len,
2653 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002654
2655 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 psa_set_key_id(&target_attributes, key_id);
2657 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2658 psa_set_key_type(&target_attributes, target_type_arg);
2659 psa_set_key_bits(&target_attributes, target_bits_arg);
2660 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2661 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2662 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002663
2664 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 TEST_EQUAL(psa_copy_key(source_key,
2666 &target_attributes, &target_key),
2667 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002670
Gilles Peskine4a644642019-05-03 17:14:08 +02002671exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 psa_reset_key_attributes(&source_attributes);
2673 psa_reset_key_attributes(&target_attributes);
2674 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002675}
2676/* END_CASE */
2677
2678/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002679void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002680{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002681 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002682 /* Test each valid way of initializing the object, except for `= {0}`, as
2683 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2684 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002685 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002687 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2688 psa_hash_operation_t zero;
2689
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002691
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002692 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2694 PSA_ERROR_BAD_STATE);
2695 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2696 PSA_ERROR_BAD_STATE);
2697 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2698 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002699
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002700 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 PSA_ASSERT(psa_hash_abort(&func));
2702 PSA_ASSERT(psa_hash_abort(&init));
2703 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002704}
2705/* END_CASE */
2706
2707/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002708void hash_setup(int alg_arg,
2709 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002710{
2711 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002712 uint8_t *output = NULL;
2713 size_t output_size = 0;
2714 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002715 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002716 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002717 psa_status_t status;
2718
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002720
Neil Armstrongedb20862022-02-07 15:47:44 +01002721 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 output_size = PSA_HASH_LENGTH(alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002723 TEST_CALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002724
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 status = psa_hash_compute(alg, NULL, 0,
2726 output, output_size, &output_length);
2727 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002728
2729 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 status = psa_hash_setup(&operation, alg);
2731 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002732
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002733 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002735
2736 /* If setup failed, reproduce the failure, so as to
2737 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 if (status != PSA_SUCCESS) {
2739 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2740 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002741
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002742 /* Now the operation object should be reusable. */
2743#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2745 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002746#endif
2747
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002748exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 mbedtls_free(output);
2750 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002751}
2752/* END_CASE */
2753
2754/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002755void hash_compute_fail(int alg_arg, data_t *input,
2756 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002757{
2758 psa_algorithm_t alg = alg_arg;
2759 uint8_t *output = NULL;
2760 size_t output_size = output_size_arg;
2761 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002762 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002763 psa_status_t expected_status = expected_status_arg;
2764 psa_status_t status;
2765
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002766 TEST_CALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002767
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002769
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002770 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 status = psa_hash_compute(alg, input->x, input->len,
2772 output, output_size, &output_length);
2773 TEST_EQUAL(status, expected_status);
2774 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002775
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002776 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 status = psa_hash_setup(&operation, alg);
2778 if (status == PSA_SUCCESS) {
2779 status = psa_hash_update(&operation, input->x, input->len);
2780 if (status == PSA_SUCCESS) {
2781 status = psa_hash_finish(&operation, output, output_size,
2782 &output_length);
2783 if (status == PSA_SUCCESS) {
2784 TEST_LE_U(output_length, output_size);
2785 } else {
2786 TEST_EQUAL(status, expected_status);
2787 }
2788 } else {
2789 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002790 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 } else {
2792 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002793 }
2794
Gilles Peskine0a749c82019-11-28 19:33:58 +01002795exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 PSA_ASSERT(psa_hash_abort(&operation));
2797 mbedtls_free(output);
2798 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002799}
2800/* END_CASE */
2801
2802/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002803void hash_compare_fail(int alg_arg, data_t *input,
2804 data_t *reference_hash,
2805 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002806{
2807 psa_algorithm_t alg = alg_arg;
2808 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002809 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002810 psa_status_t status;
2811
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002813
Neil Armstrong55a1be12022-02-07 11:23:20 +01002814 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 status = psa_hash_compare(alg, input->x, input->len,
2816 reference_hash->x, reference_hash->len);
2817 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002818
Neil Armstrong55a1be12022-02-07 11:23:20 +01002819 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 status = psa_hash_setup(&operation, alg);
2821 if (status == PSA_SUCCESS) {
2822 status = psa_hash_update(&operation, input->x, input->len);
2823 if (status == PSA_SUCCESS) {
2824 status = psa_hash_verify(&operation, reference_hash->x,
2825 reference_hash->len);
2826 TEST_EQUAL(status, expected_status);
2827 } else {
2828 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002829 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 } else {
2831 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002832 }
2833
Gilles Peskine88e08462020-01-28 20:43:00 +01002834exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 PSA_ASSERT(psa_hash_abort(&operation));
2836 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002837}
2838/* END_CASE */
2839
2840/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002841void hash_compute_compare(int alg_arg, data_t *input,
2842 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002843{
2844 psa_algorithm_t alg = alg_arg;
2845 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2846 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002847 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002848 size_t i;
2849
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002851
Neil Armstrongca30a002022-02-07 11:40:23 +01002852 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2854 output, 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);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002859
Neil Armstrongca30a002022-02-07 11:40:23 +01002860 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 PSA_ASSERT(psa_hash_setup(&operation, alg));
2862 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2863 PSA_ASSERT(psa_hash_finish(&operation, output,
2864 PSA_HASH_LENGTH(alg),
2865 &output_length));
2866 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002867 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002868 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002869
2870 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2872 output, sizeof(output),
2873 &output_length));
2874 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002875 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002876 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002877
Neil Armstrongca30a002022-02-07 11:40:23 +01002878 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 PSA_ASSERT(psa_hash_setup(&operation, alg));
2880 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2881 PSA_ASSERT(psa_hash_finish(&operation, output,
2882 sizeof(output), &output_length));
2883 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002884 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002885 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002886
2887 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2889 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002890
Neil Armstrongca30a002022-02-07 11:40:23 +01002891 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 PSA_ASSERT(psa_hash_setup(&operation, alg));
2893 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2894 PSA_ASSERT(psa_hash_verify(&operation, output,
2895 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002896
2897 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2899 output, output_length + 1),
2900 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002901
Neil Armstrongca30a002022-02-07 11:40:23 +01002902 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 PSA_ASSERT(psa_hash_setup(&operation, alg));
2904 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2905 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2906 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002907
2908 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2910 output, output_length - 1),
2911 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002912
Neil Armstrongca30a002022-02-07 11:40:23 +01002913 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 PSA_ASSERT(psa_hash_setup(&operation, alg));
2915 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2916 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2917 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002918
Gilles Peskine0a749c82019-11-28 19:33:58 +01002919 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 for (i = 0; i < output_length; i++) {
2921 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002922 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002923
2924 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2926 output, output_length),
2927 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002928
2929 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 PSA_ASSERT(psa_hash_setup(&operation, alg));
2931 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2932 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2933 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002934
Gilles Peskine0a749c82019-11-28 19:33:58 +01002935 output[i] ^= 1;
2936 }
2937
2938exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 PSA_ASSERT(psa_hash_abort(&operation));
2940 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002941}
2942/* END_CASE */
2943
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002944/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002945void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002946{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002947 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002948 unsigned char input[] = "";
2949 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002950 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002951 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2952 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2954 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002955 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002956 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002957 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002958
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002960
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002961 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 PSA_ASSERT(psa_hash_setup(&operation, alg));
2963 ASSERT_OPERATION_IS_ACTIVE(operation);
2964 TEST_EQUAL(psa_hash_setup(&operation, alg),
2965 PSA_ERROR_BAD_STATE);
2966 ASSERT_OPERATION_IS_INACTIVE(operation);
2967 PSA_ASSERT(psa_hash_abort(&operation));
2968 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002969
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002970 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2972 PSA_ERROR_BAD_STATE);
2973 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002974
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002975 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002977 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 ASSERT_OPERATION_IS_ACTIVE(operation);
2979 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2980 PSA_ERROR_BAD_STATE);
2981 ASSERT_OPERATION_IS_INACTIVE(operation);
2982 PSA_ASSERT(psa_hash_abort(&operation));
2983 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002984
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002985 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 PSA_ASSERT(psa_hash_setup(&operation, alg));
2987 PSA_ASSERT(psa_hash_finish(&operation,
2988 hash, sizeof(hash), &hash_len));
2989 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2990 PSA_ERROR_BAD_STATE);
2991 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002992
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002993 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 TEST_EQUAL(psa_hash_verify(&operation,
2995 valid_hash, sizeof(valid_hash)),
2996 PSA_ERROR_BAD_STATE);
2997 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002998
2999 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 PSA_ASSERT(psa_hash_setup(&operation, alg));
3001 PSA_ASSERT(psa_hash_finish(&operation,
3002 hash, sizeof(hash), &hash_len));
3003 TEST_EQUAL(psa_hash_verify(&operation,
3004 valid_hash, sizeof(valid_hash)),
3005 PSA_ERROR_BAD_STATE);
3006 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003007
3008 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 PSA_ASSERT(psa_hash_setup(&operation, alg));
3010 ASSERT_OPERATION_IS_ACTIVE(operation);
3011 PSA_ASSERT(psa_hash_verify(&operation,
3012 valid_hash, sizeof(valid_hash)));
3013 ASSERT_OPERATION_IS_INACTIVE(operation);
3014 TEST_EQUAL(psa_hash_verify(&operation,
3015 valid_hash, sizeof(valid_hash)),
3016 PSA_ERROR_BAD_STATE);
3017 ASSERT_OPERATION_IS_INACTIVE(operation);
3018 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003019
3020 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 TEST_EQUAL(psa_hash_finish(&operation,
3022 hash, sizeof(hash), &hash_len),
3023 PSA_ERROR_BAD_STATE);
3024 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003025
3026 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 PSA_ASSERT(psa_hash_setup(&operation, alg));
3028 PSA_ASSERT(psa_hash_finish(&operation,
3029 hash, sizeof(hash), &hash_len));
3030 TEST_EQUAL(psa_hash_finish(&operation,
3031 hash, sizeof(hash), &hash_len),
3032 PSA_ERROR_BAD_STATE);
3033 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003034
3035 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 PSA_ASSERT(psa_hash_setup(&operation, alg));
3037 PSA_ASSERT(psa_hash_verify(&operation,
3038 valid_hash, sizeof(valid_hash)));
3039 TEST_EQUAL(psa_hash_finish(&operation,
3040 hash, sizeof(hash), &hash_len),
3041 PSA_ERROR_BAD_STATE);
3042 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02003043
3044exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02003046}
3047/* END_CASE */
3048
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003049/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003050void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03003051{
3052 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02003053 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
3054 * appended to it */
3055 unsigned char hash[] = {
3056 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
3057 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
3059 };
3060 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003061 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03003062
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03003064
itayzafrir27e69452018-11-01 14:26:34 +02003065 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 PSA_ASSERT(psa_hash_setup(&operation, alg));
3067 ASSERT_OPERATION_IS_ACTIVE(operation);
3068 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
3069 PSA_ERROR_INVALID_SIGNATURE);
3070 ASSERT_OPERATION_IS_INACTIVE(operation);
3071 PSA_ASSERT(psa_hash_abort(&operation));
3072 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03003073
itayzafrir27e69452018-11-01 14:26:34 +02003074 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 PSA_ASSERT(psa_hash_setup(&operation, alg));
3076 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
3077 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03003078
itayzafrir27e69452018-11-01 14:26:34 +02003079 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 PSA_ASSERT(psa_hash_setup(&operation, alg));
3081 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
3082 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03003083
itayzafrirec93d302018-10-18 18:01:10 +03003084exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03003086}
3087/* END_CASE */
3088
Ronald Cronee414c72021-03-18 18:50:08 +01003089/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003090void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03003091{
3092 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003093 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003095 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003096 size_t hash_len;
3097
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03003099
itayzafrir58028322018-10-25 10:22:01 +03003100 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 PSA_ASSERT(psa_hash_setup(&operation, alg));
3102 TEST_EQUAL(psa_hash_finish(&operation,
3103 hash, expected_size - 1, &hash_len),
3104 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003105
3106exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003108}
3109/* END_CASE */
3110
Ronald Cronee414c72021-03-18 18:50:08 +01003111/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003112void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003113{
3114 psa_algorithm_t alg = PSA_ALG_SHA_256;
3115 unsigned char hash[PSA_HASH_MAX_SIZE];
3116 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3117 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3118 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3119 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3120 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3121 size_t hash_len;
3122
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 PSA_ASSERT(psa_crypto_init());
3124 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003125
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3127 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3128 PSA_ASSERT(psa_hash_finish(&op_finished,
3129 hash, sizeof(hash), &hash_len));
3130 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3131 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003132
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3134 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003135
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3137 PSA_ASSERT(psa_hash_finish(&op_init,
3138 hash, sizeof(hash), &hash_len));
3139 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3140 PSA_ASSERT(psa_hash_finish(&op_finished,
3141 hash, sizeof(hash), &hash_len));
3142 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3143 PSA_ASSERT(psa_hash_finish(&op_aborted,
3144 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003145
3146exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 psa_hash_abort(&op_source);
3148 psa_hash_abort(&op_init);
3149 psa_hash_abort(&op_setup);
3150 psa_hash_abort(&op_finished);
3151 psa_hash_abort(&op_aborted);
3152 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003153}
3154/* END_CASE */
3155
Ronald Cronee414c72021-03-18 18:50:08 +01003156/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003157void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003158{
3159 psa_algorithm_t alg = PSA_ALG_SHA_256;
3160 unsigned char hash[PSA_HASH_MAX_SIZE];
3161 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3162 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3163 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3164 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3165 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3166 size_t hash_len;
3167
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003169
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3171 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3172 PSA_ASSERT(psa_hash_finish(&op_finished,
3173 hash, sizeof(hash), &hash_len));
3174 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3175 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003176
Gilles Peskine449bd832023-01-11 14:50:10 +01003177 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3178 PSA_ASSERT(psa_hash_finish(&op_target,
3179 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003180
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3182 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3183 PSA_ERROR_BAD_STATE);
3184 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3185 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003186
3187exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 psa_hash_abort(&op_target);
3189 psa_hash_abort(&op_init);
3190 psa_hash_abort(&op_setup);
3191 psa_hash_abort(&op_finished);
3192 psa_hash_abort(&op_aborted);
3193 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003194}
3195/* END_CASE */
3196
itayzafrir58028322018-10-25 10:22:01 +03003197/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003198void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003199{
Jaeden Amero252ef282019-02-15 14:05:35 +00003200 const uint8_t input[1] = { 0 };
3201
Jaeden Amero769ce272019-01-04 11:48:03 +00003202 /* Test each valid way of initializing the object, except for `= {0}`, as
3203 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3204 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003205 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003207 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3208 psa_mac_operation_t zero;
3209
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003211
Jaeden Amero252ef282019-02-15 14:05:35 +00003212 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003213 TEST_EQUAL(psa_mac_update(&func,
3214 input, sizeof(input)),
3215 PSA_ERROR_BAD_STATE);
3216 TEST_EQUAL(psa_mac_update(&init,
3217 input, sizeof(input)),
3218 PSA_ERROR_BAD_STATE);
3219 TEST_EQUAL(psa_mac_update(&zero,
3220 input, sizeof(input)),
3221 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003222
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003223 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 PSA_ASSERT(psa_mac_abort(&func));
3225 PSA_ASSERT(psa_mac_abort(&init));
3226 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003227}
3228/* END_CASE */
3229
3230/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003231void mac_setup(int key_type_arg,
3232 data_t *key,
3233 int alg_arg,
3234 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003235{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003236 psa_key_type_t key_type = key_type_arg;
3237 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003238 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003239 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003240 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3241#if defined(KNOWN_SUPPORTED_MAC_ALG)
3242 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3243#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003244
Gilles Peskine449bd832023-01-11 14:50:10 +01003245 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003246
Gilles Peskine449bd832023-01-11 14:50:10 +01003247 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3248 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003249 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 }
3251 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003252
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003253 /* The operation object should be reusable. */
3254#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003255 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3256 smoke_test_key_data,
3257 sizeof(smoke_test_key_data),
3258 KNOWN_SUPPORTED_MAC_ALG,
3259 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003260 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 }
3262 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003263#endif
3264
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003265exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003266 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003267}
3268/* END_CASE */
3269
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003270/* 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 +01003271void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003272{
Ronald Cron5425a212020-08-04 14:58:35 +02003273 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003274 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3275 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003276 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003277 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3278 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3280 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003281 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003282 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3283 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3284 size_t sign_mac_length = 0;
3285 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3286 const uint8_t verify_mac[] = {
3287 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3288 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003289 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3290 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003291
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 PSA_ASSERT(psa_crypto_init());
3293 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3294 psa_set_key_algorithm(&attributes, alg);
3295 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003296
Gilles Peskine449bd832023-01-11 14:50:10 +01003297 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3298 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003299
Jaeden Amero252ef282019-02-15 14:05:35 +00003300 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003301 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3302 PSA_ERROR_BAD_STATE);
3303 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003304
3305 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3307 &sign_mac_length),
3308 PSA_ERROR_BAD_STATE);
3309 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003310
3311 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003312 TEST_EQUAL(psa_mac_verify_finish(&operation,
3313 verify_mac, sizeof(verify_mac)),
3314 PSA_ERROR_BAD_STATE);
3315 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003316
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003317 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003318 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3319 ASSERT_OPERATION_IS_ACTIVE(operation);
3320 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3321 PSA_ERROR_BAD_STATE);
3322 ASSERT_OPERATION_IS_INACTIVE(operation);
3323 PSA_ASSERT(psa_mac_abort(&operation));
3324 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003325
Jaeden Amero252ef282019-02-15 14:05:35 +00003326 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3328 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3329 PSA_ASSERT(psa_mac_sign_finish(&operation,
3330 sign_mac, sizeof(sign_mac),
3331 &sign_mac_length));
3332 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3333 PSA_ERROR_BAD_STATE);
3334 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003335
3336 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003337 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3338 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3339 PSA_ASSERT(psa_mac_verify_finish(&operation,
3340 verify_mac, sizeof(verify_mac)));
3341 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3342 PSA_ERROR_BAD_STATE);
3343 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003344
3345 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3347 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3348 PSA_ASSERT(psa_mac_sign_finish(&operation,
3349 sign_mac, sizeof(sign_mac),
3350 &sign_mac_length));
3351 TEST_EQUAL(psa_mac_sign_finish(&operation,
3352 sign_mac, sizeof(sign_mac),
3353 &sign_mac_length),
3354 PSA_ERROR_BAD_STATE);
3355 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003356
3357 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003358 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3359 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3360 PSA_ASSERT(psa_mac_verify_finish(&operation,
3361 verify_mac, sizeof(verify_mac)));
3362 TEST_EQUAL(psa_mac_verify_finish(&operation,
3363 verify_mac, sizeof(verify_mac)),
3364 PSA_ERROR_BAD_STATE);
3365 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003366
3367 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003368 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3369 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3370 ASSERT_OPERATION_IS_ACTIVE(operation);
3371 TEST_EQUAL(psa_mac_verify_finish(&operation,
3372 verify_mac, sizeof(verify_mac)),
3373 PSA_ERROR_BAD_STATE);
3374 ASSERT_OPERATION_IS_INACTIVE(operation);
3375 PSA_ASSERT(psa_mac_abort(&operation));
3376 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003377
3378 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3380 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3381 ASSERT_OPERATION_IS_ACTIVE(operation);
3382 TEST_EQUAL(psa_mac_sign_finish(&operation,
3383 sign_mac, sizeof(sign_mac),
3384 &sign_mac_length),
3385 PSA_ERROR_BAD_STATE);
3386 ASSERT_OPERATION_IS_INACTIVE(operation);
3387 PSA_ASSERT(psa_mac_abort(&operation));
3388 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003389
Gilles Peskine449bd832023-01-11 14:50:10 +01003390 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003391
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003392exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003394}
3395/* END_CASE */
3396
3397/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003398void mac_sign_verify_multi(int key_type_arg,
3399 data_t *key_data,
3400 int alg_arg,
3401 data_t *input,
3402 int is_verify,
3403 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003404{
3405 size_t data_part_len = 0;
3406
Gilles Peskine449bd832023-01-11 14:50:10 +01003407 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003408 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003410
Gilles Peskine449bd832023-01-11 14:50:10 +01003411 if (mac_multipart_internal_func(key_type_arg, key_data,
3412 alg_arg,
3413 input, data_part_len,
3414 expected_mac,
3415 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003416 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003417 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003418
3419 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003421
Gilles Peskine449bd832023-01-11 14:50:10 +01003422 if (mac_multipart_internal_func(key_type_arg, key_data,
3423 alg_arg,
3424 input, data_part_len,
3425 expected_mac,
3426 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003427 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003429 }
3430
3431 /* Goto is required to silence warnings about unused labels, as we
3432 * don't actually do any test assertions in this function. */
3433 goto exit;
3434}
3435/* END_CASE */
3436
3437/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003438void mac_sign(int key_type_arg,
3439 data_t *key_data,
3440 int alg_arg,
3441 data_t *input,
3442 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003443{
Ronald Cron5425a212020-08-04 14:58:35 +02003444 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003445 psa_key_type_t key_type = key_type_arg;
3446 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003447 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003448 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003449 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003450 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003451 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003452 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003453 const size_t output_sizes_to_test[] = {
3454 0,
3455 1,
3456 expected_mac->len - 1,
3457 expected_mac->len,
3458 expected_mac->len + 1,
3459 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003460
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003462 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003463 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003464
Gilles Peskine449bd832023-01-11 14:50:10 +01003465 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003466
Gilles Peskine449bd832023-01-11 14:50:10 +01003467 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3468 psa_set_key_algorithm(&attributes, alg);
3469 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003470
Gilles Peskine449bd832023-01-11 14:50:10 +01003471 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3472 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003473
Gilles Peskine449bd832023-01-11 14:50:10 +01003474 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003475 const size_t output_size = output_sizes_to_test[i];
3476 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003477 (output_size >= expected_mac->len ? PSA_SUCCESS :
3478 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003479
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 mbedtls_test_set_step(output_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003481 TEST_CALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003482
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003483 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003484 TEST_EQUAL(psa_mac_compute(key, alg,
3485 input->x, input->len,
3486 actual_mac, output_size, &mac_length),
3487 expected_status);
3488 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003489 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003490 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003491 }
3492
Gilles Peskine449bd832023-01-11 14:50:10 +01003493 if (output_size > 0) {
3494 memset(actual_mac, 0, output_size);
3495 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003496
3497 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3499 PSA_ASSERT(psa_mac_update(&operation,
3500 input->x, input->len));
3501 TEST_EQUAL(psa_mac_sign_finish(&operation,
3502 actual_mac, output_size,
3503 &mac_length),
3504 expected_status);
3505 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003506
Gilles Peskine449bd832023-01-11 14:50:10 +01003507 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003508 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003509 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003510 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003511 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003512 actual_mac = NULL;
3513 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003514
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003515exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003516 psa_mac_abort(&operation);
3517 psa_destroy_key(key);
3518 PSA_DONE();
3519 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003520}
3521/* END_CASE */
3522
3523/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003524void mac_verify(int key_type_arg,
3525 data_t *key_data,
3526 int alg_arg,
3527 data_t *input,
3528 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003529{
Ronald Cron5425a212020-08-04 14:58:35 +02003530 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003531 psa_key_type_t key_type = key_type_arg;
3532 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003533 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003534 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003535 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003536
Gilles Peskine449bd832023-01-11 14:50:10 +01003537 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003538
Gilles Peskine449bd832023-01-11 14:50:10 +01003539 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003540
Gilles Peskine449bd832023-01-11 14:50:10 +01003541 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3542 psa_set_key_algorithm(&attributes, alg);
3543 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003544
Gilles Peskine449bd832023-01-11 14:50:10 +01003545 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3546 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003547
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003548 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003549 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3550 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003551
3552 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003553 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3554 PSA_ASSERT(psa_mac_update(&operation,
3555 input->x, input->len));
3556 PSA_ASSERT(psa_mac_verify_finish(&operation,
3557 expected_mac->x,
3558 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003559
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003560 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003561 TEST_EQUAL(psa_mac_verify(key, alg,
3562 input->x, input->len,
3563 expected_mac->x,
3564 expected_mac->len - 1),
3565 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003566
3567 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003568 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3569 PSA_ASSERT(psa_mac_update(&operation,
3570 input->x, input->len));
3571 TEST_EQUAL(psa_mac_verify_finish(&operation,
3572 expected_mac->x,
3573 expected_mac->len - 1),
3574 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003575
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003576 /* Test a MAC that's too long, one-shot case. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003577 TEST_CALLOC(perturbed_mac, expected_mac->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01003578 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3579 TEST_EQUAL(psa_mac_verify(key, alg,
3580 input->x, input->len,
3581 perturbed_mac, expected_mac->len + 1),
3582 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003583
3584 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003585 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3586 PSA_ASSERT(psa_mac_update(&operation,
3587 input->x, input->len));
3588 TEST_EQUAL(psa_mac_verify_finish(&operation,
3589 perturbed_mac,
3590 expected_mac->len + 1),
3591 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003592
3593 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003594 for (size_t i = 0; i < expected_mac->len; i++) {
3595 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003596 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003597
Gilles Peskine449bd832023-01-11 14:50:10 +01003598 TEST_EQUAL(psa_mac_verify(key, alg,
3599 input->x, input->len,
3600 perturbed_mac, expected_mac->len),
3601 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003602
Gilles Peskine449bd832023-01-11 14:50:10 +01003603 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3604 PSA_ASSERT(psa_mac_update(&operation,
3605 input->x, input->len));
3606 TEST_EQUAL(psa_mac_verify_finish(&operation,
3607 perturbed_mac,
3608 expected_mac->len),
3609 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003610 perturbed_mac[i] ^= 1;
3611 }
3612
Gilles Peskine8c9def32018-02-08 10:02:12 +01003613exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003614 psa_mac_abort(&operation);
3615 psa_destroy_key(key);
3616 PSA_DONE();
3617 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003618}
3619/* END_CASE */
3620
3621/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003622void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003623{
Jaeden Ameroab439972019-02-15 14:12:05 +00003624 const uint8_t input[1] = { 0 };
3625 unsigned char output[1] = { 0 };
3626 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003627 /* Test each valid way of initializing the object, except for `= {0}`, as
3628 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3629 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003630 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003631 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003632 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3633 psa_cipher_operation_t zero;
3634
Gilles Peskine449bd832023-01-11 14:50:10 +01003635 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003636
Jaeden Ameroab439972019-02-15 14:12:05 +00003637 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003638 TEST_EQUAL(psa_cipher_update(&func,
3639 input, sizeof(input),
3640 output, sizeof(output),
3641 &output_length),
3642 PSA_ERROR_BAD_STATE);
3643 TEST_EQUAL(psa_cipher_update(&init,
3644 input, sizeof(input),
3645 output, sizeof(output),
3646 &output_length),
3647 PSA_ERROR_BAD_STATE);
3648 TEST_EQUAL(psa_cipher_update(&zero,
3649 input, sizeof(input),
3650 output, sizeof(output),
3651 &output_length),
3652 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003653
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003654 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003655 PSA_ASSERT(psa_cipher_abort(&func));
3656 PSA_ASSERT(psa_cipher_abort(&init));
3657 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003658}
3659/* END_CASE */
3660
3661/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003662void cipher_setup(int key_type_arg,
3663 data_t *key,
3664 int alg_arg,
3665 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003666{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003667 psa_key_type_t key_type = key_type_arg;
3668 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003669 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003670 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003671 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003672#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003673 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3674#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003675
Gilles Peskine449bd832023-01-11 14:50:10 +01003676 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003677
Gilles Peskine449bd832023-01-11 14:50:10 +01003678 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3679 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003680 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003681 }
3682 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003683
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003684 /* The operation object should be reusable. */
3685#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003686 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3687 smoke_test_key_data,
3688 sizeof(smoke_test_key_data),
3689 KNOWN_SUPPORTED_CIPHER_ALG,
3690 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003691 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003692 }
3693 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003694#endif
3695
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003696exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003697 psa_cipher_abort(&operation);
3698 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003699}
3700/* END_CASE */
3701
Ronald Cronee414c72021-03-18 18:50:08 +01003702/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003703void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003704{
Ronald Cron5425a212020-08-04 14:58:35 +02003705 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003706 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3707 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003708 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003709 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003710 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003711 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003712 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003713 0xaa, 0xaa, 0xaa, 0xaa
3714 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003715 const uint8_t text[] = {
3716 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003717 0xbb, 0xbb, 0xbb, 0xbb
3718 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003719 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003720 size_t length = 0;
3721
Gilles Peskine449bd832023-01-11 14:50:10 +01003722 PSA_ASSERT(psa_crypto_init());
3723 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3724 psa_set_key_algorithm(&attributes, alg);
3725 psa_set_key_type(&attributes, key_type);
3726 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3727 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003728
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003729 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003730 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3731 ASSERT_OPERATION_IS_ACTIVE(operation);
3732 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3733 PSA_ERROR_BAD_STATE);
3734 ASSERT_OPERATION_IS_INACTIVE(operation);
3735 PSA_ASSERT(psa_cipher_abort(&operation));
3736 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003737
3738 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003739 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3740 ASSERT_OPERATION_IS_ACTIVE(operation);
3741 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3742 PSA_ERROR_BAD_STATE);
3743 ASSERT_OPERATION_IS_INACTIVE(operation);
3744 PSA_ASSERT(psa_cipher_abort(&operation));
3745 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003746
Jaeden Ameroab439972019-02-15 14:12:05 +00003747 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003748 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3749 buffer, sizeof(buffer),
3750 &length),
3751 PSA_ERROR_BAD_STATE);
3752 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003753
3754 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003755 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3756 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3757 buffer, sizeof(buffer),
3758 &length));
3759 ASSERT_OPERATION_IS_ACTIVE(operation);
3760 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3761 buffer, sizeof(buffer),
3762 &length),
3763 PSA_ERROR_BAD_STATE);
3764 ASSERT_OPERATION_IS_INACTIVE(operation);
3765 PSA_ASSERT(psa_cipher_abort(&operation));
3766 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003767
3768 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003769 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3770 PSA_ASSERT(psa_cipher_set_iv(&operation,
3771 iv, sizeof(iv)));
3772 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3773 buffer, sizeof(buffer),
3774 &length),
3775 PSA_ERROR_BAD_STATE);
3776 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003777
3778 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003779 TEST_EQUAL(psa_cipher_set_iv(&operation,
3780 iv, sizeof(iv)),
3781 PSA_ERROR_BAD_STATE);
3782 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003783
3784 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003785 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3786 PSA_ASSERT(psa_cipher_set_iv(&operation,
3787 iv, sizeof(iv)));
3788 ASSERT_OPERATION_IS_ACTIVE(operation);
3789 TEST_EQUAL(psa_cipher_set_iv(&operation,
3790 iv, sizeof(iv)),
3791 PSA_ERROR_BAD_STATE);
3792 ASSERT_OPERATION_IS_INACTIVE(operation);
3793 PSA_ASSERT(psa_cipher_abort(&operation));
3794 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003795
3796 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003797 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3798 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3799 buffer, sizeof(buffer),
3800 &length));
3801 TEST_EQUAL(psa_cipher_set_iv(&operation,
3802 iv, sizeof(iv)),
3803 PSA_ERROR_BAD_STATE);
3804 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003805
3806 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003807 TEST_EQUAL(psa_cipher_update(&operation,
3808 text, sizeof(text),
3809 buffer, sizeof(buffer),
3810 &length),
3811 PSA_ERROR_BAD_STATE);
3812 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003813
3814 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003815 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3816 ASSERT_OPERATION_IS_ACTIVE(operation);
3817 TEST_EQUAL(psa_cipher_update(&operation,
3818 text, sizeof(text),
3819 buffer, sizeof(buffer),
3820 &length),
3821 PSA_ERROR_BAD_STATE);
3822 ASSERT_OPERATION_IS_INACTIVE(operation);
3823 PSA_ASSERT(psa_cipher_abort(&operation));
3824 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003825
3826 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003827 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3828 PSA_ASSERT(psa_cipher_set_iv(&operation,
3829 iv, sizeof(iv)));
3830 PSA_ASSERT(psa_cipher_finish(&operation,
3831 buffer, sizeof(buffer), &length));
3832 TEST_EQUAL(psa_cipher_update(&operation,
3833 text, sizeof(text),
3834 buffer, sizeof(buffer),
3835 &length),
3836 PSA_ERROR_BAD_STATE);
3837 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003838
3839 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003840 TEST_EQUAL(psa_cipher_finish(&operation,
3841 buffer, sizeof(buffer), &length),
3842 PSA_ERROR_BAD_STATE);
3843 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003844
3845 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003846 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003847 /* Not calling update means we are encrypting an empty buffer, which is OK
3848 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003849 ASSERT_OPERATION_IS_ACTIVE(operation);
3850 TEST_EQUAL(psa_cipher_finish(&operation,
3851 buffer, sizeof(buffer), &length),
3852 PSA_ERROR_BAD_STATE);
3853 ASSERT_OPERATION_IS_INACTIVE(operation);
3854 PSA_ASSERT(psa_cipher_abort(&operation));
3855 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003856
3857 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003858 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3859 PSA_ASSERT(psa_cipher_set_iv(&operation,
3860 iv, sizeof(iv)));
3861 PSA_ASSERT(psa_cipher_finish(&operation,
3862 buffer, sizeof(buffer), &length));
3863 TEST_EQUAL(psa_cipher_finish(&operation,
3864 buffer, sizeof(buffer), &length),
3865 PSA_ERROR_BAD_STATE);
3866 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003867
Gilles Peskine449bd832023-01-11 14:50:10 +01003868 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003869
Jaeden Ameroab439972019-02-15 14:12:05 +00003870exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003871 psa_cipher_abort(&operation);
3872 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003873}
3874/* END_CASE */
3875
3876/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003877void cipher_encrypt_fail(int alg_arg,
3878 int key_type_arg,
3879 data_t *key_data,
3880 data_t *input,
3881 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003882{
Ronald Cron5425a212020-08-04 14:58:35 +02003883 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003884 psa_status_t status;
3885 psa_key_type_t key_type = key_type_arg;
3886 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003887 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003888 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003889 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3890 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003891 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003892 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003893 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003894 size_t function_output_length;
3895 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003896 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3897
Gilles Peskine449bd832023-01-11 14:50:10 +01003898 if (PSA_ERROR_BAD_STATE != expected_status) {
3899 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003900
Gilles Peskine449bd832023-01-11 14:50:10 +01003901 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3902 psa_set_key_algorithm(&attributes, alg);
3903 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003904
Gilles Peskine449bd832023-01-11 14:50:10 +01003905 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3906 input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003907 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003908
Gilles Peskine449bd832023-01-11 14:50:10 +01003909 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3910 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003911 }
3912
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003913 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003914 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3915 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003916
Gilles Peskine449bd832023-01-11 14:50:10 +01003917 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003918
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003919 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003920 status = psa_cipher_encrypt_setup(&operation, key, alg);
3921 if (status == PSA_SUCCESS) {
3922 if (alg != PSA_ALG_ECB_NO_PADDING) {
3923 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3924 iv, iv_size,
3925 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003926 }
3927
Gilles Peskine449bd832023-01-11 14:50:10 +01003928 status = psa_cipher_update(&operation, input->x, input->len,
3929 output, output_buffer_size,
3930 &function_output_length);
3931 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003932 output_length += function_output_length;
3933
Gilles Peskine449bd832023-01-11 14:50:10 +01003934 status = psa_cipher_finish(&operation, output + output_length,
3935 output_buffer_size - output_length,
3936 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003937
Gilles Peskine449bd832023-01-11 14:50:10 +01003938 TEST_EQUAL(status, expected_status);
3939 } else {
3940 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003941 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003942 } else {
3943 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003944 }
3945
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003946exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003947 psa_cipher_abort(&operation);
3948 mbedtls_free(output);
3949 psa_destroy_key(key);
3950 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003951}
3952/* END_CASE */
3953
3954/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003955void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3956 data_t *input, int iv_length,
3957 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003958{
3959 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3960 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3961 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3962 size_t output_buffer_size = 0;
3963 unsigned char *output = NULL;
3964
Gilles Peskine449bd832023-01-11 14:50:10 +01003965 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003966 TEST_CALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003967
Gilles Peskine449bd832023-01-11 14:50:10 +01003968 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003969
Gilles Peskine449bd832023-01-11 14:50:10 +01003970 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3971 psa_set_key_algorithm(&attributes, alg);
3972 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003973
Gilles Peskine449bd832023-01-11 14:50:10 +01003974 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3975 &key));
3976 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3977 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3978 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003979
3980exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003981 psa_cipher_abort(&operation);
3982 mbedtls_free(output);
3983 psa_destroy_key(key);
3984 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003985}
3986/* END_CASE */
3987
3988/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003989void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3990 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003991{
3992 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3993 psa_key_type_t key_type = key_type_arg;
3994 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003995 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3996 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003997 unsigned char *output = NULL;
3998 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003999 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004000 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4001
Gilles Peskine449bd832023-01-11 14:50:10 +01004002 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004003
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004004 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01004005 TEST_LE_U(ciphertext->len,
4006 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
4007 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
4008 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
4009 TEST_LE_U(plaintext->len,
4010 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
4011 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
4012 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004013
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004014
4015 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01004016 psa_set_key_usage_flags(&attributes,
4017 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4018 psa_set_key_algorithm(&attributes, alg);
4019 psa_set_key_type(&attributes, key_type);
4020 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4021 &key));
4022 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
4023 plaintext->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004024 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004025
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004026 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4028 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
4029 PSA_ERROR_BAD_STATE);
4030 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
4031 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
4032 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02004033
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004034 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4036 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
4037 &length),
4038 PSA_ERROR_BAD_STATE);
4039 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
4040 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
4041 &length),
4042 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004043
Gilles Peskine286c3142022-04-20 17:09:38 +02004044 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02004046 output_length = 0;
4047 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004048 PSA_ASSERT(psa_cipher_update(&operation,
4049 plaintext->x, plaintext->len,
4050 output, output_buffer_size,
4051 &length));
4052 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02004053 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004054 PSA_ASSERT(psa_cipher_finish(&operation,
4055 mbedtls_buffer_offset(output, output_length),
4056 output_buffer_size - output_length,
4057 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02004058 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004059 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004060 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004061
Gilles Peskine286c3142022-04-20 17:09:38 +02004062 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01004063 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02004064 output_length = 0;
4065 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004066 PSA_ASSERT(psa_cipher_update(&operation,
4067 ciphertext->x, ciphertext->len,
4068 output, output_buffer_size,
4069 &length));
4070 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02004071 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004072 PSA_ASSERT(psa_cipher_finish(&operation,
4073 mbedtls_buffer_offset(output, output_length),
4074 output_buffer_size - output_length,
4075 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02004076 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004077 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004078 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004079
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004080 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004081 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004082 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
4083 output, output_buffer_size,
4084 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004085 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004086 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004087
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004088 /* One-shot decryption */
4089 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004090 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
4091 output, output_buffer_size,
4092 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004093 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004094 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004095
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004096exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004097 PSA_ASSERT(psa_cipher_abort(&operation));
4098 mbedtls_free(output);
4099 psa_cipher_abort(&operation);
4100 psa_destroy_key(key);
4101 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004102}
4103/* END_CASE */
4104
4105/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004106void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004107{
4108 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4109 psa_algorithm_t alg = alg_arg;
4110 psa_key_type_t key_type = key_type_arg;
4111 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4112 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4113 psa_status_t status;
4114
Gilles Peskine449bd832023-01-11 14:50:10 +01004115 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004116
Gilles Peskine449bd832023-01-11 14:50:10 +01004117 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4118 psa_set_key_algorithm(&attributes, alg);
4119 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004120
4121 /* Usage of either of these two size macros would cause divide by zero
4122 * with incorrect key types previously. Input length should be irrelevant
4123 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4125 0);
4126 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004127
4128
Gilles Peskine449bd832023-01-11 14:50:10 +01004129 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4130 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004131
4132 /* Should fail due to invalid alg type (to support invalid key type).
4133 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004134 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004135
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004137
4138exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004139 psa_cipher_abort(&operation);
4140 psa_destroy_key(key);
4141 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004142}
4143/* END_CASE */
4144
4145/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004146void cipher_encrypt_validation(int alg_arg,
4147 int key_type_arg,
4148 data_t *key_data,
4149 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004150{
4151 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4152 psa_key_type_t key_type = key_type_arg;
4153 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004155 unsigned char *output1 = NULL;
4156 size_t output1_buffer_size = 0;
4157 size_t output1_length = 0;
4158 unsigned char *output2 = NULL;
4159 size_t output2_buffer_size = 0;
4160 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004161 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004162 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004163 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004164
Gilles Peskine449bd832023-01-11 14:50:10 +01004165 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004166
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4168 psa_set_key_algorithm(&attributes, alg);
4169 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004170
Gilles Peskine449bd832023-01-11 14:50:10 +01004171 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4172 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4173 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004174 TEST_CALLOC(output1, output1_buffer_size);
4175 TEST_CALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004176
Gilles Peskine449bd832023-01-11 14:50:10 +01004177 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4178 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004179
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004180 /* The one-shot cipher encryption uses generated iv so validating
4181 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004182 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4183 output1_buffer_size, &output1_length));
4184 TEST_LE_U(output1_length,
4185 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4186 TEST_LE_U(output1_length,
4187 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004188
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4190 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004191
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 PSA_ASSERT(psa_cipher_update(&operation,
4193 input->x, input->len,
4194 output2, output2_buffer_size,
4195 &function_output_length));
4196 TEST_LE_U(function_output_length,
4197 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4198 TEST_LE_U(function_output_length,
4199 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004200 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004201
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 PSA_ASSERT(psa_cipher_finish(&operation,
4203 output2 + output2_length,
4204 output2_buffer_size - output2_length,
4205 &function_output_length));
4206 TEST_LE_U(function_output_length,
4207 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4208 TEST_LE_U(function_output_length,
4209 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004210 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004211
Gilles Peskine449bd832023-01-11 14:50:10 +01004212 PSA_ASSERT(psa_cipher_abort(&operation));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004213 TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004214 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004215
Gilles Peskine50e586b2018-06-08 14:28:46 +02004216exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 psa_cipher_abort(&operation);
4218 mbedtls_free(output1);
4219 mbedtls_free(output2);
4220 psa_destroy_key(key);
4221 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004222}
4223/* END_CASE */
4224
4225/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004226void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4227 data_t *key_data, data_t *iv,
4228 data_t *input,
4229 int first_part_size_arg,
4230 int output1_length_arg, int output2_length_arg,
4231 data_t *expected_output,
4232 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004233{
Ronald Cron5425a212020-08-04 14:58:35 +02004234 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004235 psa_key_type_t key_type = key_type_arg;
4236 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004237 psa_status_t status;
4238 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004239 size_t first_part_size = first_part_size_arg;
4240 size_t output1_length = output1_length_arg;
4241 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004242 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004243 size_t output_buffer_size = 0;
4244 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004245 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004246 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004247 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004248
Gilles Peskine449bd832023-01-11 14:50:10 +01004249 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004250
Gilles Peskine449bd832023-01-11 14:50:10 +01004251 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4252 psa_set_key_algorithm(&attributes, alg);
4253 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004254
Gilles Peskine449bd832023-01-11 14:50:10 +01004255 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4256 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004257
Gilles Peskine449bd832023-01-11 14:50:10 +01004258 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004259
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 if (iv->len > 0) {
4261 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004262 }
4263
Gilles Peskine449bd832023-01-11 14:50:10 +01004264 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4265 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004266 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004267
Gilles Peskine449bd832023-01-11 14:50:10 +01004268 TEST_LE_U(first_part_size, input->len);
4269 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4270 output, output_buffer_size,
4271 &function_output_length));
4272 TEST_ASSERT(function_output_length == output1_length);
4273 TEST_LE_U(function_output_length,
4274 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4275 TEST_LE_U(function_output_length,
4276 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004277 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004278
Gilles Peskine449bd832023-01-11 14:50:10 +01004279 if (first_part_size < input->len) {
4280 PSA_ASSERT(psa_cipher_update(&operation,
4281 input->x + first_part_size,
4282 input->len - first_part_size,
4283 (output_buffer_size == 0 ? NULL :
4284 output + total_output_length),
4285 output_buffer_size - total_output_length,
4286 &function_output_length));
4287 TEST_ASSERT(function_output_length == output2_length);
4288 TEST_LE_U(function_output_length,
4289 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4290 alg,
4291 input->len - first_part_size));
4292 TEST_LE_U(function_output_length,
4293 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004294 total_output_length += function_output_length;
4295 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004296
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 status = psa_cipher_finish(&operation,
4298 (output_buffer_size == 0 ? NULL :
4299 output + total_output_length),
4300 output_buffer_size - total_output_length,
4301 &function_output_length);
4302 TEST_LE_U(function_output_length,
4303 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4304 TEST_LE_U(function_output_length,
4305 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004306 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004308
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 if (expected_status == PSA_SUCCESS) {
4310 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004311
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004312 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004313 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004314 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004315
4316exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 psa_cipher_abort(&operation);
4318 mbedtls_free(output);
4319 psa_destroy_key(key);
4320 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004321}
4322/* END_CASE */
4323
4324/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004325void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4326 data_t *key_data, data_t *iv,
4327 data_t *input,
4328 int first_part_size_arg,
4329 int output1_length_arg, int output2_length_arg,
4330 data_t *expected_output,
4331 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004332{
Ronald Cron5425a212020-08-04 14:58:35 +02004333 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004334 psa_key_type_t key_type = key_type_arg;
4335 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004336 psa_status_t status;
4337 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004338 size_t first_part_size = first_part_size_arg;
4339 size_t output1_length = output1_length_arg;
4340 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004341 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004342 size_t output_buffer_size = 0;
4343 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004344 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004345 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004346 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004347
Gilles Peskine449bd832023-01-11 14:50:10 +01004348 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004349
Gilles Peskine449bd832023-01-11 14:50:10 +01004350 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4351 psa_set_key_algorithm(&attributes, alg);
4352 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004353
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4355 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004356
Gilles Peskine449bd832023-01-11 14:50:10 +01004357 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004358
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 if (iv->len > 0) {
4360 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004361 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004362
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4364 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004365 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004366
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 TEST_LE_U(first_part_size, input->len);
4368 PSA_ASSERT(psa_cipher_update(&operation,
4369 input->x, first_part_size,
4370 output, output_buffer_size,
4371 &function_output_length));
4372 TEST_ASSERT(function_output_length == output1_length);
4373 TEST_LE_U(function_output_length,
4374 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4375 TEST_LE_U(function_output_length,
4376 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004377 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004378
Gilles Peskine449bd832023-01-11 14:50:10 +01004379 if (first_part_size < input->len) {
4380 PSA_ASSERT(psa_cipher_update(&operation,
4381 input->x + first_part_size,
4382 input->len - first_part_size,
4383 (output_buffer_size == 0 ? NULL :
4384 output + total_output_length),
4385 output_buffer_size - total_output_length,
4386 &function_output_length));
4387 TEST_ASSERT(function_output_length == output2_length);
4388 TEST_LE_U(function_output_length,
4389 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4390 alg,
4391 input->len - first_part_size));
4392 TEST_LE_U(function_output_length,
4393 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004394 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004395 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004396
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 status = psa_cipher_finish(&operation,
4398 (output_buffer_size == 0 ? NULL :
4399 output + total_output_length),
4400 output_buffer_size - total_output_length,
4401 &function_output_length);
4402 TEST_LE_U(function_output_length,
4403 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4404 TEST_LE_U(function_output_length,
4405 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004406 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004407 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004408
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 if (expected_status == PSA_SUCCESS) {
4410 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004411
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004412 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004413 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004414 }
4415
Gilles Peskine50e586b2018-06-08 14:28:46 +02004416exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004417 psa_cipher_abort(&operation);
4418 mbedtls_free(output);
4419 psa_destroy_key(key);
4420 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004421}
4422/* END_CASE */
4423
Gilles Peskine50e586b2018-06-08 14:28:46 +02004424/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004425void cipher_decrypt_fail(int alg_arg,
4426 int key_type_arg,
4427 data_t *key_data,
4428 data_t *iv,
4429 data_t *input_arg,
4430 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004431{
4432 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4433 psa_status_t status;
4434 psa_key_type_t key_type = key_type_arg;
4435 psa_algorithm_t alg = alg_arg;
4436 psa_status_t expected_status = expected_status_arg;
4437 unsigned char *input = NULL;
4438 size_t input_buffer_size = 0;
4439 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004440 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004441 size_t output_buffer_size = 0;
4442 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004443 size_t function_output_length;
4444 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004445 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4446
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 if (PSA_ERROR_BAD_STATE != expected_status) {
4448 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004449
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4451 psa_set_key_algorithm(&attributes, alg);
4452 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004453
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4455 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004456 }
4457
4458 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4460 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004461 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 memcpy(input, iv->x, iv->len);
4463 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004464 }
4465
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004467 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004468
Neil Armstrong66a479f2022-02-07 15:41:19 +01004469 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004470 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4471 output_buffer_size, &output_length);
4472 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004473
Neil Armstrong66a479f2022-02-07 15:41:19 +01004474 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004475 status = psa_cipher_decrypt_setup(&operation, key, alg);
4476 if (status == PSA_SUCCESS) {
4477 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4478 input_arg->len) +
4479 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004480 TEST_CALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004481
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 if (iv->len > 0) {
4483 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004484
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 if (status != PSA_SUCCESS) {
4486 TEST_EQUAL(status, expected_status);
4487 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004488 }
4489
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 if (status == PSA_SUCCESS) {
4491 status = psa_cipher_update(&operation,
4492 input_arg->x, input_arg->len,
4493 output_multi, output_buffer_size,
4494 &function_output_length);
4495 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004496 output_length = function_output_length;
4497
Gilles Peskine449bd832023-01-11 14:50:10 +01004498 status = psa_cipher_finish(&operation,
4499 output_multi + output_length,
4500 output_buffer_size - output_length,
4501 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004502
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 TEST_EQUAL(status, expected_status);
4504 } else {
4505 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004506 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 } else {
4508 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004509 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 } else {
4511 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004512 }
4513
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004514exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004515 psa_cipher_abort(&operation);
4516 mbedtls_free(input);
4517 mbedtls_free(output);
4518 mbedtls_free(output_multi);
4519 psa_destroy_key(key);
4520 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004521}
4522/* END_CASE */
4523
4524/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004525void cipher_decrypt(int alg_arg,
4526 int key_type_arg,
4527 data_t *key_data,
4528 data_t *iv,
4529 data_t *input_arg,
4530 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004531{
4532 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4533 psa_key_type_t key_type = key_type_arg;
4534 psa_algorithm_t alg = alg_arg;
4535 unsigned char *input = NULL;
4536 size_t input_buffer_size = 0;
4537 unsigned char *output = NULL;
4538 size_t output_buffer_size = 0;
4539 size_t output_length = 0;
4540 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4541
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004543
Gilles Peskine449bd832023-01-11 14:50:10 +01004544 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4545 psa_set_key_algorithm(&attributes, alg);
4546 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004547
4548 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4550 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004551 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 memcpy(input, iv->x, iv->len);
4553 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004554 }
4555
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004557 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004558
Gilles Peskine449bd832023-01-11 14:50:10 +01004559 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4560 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004561
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4563 output_buffer_size, &output_length));
4564 TEST_LE_U(output_length,
4565 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4566 TEST_LE_U(output_length,
4567 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004568
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004569 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004570 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004571exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 mbedtls_free(input);
4573 mbedtls_free(output);
4574 psa_destroy_key(key);
4575 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004576}
4577/* END_CASE */
4578
4579/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004580void cipher_verify_output(int alg_arg,
4581 int key_type_arg,
4582 data_t *key_data,
4583 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004584{
Ronald Cron5425a212020-08-04 14:58:35 +02004585 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004586 psa_key_type_t key_type = key_type_arg;
4587 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004588 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004589 size_t output1_size = 0;
4590 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004591 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004592 size_t output2_size = 0;
4593 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004594 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004595
Gilles Peskine449bd832023-01-11 14:50:10 +01004596 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004597
Gilles Peskine449bd832023-01-11 14:50:10 +01004598 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4599 psa_set_key_algorithm(&attributes, alg);
4600 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004601
Gilles Peskine449bd832023-01-11 14:50:10 +01004602 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4603 &key));
4604 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004605 TEST_CALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004606
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4608 output1, output1_size,
4609 &output1_length));
4610 TEST_LE_U(output1_length,
4611 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4612 TEST_LE_U(output1_length,
4613 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004614
4615 output2_size = output1_length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004616 TEST_CALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004617
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4619 output2, output2_size,
4620 &output2_length));
4621 TEST_LE_U(output2_length,
4622 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4623 TEST_LE_U(output2_length,
4624 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004625
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004626 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004627
4628exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 mbedtls_free(output1);
4630 mbedtls_free(output2);
4631 psa_destroy_key(key);
4632 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004633}
4634/* END_CASE */
4635
4636/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004637void cipher_verify_output_multipart(int alg_arg,
4638 int key_type_arg,
4639 data_t *key_data,
4640 data_t *input,
4641 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004642{
Ronald Cron5425a212020-08-04 14:58:35 +02004643 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004644 psa_key_type_t key_type = key_type_arg;
4645 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004646 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004648 size_t iv_size = 16;
4649 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004650 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004651 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004652 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004653 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004654 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004655 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004656 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004657 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4658 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004659 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004660
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004662
Gilles Peskine449bd832023-01-11 14:50:10 +01004663 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4664 psa_set_key_algorithm(&attributes, alg);
4665 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004666
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4668 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004669
Gilles Peskine449bd832023-01-11 14:50:10 +01004670 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4671 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004672
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 if (alg != PSA_ALG_ECB_NO_PADDING) {
4674 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4675 iv, iv_size,
4676 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004677 }
4678
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4680 TEST_LE_U(output1_buffer_size,
4681 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004682 TEST_CALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004683
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004685
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4687 output1, output1_buffer_size,
4688 &function_output_length));
4689 TEST_LE_U(function_output_length,
4690 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4691 TEST_LE_U(function_output_length,
4692 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004693 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004694
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 PSA_ASSERT(psa_cipher_update(&operation1,
4696 input->x + first_part_size,
4697 input->len - first_part_size,
4698 output1, output1_buffer_size,
4699 &function_output_length));
4700 TEST_LE_U(function_output_length,
4701 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4702 alg,
4703 input->len - first_part_size));
4704 TEST_LE_U(function_output_length,
4705 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004706 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004707
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 PSA_ASSERT(psa_cipher_finish(&operation1,
4709 output1 + output1_length,
4710 output1_buffer_size - output1_length,
4711 &function_output_length));
4712 TEST_LE_U(function_output_length,
4713 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4714 TEST_LE_U(function_output_length,
4715 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004716 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004717
Gilles Peskine449bd832023-01-11 14:50:10 +01004718 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004719
Gilles Peskine048b7f02018-06-08 14:20:49 +02004720 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 TEST_LE_U(output2_buffer_size,
4722 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4723 TEST_LE_U(output2_buffer_size,
4724 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004725 TEST_CALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004726
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 if (iv_length > 0) {
4728 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4729 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004730 }
Moran Pekerded84402018-06-06 16:36:50 +03004731
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4733 output2, output2_buffer_size,
4734 &function_output_length));
4735 TEST_LE_U(function_output_length,
4736 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4737 TEST_LE_U(function_output_length,
4738 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004739 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004740
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 PSA_ASSERT(psa_cipher_update(&operation2,
4742 output1 + first_part_size,
4743 output1_length - first_part_size,
4744 output2, output2_buffer_size,
4745 &function_output_length));
4746 TEST_LE_U(function_output_length,
4747 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4748 alg,
4749 output1_length - first_part_size));
4750 TEST_LE_U(function_output_length,
4751 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004752 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004753
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 PSA_ASSERT(psa_cipher_finish(&operation2,
4755 output2 + output2_length,
4756 output2_buffer_size - output2_length,
4757 &function_output_length));
4758 TEST_LE_U(function_output_length,
4759 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4760 TEST_LE_U(function_output_length,
4761 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004762 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004763
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004765
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004766 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004767
4768exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 psa_cipher_abort(&operation1);
4770 psa_cipher_abort(&operation2);
4771 mbedtls_free(output1);
4772 mbedtls_free(output2);
4773 psa_destroy_key(key);
4774 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004775}
4776/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004777
Gilles Peskine20035e32018-02-03 22:44:14 +01004778/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004779void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4780 int alg_arg,
4781 data_t *nonce,
4782 data_t *additional_data,
4783 data_t *input_data,
4784 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004785{
Ronald Cron5425a212020-08-04 14:58:35 +02004786 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004787 psa_key_type_t key_type = key_type_arg;
4788 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004789 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004790 unsigned char *output_data = NULL;
4791 size_t output_size = 0;
4792 size_t output_length = 0;
4793 unsigned char *output_data2 = NULL;
4794 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004795 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004796 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004797 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004798
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004800
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4802 psa_set_key_algorithm(&attributes, alg);
4803 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004804
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4806 &key));
4807 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4808 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004809
Gilles Peskine449bd832023-01-11 14:50:10 +01004810 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4811 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004812 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4813 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4815 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4816 TEST_EQUAL(output_size,
4817 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4818 TEST_LE_U(output_size,
4819 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004820 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004821 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004822
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 status = psa_aead_encrypt(key, alg,
4824 nonce->x, nonce->len,
4825 additional_data->x,
4826 additional_data->len,
4827 input_data->x, input_data->len,
4828 output_data, output_size,
4829 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004830
4831 /* If the operation is not supported, just skip and not fail in case the
4832 * encryption involves a common limitation of cryptography hardwares and
4833 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 if (status == PSA_ERROR_NOT_SUPPORTED) {
4835 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4836 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004837 }
4838
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004840
Gilles Peskine449bd832023-01-11 14:50:10 +01004841 if (PSA_SUCCESS == expected_result) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004842 TEST_CALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004843
Gilles Peskine003a4a92019-05-14 16:09:40 +02004844 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4845 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004846 TEST_EQUAL(input_data->len,
4847 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004848
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 TEST_LE_U(input_data->len,
4850 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004851
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 TEST_EQUAL(psa_aead_decrypt(key, alg,
4853 nonce->x, nonce->len,
4854 additional_data->x,
4855 additional_data->len,
4856 output_data, output_length,
4857 output_data2, output_length,
4858 &output_length2),
4859 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004860
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004861 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004862 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004863 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004864
Gilles Peskinea1cac842018-06-11 19:33:02 +02004865exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004866 psa_destroy_key(key);
4867 mbedtls_free(output_data);
4868 mbedtls_free(output_data2);
4869 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004870}
4871/* END_CASE */
4872
4873/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004874void aead_encrypt(int key_type_arg, data_t *key_data,
4875 int alg_arg,
4876 data_t *nonce,
4877 data_t *additional_data,
4878 data_t *input_data,
4879 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004880{
Ronald Cron5425a212020-08-04 14:58:35 +02004881 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004882 psa_key_type_t key_type = key_type_arg;
4883 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004884 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004885 unsigned char *output_data = NULL;
4886 size_t output_size = 0;
4887 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004888 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004889 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004890
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004892
Gilles Peskine449bd832023-01-11 14:50:10 +01004893 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4894 psa_set_key_algorithm(&attributes, alg);
4895 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004896
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4898 &key));
4899 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4900 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004901
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4903 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004904 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4905 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 TEST_EQUAL(output_size,
4907 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4908 TEST_LE_U(output_size,
4909 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004910 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004911
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 status = psa_aead_encrypt(key, alg,
4913 nonce->x, nonce->len,
4914 additional_data->x, additional_data->len,
4915 input_data->x, input_data->len,
4916 output_data, output_size,
4917 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004918
Ronald Cron28a45ed2021-02-09 20:35:42 +01004919 /* If the operation is not supported, just skip and not fail in case the
4920 * encryption involves a common limitation of cryptography hardwares and
4921 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004922 if (status == PSA_ERROR_NOT_SUPPORTED) {
4923 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4924 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004925 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004926
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 PSA_ASSERT(status);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004928 TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004929 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004930
Gilles Peskinea1cac842018-06-11 19:33:02 +02004931exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 psa_destroy_key(key);
4933 mbedtls_free(output_data);
4934 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004935}
4936/* END_CASE */
4937
4938/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004939void aead_decrypt(int key_type_arg, data_t *key_data,
4940 int alg_arg,
4941 data_t *nonce,
4942 data_t *additional_data,
4943 data_t *input_data,
4944 data_t *expected_data,
4945 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004946{
Ronald Cron5425a212020-08-04 14:58:35 +02004947 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004948 psa_key_type_t key_type = key_type_arg;
4949 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004950 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004951 unsigned char *output_data = NULL;
4952 size_t output_size = 0;
4953 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004954 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004955 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004956 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004957
Gilles Peskine449bd832023-01-11 14:50:10 +01004958 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004959
Gilles Peskine449bd832023-01-11 14:50:10 +01004960 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4961 psa_set_key_algorithm(&attributes, alg);
4962 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004963
Gilles Peskine449bd832023-01-11 14:50:10 +01004964 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4965 &key));
4966 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4967 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004968
Gilles Peskine449bd832023-01-11 14:50:10 +01004969 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4970 alg);
4971 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4972 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004973 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4974 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004975 TEST_EQUAL(output_size,
4976 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4977 TEST_LE_U(output_size,
4978 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004979 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004980 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004981
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 status = psa_aead_decrypt(key, alg,
4983 nonce->x, nonce->len,
4984 additional_data->x,
4985 additional_data->len,
4986 input_data->x, input_data->len,
4987 output_data, output_size,
4988 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004989
Ronald Cron28a45ed2021-02-09 20:35:42 +01004990 /* If the operation is not supported, just skip and not fail in case the
4991 * decryption involves a common limitation of cryptography hardwares and
4992 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004993 if (status == PSA_ERROR_NOT_SUPPORTED) {
4994 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4995 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004996 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004997
Gilles Peskine449bd832023-01-11 14:50:10 +01004998 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004999
Gilles Peskine449bd832023-01-11 14:50:10 +01005000 if (expected_result == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01005001 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01005002 output_data, output_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01005003 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02005004
Gilles Peskinea1cac842018-06-11 19:33:02 +02005005exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 psa_destroy_key(key);
5007 mbedtls_free(output_data);
5008 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02005009}
5010/* END_CASE */
5011
5012/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005013void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
5014 int alg_arg,
5015 data_t *nonce,
5016 data_t *additional_data,
5017 data_t *input_data,
5018 int do_set_lengths,
5019 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005020{
Paul Elliottd3f82412021-06-16 16:52:21 +01005021 size_t ad_part_len = 0;
5022 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005023 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005024
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
5026 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005027
Gilles Peskine449bd832023-01-11 14:50:10 +01005028 if (do_set_lengths) {
5029 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005030 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005031 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005032 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005034 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005035
5036 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 if (!aead_multipart_internal_func(key_type_arg, key_data,
5038 alg_arg, nonce,
5039 additional_data,
5040 ad_part_len,
5041 input_data, -1,
5042 set_lengths_method,
5043 expected_output,
5044 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005045 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005046 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005047
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 /* length(0) part, length(ad_part_len) part, length(0) part... */
5049 mbedtls_test_set_step(1000 + ad_part_len);
5050
5051 if (!aead_multipart_internal_func(key_type_arg, key_data,
5052 alg_arg, nonce,
5053 additional_data,
5054 ad_part_len,
5055 input_data, -1,
5056 set_lengths_method,
5057 expected_output,
5058 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005059 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 }
5061 }
5062
5063 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
5064 /* Split data into length(data_part_len) parts. */
5065 mbedtls_test_set_step(2000 + data_part_len);
5066
5067 if (do_set_lengths) {
5068 if (data_part_len & 0x01) {
5069 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5070 } else {
5071 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
5072 }
5073 }
5074
5075 if (!aead_multipart_internal_func(key_type_arg, key_data,
5076 alg_arg, nonce,
5077 additional_data, -1,
5078 input_data, data_part_len,
5079 set_lengths_method,
5080 expected_output,
5081 1, 0)) {
5082 break;
5083 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005084
5085 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005087
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 if (!aead_multipart_internal_func(key_type_arg, key_data,
5089 alg_arg, nonce,
5090 additional_data, -1,
5091 input_data, data_part_len,
5092 set_lengths_method,
5093 expected_output,
5094 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005095 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005097 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005098
Paul Elliott8fc45162021-06-23 16:06:01 +01005099 /* Goto is required to silence warnings about unused labels, as we
5100 * don't actually do any test assertions in this function. */
5101 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005102}
5103/* END_CASE */
5104
5105/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005106void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5107 int alg_arg,
5108 data_t *nonce,
5109 data_t *additional_data,
5110 data_t *input_data,
5111 int do_set_lengths,
5112 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005113{
Paul Elliottd3f82412021-06-16 16:52:21 +01005114 size_t ad_part_len = 0;
5115 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005116 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005117
Gilles Peskine449bd832023-01-11 14:50:10 +01005118 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005119 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005121
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 if (do_set_lengths) {
5123 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005124 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005126 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005127 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005128 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005129
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 if (!aead_multipart_internal_func(key_type_arg, key_data,
5131 alg_arg, nonce,
5132 additional_data,
5133 ad_part_len,
5134 input_data, -1,
5135 set_lengths_method,
5136 expected_output,
5137 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005138 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005140
5141 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005143
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 if (!aead_multipart_internal_func(key_type_arg, key_data,
5145 alg_arg, nonce,
5146 additional_data,
5147 ad_part_len,
5148 input_data, -1,
5149 set_lengths_method,
5150 expected_output,
5151 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005152 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005154 }
5155
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005157 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005159
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 if (do_set_lengths) {
5161 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005162 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005164 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005165 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005166 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005167
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 if (!aead_multipart_internal_func(key_type_arg, key_data,
5169 alg_arg, nonce,
5170 additional_data, -1,
5171 input_data, data_part_len,
5172 set_lengths_method,
5173 expected_output,
5174 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005175 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005176 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005177
5178 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005180
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 if (!aead_multipart_internal_func(key_type_arg, key_data,
5182 alg_arg, nonce,
5183 additional_data, -1,
5184 input_data, data_part_len,
5185 set_lengths_method,
5186 expected_output,
5187 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005188 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005190 }
5191
Paul Elliott8fc45162021-06-23 16:06:01 +01005192 /* Goto is required to silence warnings about unused labels, as we
5193 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005194 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005195}
5196/* END_CASE */
5197
5198/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005199void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5200 int alg_arg,
5201 int nonce_length,
5202 int expected_nonce_length_arg,
5203 data_t *additional_data,
5204 data_t *input_data,
5205 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005206{
5207
5208 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5209 psa_key_type_t key_type = key_type_arg;
5210 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005211 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005212 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5213 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5214 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005215 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005216 size_t actual_nonce_length = 0;
5217 size_t expected_nonce_length = expected_nonce_length_arg;
5218 unsigned char *output = NULL;
5219 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005220 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005221 size_t ciphertext_size = 0;
5222 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005223 size_t tag_length = 0;
5224 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005225
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005227
Gilles Peskine449bd832023-01-11 14:50:10 +01005228 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5229 psa_set_key_algorithm(&attributes, alg);
5230 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005231
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5233 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005234
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005236
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005238
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005239 TEST_CALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005240
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005242
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005244
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005245 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005246
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005248
5249 /* If the operation is not supported, just skip and not fail in case the
5250 * encryption involves a common limitation of cryptography hardwares and
5251 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005252 if (status == PSA_ERROR_NOT_SUPPORTED) {
5253 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5254 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005255 }
5256
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005258
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5260 nonce_length,
5261 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005262
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005264
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005266
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 if (expected_status == PSA_SUCCESS) {
5268 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5269 alg));
5270 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005271
Gilles Peskine449bd832023-01-11 14:50:10 +01005272 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005273
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005275 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5277 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005278
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5280 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005281
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5283 output, output_size,
5284 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005285
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5287 &ciphertext_length, tag_buffer,
5288 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005289 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005290
5291exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 psa_destroy_key(key);
5293 mbedtls_free(output);
5294 mbedtls_free(ciphertext);
5295 psa_aead_abort(&operation);
5296 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005297}
5298/* END_CASE */
5299
5300/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005301void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5302 int alg_arg,
5303 int nonce_length_arg,
5304 int set_lengths_method_arg,
5305 data_t *additional_data,
5306 data_t *input_data,
5307 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005308{
5309
5310 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5311 psa_key_type_t key_type = key_type_arg;
5312 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005313 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005314 uint8_t *nonce_buffer = NULL;
5315 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5316 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5317 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005318 unsigned char *output = NULL;
5319 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005320 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005321 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005322 size_t ciphertext_size = 0;
5323 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005324 size_t tag_length = 0;
5325 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005326 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005327 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005328
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005330
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5332 psa_set_key_algorithm(&attributes, alg);
5333 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005334
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5336 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005337
Gilles Peskine449bd832023-01-11 14:50:10 +01005338 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005339
Gilles Peskine449bd832023-01-11 14:50:10 +01005340 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005341
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005342 TEST_CALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005343
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005345
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005347
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005348 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005349
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005351
5352 /* If the operation is not supported, just skip and not fail in case the
5353 * encryption involves a common limitation of cryptography hardwares and
5354 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 if (status == PSA_ERROR_NOT_SUPPORTED) {
5356 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5357 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005358 }
5359
Gilles Peskine449bd832023-01-11 14:50:10 +01005360 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005361
Paul Elliott4023ffd2021-09-10 16:21:22 +01005362 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 if (nonce_length_arg == -1) {
5364 /* Arbitrary size buffer, to test zero length valid buffer. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005365 TEST_CALLOC(nonce_buffer, 4);
Gilles Peskine449bd832023-01-11 14:50:10 +01005366 nonce_length = 0;
5367 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005368 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 nonce_length = (size_t) nonce_length_arg;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005370 TEST_CALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005371
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 if (nonce_buffer) {
5373 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005374 nonce_buffer[index] = 'a' + index;
5375 }
Paul Elliott66696b52021-08-16 18:42:41 +01005376 }
Paul Elliott863864a2021-07-23 17:28:31 +01005377 }
5378
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5380 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5381 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005382 }
5383
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005385
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005387
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 if (expected_status == PSA_SUCCESS) {
5389 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5390 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5391 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005392 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005394 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 }
Paul Elliott863864a2021-07-23 17:28:31 +01005396
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005397 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005398 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5399 additional_data->len),
5400 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005401
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5403 output, output_size,
5404 &ciphertext_length),
5405 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005406
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5408 &ciphertext_length, tag_buffer,
5409 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5410 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005411 }
5412
5413exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005414 psa_destroy_key(key);
5415 mbedtls_free(output);
5416 mbedtls_free(ciphertext);
5417 mbedtls_free(nonce_buffer);
5418 psa_aead_abort(&operation);
5419 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005420}
5421/* END_CASE */
5422
5423/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005424void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005425 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005426 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005427 data_t *nonce,
5428 data_t *additional_data,
5429 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005430 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005431{
5432
5433 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5434 psa_key_type_t key_type = key_type_arg;
5435 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005436 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005437 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5438 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5439 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005440 unsigned char *output = NULL;
5441 unsigned char *ciphertext = NULL;
5442 size_t output_size = output_size_arg;
5443 size_t ciphertext_size = 0;
5444 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005445 size_t tag_length = 0;
5446 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5447
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005449
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5451 psa_set_key_algorithm(&attributes, alg);
5452 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005453
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5455 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005456
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005458
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005459 TEST_CALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005460
Gilles Peskine449bd832023-01-11 14:50:10 +01005461 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005462
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005463 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005464
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005466
5467 /* If the operation is not supported, just skip and not fail in case the
5468 * encryption involves a common limitation of cryptography hardwares and
5469 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 if (status == PSA_ERROR_NOT_SUPPORTED) {
5471 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5472 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005473 }
5474
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005476
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5478 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005479
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005481
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5483 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005484
Gilles Peskine449bd832023-01-11 14:50:10 +01005485 status = psa_aead_update(&operation, input_data->x, input_data->len,
5486 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005487
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005489
Gilles Peskine449bd832023-01-11 14:50:10 +01005490 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005491 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5493 &ciphertext_length, tag_buffer,
5494 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005495 }
5496
5497exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 psa_destroy_key(key);
5499 mbedtls_free(output);
5500 mbedtls_free(ciphertext);
5501 psa_aead_abort(&operation);
5502 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005503}
5504/* END_CASE */
5505
Paul Elliott91b021e2021-07-23 18:52:31 +01005506/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005507void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5508 int alg_arg,
5509 int finish_ciphertext_size_arg,
5510 int tag_size_arg,
5511 data_t *nonce,
5512 data_t *additional_data,
5513 data_t *input_data,
5514 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005515{
5516
5517 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5518 psa_key_type_t key_type = key_type_arg;
5519 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005520 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005521 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5522 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5523 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005524 unsigned char *ciphertext = NULL;
5525 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005526 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005527 size_t ciphertext_size = 0;
5528 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5530 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005531 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005532
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005534
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5536 psa_set_key_algorithm(&attributes, alg);
5537 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005538
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5540 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005541
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005543
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005545
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005546 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005547
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005548 TEST_CALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005549
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005550 TEST_CALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005551
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005553
5554 /* If the operation is not supported, just skip and not fail in case the
5555 * encryption involves a common limitation of cryptography hardwares and
5556 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 if (status == PSA_ERROR_NOT_SUPPORTED) {
5558 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5559 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005560 }
5561
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005563
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005565
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5567 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005568
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5570 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005571
Gilles Peskine449bd832023-01-11 14:50:10 +01005572 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5573 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005574
5575 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 status = psa_aead_finish(&operation, finish_ciphertext,
5577 finish_ciphertext_size,
5578 &ciphertext_length, tag_buffer,
5579 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005580
Gilles Peskine449bd832023-01-11 14:50:10 +01005581 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005582
5583exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 psa_destroy_key(key);
5585 mbedtls_free(ciphertext);
5586 mbedtls_free(finish_ciphertext);
5587 mbedtls_free(tag_buffer);
5588 psa_aead_abort(&operation);
5589 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005590}
5591/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005592
5593/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005594void aead_multipart_verify(int key_type_arg, data_t *key_data,
5595 int alg_arg,
5596 data_t *nonce,
5597 data_t *additional_data,
5598 data_t *input_data,
5599 data_t *tag,
5600 int tag_usage_arg,
5601 int expected_setup_status_arg,
5602 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005603{
5604 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5605 psa_key_type_t key_type = key_type_arg;
5606 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005607 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005608 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5609 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5610 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005611 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005612 unsigned char *plaintext = NULL;
5613 unsigned char *finish_plaintext = NULL;
5614 size_t plaintext_size = 0;
5615 size_t plaintext_length = 0;
5616 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005617 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005618 unsigned char *tag_buffer = NULL;
5619 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005620
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005622
Gilles Peskine449bd832023-01-11 14:50:10 +01005623 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5624 psa_set_key_algorithm(&attributes, alg);
5625 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005626
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5628 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005629
Gilles Peskine449bd832023-01-11 14:50:10 +01005630 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005631
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5633 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005634
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005635 TEST_CALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005636
Gilles Peskine449bd832023-01-11 14:50:10 +01005637 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005638
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005639 TEST_CALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005640
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005642
5643 /* If the operation is not supported, just skip and not fail in case the
5644 * encryption involves a common limitation of cryptography hardwares and
5645 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005646 if (status == PSA_ERROR_NOT_SUPPORTED) {
5647 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5648 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005649 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005650 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005651
Gilles Peskine449bd832023-01-11 14:50:10 +01005652 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005653 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005654 }
Paul Elliott9961a662021-09-17 19:19:02 +01005655
Gilles Peskine449bd832023-01-11 14:50:10 +01005656 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005657
Gilles Peskine449bd832023-01-11 14:50:10 +01005658 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005659
Gilles Peskine449bd832023-01-11 14:50:10 +01005660 status = psa_aead_set_lengths(&operation, additional_data->len,
5661 input_data->len);
5662 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005663
Gilles Peskine449bd832023-01-11 14:50:10 +01005664 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5665 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005666
Gilles Peskine449bd832023-01-11 14:50:10 +01005667 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5668 input_data->len,
5669 plaintext, plaintext_size,
5670 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005671
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005673 tag_buffer = tag->x;
5674 tag_size = tag->len;
5675 }
5676
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 status = psa_aead_verify(&operation, finish_plaintext,
5678 verify_plaintext_size,
5679 &plaintext_length,
5680 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005681
Gilles Peskine449bd832023-01-11 14:50:10 +01005682 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005683
5684exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005685 psa_destroy_key(key);
5686 mbedtls_free(plaintext);
5687 mbedtls_free(finish_plaintext);
5688 psa_aead_abort(&operation);
5689 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005690}
5691/* END_CASE */
5692
Paul Elliott9961a662021-09-17 19:19:02 +01005693/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005694void aead_multipart_setup(int key_type_arg, data_t *key_data,
5695 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005696{
5697 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5698 psa_key_type_t key_type = key_type_arg;
5699 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005700 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005701 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5702 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5703 psa_status_t expected_status = expected_status_arg;
5704
Gilles Peskine449bd832023-01-11 14:50:10 +01005705 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005706
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 psa_set_key_usage_flags(&attributes,
5708 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5709 psa_set_key_algorithm(&attributes, alg);
5710 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005711
Gilles Peskine449bd832023-01-11 14:50:10 +01005712 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5713 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005714
Gilles Peskine449bd832023-01-11 14:50:10 +01005715 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005716
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005718
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005720
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005722
Gilles Peskine449bd832023-01-11 14:50:10 +01005723 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005724
5725exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 psa_destroy_key(key);
5727 psa_aead_abort(&operation);
5728 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005729}
5730/* END_CASE */
5731
5732/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005733void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5734 int alg_arg,
5735 data_t *nonce,
5736 data_t *additional_data,
5737 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005738{
5739 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5740 psa_key_type_t key_type = key_type_arg;
5741 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005742 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005743 unsigned char *output_data = NULL;
5744 unsigned char *final_data = NULL;
5745 size_t output_size = 0;
5746 size_t finish_output_size = 0;
5747 size_t output_length = 0;
5748 size_t key_bits = 0;
5749 size_t tag_length = 0;
5750 size_t tag_size = 0;
5751 size_t nonce_length = 0;
5752 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5753 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5754 size_t output_part_length = 0;
5755 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5756
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005758
Gilles Peskine449bd832023-01-11 14:50:10 +01005759 psa_set_key_usage_flags(&attributes,
5760 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5761 psa_set_key_algorithm(&attributes, alg);
5762 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005763
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5765 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005766
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5768 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005769
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005771
Gilles Peskine449bd832023-01-11 14:50:10 +01005772 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005773
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005775
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005776 TEST_CALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005777
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005779
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005781
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005782 TEST_CALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005783
5784 /* Test all operations error without calling setup first. */
5785
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5787 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005788
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005790
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5792 PSA_AEAD_NONCE_MAX_SIZE,
5793 &nonce_length),
5794 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005795
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005797
Paul Elliott481be342021-07-16 17:38:47 +01005798 /* ------------------------------------------------------- */
5799
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5801 input_data->len),
5802 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005803
Gilles Peskine449bd832023-01-11 14:50:10 +01005804 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005805
Paul Elliott481be342021-07-16 17:38:47 +01005806 /* ------------------------------------------------------- */
5807
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5809 additional_data->len),
5810 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005811
Gilles Peskine449bd832023-01-11 14:50:10 +01005812 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005813
Paul Elliott481be342021-07-16 17:38:47 +01005814 /* ------------------------------------------------------- */
5815
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5817 input_data->len, output_data,
5818 output_size, &output_length),
5819 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005820
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005822
Paul Elliott481be342021-07-16 17:38:47 +01005823 /* ------------------------------------------------------- */
5824
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5826 finish_output_size,
5827 &output_part_length,
5828 tag_buffer, tag_length,
5829 &tag_size),
5830 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005831
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005833
Paul Elliott481be342021-07-16 17:38:47 +01005834 /* ------------------------------------------------------- */
5835
Gilles Peskine449bd832023-01-11 14:50:10 +01005836 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5837 finish_output_size,
5838 &output_part_length,
5839 tag_buffer,
5840 tag_length),
5841 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005842
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005844
5845 /* Test for double setups. */
5846
Gilles Peskine449bd832023-01-11 14:50:10 +01005847 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005848
Gilles Peskine449bd832023-01-11 14:50:10 +01005849 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5850 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005851
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005853
Paul Elliott481be342021-07-16 17:38:47 +01005854 /* ------------------------------------------------------- */
5855
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005857
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5859 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005860
Gilles Peskine449bd832023-01-11 14:50:10 +01005861 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005862
Paul Elliott374a2be2021-07-16 17:53:40 +01005863 /* ------------------------------------------------------- */
5864
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005866
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5868 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005869
Gilles Peskine449bd832023-01-11 14:50:10 +01005870 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005871
5872 /* ------------------------------------------------------- */
5873
Gilles Peskine449bd832023-01-11 14:50:10 +01005874 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005875
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5877 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005878
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005880
Paul Elliottc23a9a02021-06-21 18:32:46 +01005881 /* Test for not setting a nonce. */
5882
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005884
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5886 additional_data->len),
5887 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005888
Gilles Peskine449bd832023-01-11 14:50:10 +01005889 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005890
Paul Elliott7f628422021-09-01 12:08:29 +01005891 /* ------------------------------------------------------- */
5892
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005894
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5896 input_data->len, output_data,
5897 output_size, &output_length),
5898 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005899
Gilles Peskine449bd832023-01-11 14:50:10 +01005900 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005901
Paul Elliottbdc2c682021-09-21 18:37:10 +01005902 /* ------------------------------------------------------- */
5903
Gilles Peskine449bd832023-01-11 14:50:10 +01005904 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005905
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5907 finish_output_size,
5908 &output_part_length,
5909 tag_buffer, tag_length,
5910 &tag_size),
5911 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005912
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005914
5915 /* ------------------------------------------------------- */
5916
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005918
Gilles Peskine449bd832023-01-11 14:50:10 +01005919 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5920 finish_output_size,
5921 &output_part_length,
5922 tag_buffer,
5923 tag_length),
5924 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005925
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005927
Paul Elliottc23a9a02021-06-21 18:32:46 +01005928 /* Test for double setting nonce. */
5929
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005931
Gilles Peskine449bd832023-01-11 14:50:10 +01005932 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005933
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5935 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005936
Gilles Peskine449bd832023-01-11 14:50:10 +01005937 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005938
Paul Elliott374a2be2021-07-16 17:53:40 +01005939 /* Test for double generating nonce. */
5940
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005942
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5944 PSA_AEAD_NONCE_MAX_SIZE,
5945 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005946
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5948 PSA_AEAD_NONCE_MAX_SIZE,
5949 &nonce_length),
5950 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005951
5952
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005954
5955 /* Test for generate nonce then set and vice versa */
5956
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005958
Gilles Peskine449bd832023-01-11 14:50:10 +01005959 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5960 PSA_AEAD_NONCE_MAX_SIZE,
5961 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005962
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5964 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005965
Gilles Peskine449bd832023-01-11 14:50:10 +01005966 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005967
Andrzej Kurekad837522021-12-15 15:28:49 +01005968 /* Test for generating nonce after calling set lengths */
5969
Gilles Peskine449bd832023-01-11 14:50:10 +01005970 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005971
Gilles Peskine449bd832023-01-11 14:50:10 +01005972 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5973 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005974
Gilles Peskine449bd832023-01-11 14:50:10 +01005975 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5976 PSA_AEAD_NONCE_MAX_SIZE,
5977 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005978
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005980
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005981 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005982
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005984
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 if (operation.alg == PSA_ALG_CCM) {
5986 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5987 input_data->len),
5988 PSA_ERROR_INVALID_ARGUMENT);
5989 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5990 PSA_AEAD_NONCE_MAX_SIZE,
5991 &nonce_length),
5992 PSA_ERROR_BAD_STATE);
5993 } else {
5994 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5995 input_data->len));
5996 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5997 PSA_AEAD_NONCE_MAX_SIZE,
5998 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005999 }
6000
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006002
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006003 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00006004#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006005 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006006
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
6008 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
6009 input_data->len),
6010 PSA_ERROR_INVALID_ARGUMENT);
6011 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6012 PSA_AEAD_NONCE_MAX_SIZE,
6013 &nonce_length),
6014 PSA_ERROR_BAD_STATE);
6015 } else {
6016 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6017 input_data->len));
6018 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6019 PSA_AEAD_NONCE_MAX_SIZE,
6020 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006021 }
6022
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00006024#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01006025
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006026 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01006027
Gilles Peskine449bd832023-01-11 14:50:10 +01006028 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006029
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6031 PSA_AEAD_NONCE_MAX_SIZE,
6032 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006033
Gilles Peskine449bd832023-01-11 14:50:10 +01006034 if (operation.alg == PSA_ALG_CCM) {
6035 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6036 input_data->len),
6037 PSA_ERROR_INVALID_ARGUMENT);
6038 } else {
6039 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6040 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006041 }
6042
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006044
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006045 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006046 /* Test for setting nonce after calling set lengths */
6047
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006049
Gilles Peskine449bd832023-01-11 14:50:10 +01006050 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6051 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006052
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006054
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006056
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006057 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006058
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006060
Gilles Peskine449bd832023-01-11 14:50:10 +01006061 if (operation.alg == PSA_ALG_CCM) {
6062 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6063 input_data->len),
6064 PSA_ERROR_INVALID_ARGUMENT);
6065 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6066 PSA_ERROR_BAD_STATE);
6067 } else {
6068 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6069 input_data->len));
6070 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006071 }
6072
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006074
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006075 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00006076#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006078
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
6080 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
6081 input_data->len),
6082 PSA_ERROR_INVALID_ARGUMENT);
6083 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6084 PSA_ERROR_BAD_STATE);
6085 } else {
6086 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6087 input_data->len));
6088 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006089 }
6090
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00006092#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006093
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006094 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006095
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006097
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006099
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 if (operation.alg == PSA_ALG_CCM) {
6101 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6102 input_data->len),
6103 PSA_ERROR_INVALID_ARGUMENT);
6104 } else {
6105 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6106 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006107 }
6108
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006110
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006111 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006112#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006114
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 if (operation.alg == PSA_ALG_GCM) {
6116 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6117 SIZE_MAX),
6118 PSA_ERROR_INVALID_ARGUMENT);
6119 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6120 PSA_ERROR_BAD_STATE);
6121 } else if (operation.alg != PSA_ALG_CCM) {
6122 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6123 SIZE_MAX));
6124 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006125 }
6126
Gilles Peskine449bd832023-01-11 14:50:10 +01006127 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006128#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006129
Tom Cosgrove1797b052022-12-04 17:19:59 +00006130 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006131#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006133
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006135
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 if (operation.alg == PSA_ALG_GCM) {
6137 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6138 SIZE_MAX),
6139 PSA_ERROR_INVALID_ARGUMENT);
6140 } else if (operation.alg != PSA_ALG_CCM) {
6141 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6142 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006143 }
6144
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006146#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006147
6148 /* ------------------------------------------------------- */
6149
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006151
Gilles Peskine449bd832023-01-11 14:50:10 +01006152 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006153
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6155 PSA_AEAD_NONCE_MAX_SIZE,
6156 &nonce_length),
6157 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006158
Gilles Peskine449bd832023-01-11 14:50:10 +01006159 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006160
Paul Elliott7220cae2021-06-22 17:25:57 +01006161 /* Test for generating nonce in decrypt setup. */
6162
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006164
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6166 PSA_AEAD_NONCE_MAX_SIZE,
6167 &nonce_length),
6168 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006169
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006171
Paul Elliottc23a9a02021-06-21 18:32:46 +01006172 /* Test for setting lengths twice. */
6173
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006175
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006177
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6179 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006180
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6182 input_data->len),
6183 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006184
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006186
Andrzej Kurekad837522021-12-15 15:28:49 +01006187 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006188
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006190
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006192
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006194
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6196 additional_data->len),
6197 PSA_ERROR_BAD_STATE);
6198 } else {
6199 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6200 additional_data->len));
6201
6202 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6203 input_data->len),
6204 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006205 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006207
6208 /* ------------------------------------------------------- */
6209
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006211
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006213
Gilles Peskine449bd832023-01-11 14:50:10 +01006214 if (operation.alg == PSA_ALG_CCM) {
6215 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6216 input_data->len, output_data,
6217 output_size, &output_length),
6218 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006219
Gilles Peskine449bd832023-01-11 14:50:10 +01006220 } else {
6221 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6222 input_data->len, output_data,
6223 output_size, &output_length));
6224
6225 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6226 input_data->len),
6227 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006228 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006230
6231 /* ------------------------------------------------------- */
6232
Gilles Peskine449bd832023-01-11 14:50:10 +01006233 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006234
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006236
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 if (operation.alg == PSA_ALG_CCM) {
6238 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6239 finish_output_size,
6240 &output_part_length,
6241 tag_buffer, tag_length,
6242 &tag_size));
6243 } else {
6244 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6245 finish_output_size,
6246 &output_part_length,
6247 tag_buffer, tag_length,
6248 &tag_size));
6249
6250 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6251 input_data->len),
6252 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006253 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006255
6256 /* Test for setting lengths after generating nonce + already starting data. */
6257
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6261 PSA_AEAD_NONCE_MAX_SIZE,
6262 &nonce_length));
6263 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006264
Gilles Peskine449bd832023-01-11 14:50:10 +01006265 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6266 additional_data->len),
6267 PSA_ERROR_BAD_STATE);
6268 } else {
6269 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6270 additional_data->len));
6271
6272 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6273 input_data->len),
6274 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006275 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006277
6278 /* ------------------------------------------------------- */
6279
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006281
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6283 PSA_AEAD_NONCE_MAX_SIZE,
6284 &nonce_length));
6285 if (operation.alg == PSA_ALG_CCM) {
6286 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6287 input_data->len, output_data,
6288 output_size, &output_length),
6289 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006290
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 } else {
6292 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6293 input_data->len, output_data,
6294 output_size, &output_length));
6295
6296 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6297 input_data->len),
6298 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006299 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006301
6302 /* ------------------------------------------------------- */
6303
Gilles Peskine449bd832023-01-11 14:50:10 +01006304 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006305
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6307 PSA_AEAD_NONCE_MAX_SIZE,
6308 &nonce_length));
6309 if (operation.alg == PSA_ALG_CCM) {
6310 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6311 finish_output_size,
6312 &output_part_length,
6313 tag_buffer, tag_length,
6314 &tag_size));
6315 } else {
6316 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6317 finish_output_size,
6318 &output_part_length,
6319 tag_buffer, tag_length,
6320 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006321
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6323 input_data->len),
6324 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006325 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006327
Paul Elliott243080c2021-07-21 19:01:17 +01006328 /* Test for not sending any additional data or data after setting non zero
6329 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006330
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006332
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006334
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6336 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006337
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6339 finish_output_size,
6340 &output_part_length,
6341 tag_buffer, tag_length,
6342 &tag_size),
6343 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006344
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006346
Paul Elliott243080c2021-07-21 19:01:17 +01006347 /* Test for not sending any additional data or data after setting non-zero
6348 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006349
Gilles Peskine449bd832023-01-11 14:50:10 +01006350 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006351
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006353
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6355 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006356
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6358 finish_output_size,
6359 &output_part_length,
6360 tag_buffer,
6361 tag_length),
6362 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006363
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006365
Paul Elliott243080c2021-07-21 19:01:17 +01006366 /* Test for not sending any additional data after setting a non-zero length
6367 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006368
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006370
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006372
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6374 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006375
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6377 input_data->len, output_data,
6378 output_size, &output_length),
6379 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006380
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006382
Paul Elliottf94bd992021-09-19 18:15:59 +01006383 /* Test for not sending any data after setting a non-zero length for it.*/
6384
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006386
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006388
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6390 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006391
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6393 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006394
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6396 finish_output_size,
6397 &output_part_length,
6398 tag_buffer, tag_length,
6399 &tag_size),
6400 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006401
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006403
Paul Elliottb0450fe2021-09-01 15:06:26 +01006404 /* Test for sending too much additional data after setting lengths. */
6405
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006407
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006409
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006411
6412
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6414 additional_data->len),
6415 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006416
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006418
Paul Elliotta2a09b02021-09-22 14:56:40 +01006419 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006420
Gilles Peskine449bd832023-01-11 14:50:10 +01006421 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006422
Gilles Peskine449bd832023-01-11 14:50:10 +01006423 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006424
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6426 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006427
Gilles Peskine449bd832023-01-11 14:50:10 +01006428 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6429 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006430
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6432 1),
6433 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006434
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006436
Paul Elliottb0450fe2021-09-01 15:06:26 +01006437 /* Test for sending too much data after setting lengths. */
6438
Gilles Peskine449bd832023-01-11 14:50:10 +01006439 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006440
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006442
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006444
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6446 input_data->len, output_data,
6447 output_size, &output_length),
6448 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006449
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006451
Paul Elliotta2a09b02021-09-22 14:56:40 +01006452 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006453
Gilles Peskine449bd832023-01-11 14:50:10 +01006454 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006455
Gilles Peskine449bd832023-01-11 14:50:10 +01006456 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006457
Gilles Peskine449bd832023-01-11 14:50:10 +01006458 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6459 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006460
Gilles Peskine449bd832023-01-11 14:50:10 +01006461 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6462 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006463
Gilles Peskine449bd832023-01-11 14:50:10 +01006464 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6465 input_data->len, output_data,
6466 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006467
Gilles Peskine449bd832023-01-11 14:50:10 +01006468 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6469 1, output_data,
6470 output_size, &output_length),
6471 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006472
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006474
Paul Elliottc23a9a02021-06-21 18:32:46 +01006475 /* Test sending additional data after data. */
6476
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006478
Gilles Peskine449bd832023-01-11 14:50:10 +01006479 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006480
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 if (operation.alg != PSA_ALG_CCM) {
6482 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6483 input_data->len, output_data,
6484 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006485
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6487 additional_data->len),
6488 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006489 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006491
Paul Elliott534d0b42021-06-22 19:15:20 +01006492 /* Test calling finish on decryption. */
6493
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006495
Gilles Peskine449bd832023-01-11 14:50:10 +01006496 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006497
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6499 finish_output_size,
6500 &output_part_length,
6501 tag_buffer, tag_length,
6502 &tag_size),
6503 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006504
Gilles Peskine449bd832023-01-11 14:50:10 +01006505 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006506
6507 /* Test calling verify on encryption. */
6508
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006510
Gilles Peskine449bd832023-01-11 14:50:10 +01006511 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006512
Gilles Peskine449bd832023-01-11 14:50:10 +01006513 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6514 finish_output_size,
6515 &output_part_length,
6516 tag_buffer,
6517 tag_length),
6518 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006519
Gilles Peskine449bd832023-01-11 14:50:10 +01006520 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006521
6522
Paul Elliottc23a9a02021-06-21 18:32:46 +01006523exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006524 psa_destroy_key(key);
6525 psa_aead_abort(&operation);
6526 mbedtls_free(output_data);
6527 mbedtls_free(final_data);
6528 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006529}
6530/* END_CASE */
6531
6532/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006533void signature_size(int type_arg,
6534 int bits,
6535 int alg_arg,
6536 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006537{
6538 psa_key_type_t type = type_arg;
6539 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006540 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006541
Gilles Peskine449bd832023-01-11 14:50:10 +01006542 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006543
Gilles Peskinee59236f2018-01-27 23:32:46 +01006544exit:
6545 ;
6546}
6547/* END_CASE */
6548
6549/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006550void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6551 int alg_arg, data_t *input_data,
6552 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006553{
Ronald Cron5425a212020-08-04 14:58:35 +02006554 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006555 psa_key_type_t key_type = key_type_arg;
6556 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006557 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006558 unsigned char *signature = NULL;
6559 size_t signature_size;
6560 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006561 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006562
Gilles Peskine449bd832023-01-11 14:50:10 +01006563 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006564
Gilles Peskine449bd832023-01-11 14:50:10 +01006565 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6566 psa_set_key_algorithm(&attributes, alg);
6567 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006568
Gilles Peskine449bd832023-01-11 14:50:10 +01006569 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6570 &key));
6571 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6572 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006573
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006574 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006575 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006576 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6577 key_bits, alg);
6578 TEST_ASSERT(signature_size != 0);
6579 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006580 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006581
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006582 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006583 PSA_ASSERT(psa_sign_hash(key, alg,
6584 input_data->x, input_data->len,
6585 signature, signature_size,
6586 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006587 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006588 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006589 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006590
6591exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006592 /*
6593 * Key attributes may have been returned by psa_get_key_attributes()
6594 * thus reset them as required.
6595 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006596 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006597
Gilles Peskine449bd832023-01-11 14:50:10 +01006598 psa_destroy_key(key);
6599 mbedtls_free(signature);
6600 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006601}
6602/* END_CASE */
6603
Paul Elliott712d5122022-12-07 14:03:10 +00006604/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006605/**
6606 * sign_hash_interruptible() test intentions:
6607 *
6608 * Note: This test can currently only handle ECDSA.
6609 *
6610 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006611 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006612 *
6613 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6614 * expected for different max_ops values.
6615 *
6616 * 3. Test that the number of ops done prior to start and after abort is zero
6617 * and that each successful stage completes some ops (this is not mandated by
6618 * the PSA specification, but is currently the case).
6619 *
6620 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6621 * complete() calls does not alter the number of ops returned.
6622 */
Paul Elliott712d5122022-12-07 14:03:10 +00006623void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6624 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006625 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006626{
6627 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6628 psa_key_type_t key_type = key_type_arg;
6629 psa_algorithm_t alg = alg_arg;
6630 size_t key_bits;
6631 unsigned char *signature = NULL;
6632 size_t signature_size;
6633 size_t signature_length = 0xdeadbeef;
6634 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6635 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006636 uint32_t num_ops = 0;
6637 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006638 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006639 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006640 size_t min_completes = 0;
6641 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006642
Paul Elliott712d5122022-12-07 14:03:10 +00006643 psa_sign_hash_interruptible_operation_t operation =
6644 psa_sign_hash_interruptible_operation_init();
6645
6646 PSA_ASSERT(psa_crypto_init());
6647
6648 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6649 psa_set_key_algorithm(&attributes, alg);
6650 psa_set_key_type(&attributes, key_type);
6651
6652 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6653 &key));
6654 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6655 key_bits = psa_get_key_bits(&attributes);
6656
6657 /* Allocate a buffer which has the size advertised by the
6658 * library. */
6659 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6660 key_bits, alg);
6661 TEST_ASSERT(signature_size != 0);
6662 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006663 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006664
Paul Elliott0c683352022-12-16 19:16:56 +00006665 psa_interruptible_set_max_ops(max_ops);
6666
Paul Elliott6f600372023-02-06 18:41:05 +00006667 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6668 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006669
Paul Elliott712d5122022-12-07 14:03:10 +00006670 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6671 TEST_ASSERT(num_ops_prior == 0);
6672
6673 /* Start performing the signature. */
6674 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6675 input_data->x, input_data->len));
6676
6677 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6678 TEST_ASSERT(num_ops_prior == 0);
6679
6680 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006681 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006682 status = psa_sign_hash_complete(&operation, signature, signature_size,
6683 &signature_length);
6684
Paul Elliott0c683352022-12-16 19:16:56 +00006685 num_completes++;
6686
Paul Elliott712d5122022-12-07 14:03:10 +00006687 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6688 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006689 /* We are asserting here that every complete makes progress
6690 * (completes some ops), which is true of the internal
6691 * implementation and probably any implementation, however this is
6692 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006693 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006694
Paul Elliott712d5122022-12-07 14:03:10 +00006695 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006696
6697 /* Ensure calling get_num_ops() twice still returns the same
6698 * number of ops as previously reported. */
6699 num_ops = psa_sign_hash_get_num_ops(&operation);
6700
6701 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006702 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006703 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006704
6705 TEST_ASSERT(status == PSA_SUCCESS);
6706
Paul Elliott0c683352022-12-16 19:16:56 +00006707 TEST_LE_U(min_completes, num_completes);
6708 TEST_LE_U(num_completes, max_completes);
6709
Paul Elliott712d5122022-12-07 14:03:10 +00006710 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006711 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006712 signature, signature_length);
Paul Elliott712d5122022-12-07 14:03:10 +00006713
6714 PSA_ASSERT(psa_sign_hash_abort(&operation));
6715
Paul Elliott59ad9452022-12-18 15:09:02 +00006716 num_ops = psa_sign_hash_get_num_ops(&operation);
6717 TEST_ASSERT(num_ops == 0);
6718
Paul Elliott712d5122022-12-07 14:03:10 +00006719exit:
6720
6721 /*
6722 * Key attributes may have been returned by psa_get_key_attributes()
6723 * thus reset them as required.
6724 */
6725 psa_reset_key_attributes(&attributes);
6726
6727 psa_destroy_key(key);
6728 mbedtls_free(signature);
6729 PSA_DONE();
6730}
6731/* END_CASE */
6732
Gilles Peskine20035e32018-02-03 22:44:14 +01006733/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006734void sign_hash_fail(int key_type_arg, data_t *key_data,
6735 int alg_arg, data_t *input_data,
6736 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006737{
Ronald Cron5425a212020-08-04 14:58:35 +02006738 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006739 psa_key_type_t key_type = key_type_arg;
6740 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006741 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006742 psa_status_t actual_status;
6743 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006744 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006745 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006746 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006747
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006748 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006749
Gilles Peskine449bd832023-01-11 14:50:10 +01006750 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006751
Gilles Peskine449bd832023-01-11 14:50:10 +01006752 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6753 psa_set_key_algorithm(&attributes, alg);
6754 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006755
Gilles Peskine449bd832023-01-11 14:50:10 +01006756 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6757 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006758
Gilles Peskine449bd832023-01-11 14:50:10 +01006759 actual_status = psa_sign_hash(key, alg,
6760 input_data->x, input_data->len,
6761 signature, signature_size,
6762 &signature_length);
6763 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006764 /* The value of *signature_length is unspecified on error, but
6765 * whatever it is, it should be less than signature_size, so that
6766 * if the caller tries to read *signature_length bytes without
6767 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006768 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006769
6770exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006771 psa_reset_key_attributes(&attributes);
6772 psa_destroy_key(key);
6773 mbedtls_free(signature);
6774 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006775}
6776/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006777
Paul Elliott91007972022-12-16 12:21:24 +00006778/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006779/**
6780 * sign_hash_fail_interruptible() test intentions:
6781 *
6782 * Note: This test can currently only handle ECDSA.
6783 *
6784 * 1. Test that various failure cases for interruptible sign hash fail with the
6785 * correct error codes, and at the correct point (at start or during
6786 * complete).
6787 *
6788 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6789 * expected for different max_ops values.
6790 *
6791 * 3. Test that the number of ops done prior to start and after abort is zero
6792 * and that each successful stage completes some ops (this is not mandated by
6793 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006794 *
6795 * 4. Check that calling complete() when start() fails and complete()
6796 * after completion results in a BAD_STATE error.
6797 *
6798 * 5. Check that calling start() again after start fails results in a BAD_STATE
6799 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006800 */
Paul Elliott91007972022-12-16 12:21:24 +00006801void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6802 int alg_arg, data_t *input_data,
6803 int signature_size_arg,
6804 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006805 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006806 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006807{
6808 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6809 psa_key_type_t key_type = key_type_arg;
6810 psa_algorithm_t alg = alg_arg;
6811 size_t signature_size = signature_size_arg;
6812 psa_status_t actual_status;
6813 psa_status_t expected_start_status = expected_start_status_arg;
6814 psa_status_t expected_complete_status = expected_complete_status_arg;
6815 unsigned char *signature = NULL;
6816 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006817 uint32_t num_ops = 0;
6818 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006819 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006820 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006821 size_t min_completes = 0;
6822 size_t max_completes = 0;
6823
Paul Elliott91007972022-12-16 12:21:24 +00006824 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6825 psa_sign_hash_interruptible_operation_t operation =
6826 psa_sign_hash_interruptible_operation_init();
6827
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006828 TEST_CALLOC(signature, signature_size);
Paul Elliott91007972022-12-16 12:21:24 +00006829
6830 PSA_ASSERT(psa_crypto_init());
6831
6832 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6833 psa_set_key_algorithm(&attributes, alg);
6834 psa_set_key_type(&attributes, key_type);
6835
6836 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6837 &key));
6838
Paul Elliott0c683352022-12-16 19:16:56 +00006839 psa_interruptible_set_max_ops(max_ops);
6840
Paul Elliott6f600372023-02-06 18:41:05 +00006841 interruptible_signverify_get_minmax_completes(max_ops,
6842 expected_complete_status,
6843 &min_completes,
6844 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006845
Paul Elliott91007972022-12-16 12:21:24 +00006846 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6847 TEST_ASSERT(num_ops_prior == 0);
6848
6849 /* Start performing the signature. */
6850 actual_status = psa_sign_hash_start(&operation, key, alg,
6851 input_data->x, input_data->len);
6852
6853 TEST_EQUAL(actual_status, expected_start_status);
6854
Paul Elliottc9774412023-02-06 15:14:07 +00006855 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006856 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006857 * start failed. */
6858 actual_status = psa_sign_hash_complete(&operation, signature,
6859 signature_size,
6860 &signature_length);
6861
6862 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6863
6864 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006865 actual_status = psa_sign_hash_start(&operation, key, alg,
6866 input_data->x, input_data->len);
6867
6868 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6869 }
6870
Paul Elliott91007972022-12-16 12:21:24 +00006871 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6872 TEST_ASSERT(num_ops_prior == 0);
6873
Paul Elliott91007972022-12-16 12:21:24 +00006874 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006875 do {
Paul Elliott91007972022-12-16 12:21:24 +00006876 actual_status = psa_sign_hash_complete(&operation, signature,
6877 signature_size,
6878 &signature_length);
6879
Paul Elliott0c683352022-12-16 19:16:56 +00006880 num_completes++;
6881
Paul Elliott334d7262023-01-20 17:29:41 +00006882 if (actual_status == PSA_SUCCESS ||
6883 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006884 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006885 /* We are asserting here that every complete makes progress
6886 * (completes some ops), which is true of the internal
6887 * implementation and probably any implementation, however this is
6888 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006889 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006890
Paul Elliott91007972022-12-16 12:21:24 +00006891 num_ops_prior = num_ops;
6892 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006893 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006894
Paul Elliottc9774412023-02-06 15:14:07 +00006895 TEST_EQUAL(actual_status, expected_complete_status);
6896
Paul Elliottefebad02023-02-15 16:56:45 +00006897 /* Check that another complete returns BAD_STATE. */
6898 actual_status = psa_sign_hash_complete(&operation, signature,
6899 signature_size,
6900 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006901
Paul Elliottefebad02023-02-15 16:56:45 +00006902 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006903
Paul Elliott91007972022-12-16 12:21:24 +00006904 PSA_ASSERT(psa_sign_hash_abort(&operation));
6905
Paul Elliott59ad9452022-12-18 15:09:02 +00006906 num_ops = psa_sign_hash_get_num_ops(&operation);
6907 TEST_ASSERT(num_ops == 0);
6908
Paul Elliott91007972022-12-16 12:21:24 +00006909 /* The value of *signature_length is unspecified on error, but
6910 * whatever it is, it should be less than signature_size, so that
6911 * if the caller tries to read *signature_length bytes without
6912 * checking the error code then they don't overflow a buffer. */
6913 TEST_LE_U(signature_length, signature_size);
6914
Paul Elliott0c683352022-12-16 19:16:56 +00006915 TEST_LE_U(min_completes, num_completes);
6916 TEST_LE_U(num_completes, max_completes);
6917
Paul Elliott91007972022-12-16 12:21:24 +00006918exit:
6919 psa_reset_key_attributes(&attributes);
6920 psa_destroy_key(key);
6921 mbedtls_free(signature);
6922 PSA_DONE();
6923}
6924/* END_CASE */
6925
mohammad16038cc1cee2018-03-28 01:21:33 +03006926/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006927void sign_verify_hash(int key_type_arg, data_t *key_data,
6928 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006929{
Ronald Cron5425a212020-08-04 14:58:35 +02006930 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006931 psa_key_type_t key_type = key_type_arg;
6932 psa_algorithm_t alg = alg_arg;
6933 size_t key_bits;
6934 unsigned char *signature = NULL;
6935 size_t signature_size;
6936 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006937 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006938
Gilles Peskine449bd832023-01-11 14:50:10 +01006939 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006940
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6942 psa_set_key_algorithm(&attributes, alg);
6943 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006944
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6946 &key));
6947 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6948 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006949
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006950 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006951 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6953 key_bits, alg);
6954 TEST_ASSERT(signature_size != 0);
6955 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006956 TEST_CALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006957
6958 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006959 PSA_ASSERT(psa_sign_hash(key, alg,
6960 input_data->x, input_data->len,
6961 signature, signature_size,
6962 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006963 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006964 TEST_LE_U(signature_length, signature_size);
6965 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006966
6967 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006968 PSA_ASSERT(psa_verify_hash(key, alg,
6969 input_data->x, input_data->len,
6970 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006971
Gilles Peskine449bd832023-01-11 14:50:10 +01006972 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006973 /* Flip a bit in the input and verify that the signature is now
6974 * detected as invalid. Flip a bit at the beginning, not at the end,
6975 * because ECDSA may ignore the last few bits of the input. */
6976 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006977 TEST_EQUAL(psa_verify_hash(key, alg,
6978 input_data->x, input_data->len,
6979 signature, signature_length),
6980 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006981 }
6982
6983exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006984 /*
6985 * Key attributes may have been returned by psa_get_key_attributes()
6986 * thus reset them as required.
6987 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006988 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006989
Gilles Peskine449bd832023-01-11 14:50:10 +01006990 psa_destroy_key(key);
6991 mbedtls_free(signature);
6992 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006993}
6994/* END_CASE */
6995
Paul Elliott712d5122022-12-07 14:03:10 +00006996/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006997/**
6998 * sign_verify_hash_interruptible() test intentions:
6999 *
7000 * Note: This test can currently only handle ECDSA.
7001 *
Paul Elliott8c092052023-03-06 17:49:14 +00007002 * 1. Test that we can sign an input hash with the given keypair and then
7003 * afterwards verify that signature. This is currently the only way to test
7004 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00007005 *
7006 * 2. Test that after corrupting the hash, the verification detects an invalid
7007 * signature.
7008 *
7009 * 3. Test the number of calls to psa_sign_hash_complete() required are as
7010 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00007011 *
7012 * 4. Test that the number of ops done prior to starting signing and after abort
7013 * is zero and that each successful signing stage completes some ops (this is
7014 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00007015 */
Paul Elliott712d5122022-12-07 14:03:10 +00007016void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00007017 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007018 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007019{
7020 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7021 psa_key_type_t key_type = key_type_arg;
7022 psa_algorithm_t alg = alg_arg;
7023 size_t key_bits;
7024 unsigned char *signature = NULL;
7025 size_t signature_size;
7026 size_t signature_length = 0xdeadbeef;
7027 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7028 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007029 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00007030 uint32_t num_ops = 0;
7031 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007032 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007033 size_t min_completes = 0;
7034 size_t max_completes = 0;
7035
Paul Elliott712d5122022-12-07 14:03:10 +00007036 psa_sign_hash_interruptible_operation_t sign_operation =
7037 psa_sign_hash_interruptible_operation_init();
7038 psa_verify_hash_interruptible_operation_t verify_operation =
7039 psa_verify_hash_interruptible_operation_init();
7040
7041 PSA_ASSERT(psa_crypto_init());
7042
Paul Elliott0c683352022-12-16 19:16:56 +00007043 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7044 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00007045 psa_set_key_algorithm(&attributes, alg);
7046 psa_set_key_type(&attributes, key_type);
7047
7048 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7049 &key));
7050 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7051 key_bits = psa_get_key_bits(&attributes);
7052
7053 /* Allocate a buffer which has the size advertised by the
7054 * library. */
7055 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7056 key_bits, alg);
7057 TEST_ASSERT(signature_size != 0);
7058 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007059 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00007060
Paul Elliott0c683352022-12-16 19:16:56 +00007061 psa_interruptible_set_max_ops(max_ops);
7062
Paul Elliott6f600372023-02-06 18:41:05 +00007063 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7064 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007065
Paul Elliott7c173082023-02-26 18:44:45 +00007066 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
7067 TEST_ASSERT(num_ops_prior == 0);
7068
Paul Elliott712d5122022-12-07 14:03:10 +00007069 /* Start performing the signature. */
7070 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7071 input_data->x, input_data->len));
7072
Paul Elliott7c173082023-02-26 18:44:45 +00007073 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
7074 TEST_ASSERT(num_ops_prior == 0);
7075
Paul Elliott712d5122022-12-07 14:03:10 +00007076 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007077 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007078
Paul Elliott0c683352022-12-16 19:16:56 +00007079 status = psa_sign_hash_complete(&sign_operation, signature,
7080 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00007081 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00007082
7083 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00007084
7085 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7086 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7087 /* We are asserting here that every complete makes progress
7088 * (completes some ops), which is true of the internal
7089 * implementation and probably any implementation, however this is
7090 * not mandated by the PSA specification. */
7091 TEST_ASSERT(num_ops > num_ops_prior);
7092
7093 num_ops_prior = num_ops;
7094 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007095 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007096
7097 TEST_ASSERT(status == PSA_SUCCESS);
7098
Paul Elliott0c683352022-12-16 19:16:56 +00007099 TEST_LE_U(min_completes, num_completes);
7100 TEST_LE_U(num_completes, max_completes);
7101
Paul Elliott712d5122022-12-07 14:03:10 +00007102 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7103
Paul Elliott7c173082023-02-26 18:44:45 +00007104 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7105 TEST_ASSERT(num_ops == 0);
7106
Paul Elliott712d5122022-12-07 14:03:10 +00007107 /* Check that the signature length looks sensible. */
7108 TEST_LE_U(signature_length, signature_size);
7109 TEST_ASSERT(signature_length > 0);
7110
Paul Elliott0c683352022-12-16 19:16:56 +00007111 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007112
7113 /* Start verification. */
7114 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7115 input_data->x, input_data->len,
7116 signature, signature_length));
7117
7118 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007119 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007120 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007121
7122 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007123 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007124
7125 TEST_ASSERT(status == PSA_SUCCESS);
7126
Paul Elliott0c683352022-12-16 19:16:56 +00007127 TEST_LE_U(min_completes, num_completes);
7128 TEST_LE_U(num_completes, max_completes);
7129
Paul Elliott712d5122022-12-07 14:03:10 +00007130 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7131
7132 verify_operation = psa_verify_hash_interruptible_operation_init();
7133
7134 if (input_data->len != 0) {
7135 /* Flip a bit in the input and verify that the signature is now
7136 * detected as invalid. Flip a bit at the beginning, not at the end,
7137 * because ECDSA may ignore the last few bits of the input. */
7138 input_data->x[0] ^= 1;
7139
Paul Elliott712d5122022-12-07 14:03:10 +00007140 /* Start verification. */
7141 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7142 input_data->x, input_data->len,
7143 signature, signature_length));
7144
7145 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007146 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007147 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007148 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007149
7150 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7151 }
7152
7153 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7154
7155exit:
7156 /*
7157 * Key attributes may have been returned by psa_get_key_attributes()
7158 * thus reset them as required.
7159 */
7160 psa_reset_key_attributes(&attributes);
7161
7162 psa_destroy_key(key);
7163 mbedtls_free(signature);
7164 PSA_DONE();
7165}
7166/* END_CASE */
7167
Gilles Peskine9911b022018-06-29 17:30:48 +02007168/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007169void verify_hash(int key_type_arg, data_t *key_data,
7170 int alg_arg, data_t *hash_data,
7171 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007172{
Ronald Cron5425a212020-08-04 14:58:35 +02007173 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007174 psa_key_type_t key_type = key_type_arg;
7175 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007176 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007177
Gilles Peskine449bd832023-01-11 14:50:10 +01007178 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007179
Gilles Peskine449bd832023-01-11 14:50:10 +01007180 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007181
Gilles Peskine449bd832023-01-11 14:50:10 +01007182 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7183 psa_set_key_algorithm(&attributes, alg);
7184 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007185
Gilles Peskine449bd832023-01-11 14:50:10 +01007186 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7187 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007188
Gilles Peskine449bd832023-01-11 14:50:10 +01007189 PSA_ASSERT(psa_verify_hash(key, alg,
7190 hash_data->x, hash_data->len,
7191 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007192
itayzafrir5c753392018-05-08 11:18:38 +03007193exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007194 psa_reset_key_attributes(&attributes);
7195 psa_destroy_key(key);
7196 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007197}
7198/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007199
Paul Elliott712d5122022-12-07 14:03:10 +00007200/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007201/**
7202 * verify_hash_interruptible() test intentions:
7203 *
7204 * Note: This test can currently only handle ECDSA.
7205 *
7206 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007207 * only). Given this test only does verification it can accept public keys as
7208 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007209 *
7210 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7211 * expected for different max_ops values.
7212 *
7213 * 3. Test that the number of ops done prior to start and after abort is zero
7214 * and that each successful stage completes some ops (this is not mandated by
7215 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007216 *
7217 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7218 * complete() calls does not alter the number of ops returned.
7219 *
7220 * 5. Test that after corrupting the hash, the verification detects an invalid
7221 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007222 */
Paul Elliott712d5122022-12-07 14:03:10 +00007223void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7224 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007225 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007226{
7227 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7228 psa_key_type_t key_type = key_type_arg;
7229 psa_algorithm_t alg = alg_arg;
7230 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7231 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007232 uint32_t num_ops = 0;
7233 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007234 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007235 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007236 size_t min_completes = 0;
7237 size_t max_completes = 0;
7238
Paul Elliott712d5122022-12-07 14:03:10 +00007239 psa_verify_hash_interruptible_operation_t operation =
7240 psa_verify_hash_interruptible_operation_init();
7241
7242 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7243
7244 PSA_ASSERT(psa_crypto_init());
7245
7246 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7247 psa_set_key_algorithm(&attributes, alg);
7248 psa_set_key_type(&attributes, key_type);
7249
7250 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7251 &key));
7252
Paul Elliott0c683352022-12-16 19:16:56 +00007253 psa_interruptible_set_max_ops(max_ops);
7254
Paul Elliott6f600372023-02-06 18:41:05 +00007255 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7256 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007257
Paul Elliott712d5122022-12-07 14:03:10 +00007258 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7259
7260 TEST_ASSERT(num_ops_prior == 0);
7261
7262 /* Start verification. */
7263 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7264 hash_data->x, hash_data->len,
7265 signature_data->x, signature_data->len)
7266 );
7267
7268 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7269
7270 TEST_ASSERT(num_ops_prior == 0);
7271
7272 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007273 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007274 status = psa_verify_hash_complete(&operation);
7275
Paul Elliott0c683352022-12-16 19:16:56 +00007276 num_completes++;
7277
Paul Elliott712d5122022-12-07 14:03:10 +00007278 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7279 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007280 /* We are asserting here that every complete makes progress
7281 * (completes some ops), which is true of the internal
7282 * implementation and probably any implementation, however this is
7283 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007284 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007285
Paul Elliott712d5122022-12-07 14:03:10 +00007286 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007287
7288 /* Ensure calling get_num_ops() twice still returns the same
7289 * number of ops as previously reported. */
7290 num_ops = psa_verify_hash_get_num_ops(&operation);
7291
7292 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007293 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007294 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007295
7296 TEST_ASSERT(status == PSA_SUCCESS);
7297
Paul Elliott0c683352022-12-16 19:16:56 +00007298 TEST_LE_U(min_completes, num_completes);
7299 TEST_LE_U(num_completes, max_completes);
7300
Paul Elliott712d5122022-12-07 14:03:10 +00007301 PSA_ASSERT(psa_verify_hash_abort(&operation));
7302
Paul Elliott59ad9452022-12-18 15:09:02 +00007303 num_ops = psa_verify_hash_get_num_ops(&operation);
7304 TEST_ASSERT(num_ops == 0);
7305
Paul Elliott8359c142023-02-24 18:40:10 +00007306 if (hash_data->len != 0) {
7307 /* Flip a bit in the hash and verify that the signature is now detected
7308 * as invalid. Flip a bit at the beginning, not at the end, because
7309 * ECDSA may ignore the last few bits of the input. */
7310 hash_data->x[0] ^= 1;
7311
7312 /* Start verification. */
7313 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7314 hash_data->x, hash_data->len,
7315 signature_data->x, signature_data->len));
7316
7317 /* Continue performing the signature until complete. */
7318 do {
7319 status = psa_verify_hash_complete(&operation);
7320 } while (status == PSA_OPERATION_INCOMPLETE);
7321
7322 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7323 }
7324
Paul Elliott712d5122022-12-07 14:03:10 +00007325exit:
7326 psa_reset_key_attributes(&attributes);
7327 psa_destroy_key(key);
7328 PSA_DONE();
7329}
7330/* END_CASE */
7331
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007332/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007333void verify_hash_fail(int key_type_arg, data_t *key_data,
7334 int alg_arg, data_t *hash_data,
7335 data_t *signature_data,
7336 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007337{
Ronald Cron5425a212020-08-04 14:58:35 +02007338 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007339 psa_key_type_t key_type = key_type_arg;
7340 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007341 psa_status_t actual_status;
7342 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007344
Gilles Peskine449bd832023-01-11 14:50:10 +01007345 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007346
Gilles Peskine449bd832023-01-11 14:50:10 +01007347 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7348 psa_set_key_algorithm(&attributes, alg);
7349 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007350
Gilles Peskine449bd832023-01-11 14:50:10 +01007351 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7352 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007353
Gilles Peskine449bd832023-01-11 14:50:10 +01007354 actual_status = psa_verify_hash(key, alg,
7355 hash_data->x, hash_data->len,
7356 signature_data->x, signature_data->len);
7357 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007358
7359exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007360 psa_reset_key_attributes(&attributes);
7361 psa_destroy_key(key);
7362 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007363}
7364/* END_CASE */
7365
Paul Elliott91007972022-12-16 12:21:24 +00007366/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007367/**
7368 * verify_hash_fail_interruptible() test intentions:
7369 *
7370 * Note: This test can currently only handle ECDSA.
7371 *
7372 * 1. Test that various failure cases for interruptible verify hash fail with
7373 * the correct error codes, and at the correct point (at start or during
7374 * complete).
7375 *
7376 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7377 * expected for different max_ops values.
7378 *
7379 * 3. Test that the number of ops done prior to start and after abort is zero
7380 * and that each successful stage completes some ops (this is not mandated by
7381 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007382 *
7383 * 4. Check that calling complete() when start() fails and complete()
7384 * after completion results in a BAD_STATE error.
7385 *
7386 * 5. Check that calling start() again after start fails results in a BAD_STATE
7387 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007388 */
Paul Elliott91007972022-12-16 12:21:24 +00007389void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7390 int alg_arg, data_t *hash_data,
7391 data_t *signature_data,
7392 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007393 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007394 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007395{
7396 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7397 psa_key_type_t key_type = key_type_arg;
7398 psa_algorithm_t alg = alg_arg;
7399 psa_status_t actual_status;
7400 psa_status_t expected_start_status = expected_start_status_arg;
7401 psa_status_t expected_complete_status = expected_complete_status_arg;
7402 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007403 uint32_t num_ops = 0;
7404 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007405 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007406 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007407 size_t min_completes = 0;
7408 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007409 psa_verify_hash_interruptible_operation_t operation =
7410 psa_verify_hash_interruptible_operation_init();
7411
7412 PSA_ASSERT(psa_crypto_init());
7413
7414 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7415 psa_set_key_algorithm(&attributes, alg);
7416 psa_set_key_type(&attributes, key_type);
7417
7418 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7419 &key));
7420
Paul Elliott0c683352022-12-16 19:16:56 +00007421 psa_interruptible_set_max_ops(max_ops);
7422
Paul Elliott6f600372023-02-06 18:41:05 +00007423 interruptible_signverify_get_minmax_completes(max_ops,
7424 expected_complete_status,
7425 &min_completes,
7426 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007427
Paul Elliott91007972022-12-16 12:21:24 +00007428 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7429 TEST_ASSERT(num_ops_prior == 0);
7430
7431 /* Start verification. */
7432 actual_status = psa_verify_hash_start(&operation, key, alg,
7433 hash_data->x, hash_data->len,
7434 signature_data->x,
7435 signature_data->len);
7436
7437 TEST_EQUAL(actual_status, expected_start_status);
7438
Paul Elliottc9774412023-02-06 15:14:07 +00007439 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007440 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007441 * start failed. */
7442 actual_status = psa_verify_hash_complete(&operation);
7443
7444 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7445
7446 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007447 actual_status = psa_verify_hash_start(&operation, key, alg,
7448 hash_data->x, hash_data->len,
7449 signature_data->x,
7450 signature_data->len);
7451
7452 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7453 }
7454
Paul Elliott91007972022-12-16 12:21:24 +00007455 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7456 TEST_ASSERT(num_ops_prior == 0);
7457
Paul Elliott91007972022-12-16 12:21:24 +00007458 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007459 do {
Paul Elliott91007972022-12-16 12:21:24 +00007460 actual_status = psa_verify_hash_complete(&operation);
7461
Paul Elliott0c683352022-12-16 19:16:56 +00007462 num_completes++;
7463
Paul Elliott334d7262023-01-20 17:29:41 +00007464 if (actual_status == PSA_SUCCESS ||
7465 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007466 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007467 /* We are asserting here that every complete makes progress
7468 * (completes some ops), which is true of the internal
7469 * implementation and probably any implementation, however this is
7470 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007471 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007472
Paul Elliott91007972022-12-16 12:21:24 +00007473 num_ops_prior = num_ops;
7474 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007475 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007476
Paul Elliottc9774412023-02-06 15:14:07 +00007477 TEST_EQUAL(actual_status, expected_complete_status);
7478
Paul Elliottefebad02023-02-15 16:56:45 +00007479 /* Check that another complete returns BAD_STATE. */
7480 actual_status = psa_verify_hash_complete(&operation);
7481 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007482
Paul Elliott0c683352022-12-16 19:16:56 +00007483 TEST_LE_U(min_completes, num_completes);
7484 TEST_LE_U(num_completes, max_completes);
7485
Paul Elliott91007972022-12-16 12:21:24 +00007486 PSA_ASSERT(psa_verify_hash_abort(&operation));
7487
Paul Elliott59ad9452022-12-18 15:09:02 +00007488 num_ops = psa_verify_hash_get_num_ops(&operation);
7489 TEST_ASSERT(num_ops == 0);
7490
Paul Elliott91007972022-12-16 12:21:24 +00007491exit:
7492 psa_reset_key_attributes(&attributes);
7493 psa_destroy_key(key);
7494 PSA_DONE();
7495}
7496/* END_CASE */
7497
Paul Elliott20a36062022-12-18 13:21:25 +00007498/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007499/**
7500 * interruptible_signverify_hash_state_test() test intentions:
7501 *
7502 * Note: This test can currently only handle ECDSA.
7503 *
7504 * 1. Test that calling the various interruptible sign and verify hash functions
7505 * in incorrect orders returns BAD_STATE errors.
7506 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007507void interruptible_signverify_hash_state_test(int key_type_arg,
7508 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007509{
7510 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7511 psa_key_type_t key_type = key_type_arg;
7512 psa_algorithm_t alg = alg_arg;
7513 size_t key_bits;
7514 unsigned char *signature = NULL;
7515 size_t signature_size;
7516 size_t signature_length = 0xdeadbeef;
7517 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7518 psa_sign_hash_interruptible_operation_t sign_operation =
7519 psa_sign_hash_interruptible_operation_init();
7520 psa_verify_hash_interruptible_operation_t verify_operation =
7521 psa_verify_hash_interruptible_operation_init();
7522
7523 PSA_ASSERT(psa_crypto_init());
7524
7525 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7526 PSA_KEY_USAGE_VERIFY_HASH);
7527 psa_set_key_algorithm(&attributes, alg);
7528 psa_set_key_type(&attributes, key_type);
7529
7530 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7531 &key));
7532 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7533 key_bits = psa_get_key_bits(&attributes);
7534
7535 /* Allocate a buffer which has the size advertised by the
7536 * library. */
7537 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7538 key_bits, alg);
7539 TEST_ASSERT(signature_size != 0);
7540 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007541 TEST_CALLOC(signature, signature_size);
Paul Elliott20a36062022-12-18 13:21:25 +00007542
7543 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7544
7545 /* --- Attempt completes prior to starts --- */
7546 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7547 signature_size,
7548 &signature_length),
7549 PSA_ERROR_BAD_STATE);
7550
7551 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7552
7553 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7554 PSA_ERROR_BAD_STATE);
7555
7556 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7557
7558 /* --- Aborts in all other places. --- */
7559 psa_sign_hash_abort(&sign_operation);
7560
7561 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7562 input_data->x, input_data->len));
7563
7564 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7565
7566 psa_interruptible_set_max_ops(1);
7567
7568 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7569 input_data->x, input_data->len));
7570
7571 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7572 signature_size,
7573 &signature_length),
7574 PSA_OPERATION_INCOMPLETE);
7575
7576 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7577
7578 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7579
7580 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7581 input_data->x, input_data->len));
7582
7583 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7584 signature_size,
7585 &signature_length));
7586
7587 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7588
7589 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7590
7591 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7592 input_data->x, input_data->len,
7593 signature, signature_length));
7594
7595 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7596
7597 psa_interruptible_set_max_ops(1);
7598
7599 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7600 input_data->x, input_data->len,
7601 signature, signature_length));
7602
7603 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7604 PSA_OPERATION_INCOMPLETE);
7605
7606 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7607
7608 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7609
7610 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7611 input_data->x, input_data->len,
7612 signature, signature_length));
7613
7614 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7615
7616 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7617
7618 /* --- Attempt double starts. --- */
7619
7620 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7621 input_data->x, input_data->len));
7622
7623 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7624 input_data->x, input_data->len),
7625 PSA_ERROR_BAD_STATE);
7626
7627 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7628
7629 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7630 input_data->x, input_data->len,
7631 signature, signature_length));
7632
7633 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7634 input_data->x, input_data->len,
7635 signature, signature_length),
7636 PSA_ERROR_BAD_STATE);
7637
7638 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7639
Paul Elliott76d671a2023-02-07 17:45:18 +00007640exit:
7641 /*
7642 * Key attributes may have been returned by psa_get_key_attributes()
7643 * thus reset them as required.
7644 */
7645 psa_reset_key_attributes(&attributes);
7646
7647 psa_destroy_key(key);
7648 mbedtls_free(signature);
7649 PSA_DONE();
7650}
7651/* END_CASE */
7652
7653/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007654/**
Paul Elliottc2033502023-02-26 17:09:14 +00007655 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007656 *
7657 * Note: This test can currently only handle ECDSA.
7658 *
7659 * 1. Test various edge cases in the interruptible sign and verify hash
7660 * interfaces.
7661 */
Paul Elliottc2033502023-02-26 17:09:14 +00007662void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007663 data_t *key_data, int alg_arg, data_t *input_data)
7664{
7665 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7666 psa_key_type_t key_type = key_type_arg;
7667 psa_algorithm_t alg = alg_arg;
7668 size_t key_bits;
7669 unsigned char *signature = NULL;
7670 size_t signature_size;
7671 size_t signature_length = 0xdeadbeef;
7672 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7673 uint8_t *input_buffer = NULL;
7674 psa_sign_hash_interruptible_operation_t sign_operation =
7675 psa_sign_hash_interruptible_operation_init();
7676 psa_verify_hash_interruptible_operation_t verify_operation =
7677 psa_verify_hash_interruptible_operation_init();
7678
7679 PSA_ASSERT(psa_crypto_init());
7680
7681 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7682 PSA_KEY_USAGE_VERIFY_HASH);
7683 psa_set_key_algorithm(&attributes, alg);
7684 psa_set_key_type(&attributes, key_type);
7685
7686 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7687 &key));
7688 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7689 key_bits = psa_get_key_bits(&attributes);
7690
7691 /* Allocate a buffer which has the size advertised by the
7692 * library. */
7693 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7694 key_bits, alg);
7695 TEST_ASSERT(signature_size != 0);
7696 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007697 TEST_CALLOC(signature, signature_size);
Paul Elliott76d671a2023-02-07 17:45:18 +00007698
Paul Elliott20a36062022-12-18 13:21:25 +00007699 /* --- Change function inputs mid run, to cause an error (sign only,
7700 * verify passes all inputs to start. --- */
7701
7702 psa_interruptible_set_max_ops(1);
7703
7704 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7705 input_data->x, input_data->len));
7706
7707 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7708 signature_size,
7709 &signature_length),
7710 PSA_OPERATION_INCOMPLETE);
7711
7712 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7713 0,
7714 &signature_length),
7715 PSA_ERROR_BUFFER_TOO_SMALL);
7716
Paul Elliottc9774412023-02-06 15:14:07 +00007717 /* And test that this invalidates the operation. */
7718 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7719 0,
7720 &signature_length),
7721 PSA_ERROR_BAD_STATE);
7722
Paul Elliott20a36062022-12-18 13:21:25 +00007723 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7724
Paul Elliottf9c91a72023-02-05 18:06:38 +00007725 /* Trash the hash buffer in between start and complete, to ensure
7726 * no reliance on external buffers. */
7727 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7728
Paul Elliott6c68df42023-10-23 15:33:37 +01007729 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007730
7731 memcpy(input_buffer, input_data->x, input_data->len);
7732
7733 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7734 input_buffer, input_data->len));
7735
7736 memset(input_buffer, '!', input_data->len);
7737 mbedtls_free(input_buffer);
7738 input_buffer = NULL;
7739
7740 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7741 signature_size,
7742 &signature_length));
7743
7744 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7745
Paul Elliott6c68df42023-10-23 15:33:37 +01007746 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007747
7748 memcpy(input_buffer, input_data->x, input_data->len);
7749
7750 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7751 input_buffer, input_data->len,
7752 signature, signature_length));
7753
7754 memset(input_buffer, '!', input_data->len);
7755 mbedtls_free(input_buffer);
7756 input_buffer = NULL;
7757
7758 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7759
7760 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7761
Paul Elliott20a36062022-12-18 13:21:25 +00007762exit:
7763 /*
7764 * Key attributes may have been returned by psa_get_key_attributes()
7765 * thus reset them as required.
7766 */
7767 psa_reset_key_attributes(&attributes);
7768
7769 psa_destroy_key(key);
7770 mbedtls_free(signature);
Paul Elliott6c68df42023-10-23 15:33:37 +01007771 mbedtls_free(input_buffer);
Paul Elliott20a36062022-12-18 13:21:25 +00007772 PSA_DONE();
7773}
7774/* END_CASE */
7775
Paul Elliotta4cb9092023-02-07 18:01:55 +00007776/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007777/**
Paul Elliott57702242023-02-26 20:36:10 +00007778 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007779 *
7780 * Note: This test can currently only handle ECDSA.
7781 *
7782 * 1. Test that setting max ops is reflected in both interruptible sign and
7783 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007784 * 2. Test that changing the value of max_ops to unlimited during an operation
7785 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007786 *
7787 * 3. Test that calling get_num_ops() between complete calls gives the same
7788 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007789 */
Paul Elliott57702242023-02-26 20:36:10 +00007790void interruptible_signverify_hash_ops_tests(int key_type_arg,
7791 data_t *key_data, int alg_arg,
7792 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007793{
7794 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7795 psa_key_type_t key_type = key_type_arg;
7796 psa_algorithm_t alg = alg_arg;
7797 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007798 size_t key_bits;
7799 unsigned char *signature = NULL;
7800 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007801 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007802 uint32_t num_ops = 0;
7803 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7804
Paul Elliotta4cb9092023-02-07 18:01:55 +00007805 psa_sign_hash_interruptible_operation_t sign_operation =
7806 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007807 psa_verify_hash_interruptible_operation_t verify_operation =
7808 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007809
7810 PSA_ASSERT(psa_crypto_init());
7811
7812 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7813 PSA_KEY_USAGE_VERIFY_HASH);
7814 psa_set_key_algorithm(&attributes, alg);
7815 psa_set_key_type(&attributes, key_type);
7816
Paul Elliottf1743e22023-02-15 18:44:16 +00007817 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7818 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7819 key_bits = psa_get_key_bits(&attributes);
7820
7821 /* Allocate a buffer which has the size advertised by the
7822 * library. */
7823 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7824
7825 TEST_ASSERT(signature_size != 0);
7826 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007827 TEST_CALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007828
7829 /* Check that default max ops gets set if we don't set it. */
7830 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7831 input_data->x, input_data->len));
7832
7833 TEST_EQUAL(psa_interruptible_get_max_ops(),
7834 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7835
7836 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7837
Paul Elliottf1743e22023-02-15 18:44:16 +00007838 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7839 input_data->x, input_data->len,
7840 signature, signature_size));
7841
7842 TEST_EQUAL(psa_interruptible_get_max_ops(),
7843 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7844
7845 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7846
Paul Elliotta4cb9092023-02-07 18:01:55 +00007847 /* Check that max ops gets set properly. */
7848
7849 psa_interruptible_set_max_ops(0xbeef);
7850
Paul Elliottf1743e22023-02-15 18:44:16 +00007851 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007852
Paul Elliott9e8819f2023-02-26 19:01:35 +00007853 /* --- Ensure changing the max ops mid operation works (operation should
7854 * complete successfully after setting max ops to unlimited --- */
7855 psa_interruptible_set_max_ops(1);
7856
7857 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7858 input_data->x, input_data->len));
7859
7860 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7861 signature_size,
7862 &signature_length),
7863 PSA_OPERATION_INCOMPLETE);
7864
7865 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7866
7867 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7868 signature_size,
7869 &signature_length));
7870
7871 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7872
7873 psa_interruptible_set_max_ops(1);
7874
7875 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7876 input_data->x, input_data->len,
7877 signature, signature_length));
7878
7879 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7880 PSA_OPERATION_INCOMPLETE);
7881
7882 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7883
7884 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7885
7886 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7887
Paul Elliottc1e04002023-02-26 20:27:23 +00007888 /* --- Test that not calling get_num_ops inbetween complete calls does not
7889 * result in lost ops. ---*/
7890
7891 psa_interruptible_set_max_ops(1);
7892
7893 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7894 input_data->x, input_data->len));
7895
7896 /* Continue performing the signature until complete. */
7897 do {
7898 status = psa_sign_hash_complete(&sign_operation, signature,
7899 signature_size,
7900 &signature_length);
7901
7902 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7903
7904 } while (status == PSA_OPERATION_INCOMPLETE);
7905
7906 PSA_ASSERT(status);
7907
7908 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7909
7910 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7911 input_data->x, input_data->len));
7912
7913 /* Continue performing the signature until complete. */
7914 do {
7915 status = psa_sign_hash_complete(&sign_operation, signature,
7916 signature_size,
7917 &signature_length);
7918 } while (status == PSA_OPERATION_INCOMPLETE);
7919
7920 PSA_ASSERT(status);
7921
7922 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7923
7924 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7925
7926 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7927 input_data->x, input_data->len,
7928 signature, signature_length));
7929
7930 /* Continue performing the verification until complete. */
7931 do {
7932 status = psa_verify_hash_complete(&verify_operation);
7933
7934 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7935
7936 } while (status == PSA_OPERATION_INCOMPLETE);
7937
7938 PSA_ASSERT(status);
7939
7940 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7941
7942 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7943 input_data->x, input_data->len,
7944 signature, signature_length));
7945
7946 /* Continue performing the verification until complete. */
7947 do {
7948 status = psa_verify_hash_complete(&verify_operation);
7949
7950 } while (status == PSA_OPERATION_INCOMPLETE);
7951
7952 PSA_ASSERT(status);
7953
7954 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7955
7956 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7957
Paul Elliotta4cb9092023-02-07 18:01:55 +00007958exit:
7959 /*
7960 * Key attributes may have been returned by psa_get_key_attributes()
7961 * thus reset them as required.
7962 */
7963 psa_reset_key_attributes(&attributes);
7964
7965 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007966 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007967 PSA_DONE();
7968}
7969/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007970
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007971/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007972void sign_message_deterministic(int key_type_arg,
7973 data_t *key_data,
7974 int alg_arg,
7975 data_t *input_data,
7976 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007977{
7978 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7979 psa_key_type_t key_type = key_type_arg;
7980 psa_algorithm_t alg = alg_arg;
7981 size_t key_bits;
7982 unsigned char *signature = NULL;
7983 size_t signature_size;
7984 size_t signature_length = 0xdeadbeef;
7985 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7986
Gilles Peskine449bd832023-01-11 14:50:10 +01007987 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007988
Gilles Peskine449bd832023-01-11 14:50:10 +01007989 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7990 psa_set_key_algorithm(&attributes, alg);
7991 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007992
Gilles Peskine449bd832023-01-11 14:50:10 +01007993 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7994 &key));
7995 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7996 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007997
Gilles Peskine449bd832023-01-11 14:50:10 +01007998 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7999 TEST_ASSERT(signature_size != 0);
8000 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008001 TEST_CALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008002
Gilles Peskine449bd832023-01-11 14:50:10 +01008003 PSA_ASSERT(psa_sign_message(key, alg,
8004 input_data->x, input_data->len,
8005 signature, signature_size,
8006 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02008007
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008008 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008009 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008010
8011exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008012 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02008013
Gilles Peskine449bd832023-01-11 14:50:10 +01008014 psa_destroy_key(key);
8015 mbedtls_free(signature);
8016 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02008017
8018}
8019/* END_CASE */
8020
8021/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008022void sign_message_fail(int key_type_arg,
8023 data_t *key_data,
8024 int alg_arg,
8025 data_t *input_data,
8026 int signature_size_arg,
8027 int expected_status_arg)
8028{
8029 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8030 psa_key_type_t key_type = key_type_arg;
8031 psa_algorithm_t alg = alg_arg;
8032 size_t signature_size = signature_size_arg;
8033 psa_status_t actual_status;
8034 psa_status_t expected_status = expected_status_arg;
8035 unsigned char *signature = NULL;
8036 size_t signature_length = 0xdeadbeef;
8037 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8038
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008039 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008040
8041 PSA_ASSERT(psa_crypto_init());
8042
8043 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8044 psa_set_key_algorithm(&attributes, alg);
8045 psa_set_key_type(&attributes, key_type);
8046
8047 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8048 &key));
8049
8050 actual_status = psa_sign_message(key, alg,
8051 input_data->x, input_data->len,
8052 signature, signature_size,
8053 &signature_length);
8054 TEST_EQUAL(actual_status, expected_status);
8055 /* The value of *signature_length is unspecified on error, but
8056 * whatever it is, it should be less than signature_size, so that
8057 * if the caller tries to read *signature_length bytes without
8058 * checking the error code then they don't overflow a buffer. */
8059 TEST_LE_U(signature_length, signature_size);
8060
8061exit:
8062 psa_reset_key_attributes(&attributes);
8063 psa_destroy_key(key);
8064 mbedtls_free(signature);
8065 PSA_DONE();
8066}
8067/* END_CASE */
8068
8069/* BEGIN_CASE */
8070void sign_verify_message(int key_type_arg,
8071 data_t *key_data,
8072 int alg_arg,
8073 data_t *input_data)
8074{
8075 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8076 psa_key_type_t key_type = key_type_arg;
8077 psa_algorithm_t alg = alg_arg;
8078 size_t key_bits;
8079 unsigned char *signature = NULL;
8080 size_t signature_size;
8081 size_t signature_length = 0xdeadbeef;
8082 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8083
8084 PSA_ASSERT(psa_crypto_init());
8085
8086 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
8087 PSA_KEY_USAGE_VERIFY_MESSAGE);
8088 psa_set_key_algorithm(&attributes, alg);
8089 psa_set_key_type(&attributes, key_type);
8090
8091 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8092 &key));
8093 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8094 key_bits = psa_get_key_bits(&attributes);
8095
8096 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8097 TEST_ASSERT(signature_size != 0);
8098 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008099 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008100
8101 PSA_ASSERT(psa_sign_message(key, alg,
8102 input_data->x, input_data->len,
8103 signature, signature_size,
8104 &signature_length));
8105 TEST_LE_U(signature_length, signature_size);
8106 TEST_ASSERT(signature_length > 0);
8107
8108 PSA_ASSERT(psa_verify_message(key, alg,
8109 input_data->x, input_data->len,
8110 signature, signature_length));
8111
8112 if (input_data->len != 0) {
8113 /* Flip a bit in the input and verify that the signature is now
8114 * detected as invalid. Flip a bit at the beginning, not at the end,
8115 * because ECDSA may ignore the last few bits of the input. */
8116 input_data->x[0] ^= 1;
8117 TEST_EQUAL(psa_verify_message(key, alg,
8118 input_data->x, input_data->len,
8119 signature, signature_length),
8120 PSA_ERROR_INVALID_SIGNATURE);
8121 }
8122
8123exit:
8124 psa_reset_key_attributes(&attributes);
8125
8126 psa_destroy_key(key);
8127 mbedtls_free(signature);
8128 PSA_DONE();
8129}
8130/* END_CASE */
8131
8132/* BEGIN_CASE */
8133void verify_message(int key_type_arg,
8134 data_t *key_data,
8135 int alg_arg,
8136 data_t *input_data,
8137 data_t *signature_data)
8138{
8139 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8140 psa_key_type_t key_type = key_type_arg;
8141 psa_algorithm_t alg = alg_arg;
8142 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8143
8144 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8145
8146 PSA_ASSERT(psa_crypto_init());
8147
8148 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8149 psa_set_key_algorithm(&attributes, alg);
8150 psa_set_key_type(&attributes, key_type);
8151
8152 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8153 &key));
8154
8155 PSA_ASSERT(psa_verify_message(key, alg,
8156 input_data->x, input_data->len,
8157 signature_data->x, signature_data->len));
8158
8159exit:
8160 psa_reset_key_attributes(&attributes);
8161 psa_destroy_key(key);
8162 PSA_DONE();
8163}
8164/* END_CASE */
8165
8166/* BEGIN_CASE */
8167void verify_message_fail(int key_type_arg,
8168 data_t *key_data,
8169 int alg_arg,
8170 data_t *hash_data,
8171 data_t *signature_data,
8172 int expected_status_arg)
8173{
8174 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8175 psa_key_type_t key_type = key_type_arg;
8176 psa_algorithm_t alg = alg_arg;
8177 psa_status_t actual_status;
8178 psa_status_t expected_status = expected_status_arg;
8179 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8180
8181 PSA_ASSERT(psa_crypto_init());
8182
8183 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8184 psa_set_key_algorithm(&attributes, alg);
8185 psa_set_key_type(&attributes, key_type);
8186
8187 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8188 &key));
8189
8190 actual_status = psa_verify_message(key, alg,
8191 hash_data->x, hash_data->len,
8192 signature_data->x,
8193 signature_data->len);
8194 TEST_EQUAL(actual_status, expected_status);
8195
8196exit:
8197 psa_reset_key_attributes(&attributes);
8198 psa_destroy_key(key);
8199 PSA_DONE();
8200}
8201/* END_CASE */
8202
8203/* BEGIN_CASE */
8204void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008205 data_t *key_data,
8206 int alg_arg,
8207 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008208 data_t *label,
8209 int expected_output_length_arg,
8210 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008211{
Ronald Cron5425a212020-08-04 14:58:35 +02008212 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008213 psa_key_type_t key_type = key_type_arg;
8214 psa_algorithm_t alg = alg_arg;
8215 size_t expected_output_length = expected_output_length_arg;
8216 size_t key_bits;
8217 unsigned char *output = NULL;
8218 size_t output_size;
8219 size_t output_length = ~0;
8220 psa_status_t actual_status;
8221 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008222 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008223
Gilles Peskine449bd832023-01-11 14:50:10 +01008224 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008225
Gilles Peskine656896e2018-06-29 19:12:28 +02008226 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008227 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8228 psa_set_key_algorithm(&attributes, alg);
8229 psa_set_key_type(&attributes, key_type);
8230 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8231 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008232
8233 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008234 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8235 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008236
Gilles Peskine449bd832023-01-11 14:50:10 +01008237 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8238 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008239 TEST_CALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008240
8241 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008242 actual_status = psa_asymmetric_encrypt(key, alg,
8243 input_data->x, input_data->len,
8244 label->x, label->len,
8245 output, output_size,
8246 &output_length);
8247 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008248 if (actual_status == PSA_SUCCESS) {
8249 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008250 } else {
8251 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008252 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008253
Gilles Peskine68428122018-06-30 18:42:41 +02008254 /* If the label is empty, the test framework puts a non-null pointer
8255 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008256 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008257 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008258 if (output_size != 0) {
8259 memset(output, 0, output_size);
8260 }
8261 actual_status = psa_asymmetric_encrypt(key, alg,
8262 input_data->x, input_data->len,
8263 NULL, label->len,
8264 output, output_size,
8265 &output_length);
8266 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008267 if (actual_status == PSA_SUCCESS) {
8268 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008269 } else {
8270 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008271 }
Gilles Peskine68428122018-06-30 18:42:41 +02008272 }
8273
Gilles Peskine656896e2018-06-29 19:12:28 +02008274exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008275 /*
8276 * Key attributes may have been returned by psa_get_key_attributes()
8277 * thus reset them as required.
8278 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008279 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008280
Gilles Peskine449bd832023-01-11 14:50:10 +01008281 psa_destroy_key(key);
8282 mbedtls_free(output);
8283 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008284}
8285/* END_CASE */
8286
8287/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008288void asymmetric_encrypt_decrypt(int key_type_arg,
8289 data_t *key_data,
8290 int alg_arg,
8291 data_t *input_data,
8292 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008293{
Ronald Cron5425a212020-08-04 14:58:35 +02008294 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008295 psa_key_type_t key_type = key_type_arg;
8296 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008297 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008298 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008299 size_t output_size;
8300 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008301 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008302 size_t output2_size;
8303 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008304 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008305
Gilles Peskine449bd832023-01-11 14:50:10 +01008306 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008307
Gilles Peskine449bd832023-01-11 14:50:10 +01008308 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8309 psa_set_key_algorithm(&attributes, alg);
8310 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008311
Gilles Peskine449bd832023-01-11 14:50:10 +01008312 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8313 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008314
8315 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008316 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8317 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008318
Gilles Peskine449bd832023-01-11 14:50:10 +01008319 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8320 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008321 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008322
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008323 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008324 TEST_LE_U(output2_size,
8325 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8326 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008327 TEST_CALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008328
Gilles Peskineeebd7382018-06-08 18:11:54 +02008329 /* We test encryption by checking that encrypt-then-decrypt gives back
8330 * the original plaintext because of the non-optional random
8331 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008332 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8333 input_data->x, input_data->len,
8334 label->x, label->len,
8335 output, output_size,
8336 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008337 /* We don't know what ciphertext length to expect, but check that
8338 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008339 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008340
Gilles Peskine449bd832023-01-11 14:50:10 +01008341 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8342 output, output_length,
8343 label->x, label->len,
8344 output2, output2_size,
8345 &output2_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008346 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008347 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008348
8349exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008350 /*
8351 * Key attributes may have been returned by psa_get_key_attributes()
8352 * thus reset them as required.
8353 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008354 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008355
Gilles Peskine449bd832023-01-11 14:50:10 +01008356 psa_destroy_key(key);
8357 mbedtls_free(output);
8358 mbedtls_free(output2);
8359 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008360}
8361/* END_CASE */
8362
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008363/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008364void asymmetric_decrypt(int key_type_arg,
8365 data_t *key_data,
8366 int alg_arg,
8367 data_t *input_data,
8368 data_t *label,
8369 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008370{
Ronald Cron5425a212020-08-04 14:58:35 +02008371 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008372 psa_key_type_t key_type = key_type_arg;
8373 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008374 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008375 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008376 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008377 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008378 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008379
Gilles Peskine449bd832023-01-11 14:50:10 +01008380 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008381
Gilles Peskine449bd832023-01-11 14:50:10 +01008382 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8383 psa_set_key_algorithm(&attributes, alg);
8384 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008385
Gilles Peskine449bd832023-01-11 14:50:10 +01008386 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8387 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008388
Gilles Peskine449bd832023-01-11 14:50:10 +01008389 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8390 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008391
8392 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008393 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8394 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008395 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008396
Gilles Peskine449bd832023-01-11 14:50:10 +01008397 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8398 input_data->x, input_data->len,
8399 label->x, label->len,
8400 output,
8401 output_size,
8402 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008403 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008404 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008405
Gilles Peskine68428122018-06-30 18:42:41 +02008406 /* If the label is empty, the test framework puts a non-null pointer
8407 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008408 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008409 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008410 if (output_size != 0) {
8411 memset(output, 0, output_size);
8412 }
8413 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8414 input_data->x, input_data->len,
8415 NULL, label->len,
8416 output,
8417 output_size,
8418 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008419 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008420 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008421 }
8422
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008423exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008424 psa_reset_key_attributes(&attributes);
8425 psa_destroy_key(key);
8426 mbedtls_free(output);
8427 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008428}
8429/* END_CASE */
8430
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008431/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008432void asymmetric_decrypt_fail(int key_type_arg,
8433 data_t *key_data,
8434 int alg_arg,
8435 data_t *input_data,
8436 data_t *label,
8437 int output_size_arg,
8438 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008439{
Ronald Cron5425a212020-08-04 14:58:35 +02008440 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008441 psa_key_type_t key_type = key_type_arg;
8442 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008443 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008444 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008445 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008446 psa_status_t actual_status;
8447 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008448 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008449
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008450 TEST_CALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008451
Gilles Peskine449bd832023-01-11 14:50:10 +01008452 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008453
Gilles Peskine449bd832023-01-11 14:50:10 +01008454 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8455 psa_set_key_algorithm(&attributes, alg);
8456 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008457
Gilles Peskine449bd832023-01-11 14:50:10 +01008458 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8459 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008460
Gilles Peskine449bd832023-01-11 14:50:10 +01008461 actual_status = psa_asymmetric_decrypt(key, alg,
8462 input_data->x, input_data->len,
8463 label->x, label->len,
8464 output, output_size,
8465 &output_length);
8466 TEST_EQUAL(actual_status, expected_status);
8467 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008468
Gilles Peskine68428122018-06-30 18:42:41 +02008469 /* If the label is empty, the test framework puts a non-null pointer
8470 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008471 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008472 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008473 if (output_size != 0) {
8474 memset(output, 0, output_size);
8475 }
8476 actual_status = psa_asymmetric_decrypt(key, alg,
8477 input_data->x, input_data->len,
8478 NULL, label->len,
8479 output, output_size,
8480 &output_length);
8481 TEST_EQUAL(actual_status, expected_status);
8482 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008483 }
8484
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008485exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008486 psa_reset_key_attributes(&attributes);
8487 psa_destroy_key(key);
8488 mbedtls_free(output);
8489 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008490}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008491/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008492
8493/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008494void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008495{
8496 /* Test each valid way of initializing the object, except for `= {0}`, as
8497 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8498 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008499 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008500 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008501 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008502 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8503 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008504
Gilles Peskine449bd832023-01-11 14:50:10 +01008505 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008506
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008507 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008508 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8509 PSA_ERROR_BAD_STATE);
8510 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8511 PSA_ERROR_BAD_STATE);
8512 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8513 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008514
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008515 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008516 PSA_ASSERT(psa_key_derivation_abort(&func));
8517 PSA_ASSERT(psa_key_derivation_abort(&init));
8518 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008519}
8520/* END_CASE */
8521
Janos Follath16de4a42019-06-13 16:32:24 +01008522/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008523void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008524{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008525 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008526 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008527 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008528
Gilles Peskine449bd832023-01-11 14:50:10 +01008529 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008530
Gilles Peskine449bd832023-01-11 14:50:10 +01008531 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8532 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008533
8534exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008535 psa_key_derivation_abort(&operation);
8536 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008537}
8538/* END_CASE */
8539
Janos Follathaf3c2a02019-06-12 12:34:34 +01008540/* BEGIN_CASE */
Kusumit Ghoderao9ffd3972023-12-01 16:40:13 +05308541void derive_set_capacity(int alg_arg, int64_t capacity_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01008542 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008543{
8544 psa_algorithm_t alg = alg_arg;
8545 size_t capacity = capacity_arg;
8546 psa_status_t expected_status = expected_status_arg;
8547 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8548
Gilles Peskine449bd832023-01-11 14:50:10 +01008549 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008550
Gilles Peskine449bd832023-01-11 14:50:10 +01008551 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008552
Gilles Peskine449bd832023-01-11 14:50:10 +01008553 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8554 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008555
8556exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008557 psa_key_derivation_abort(&operation);
8558 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008559}
8560/* END_CASE */
8561
8562/* BEGIN_CASE */
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308563void parse_binary_string_test(data_t *input, int output)
8564{
8565 uint64_t value;
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308566 value = mbedtls_test_parse_binary_string(input);
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308567 TEST_EQUAL(value, output);
8568}
8569/* END_CASE */
8570
8571/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008572void derive_input(int alg_arg,
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308573 int step_arg1, int key_type_arg1, data_t *input1,
8574 int expected_status_arg1,
8575 int step_arg2, int key_type_arg2, data_t *input2,
8576 int expected_status_arg2,
8577 int step_arg3, int key_type_arg3, data_t *input3,
8578 int expected_status_arg3,
Gilles Peskine449bd832023-01-11 14:50:10 +01008579 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008580{
8581 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008582 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308583 uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
Gilles Peskine449bd832023-01-11 14:50:10 +01008584 psa_status_t expected_statuses[] = { expected_status_arg1,
8585 expected_status_arg2,
8586 expected_status_arg3 };
8587 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008588 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8589 MBEDTLS_SVC_KEY_ID_INIT,
8590 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008591 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8592 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8593 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008594 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008595 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008596 psa_status_t expected_output_status = expected_output_status_arg;
8597 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008598
Gilles Peskine449bd832023-01-11 14:50:10 +01008599 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008600
Gilles Peskine449bd832023-01-11 14:50:10 +01008601 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8602 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008603
Gilles Peskine449bd832023-01-11 14:50:10 +01008604 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008605
Gilles Peskine449bd832023-01-11 14:50:10 +01008606 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8607 mbedtls_test_set_step(i);
8608 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008609 /* Skip this step */
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308610 } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE &&
8611 key_types[i] != INPUT_INTEGER) {
8612 psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i]));
Gilles Peskine449bd832023-01-11 14:50:10 +01008613 PSA_ASSERT(psa_import_key(&attributes,
8614 inputs[i]->x, inputs[i]->len,
8615 &keys[i]));
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308616 if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008617 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008618 // When taking a private key as secret input, use key agreement
8619 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008620 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8621 &operation, keys[i]),
8622 expected_statuses[i]);
8623 } else {
8624 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8625 keys[i]),
8626 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008627 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008628 } else {
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308629 if (key_types[i] == INPUT_INTEGER) {
8630 TEST_EQUAL(psa_key_derivation_input_integer(
8631 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308632 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308633 expected_statuses[i]);
8634 } else {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308635 TEST_EQUAL(psa_key_derivation_input_bytes(
8636 &operation, steps[i],
8637 inputs[i]->x, inputs[i]->len),
8638 expected_statuses[i]);
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308639 }
Janos Follathaf3c2a02019-06-12 12:34:34 +01008640 }
8641 }
8642
Gilles Peskine449bd832023-01-11 14:50:10 +01008643 if (output_key_type != PSA_KEY_TYPE_NONE) {
8644 psa_reset_key_attributes(&attributes);
8645 psa_set_key_type(&attributes, output_key_type);
8646 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008647 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008648 psa_key_derivation_output_key(&attributes, &operation,
8649 &output_key);
8650 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008651 uint8_t buffer[1];
8652 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008653 psa_key_derivation_output_bytes(&operation,
8654 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008655 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008656 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008657
Janos Follathaf3c2a02019-06-12 12:34:34 +01008658exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008659 psa_key_derivation_abort(&operation);
8660 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8661 psa_destroy_key(keys[i]);
8662 }
8663 psa_destroy_key(output_key);
8664 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008665}
8666/* END_CASE */
8667
Kusumit Ghoderao42b02b92023-06-06 16:48:46 +05308668/* BEGIN_CASE*/
8669void derive_input_invalid_cost(int alg_arg, int64_t cost)
8670{
8671 psa_algorithm_t alg = alg_arg;
8672 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8673
8674 PSA_ASSERT(psa_crypto_init());
8675 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8676
8677 TEST_EQUAL(psa_key_derivation_input_integer(&operation,
8678 PSA_KEY_DERIVATION_INPUT_COST,
8679 cost),
8680 PSA_ERROR_NOT_SUPPORTED);
8681
8682exit:
8683 psa_key_derivation_abort(&operation);
8684 PSA_DONE();
8685}
8686/* END_CASE*/
8687
Janos Follathd958bb72019-07-03 15:02:16 +01008688/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008689void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008690{
Janos Follathd958bb72019-07-03 15:02:16 +01008691 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008692 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008693 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008694 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008695 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008696 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008697 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008698 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008699 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008700 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008701 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8702 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008703 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008704 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008705
Gilles Peskine449bd832023-01-11 14:50:10 +01008706 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008707
Gilles Peskine449bd832023-01-11 14:50:10 +01008708 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8709 psa_set_key_algorithm(&attributes, alg);
8710 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008711
Gilles Peskine449bd832023-01-11 14:50:10 +01008712 PSA_ASSERT(psa_import_key(&attributes,
8713 key_data, sizeof(key_data),
8714 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008715
8716 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008717 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8718 input1, input1_length,
8719 input2, input2_length,
8720 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008721 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008722 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008723
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008724 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008725 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8726 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008727
Gilles Peskine449bd832023-01-11 14:50:10 +01008728 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008729
Gilles Peskine449bd832023-01-11 14:50:10 +01008730 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8731 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008732
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008733exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008734 psa_key_derivation_abort(&operation);
8735 psa_destroy_key(key);
8736 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008737}
8738/* END_CASE */
8739
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008740/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008741void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008742{
8743 uint8_t output_buffer[16];
8744 size_t buffer_size = 16;
8745 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008746 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008747
Gilles Peskine449bd832023-01-11 14:50:10 +01008748 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8749 output_buffer, buffer_size)
8750 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008751
Gilles Peskine449bd832023-01-11 14:50:10 +01008752 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8753 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008754
Gilles Peskine449bd832023-01-11 14:50:10 +01008755 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008756
Gilles Peskine449bd832023-01-11 14:50:10 +01008757 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8758 output_buffer, buffer_size)
8759 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008760
Gilles Peskine449bd832023-01-11 14:50:10 +01008761 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8762 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008763
8764exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008765 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008766}
8767/* END_CASE */
8768
8769/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008770void derive_output(int alg_arg,
8771 int step1_arg, data_t *input1, int expected_status_arg1,
8772 int step2_arg, data_t *input2, int expected_status_arg2,
8773 int step3_arg, data_t *input3, int expected_status_arg3,
8774 int step4_arg, data_t *input4, int expected_status_arg4,
8775 data_t *key_agreement_peer_key,
8776 int requested_capacity_arg,
8777 data_t *expected_output1,
8778 data_t *expected_output2,
8779 int other_key_input_type,
8780 int key_input_type,
8781 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008782{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008783 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008784 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8785 data_t *inputs[] = { input1, input2, input3, input4 };
8786 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8787 MBEDTLS_SVC_KEY_ID_INIT,
8788 MBEDTLS_SVC_KEY_ID_INIT,
8789 MBEDTLS_SVC_KEY_ID_INIT };
8790 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8791 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008792 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008793 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008794 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008795 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008796 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008797 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008798 size_t output_buffer_size = 0;
8799 uint8_t *output_buffer = NULL;
8800 size_t expected_capacity;
8801 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008802 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8803 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8804 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8805 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008806 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008807 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008808 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008809
Gilles Peskine449bd832023-01-11 14:50:10 +01008810 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8811 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008812 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008813 }
8814 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008815 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008817 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008818 TEST_CALLOC(output_buffer, output_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008819 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008820
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008821 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008822 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8823 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8824 requested_capacity));
8825 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8826 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008827 case 0:
8828 break;
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308829 case PSA_KEY_DERIVATION_INPUT_COST:
8830 TEST_EQUAL(psa_key_derivation_input_integer(
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308831 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308832 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308833 statuses[i]);
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308834 if (statuses[i] != PSA_SUCCESS) {
8835 goto exit;
8836 }
8837 break;
8838 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Gilles Peskine1468da72019-05-29 17:35:49 +02008839 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008840 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008841 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008842 TEST_EQUAL(psa_key_derivation_input_bytes(
8843 &operation, steps[i],
8844 inputs[i]->x, inputs[i]->len),
8845 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008846
Gilles Peskine449bd832023-01-11 14:50:10 +01008847 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008848 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008849 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008850 break;
8851 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008852 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8853 psa_set_key_algorithm(&attributes1, alg);
8854 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008855
Gilles Peskine449bd832023-01-11 14:50:10 +01008856 PSA_ASSERT(psa_import_key(&attributes1,
8857 inputs[i]->x, inputs[i]->len,
8858 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008859
Gilles Peskine449bd832023-01-11 14:50:10 +01008860 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8861 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8862 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8863 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008864 }
8865
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308866 TEST_EQUAL(psa_key_derivation_input_key(&operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01008867 steps[i],
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308868 keys[i]),
8869 statuses[i]);
8870
8871 if (statuses[i] != PSA_SUCCESS) {
8872 goto exit;
8873 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008874 break;
8875 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008876 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008877 break;
8878 }
8879 break;
8880 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008881 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008882 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008883 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8884 steps[i],
8885 inputs[i]->x,
8886 inputs[i]->len),
8887 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008888 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008889 case 1: // input key, type DERIVE
8890 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008891 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8892 psa_set_key_algorithm(&attributes2, alg);
8893 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008894
8895 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008896 if (other_key_input_type == 11) {
8897 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8898 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008899
Gilles Peskine449bd832023-01-11 14:50:10 +01008900 PSA_ASSERT(psa_import_key(&attributes2,
8901 inputs[i]->x, inputs[i]->len,
8902 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008903
Gilles Peskine449bd832023-01-11 14:50:10 +01008904 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8905 steps[i],
8906 keys[i]),
8907 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008908 break;
8909 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008910 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8911 psa_set_key_algorithm(&attributes3, alg);
8912 psa_set_key_type(&attributes3,
8913 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008914
Gilles Peskine449bd832023-01-11 14:50:10 +01008915 PSA_ASSERT(psa_import_key(&attributes3,
8916 inputs[i]->x, inputs[i]->len,
8917 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008918
Gilles Peskine449bd832023-01-11 14:50:10 +01008919 TEST_EQUAL(psa_key_derivation_key_agreement(
8920 &operation,
8921 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8922 keys[i], key_agreement_peer_key->x,
8923 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008924 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008925 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008926 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008927 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008928 }
8929
Gilles Peskine449bd832023-01-11 14:50:10 +01008930 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008931 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008932 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008933 break;
8934 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008935 TEST_EQUAL(psa_key_derivation_input_bytes(
8936 &operation, steps[i],
8937 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008938
Gilles Peskine449bd832023-01-11 14:50:10 +01008939 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008940 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008941 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008942 break;
8943 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008944 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008945
Gilles Peskine449bd832023-01-11 14:50:10 +01008946 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8947 &current_capacity));
8948 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008949 expected_capacity = requested_capacity;
8950
Gilles Peskine449bd832023-01-11 14:50:10 +01008951 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008952 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8953
8954 /* For output key derivation secret must be provided using
8955 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008956 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008957 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008958 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008959
Gilles Peskine449bd832023-01-11 14:50:10 +01008960 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8961 psa_set_key_algorithm(&attributes4, alg);
8962 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8963 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008964
Gilles Peskine449bd832023-01-11 14:50:10 +01008965 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8966 &derived_key), expected_status);
8967 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008968 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008969 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008970 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008971 status = psa_key_derivation_output_bytes(&operation,
8972 output_buffer, output_sizes[i]);
8973 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008974 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008975 TEST_ASSERT(status == PSA_SUCCESS ||
8976 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008977 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008978 } else if (expected_capacity == 0 ||
8979 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008980 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008981 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008982 expected_capacity = 0;
8983 continue;
8984 }
8985 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008986 PSA_ASSERT(status);
8987 if (output_sizes[i] != 0) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008988 TEST_MEMORY_COMPARE(output_buffer, output_sizes[i],
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008989 expected_outputs[i], output_sizes[i]);
Gilles Peskine449bd832023-01-11 14:50:10 +01008990 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008991 /* Check the operation status. */
8992 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008993 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8994 &current_capacity));
8995 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008996 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008997 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008998 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008999
9000exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009001 mbedtls_free(output_buffer);
9002 psa_key_derivation_abort(&operation);
9003 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
9004 psa_destroy_key(keys[i]);
9005 }
9006 psa_destroy_key(derived_key);
9007 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02009008}
9009/* END_CASE */
9010
9011/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009012void derive_full(int alg_arg,
9013 data_t *key_data,
9014 data_t *input1,
9015 data_t *input2,
9016 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02009017{
Ronald Cron5425a212020-08-04 14:58:35 +02009018 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02009019 psa_algorithm_t alg = alg_arg;
9020 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009021 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Kusumit Ghoderao9ffd3972023-12-01 16:40:13 +05309022 unsigned char output_buffer[32];
Gilles Peskined54931c2018-07-17 21:06:59 +02009023 size_t expected_capacity = requested_capacity;
9024 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009025 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02009026
Gilles Peskine449bd832023-01-11 14:50:10 +01009027 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02009028
Gilles Peskine449bd832023-01-11 14:50:10 +01009029 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9030 psa_set_key_algorithm(&attributes, alg);
9031 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02009032
Gilles Peskine449bd832023-01-11 14:50:10 +01009033 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9034 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02009035
Gilles Peskine449bd832023-01-11 14:50:10 +01009036 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
9037 input1->x, input1->len,
9038 input2->x, input2->len,
9039 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01009040 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009041 }
Janos Follath47f27ed2019-06-25 13:24:52 +01009042
Gilles Peskine449bd832023-01-11 14:50:10 +01009043 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9044 &current_capacity));
9045 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02009046
9047 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009048 while (current_capacity > 0) {
9049 size_t read_size = sizeof(output_buffer);
9050 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02009051 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01009052 }
9053 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9054 output_buffer,
9055 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02009056 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01009057 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9058 &current_capacity));
9059 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02009060 }
9061
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009062 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009063 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
9064 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02009065
Gilles Peskine449bd832023-01-11 14:50:10 +01009066 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02009067
9068exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009069 psa_key_derivation_abort(&operation);
9070 psa_destroy_key(key);
9071 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02009072}
9073/* END_CASE */
9074
Stephan Koch78109f52023-04-12 14:19:36 +02009075/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01009076void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
9077 int derivation_step,
9078 int capacity, int expected_capacity_status_arg,
9079 data_t *expected_output,
9080 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009081{
9082 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
9083 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04009084 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009085 uint8_t *output_buffer = NULL;
9086 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04009087 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
9088 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
9089 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009090
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009091 TEST_CALLOC(output_buffer, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009092 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009093
Gilles Peskine449bd832023-01-11 14:50:10 +01009094 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9095 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
9096 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009097
Gilles Peskine449bd832023-01-11 14:50:10 +01009098 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
9099 step, input->x, input->len),
9100 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009101
Gilles Peskine449bd832023-01-11 14:50:10 +01009102 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009103 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009104 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009105
Gilles Peskine449bd832023-01-11 14:50:10 +01009106 status = psa_key_derivation_output_bytes(&operation, output_buffer,
9107 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009108
Gilles Peskine449bd832023-01-11 14:50:10 +01009109 TEST_EQUAL(status, expected_output_status);
9110 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009111 TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009112 expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009113 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009114
9115exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009116 mbedtls_free(output_buffer);
9117 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009118 PSA_DONE();
9119}
9120/* END_CASE */
9121
Janos Follathe60c9052019-07-03 13:51:30 +01009122/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009123void derive_key_exercise(int alg_arg,
9124 data_t *key_data,
9125 data_t *input1,
9126 data_t *input2,
9127 int derived_type_arg,
9128 int derived_bits_arg,
9129 int derived_usage_arg,
9130 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009131{
Ronald Cron5425a212020-08-04 14:58:35 +02009132 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9133 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009134 psa_algorithm_t alg = alg_arg;
9135 psa_key_type_t derived_type = derived_type_arg;
9136 size_t derived_bits = derived_bits_arg;
9137 psa_key_usage_t derived_usage = derived_usage_arg;
9138 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009139 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009140 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009141 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009142 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009143
Gilles Peskine449bd832023-01-11 14:50:10 +01009144 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009145
Gilles Peskine449bd832023-01-11 14:50:10 +01009146 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9147 psa_set_key_algorithm(&attributes, alg);
9148 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9149 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9150 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009151
9152 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009153 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9154 input1->x, input1->len,
9155 input2->x, input2->len,
9156 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009157 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009158 }
Janos Follathe60c9052019-07-03 13:51:30 +01009159
Gilles Peskine449bd832023-01-11 14:50:10 +01009160 psa_set_key_usage_flags(&attributes, derived_usage);
9161 psa_set_key_algorithm(&attributes, derived_alg);
9162 psa_set_key_type(&attributes, derived_type);
9163 psa_set_key_bits(&attributes, derived_bits);
9164 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9165 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009166
9167 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009168 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9169 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9170 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009171
9172 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009173 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009174 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009175 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009176
9177exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009178 /*
9179 * Key attributes may have been returned by psa_get_key_attributes()
9180 * thus reset them as required.
9181 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009182 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009183
Gilles Peskine449bd832023-01-11 14:50:10 +01009184 psa_key_derivation_abort(&operation);
9185 psa_destroy_key(base_key);
9186 psa_destroy_key(derived_key);
9187 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009188}
9189/* END_CASE */
9190
Janos Follath42fd8882019-07-03 14:17:09 +01009191/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009192void derive_key_export(int alg_arg,
9193 data_t *key_data,
9194 data_t *input1,
9195 data_t *input2,
9196 int bytes1_arg,
9197 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009198{
Ronald Cron5425a212020-08-04 14:58:35 +02009199 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9200 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009201 psa_algorithm_t alg = alg_arg;
9202 size_t bytes1 = bytes1_arg;
9203 size_t bytes2 = bytes2_arg;
9204 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009205 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009206 uint8_t *output_buffer = NULL;
9207 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009208 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9209 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009210 size_t length;
9211
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009212 TEST_CALLOC(output_buffer, capacity);
9213 TEST_CALLOC(export_buffer, capacity);
Gilles Peskine449bd832023-01-11 14:50:10 +01009214 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009215
Gilles Peskine449bd832023-01-11 14:50:10 +01009216 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9217 psa_set_key_algorithm(&base_attributes, alg);
9218 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9219 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9220 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009221
9222 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009223 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9224 input1->x, input1->len,
9225 input2->x, input2->len,
9226 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009227 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009228 }
Janos Follath42fd8882019-07-03 14:17:09 +01009229
Gilles Peskine449bd832023-01-11 14:50:10 +01009230 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9231 output_buffer,
9232 capacity));
9233 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009234
9235 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009236 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9237 input1->x, input1->len,
9238 input2->x, input2->len,
9239 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009240 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009241 }
Janos Follath42fd8882019-07-03 14:17:09 +01009242
Gilles Peskine449bd832023-01-11 14:50:10 +01009243 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9244 psa_set_key_algorithm(&derived_attributes, 0);
9245 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9246 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9247 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9248 &derived_key));
9249 PSA_ASSERT(psa_export_key(derived_key,
9250 export_buffer, bytes1,
9251 &length));
9252 TEST_EQUAL(length, bytes1);
9253 PSA_ASSERT(psa_destroy_key(derived_key));
9254 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9255 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9256 &derived_key));
9257 PSA_ASSERT(psa_export_key(derived_key,
9258 export_buffer + bytes1, bytes2,
9259 &length));
9260 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009261
9262 /* Compare the outputs from the two runs. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009263 TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009264 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009265
9266exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009267 mbedtls_free(output_buffer);
9268 mbedtls_free(export_buffer);
9269 psa_key_derivation_abort(&operation);
9270 psa_destroy_key(base_key);
9271 psa_destroy_key(derived_key);
9272 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009273}
9274/* END_CASE */
9275
9276/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009277void derive_key_type(int alg_arg,
9278 data_t *key_data,
9279 data_t *input1,
9280 data_t *input2,
9281 int key_type_arg, int bits_arg,
9282 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009283{
9284 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9285 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9286 const psa_algorithm_t alg = alg_arg;
9287 const psa_key_type_t key_type = key_type_arg;
9288 const size_t bits = bits_arg;
9289 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9290 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009291 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009292 uint8_t *export_buffer = NULL;
9293 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9294 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9295 size_t export_length;
9296
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009297 TEST_CALLOC(export_buffer, export_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01009298 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009299
Gilles Peskine449bd832023-01-11 14:50:10 +01009300 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9301 psa_set_key_algorithm(&base_attributes, alg);
9302 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9303 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9304 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009305
Gilles Peskine449bd832023-01-11 14:50:10 +01009306 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009307 &operation, base_key, alg,
9308 input1->x, input1->len,
9309 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009310 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009311 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009312 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009313
Gilles Peskine449bd832023-01-11 14:50:10 +01009314 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9315 psa_set_key_algorithm(&derived_attributes, 0);
9316 psa_set_key_type(&derived_attributes, key_type);
9317 psa_set_key_bits(&derived_attributes, bits);
9318 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9319 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009320
Gilles Peskine449bd832023-01-11 14:50:10 +01009321 PSA_ASSERT(psa_export_key(derived_key,
9322 export_buffer, export_buffer_size,
9323 &export_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009324 TEST_MEMORY_COMPARE(export_buffer, export_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009325 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009326
9327exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009328 mbedtls_free(export_buffer);
9329 psa_key_derivation_abort(&operation);
9330 psa_destroy_key(base_key);
9331 psa_destroy_key(derived_key);
9332 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009333}
9334/* END_CASE */
9335
9336/* BEGIN_CASE */
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009337void derive_key_ext(int alg_arg,
9338 data_t *key_data,
9339 data_t *input1,
9340 data_t *input2,
9341 int key_type_arg, int bits_arg,
Gilles Peskinec81393b2024-02-14 20:51:28 +01009342 int flags_arg,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009343 data_t *method_data,
9344 psa_status_t expected_status,
9345 data_t *expected_export)
9346{
9347 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9348 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9349 const psa_algorithm_t alg = alg_arg;
9350 const psa_key_type_t key_type = key_type_arg;
9351 const size_t bits = bits_arg;
9352 psa_key_generation_method_t *method = NULL;
Gilles Peskinec81393b2024-02-14 20:51:28 +01009353 size_t method_data_length = 0;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009354 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9355 const size_t export_buffer_size =
9356 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
9357 uint8_t *export_buffer = NULL;
9358 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9359 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9360 size_t export_length;
9361
9362 TEST_CALLOC(export_buffer, export_buffer_size);
9363 PSA_ASSERT(psa_crypto_init());
9364
9365 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9366 psa_set_key_algorithm(&base_attributes, alg);
9367 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9368 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9369 &base_key));
9370
9371 if (mbedtls_test_psa_setup_key_derivation_wrap(
9372 &operation, base_key, alg,
9373 input1->x, input1->len,
9374 input2->x, input2->len,
9375 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
9376 goto exit;
9377 }
9378
9379 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9380 psa_set_key_algorithm(&derived_attributes, 0);
9381 psa_set_key_type(&derived_attributes, key_type);
9382 psa_set_key_bits(&derived_attributes, bits);
Gilles Peskinec81393b2024-02-14 20:51:28 +01009383 if (!setup_key_generation_method(&method, &method_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009384 flags_arg, method_data)) {
9385 goto exit;
9386 }
9387
9388 TEST_EQUAL(psa_key_derivation_output_key_ext(&derived_attributes, &operation,
Gilles Peskinec81393b2024-02-14 20:51:28 +01009389 method, method_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009390 &derived_key),
9391 expected_status);
9392
9393 if (expected_status == PSA_SUCCESS) {
9394 PSA_ASSERT(psa_export_key(derived_key,
9395 export_buffer, export_buffer_size,
9396 &export_length));
9397 TEST_MEMORY_COMPARE(export_buffer, export_length,
9398 expected_export->x, expected_export->len);
9399 }
9400
9401exit:
9402 mbedtls_free(export_buffer);
9403 mbedtls_free(method);
9404 psa_key_derivation_abort(&operation);
9405 psa_destroy_key(base_key);
9406 psa_destroy_key(derived_key);
9407 PSA_DONE();
9408}
9409/* END_CASE */
9410
9411/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009412void derive_key(int alg_arg,
9413 data_t *key_data, data_t *input1, data_t *input2,
9414 int type_arg, int bits_arg,
9415 int expected_status_arg,
9416 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009417{
Ronald Cron5425a212020-08-04 14:58:35 +02009418 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9419 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009420 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009421 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009422 size_t bits = bits_arg;
9423 psa_status_t expected_status = expected_status_arg;
9424 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9425 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9426 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9427
Gilles Peskine449bd832023-01-11 14:50:10 +01009428 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009429
Gilles Peskine449bd832023-01-11 14:50:10 +01009430 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9431 psa_set_key_algorithm(&base_attributes, alg);
9432 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9433 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9434 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009435
Gilles Peskine449bd832023-01-11 14:50:10 +01009436 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9437 input1->x, input1->len,
9438 input2->x, input2->len,
9439 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009440 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009441 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009442
Gilles Peskine449bd832023-01-11 14:50:10 +01009443 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9444 psa_set_key_algorithm(&derived_attributes, 0);
9445 psa_set_key_type(&derived_attributes, type);
9446 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009447
9448 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009449 psa_key_derivation_output_key(&derived_attributes,
9450 &operation,
9451 &derived_key);
9452 if (is_large_output > 0) {
9453 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9454 }
9455 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009456
9457exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009458 psa_key_derivation_abort(&operation);
9459 psa_destroy_key(base_key);
9460 psa_destroy_key(derived_key);
9461 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009462}
9463/* END_CASE */
9464
9465/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009466void key_agreement_setup(int alg_arg,
9467 int our_key_type_arg, int our_key_alg_arg,
9468 data_t *our_key_data, data_t *peer_key_data,
9469 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009470{
Ronald Cron5425a212020-08-04 14:58:35 +02009471 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009472 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009473 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009474 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009475 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009476 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009477 psa_status_t expected_status = expected_status_arg;
9478 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009479
Gilles Peskine449bd832023-01-11 14:50:10 +01009480 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009481
Gilles Peskine449bd832023-01-11 14:50:10 +01009482 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9483 psa_set_key_algorithm(&attributes, our_key_alg);
9484 psa_set_key_type(&attributes, our_key_type);
9485 PSA_ASSERT(psa_import_key(&attributes,
9486 our_key_data->x, our_key_data->len,
9487 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009488
Gilles Peskine77f40d82019-04-11 21:27:06 +02009489 /* The tests currently include inputs that should fail at either step.
9490 * Test cases that fail at the setup step should be changed to call
9491 * key_derivation_setup instead, and this function should be renamed
9492 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009493 status = psa_key_derivation_setup(&operation, alg);
9494 if (status == PSA_SUCCESS) {
9495 TEST_EQUAL(psa_key_derivation_key_agreement(
9496 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9497 our_key,
9498 peer_key_data->x, peer_key_data->len),
9499 expected_status);
9500 } else {
9501 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009502 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009503
9504exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009505 psa_key_derivation_abort(&operation);
9506 psa_destroy_key(our_key);
9507 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009508}
9509/* END_CASE */
9510
9511/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009512void raw_key_agreement(int alg_arg,
9513 int our_key_type_arg, data_t *our_key_data,
9514 data_t *peer_key_data,
9515 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009516{
Ronald Cron5425a212020-08-04 14:58:35 +02009517 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009518 psa_algorithm_t alg = alg_arg;
9519 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009521 unsigned char *output = NULL;
9522 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009523 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009524
Gilles Peskine449bd832023-01-11 14:50:10 +01009525 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009526
Gilles Peskine449bd832023-01-11 14:50:10 +01009527 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9528 psa_set_key_algorithm(&attributes, alg);
9529 psa_set_key_type(&attributes, our_key_type);
9530 PSA_ASSERT(psa_import_key(&attributes,
9531 our_key_data->x, our_key_data->len,
9532 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009533
Gilles Peskine449bd832023-01-11 14:50:10 +01009534 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9535 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009536
Gilles Peskine992bee82022-04-13 23:25:52 +02009537 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009538 TEST_LE_U(expected_output->len,
9539 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9540 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9541 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009542
9543 /* Good case with exact output size */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009544 TEST_CALLOC(output, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009545 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9546 peer_key_data->x, peer_key_data->len,
9547 output, expected_output->len,
9548 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009549 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009550 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009551 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009552 output = NULL;
9553 output_length = ~0;
9554
9555 /* Larger buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009556 TEST_CALLOC(output, expected_output->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009557 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9558 peer_key_data->x, peer_key_data->len,
9559 output, expected_output->len + 1,
9560 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009561 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009562 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009563 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009564 output = NULL;
9565 output_length = ~0;
9566
9567 /* Buffer too small */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009568 TEST_CALLOC(output, expected_output->len - 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009569 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9570 peer_key_data->x, peer_key_data->len,
9571 output, expected_output->len - 1,
9572 &output_length),
9573 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009574 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009575 TEST_LE_U(output_length, expected_output->len - 1);
9576 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009577 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009578
9579exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009580 mbedtls_free(output);
9581 psa_destroy_key(our_key);
9582 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009583}
9584/* END_CASE */
9585
9586/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009587void key_agreement_capacity(int alg_arg,
9588 int our_key_type_arg, data_t *our_key_data,
9589 data_t *peer_key_data,
9590 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009591{
Ronald Cron5425a212020-08-04 14:58:35 +02009592 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009593 psa_algorithm_t alg = alg_arg;
9594 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009595 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009596 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009597 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009598 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009599
Gilles Peskine449bd832023-01-11 14:50:10 +01009600 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009601
Gilles Peskine449bd832023-01-11 14:50:10 +01009602 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9603 psa_set_key_algorithm(&attributes, alg);
9604 psa_set_key_type(&attributes, our_key_type);
9605 PSA_ASSERT(psa_import_key(&attributes,
9606 our_key_data->x, our_key_data->len,
9607 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009608
Gilles Peskine449bd832023-01-11 14:50:10 +01009609 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9610 PSA_ASSERT(psa_key_derivation_key_agreement(
9611 &operation,
9612 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9613 peer_key_data->x, peer_key_data->len));
9614 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009615 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009616 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9617 PSA_KEY_DERIVATION_INPUT_INFO,
9618 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009619 }
Gilles Peskine59685592018-09-18 12:11:34 +02009620
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009621 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009622 PSA_ASSERT(psa_key_derivation_get_capacity(
9623 &operation, &actual_capacity));
9624 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009625
Gilles Peskinebf491972018-10-25 22:36:12 +02009626 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009627 while (actual_capacity > sizeof(output)) {
9628 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9629 output, sizeof(output)));
9630 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009631 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009632 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9633 output, actual_capacity));
9634 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9635 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009636
Gilles Peskine59685592018-09-18 12:11:34 +02009637exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009638 psa_key_derivation_abort(&operation);
9639 psa_destroy_key(our_key);
9640 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009641}
9642/* END_CASE */
9643
Valerio Settiad819672023-12-29 12:14:41 +01009644/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9645void ecc_conversion_functions(int grp_id_arg, int psa_family_arg, int bits_arg)
Valerio Settibf999cb2023-12-28 17:48:13 +01009646{
9647 mbedtls_ecp_group_id grp_id = grp_id_arg;
Valerio Settiad819672023-12-29 12:14:41 +01009648 psa_ecc_family_t ecc_family = psa_family_arg;
9649 size_t bits = bits_arg;
9650 size_t bits_tmp;
Valerio Settibf999cb2023-12-28 17:48:13 +01009651
Valerio Settiad819672023-12-29 12:14:41 +01009652 TEST_EQUAL(ecc_family, mbedtls_ecc_group_to_psa(grp_id, &bits_tmp));
9653 TEST_EQUAL(bits, bits_tmp);
Valerio Settiac739522024-01-04 10:22:01 +01009654 TEST_EQUAL(grp_id, mbedtls_ecc_group_from_psa(ecc_family, bits));
Valerio Settibf999cb2023-12-28 17:48:13 +01009655}
9656/* END_CASE */
9657
Valerio Settiac739522024-01-04 10:22:01 +01009658/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9659void ecc_conversion_functions_fail()
9660{
9661 size_t bits;
9662
Valerio Settidb6e0292024-01-05 10:15:45 +01009663 /* Invalid legacy curve identifiers. */
9664 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_MAX, &bits));
9665 TEST_EQUAL(0, bits);
Valerio Settiac739522024-01-04 10:22:01 +01009666 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_NONE, &bits));
9667 TEST_EQUAL(0, bits);
9668
9669 /* Invalid PSA EC family. */
9670 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(0, 192));
9671 /* Invalid bit-size for a valid EC family. */
9672 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_SECP_R1, 512));
9673
9674 /* Twisted-Edward curves are not supported yet. */
9675 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9676 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255));
9677 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9678 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 448));
9679}
9680/* END_CASE */
9681
9682
Valerio Settibf999cb2023-12-28 17:48:13 +01009683/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009684void key_agreement_output(int alg_arg,
9685 int our_key_type_arg, data_t *our_key_data,
9686 data_t *peer_key_data,
9687 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009688{
Ronald Cron5425a212020-08-04 14:58:35 +02009689 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009690 psa_algorithm_t alg = alg_arg;
9691 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009692 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009693 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009694 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009695
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009696 TEST_CALLOC(actual_output, MAX(expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009697 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009698
Gilles Peskine449bd832023-01-11 14:50:10 +01009699 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009700
Gilles Peskine449bd832023-01-11 14:50:10 +01009701 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9702 psa_set_key_algorithm(&attributes, alg);
9703 psa_set_key_type(&attributes, our_key_type);
9704 PSA_ASSERT(psa_import_key(&attributes,
9705 our_key_data->x, our_key_data->len,
9706 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009707
Gilles Peskine449bd832023-01-11 14:50:10 +01009708 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9709 PSA_ASSERT(psa_key_derivation_key_agreement(
9710 &operation,
9711 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9712 peer_key_data->x, peer_key_data->len));
9713 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009714 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009715 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9716 PSA_KEY_DERIVATION_INPUT_INFO,
9717 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009718 }
Gilles Peskine59685592018-09-18 12:11:34 +02009719
Gilles Peskine449bd832023-01-11 14:50:10 +01009720 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9721 actual_output,
9722 expected_output1->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009723 TEST_MEMORY_COMPARE(actual_output, expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009724 expected_output1->x, expected_output1->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009725 if (expected_output2->len != 0) {
9726 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9727 actual_output,
9728 expected_output2->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009729 TEST_MEMORY_COMPARE(actual_output, expected_output2->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009730 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009731 }
Gilles Peskine59685592018-09-18 12:11:34 +02009732
9733exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009734 psa_key_derivation_abort(&operation);
9735 psa_destroy_key(our_key);
9736 PSA_DONE();
9737 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009738}
9739/* END_CASE */
9740
9741/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009742void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009743{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009744 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009745 unsigned char *output = NULL;
9746 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009747 size_t i;
9748 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009749
Gilles Peskine449bd832023-01-11 14:50:10 +01009750 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009751
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009752 TEST_CALLOC(output, bytes);
9753 TEST_CALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009754
Gilles Peskine449bd832023-01-11 14:50:10 +01009755 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009756
Gilles Peskinea50d7392018-06-21 10:22:13 +02009757 /* Run several times, to ensure that every output byte will be
9758 * nonzero at least once with overwhelming probability
9759 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009760 for (run = 0; run < 10; run++) {
9761 if (bytes != 0) {
9762 memset(output, 0, bytes);
9763 }
9764 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009765
Gilles Peskine449bd832023-01-11 14:50:10 +01009766 for (i = 0; i < bytes; i++) {
9767 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009768 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009769 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009770 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009771 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009772
9773 /* Check that every byte was changed to nonzero at least once. This
9774 * validates that psa_generate_random is overwriting every byte of
9775 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009776 for (i = 0; i < bytes; i++) {
9777 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009778 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009779
9780exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009781 PSA_DONE();
9782 mbedtls_free(output);
9783 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009784}
9785/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009786
9787/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009788void generate_key(int type_arg,
9789 int bits_arg,
9790 int usage_arg,
9791 int alg_arg,
9792 int expected_status_arg,
9793 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009794{
Ronald Cron5425a212020-08-04 14:58:35 +02009795 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009796 psa_key_type_t type = type_arg;
9797 psa_key_usage_t usage = usage_arg;
9798 size_t bits = bits_arg;
9799 psa_algorithm_t alg = alg_arg;
9800 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009801 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009802 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009803
Gilles Peskine449bd832023-01-11 14:50:10 +01009804 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009805
Gilles Peskine449bd832023-01-11 14:50:10 +01009806 psa_set_key_usage_flags(&attributes, usage);
9807 psa_set_key_algorithm(&attributes, alg);
9808 psa_set_key_type(&attributes, type);
9809 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009810
9811 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009812 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009813
Gilles Peskine449bd832023-01-11 14:50:10 +01009814 if (is_large_key > 0) {
9815 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9816 }
9817 TEST_EQUAL(status, expected_status);
9818 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009819 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009820 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009821
9822 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009823 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9824 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9825 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009826
Gilles Peskine818ca122018-06-20 18:16:48 +02009827 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009828 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009829 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009830 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009831
9832exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009833 /*
9834 * Key attributes may have been returned by psa_get_key_attributes()
9835 * thus reset them as required.
9836 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009837 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009838
Gilles Peskine449bd832023-01-11 14:50:10 +01009839 psa_destroy_key(key);
9840 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009841}
9842/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009843
Valerio Setti19fec542023-07-25 12:31:50 +02009844/* 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 +01009845void generate_key_rsa(int bits_arg,
9846 data_t *e_arg,
9847 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009848{
Ronald Cron5425a212020-08-04 14:58:35 +02009849 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009850 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009851 size_t bits = bits_arg;
9852 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9853 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9854 psa_status_t expected_status = expected_status_arg;
9855 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009856 uint8_t *e_read_buffer = NULL;
9857 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009858 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009859 size_t e_read_length = SIZE_MAX;
9860
Gilles Peskine449bd832023-01-11 14:50:10 +01009861 if (e_arg->len == 0 ||
9862 (e_arg->len == 3 &&
9863 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009864 is_default_public_exponent = 1;
9865 e_read_size = 0;
9866 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009867 TEST_CALLOC(e_read_buffer, e_read_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009868
Gilles Peskine449bd832023-01-11 14:50:10 +01009869 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009870
Gilles Peskine449bd832023-01-11 14:50:10 +01009871 psa_set_key_usage_flags(&attributes, usage);
9872 psa_set_key_algorithm(&attributes, alg);
9873 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9874 e_arg->x, e_arg->len));
9875 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009876
9877 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009878 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9879 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009880 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009881 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009882
9883 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009884 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9885 TEST_EQUAL(psa_get_key_type(&attributes), type);
9886 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009887 psa_status_t status = psa_get_key_domain_parameters(&attributes,
9888 e_read_buffer, e_read_size,
9889 &e_read_length);
9890
9891
9892#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
9893 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
9894 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +01009895 if (is_default_public_exponent) {
9896 TEST_EQUAL(e_read_length, 0);
9897 } else {
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009898 TEST_EQUAL(status, PSA_SUCCESS);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009899 TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009900 }
Pengyu Lv9e976f32023-12-06 16:58:05 +08009901#else
Pengyu Lv9e976f32023-12-06 16:58:05 +08009902 (void) is_default_public_exponent;
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009903 TEST_EQUAL(status, PSA_ERROR_NOT_SUPPORTED);
Pengyu Lv9e976f32023-12-06 16:58:05 +08009904#endif
Gilles Peskinee56e8782019-04-26 17:34:02 +02009905
9906 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009907 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009908 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009909 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009910
Gilles Peskine1d25a0a2024-02-12 16:40:04 +01009911 TEST_ASSERT(rsa_test_e(key, bits, e_arg));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009912
9913exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009914 /*
9915 * Key attributes may have been returned by psa_get_key_attributes() or
9916 * set by psa_set_key_domain_parameters() thus reset them as required.
9917 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009918 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009919
Gilles Peskine449bd832023-01-11 14:50:10 +01009920 psa_destroy_key(key);
9921 PSA_DONE();
9922 mbedtls_free(e_read_buffer);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009923}
9924/* END_CASE */
9925
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009926/* BEGIN_CASE */
9927void generate_key_ext(int type_arg,
9928 int bits_arg,
9929 int usage_arg,
9930 int alg_arg,
Gilles Peskinec81393b2024-02-14 20:51:28 +01009931 int flags_arg,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009932 data_t *method_data,
9933 int expected_status_arg)
9934{
9935 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9936 psa_key_type_t type = type_arg;
9937 psa_key_usage_t usage = usage_arg;
9938 size_t bits = bits_arg;
9939 psa_algorithm_t alg = alg_arg;
9940 psa_status_t expected_status = expected_status_arg;
9941 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9942 psa_key_generation_method_t *method = NULL;
Gilles Peskinec81393b2024-02-14 20:51:28 +01009943 size_t method_data_length = 0;
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009944 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
9945
9946 PSA_ASSERT(psa_crypto_init());
9947
9948 psa_set_key_usage_flags(&attributes, usage);
9949 psa_set_key_algorithm(&attributes, alg);
9950 psa_set_key_type(&attributes, type);
9951 psa_set_key_bits(&attributes, bits);
9952
Gilles Peskinec81393b2024-02-14 20:51:28 +01009953 if (!setup_key_generation_method(&method, &method_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009954 flags_arg, method_data)) {
9955 goto exit;
9956 }
9957
9958 /* Generate a key */
9959 psa_status_t status = psa_generate_key_ext(&attributes,
Gilles Peskinec81393b2024-02-14 20:51:28 +01009960 method, method_data_length,
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009961 &key);
9962
9963 TEST_EQUAL(status, expected_status);
9964 if (expected_status != PSA_SUCCESS) {
9965 goto exit;
9966 }
9967
9968 /* Test the key information */
9969 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9970 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9971 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
9972
Gilles Peskine7a18f962024-02-12 16:48:11 +01009973#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
9974 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
9975 TEST_ASSERT(rsa_test_e(key, bits, method_data));
9976 }
9977#endif
9978
Gilles Peskinef0765fa2024-02-12 16:46:16 +01009979 /* Do something with the key according to its type and permitted usage. */
9980 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
9981 goto exit;
9982 }
9983
9984exit:
9985 /*
9986 * Key attributes may have been returned by psa_get_key_attributes()
9987 * thus reset them as required.
9988 */
9989 psa_reset_key_attributes(&got_attributes);
9990 mbedtls_free(method);
9991 psa_destroy_key(key);
9992 PSA_DONE();
9993}
9994/* END_CASE */
9995
9996/* BEGIN_CASE */
9997void key_generation_method_init()
9998{
9999 psa_key_generation_method_t func = psa_key_generation_method_init();
10000 psa_key_generation_method_t init = PSA_KEY_GENERATION_METHOD_INIT;
10001 psa_key_generation_method_t zero;
10002 memset(&zero, 0, sizeof(zero));
10003
Gilles Peskinef0765fa2024-02-12 16:46:16 +010010004 TEST_EQUAL(func.flags, 0);
10005 TEST_EQUAL(init.flags, 0);
10006 TEST_EQUAL(zero.flags, 0);
10007}
10008/* END_CASE */
10009
Darryl Greend49a4992018-06-18 17:27:26 +010010010/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010010011void persistent_key_load_key_from_storage(data_t *data,
10012 int type_arg, int bits_arg,
10013 int usage_flags_arg, int alg_arg,
10014 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +010010015{
Gilles Peskine449bd832023-01-11 14:50:10 +010010016 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010017 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +020010018 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10019 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010020 psa_key_type_t type = type_arg;
10021 size_t bits = bits_arg;
10022 psa_key_usage_t usage_flags = usage_flags_arg;
10023 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +020010024 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +010010025 unsigned char *first_export = NULL;
10026 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +010010027 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Sergeybef1f632023-03-06 15:25:06 -070010028 size_t first_exported_length = 0;
Darryl Greend49a4992018-06-18 17:27:26 +010010029 size_t second_exported_length;
10030
Gilles Peskine449bd832023-01-11 14:50:10 +010010031 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +010010032 TEST_CALLOC(first_export, export_size);
10033 TEST_CALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010034 }
Darryl Greend49a4992018-06-18 17:27:26 +010010035
Gilles Peskine449bd832023-01-11 14:50:10 +010010036 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +010010037
Gilles Peskine449bd832023-01-11 14:50:10 +010010038 psa_set_key_id(&attributes, key_id);
10039 psa_set_key_usage_flags(&attributes, usage_flags);
10040 psa_set_key_algorithm(&attributes, alg);
10041 psa_set_key_type(&attributes, type);
10042 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +010010043
Gilles Peskine449bd832023-01-11 14:50:10 +010010044 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +000010045 case IMPORT_KEY:
10046 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010047 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
10048 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +000010049 break;
Darryl Greend49a4992018-06-18 17:27:26 +010010050
Darryl Green0c6575a2018-11-07 16:05:30 +000010051 case GENERATE_KEY:
10052 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010053 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +000010054 break;
10055
10056 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +010010057#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +010010058 {
10059 /* Create base key */
10060 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
10061 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
10062 psa_set_key_usage_flags(&base_attributes,
10063 PSA_KEY_USAGE_DERIVE);
10064 psa_set_key_algorithm(&base_attributes, derive_alg);
10065 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
10066 PSA_ASSERT(psa_import_key(&base_attributes,
10067 data->x, data->len,
10068 &base_key));
10069 /* Derive a key. */
10070 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
10071 PSA_ASSERT(psa_key_derivation_input_key(
10072 &operation,
10073 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
10074 PSA_ASSERT(psa_key_derivation_input_bytes(
10075 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
10076 NULL, 0));
10077 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
10078 &operation,
10079 &key));
10080 PSA_ASSERT(psa_key_derivation_abort(&operation));
10081 PSA_ASSERT(psa_destroy_key(base_key));
10082 base_key = MBEDTLS_SVC_KEY_ID_INIT;
10083 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +010010084#else
Gilles Peskine449bd832023-01-11 14:50:10 +010010085 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +010010086#endif
10087 break;
10088
10089 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +010010090 TEST_FAIL("generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +010010091 break;
Darryl Green0c6575a2018-11-07 16:05:30 +000010092 }
Gilles Peskine449bd832023-01-11 14:50:10 +010010093 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +010010094
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010095 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +010010096 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
10097 PSA_ASSERT(psa_export_key(key,
10098 first_export, export_size,
10099 &first_exported_length));
10100 if (generation_method == IMPORT_KEY) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +010010101 TEST_MEMORY_COMPARE(data->x, data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +010010102 first_export, first_exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +010010103 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010104 }
Darryl Greend49a4992018-06-18 17:27:26 +010010105
10106 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +010010107 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +020010108 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +010010109 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +010010110
Darryl Greend49a4992018-06-18 17:27:26 +010010111 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +010010112 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
10113 TEST_ASSERT(mbedtls_svc_key_id_equal(
10114 psa_get_key_id(&attributes), key_id));
10115 TEST_EQUAL(psa_get_key_lifetime(&attributes),
10116 PSA_KEY_LIFETIME_PERSISTENT);
10117 TEST_EQUAL(psa_get_key_type(&attributes), type);
10118 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
10119 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
10120 mbedtls_test_update_key_usage_flags(usage_flags));
10121 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +010010122
Gilles Peskine5c648ab2019-04-19 14:06:53 +020010123 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +010010124 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
10125 PSA_ASSERT(psa_export_key(key,
10126 second_export, export_size,
10127 &second_exported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +010010128 TEST_MEMORY_COMPARE(first_export, first_exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +010010129 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +000010130 }
10131
10132 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +010010133 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +000010134 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010135 }
Darryl Greend49a4992018-06-18 17:27:26 +010010136
10137exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010138 /*
10139 * Key attributes may have been returned by psa_get_key_attributes()
10140 * thus reset them as required.
10141 */
Gilles Peskine449bd832023-01-11 14:50:10 +010010142 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +010010143
Gilles Peskine449bd832023-01-11 14:50:10 +010010144 mbedtls_free(first_export);
10145 mbedtls_free(second_export);
10146 psa_key_derivation_abort(&operation);
10147 psa_destroy_key(base_key);
10148 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +020010149 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +010010150}
10151/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +020010152
Neil Armstronga557cb82022-06-10 08:58:32 +020010153/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010154void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
10155 int primitive_arg, int hash_arg, int role_arg,
10156 int test_input, data_t *pw_data,
10157 int inj_err_type_arg,
10158 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010159{
10160 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10161 psa_pake_operation_t operation = psa_pake_operation_init();
10162 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010163 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +020010164 psa_key_type_t key_type_pw = key_type_pw_arg;
10165 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010166 psa_algorithm_t hash_alg = hash_arg;
10167 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010168 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010170 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
10171 psa_status_t expected_error = expected_error_arg;
10172 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010173 unsigned char *output_buffer = NULL;
10174 size_t output_len = 0;
10175
Gilles Peskine449bd832023-01-11 14:50:10 +010010176 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010177
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010178 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +010010179 PSA_PAKE_STEP_KEY_SHARE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +010010180 TEST_CALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010181
Gilles Peskine449bd832023-01-11 14:50:10 +010010182 if (pw_data->len > 0) {
10183 psa_set_key_usage_flags(&attributes, key_usage_pw);
10184 psa_set_key_algorithm(&attributes, alg);
10185 psa_set_key_type(&attributes, key_type_pw);
10186 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10187 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010188 }
10189
Gilles Peskine449bd832023-01-11 14:50:10 +010010190 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10191 psa_pake_cs_set_primitive(&cipher_suite, primitive);
10192 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010193
Gilles Peskine449bd832023-01-11 14:50:10 +010010194 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +020010195
Gilles Peskine449bd832023-01-11 14:50:10 +010010196 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
10197 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10198 expected_error);
10199 PSA_ASSERT(psa_pake_abort(&operation));
10200 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10201 expected_error);
10202 PSA_ASSERT(psa_pake_abort(&operation));
10203 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
10204 expected_error);
10205 PSA_ASSERT(psa_pake_abort(&operation));
10206 TEST_EQUAL(psa_pake_set_role(&operation, role),
10207 expected_error);
10208 PSA_ASSERT(psa_pake_abort(&operation));
10209 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10210 NULL, 0, NULL),
10211 expected_error);
10212 PSA_ASSERT(psa_pake_abort(&operation));
10213 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
10214 expected_error);
10215 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010216 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010217 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010218
Gilles Peskine449bd832023-01-11 14:50:10 +010010219 status = psa_pake_setup(&operation, &cipher_suite);
10220 if (status != PSA_SUCCESS) {
10221 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010222 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010223 }
10224
Gilles Peskine449bd832023-01-11 14:50:10 +010010225 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
10226 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
10227 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010228 goto exit;
10229 }
10230
Gilles Peskine449bd832023-01-11 14:50:10 +010010231 status = psa_pake_set_role(&operation, role);
10232 if (status != PSA_SUCCESS) {
10233 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010234 goto exit;
10235 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010236
Gilles Peskine449bd832023-01-11 14:50:10 +010010237 if (pw_data->len > 0) {
10238 status = psa_pake_set_password_key(&operation, key);
10239 if (status != PSA_SUCCESS) {
10240 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010241 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010242 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010243 }
10244
Gilles Peskine449bd832023-01-11 14:50:10 +010010245 if (inj_err_type == INJECT_ERR_INVALID_USER) {
10246 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10247 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010248 goto exit;
10249 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010250
Gilles Peskine449bd832023-01-11 14:50:10 +010010251 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
10252 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10253 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010254 goto exit;
10255 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010256
Gilles Peskine449bd832023-01-11 14:50:10 +010010257 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010258 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010259 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
10260 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010261 goto exit;
10262 }
10263
Gilles Peskine449bd832023-01-11 14:50:10 +010010264 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010265 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010266 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
10267 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010268 goto exit;
10269 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010270
Gilles Peskine449bd832023-01-11 14:50:10 +010010271 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
10272 PSA_PAKE_STEP_KEY_SHARE);
10273 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
10274 PSA_PAKE_STEP_ZK_PUBLIC);
10275 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
10276 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010277
Gilles Peskine449bd832023-01-11 14:50:10 +010010278 if (test_input) {
10279 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10280 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
10281 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010282 goto exit;
10283 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010284
Gilles Peskine449bd832023-01-11 14:50:10 +010010285 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10286 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10287 output_buffer, size_zk_proof),
10288 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010289 goto exit;
10290 }
10291
Gilles Peskine449bd832023-01-11 14:50:10 +010010292 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10293 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
10294 output_buffer, size_zk_proof),
10295 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010296 goto exit;
10297 }
10298
Gilles Peskine449bd832023-01-11 14:50:10 +010010299 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
10300 output_buffer, size_key_share);
10301 if (status != PSA_SUCCESS) {
10302 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010303 goto exit;
10304 }
10305
Gilles Peskine449bd832023-01-11 14:50:10 +010010306 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10307 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10308 output_buffer, size_zk_public + 1),
10309 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010310 goto exit;
10311 }
10312
Gilles Peskine449bd832023-01-11 14:50:10 +010010313 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010314 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010315 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10316 output_buffer, size_zk_public + 1);
10317 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10318 output_buffer, size_zk_public),
10319 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010320 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010321 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010322 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010323 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10324 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10325 NULL, 0, NULL),
10326 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010327 goto exit;
10328 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010329
Gilles Peskine449bd832023-01-11 14:50:10 +010010330 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10331 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10332 output_buffer, buf_size, &output_len),
10333 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010334 goto exit;
10335 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010336
Gilles Peskine449bd832023-01-11 14:50:10 +010010337 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10338 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10339 output_buffer, buf_size, &output_len),
10340 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010341 goto exit;
10342 }
10343
Gilles Peskine449bd832023-01-11 14:50:10 +010010344 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10345 output_buffer, buf_size, &output_len);
10346 if (status != PSA_SUCCESS) {
10347 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010348 goto exit;
10349 }
10350
Gilles Peskine449bd832023-01-11 14:50:10 +010010351 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010352
Gilles Peskine449bd832023-01-11 14:50:10 +010010353 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10354 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10355 output_buffer, size_zk_public - 1, &output_len),
10356 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010357 goto exit;
10358 }
10359
Gilles Peskine449bd832023-01-11 14:50:10 +010010360 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010361 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010362 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10363 output_buffer, size_zk_public - 1, &output_len);
10364 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10365 output_buffer, buf_size, &output_len),
10366 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010367 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010368 }
10369 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010370
10371exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010372 PSA_ASSERT(psa_destroy_key(key));
10373 PSA_ASSERT(psa_pake_abort(&operation));
10374 mbedtls_free(output_buffer);
10375 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010376}
10377/* END_CASE */
10378
Neil Armstronga557cb82022-06-10 08:58:32 +020010379/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010380void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10381 int client_input_first, int inject_error,
10382 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010383{
10384 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10385 psa_pake_operation_t server = psa_pake_operation_init();
10386 psa_pake_operation_t client = psa_pake_operation_init();
10387 psa_algorithm_t alg = alg_arg;
10388 psa_algorithm_t hash_alg = hash_arg;
10389 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10390 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10391
Gilles Peskine449bd832023-01-11 14:50:10 +010010392 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010393
Gilles Peskine449bd832023-01-11 14:50:10 +010010394 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10395 psa_set_key_algorithm(&attributes, alg);
10396 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10397 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10398 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010399
Gilles Peskine449bd832023-01-11 14:50:10 +010010400 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10401 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10402 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010403
10404
Gilles Peskine449bd832023-01-11 14:50:10 +010010405 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10406 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010407
Gilles Peskine449bd832023-01-11 14:50:10 +010010408 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10409 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010410
Gilles Peskine449bd832023-01-11 14:50:10 +010010411 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10412 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010413
Gilles Peskine449bd832023-01-11 14:50:10 +010010414 ecjpake_do_round(alg, primitive_arg, &server, &client,
10415 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010416
Gilles Peskine449bd832023-01-11 14:50:10 +010010417 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010418 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010419 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010420
Gilles Peskine449bd832023-01-11 14:50:10 +010010421 ecjpake_do_round(alg, primitive_arg, &server, &client,
10422 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010423
10424exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010425 psa_destroy_key(key);
10426 psa_pake_abort(&server);
10427 psa_pake_abort(&client);
10428 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010429}
10430/* END_CASE */
10431
10432/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010433void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10434 int derive_alg_arg, data_t *pw_data,
10435 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010436{
10437 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10438 psa_pake_operation_t server = psa_pake_operation_init();
10439 psa_pake_operation_t client = psa_pake_operation_init();
10440 psa_algorithm_t alg = alg_arg;
10441 psa_algorithm_t hash_alg = hash_arg;
10442 psa_algorithm_t derive_alg = derive_alg_arg;
10443 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10444 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10445 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010446 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010447 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010448 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010449 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010450
Gilles Peskine449bd832023-01-11 14:50:10 +010010451 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010452
Gilles Peskine449bd832023-01-11 14:50:10 +010010453 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10454 psa_set_key_algorithm(&attributes, alg);
10455 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10456 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10457 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010458
Gilles Peskine449bd832023-01-11 14:50:10 +010010459 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10460 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10461 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010462
Neil Armstrong1e855602022-06-15 11:32:11 +020010463 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010464 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10465 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010466
Gilles Peskine449bd832023-01-11 14:50:10 +010010467 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10468 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10469 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10470 PSA_KEY_DERIVATION_INPUT_SEED,
10471 (const uint8_t *) "", 0));
10472 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10473 PSA_KEY_DERIVATION_INPUT_SEED,
10474 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010475 }
10476
Gilles Peskine449bd832023-01-11 14:50:10 +010010477 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10478 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010479
Gilles Peskine449bd832023-01-11 14:50:10 +010010480 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10481 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010482
Gilles Peskine449bd832023-01-11 14:50:10 +010010483 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10484 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010485
Gilles Peskine449bd832023-01-11 14:50:10 +010010486 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10487 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10488 PSA_ERROR_BAD_STATE);
10489 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10490 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010491 goto exit;
10492 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010493
Neil Armstrongf983caf2022-06-15 15:27:48 +020010494 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010495 ecjpake_do_round(alg, primitive_arg, &server, &client,
10496 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010497
Gilles Peskine449bd832023-01-11 14:50:10 +010010498 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10499 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10500 PSA_ERROR_BAD_STATE);
10501 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10502 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010503 goto exit;
10504 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010505
Neil Armstrongf983caf2022-06-15 15:27:48 +020010506 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010507 ecjpake_do_round(alg, primitive_arg, &server, &client,
10508 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010509
Gilles Peskine449bd832023-01-11 14:50:10 +010010510 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10511 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010512
10513exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010514 psa_key_derivation_abort(&server_derive);
10515 psa_key_derivation_abort(&client_derive);
10516 psa_destroy_key(key);
10517 psa_pake_abort(&server);
10518 psa_pake_abort(&client);
10519 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010520}
10521/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010522
10523/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010524void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010525{
10526 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10527 const size_t bits = 256;
10528 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010529 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010530 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010531 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010532
10533 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10534 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010535 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10536 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10537 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10538 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010539 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010540 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10541 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010542
10543 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010544 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10545 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10546 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10547 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10548 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10549 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010550
10551 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010552 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10553 PSA_PAKE_OUTPUT_MAX_SIZE);
10554 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10555 PSA_PAKE_OUTPUT_MAX_SIZE);
10556 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10557 PSA_PAKE_OUTPUT_MAX_SIZE);
10558 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10559 PSA_PAKE_INPUT_MAX_SIZE);
10560 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10561 PSA_PAKE_INPUT_MAX_SIZE);
10562 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10563 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010564}
10565/* END_CASE */