blob: f88121fa5cd93231f745dc55f5585a51f6ad14f4 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
Gilles Peskine42649d92022-11-23 14:15:57 +01007#include "common.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008
Valerio Settibf999cb2023-12-28 17:48:13 +01009#include "mbedtls/psa_util.h"
10
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020011/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
12 * uses mbedtls_ctr_drbg internally. */
13#include "mbedtls/ctr_drbg.h"
14
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020015#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020016#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020017
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +010018/* For psa_can_do_hash() */
19#include "psa_crypto_core.h"
20
Gilles Peskine8e94efe2021-02-13 00:25:53 +010021#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010022#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010023#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053024#if defined(PSA_CRYPTO_DRIVER_TEST)
25#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053026#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
27#else
28#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053029#endif
Ronald Cron28a45ed2021-02-09 20:35:42 +010030
Gilles Peskine4023c012021-05-27 13:21:20 +020031/* If this comes up, it's a bug in the test code or in the test data. */
32#define UNUSED 0xdeadbeef
33
Dave Rodgman647791d2021-06-23 12:49:59 +010034/* Assert that an operation is (not) active.
35 * This serves as a proxy for checking if the operation is aborted. */
Gilles Peskine449bd832023-01-11 14:50:10 +010036#define ASSERT_OPERATION_IS_ACTIVE(operation) TEST_ASSERT(operation.id != 0)
37#define ASSERT_OPERATION_IS_INACTIVE(operation) TEST_ASSERT(operation.id == 0)
Gilles Peskinef426e0f2019-02-25 17:42:03 +010038
Przemek Stekiel7c795482022-11-15 22:26:12 +010039#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +010040int ecjpake_operation_setup(psa_pake_operation_t *operation,
41 psa_pake_cipher_suite_t *cipher_suite,
42 psa_pake_role_t role,
43 mbedtls_svc_key_id_t key,
44 size_t key_available)
Przemek Stekiel7c795482022-11-15 22:26:12 +010045{
Gilles Peskine449bd832023-01-11 14:50:10 +010046 PSA_ASSERT(psa_pake_abort(operation));
Przemek Stekiel7c795482022-11-15 22:26:12 +010047
Gilles Peskine449bd832023-01-11 14:50:10 +010048 PSA_ASSERT(psa_pake_setup(operation, cipher_suite));
Przemek Stekiel7c795482022-11-15 22:26:12 +010049
Gilles Peskine449bd832023-01-11 14:50:10 +010050 PSA_ASSERT(psa_pake_set_role(operation, role));
Przemek Stekiel7c795482022-11-15 22:26:12 +010051
Gilles Peskine449bd832023-01-11 14:50:10 +010052 if (key_available) {
53 PSA_ASSERT(psa_pake_set_password_key(operation, key));
54 }
Przemek Stekielf82effa2022-11-21 15:10:32 +010055 return 0;
Przemek Stekiel7c795482022-11-15 22:26:12 +010056exit:
Przemek Stekielf82effa2022-11-21 15:10:32 +010057 return 1;
Przemek Stekiel7c795482022-11-15 22:26:12 +010058}
59#endif
60
Jaeden Amerof24c7f82018-06-27 17:20:43 +010061/** An invalid export length that will never be set by psa_export_key(). */
62static const size_t INVALID_EXPORT_LENGTH = ~0U;
63
Gilles Peskinea7aa4422018-08-14 15:17:54 +020064/** Test if a buffer contains a constant byte value.
65 *
66 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020067 *
68 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020069 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020070 * \param size Size of the buffer in bytes.
71 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020072 * \return 1 if the buffer is all-bits-zero.
73 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020074 */
Gilles Peskine449bd832023-01-11 14:50:10 +010075static int mem_is_char(void *buffer, unsigned char c, size_t size)
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020076{
77 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010078 for (i = 0; i < size; i++) {
79 if (((unsigned char *) buffer)[i] != c) {
80 return 0;
81 }
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020082 }
Gilles Peskine449bd832023-01-11 14:50:10 +010083 return 1;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020084}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010085#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020086/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
Gilles Peskine449bd832023-01-11 14:50:10 +010087static int asn1_write_10x(unsigned char **p,
88 unsigned char *start,
89 size_t bits,
90 unsigned char x)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020091{
92 int ret;
93 int len = bits / 8 + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010094 if (bits == 0) {
95 return MBEDTLS_ERR_ASN1_INVALID_DATA;
96 }
97 if (bits <= 8 && x >= 1 << (bits - 1)) {
98 return MBEDTLS_ERR_ASN1_INVALID_DATA;
99 }
100 if (*p < start || *p - start < (ptrdiff_t) len) {
101 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
102 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200103 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 (*p)[len-1] = x;
105 if (bits % 8 == 0) {
106 (*p)[1] |= 1;
107 } else {
108 (*p)[0] |= 1 << (bits % 8);
109 }
110 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
111 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
112 MBEDTLS_ASN1_INTEGER));
113 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200114}
115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116static int construct_fake_rsa_key(unsigned char *buffer,
117 size_t buffer_size,
118 unsigned char **p,
119 size_t bits,
120 int keypair)
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200121{
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 size_t half_bits = (bits + 1) / 2;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200123 int ret;
124 int len = 0;
125 /* Construct something that looks like a DER encoding of
126 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
127 * RSAPrivateKey ::= SEQUENCE {
128 * version Version,
129 * modulus INTEGER, -- n
130 * publicExponent INTEGER, -- e
131 * privateExponent INTEGER, -- d
132 * prime1 INTEGER, -- p
133 * prime2 INTEGER, -- q
134 * exponent1 INTEGER, -- d mod (p-1)
135 * exponent2 INTEGER, -- d mod (q-1)
136 * coefficient INTEGER, -- (inverse of q) mod p
137 * otherPrimeInfos OtherPrimeInfos OPTIONAL
138 * }
139 * Or, for a public key, the same structure with only
140 * version, modulus and publicExponent.
141 */
142 *p = buffer + buffer_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 if (keypair) {
144 MBEDTLS_ASN1_CHK_ADD(len, /* pq */
145 asn1_write_10x(p, buffer, half_bits, 1));
146 MBEDTLS_ASN1_CHK_ADD(len, /* dq */
147 asn1_write_10x(p, buffer, half_bits, 1));
148 MBEDTLS_ASN1_CHK_ADD(len, /* dp */
149 asn1_write_10x(p, buffer, half_bits, 1));
150 MBEDTLS_ASN1_CHK_ADD(len, /* q */
151 asn1_write_10x(p, buffer, half_bits, 1));
152 MBEDTLS_ASN1_CHK_ADD(len, /* p != q to pass mbedtls sanity checks */
153 asn1_write_10x(p, buffer, half_bits, 3));
154 MBEDTLS_ASN1_CHK_ADD(len, /* d */
155 asn1_write_10x(p, buffer, bits, 1));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200156 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 MBEDTLS_ASN1_CHK_ADD(len, /* e = 65537 */
158 asn1_write_10x(p, buffer, 17, 1));
159 MBEDTLS_ASN1_CHK_ADD(len, /* n */
160 asn1_write_10x(p, buffer, bits, 1));
161 if (keypair) {
162 MBEDTLS_ASN1_CHK_ADD(len, /* version = 0 */
163 mbedtls_asn1_write_int(p, buffer, 0));
164 }
165 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, buffer, len));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200166 {
167 const unsigned char tag =
168 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, buffer, tag));
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200170 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 return len;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200172}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100173#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175int exercise_mac_setup(psa_key_type_t key_type,
176 const unsigned char *key_bytes,
177 size_t key_length,
178 psa_algorithm_t alg,
179 psa_mac_operation_t *operation,
180 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100181{
Ronald Cron5425a212020-08-04 14:58:35 +0200182 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
186 psa_set_key_algorithm(&attributes, alg);
187 psa_set_key_type(&attributes, key_type);
188 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 *status = psa_mac_sign_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100191 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 PSA_ASSERT(psa_mac_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100193 /* If setup failed, reproduce the failure, so that the caller can
194 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 if (*status != PSA_SUCCESS) {
196 TEST_EQUAL(psa_mac_sign_setup(operation, key, alg), *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197 }
198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 psa_destroy_key(key);
200 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100201
202exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 psa_destroy_key(key);
204 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100205}
206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207int exercise_cipher_setup(psa_key_type_t key_type,
208 const unsigned char *key_bytes,
209 size_t key_length,
210 psa_algorithm_t alg,
211 psa_cipher_operation_t *operation,
212 psa_status_t *status)
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100213{
Ronald Cron5425a212020-08-04 14:58:35 +0200214 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200215 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
218 psa_set_key_algorithm(&attributes, alg);
219 psa_set_key_type(&attributes, key_type);
220 PSA_ASSERT(psa_import_key(&attributes, key_bytes, key_length, &key));
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 *status = psa_cipher_encrypt_setup(operation, key, alg);
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100223 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 PSA_ASSERT(psa_cipher_abort(operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100225 /* If setup failed, reproduce the failure, so that the caller can
226 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 if (*status != PSA_SUCCESS) {
228 TEST_EQUAL(psa_cipher_encrypt_setup(operation, key, alg),
229 *status);
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100230 }
231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 psa_destroy_key(key);
233 return 1;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100234
235exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 psa_destroy_key(key);
237 return 0;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100238}
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240static int test_operations_on_invalid_key(mbedtls_svc_key_id_t key)
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200241{
242 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 0x6964);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200244 uint8_t buffer[1];
245 size_t length;
246 int ok = 0;
247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 psa_set_key_id(&attributes, key_id);
249 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
250 psa_set_key_algorithm(&attributes, PSA_ALG_CTR);
251 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
252 TEST_EQUAL(psa_get_key_attributes(key, &attributes),
253 PSA_ERROR_INVALID_HANDLE);
Ronald Cronecfb2372020-07-23 17:13:42 +0200254 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
Ronald Cronecfb2372020-07-23 17:13:42 +0200256 TEST_EQUAL(
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
258 TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
259 TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
260 TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
261 TEST_EQUAL(psa_get_key_type(&attributes), 0);
262 TEST_EQUAL(psa_get_key_bits(&attributes), 0);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 TEST_EQUAL(psa_export_key(key, buffer, sizeof(buffer), &length),
265 PSA_ERROR_INVALID_HANDLE);
266 TEST_EQUAL(psa_export_public_key(key,
267 buffer, sizeof(buffer), &length),
268 PSA_ERROR_INVALID_HANDLE);
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200269
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200270 ok = 1;
271
272exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100273 /*
274 * Key attributes may have been returned by psa_get_key_attributes()
275 * thus reset them as required.
276 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return ok;
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200280}
281
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200282/* Assert that a key isn't reported as having a slot number. */
283#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100284#define ASSERT_NO_SLOT_NUMBER(attributes) \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200285 do \
286 { \
287 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 TEST_EQUAL(psa_get_key_slot_number( \
289 attributes, \
290 &ASSERT_NO_SLOT_NUMBER_slot_number), \
291 PSA_ERROR_INVALID_ARGUMENT); \
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200292 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 while (0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200294#else /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100295#define ASSERT_NO_SLOT_NUMBER(attributes) \
296 ((void) 0)
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200297#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
298
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +0530299#define INPUT_INTEGER 0x10000 /* Out of range of psa_key_type_t */
300
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100301/* An overapproximation of the amount of storage needed for a key of the
302 * given type and with the given content. The API doesn't make it easy
303 * to find a good value for the size. The current implementation doesn't
304 * care about the value anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100305#define KEY_BITS_FROM_DATA(type, data) \
306 (data)->len
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100307
Darryl Green0c6575a2018-11-07 16:05:30 +0000308typedef enum {
309 IMPORT_KEY = 0,
310 GENERATE_KEY = 1,
311 DERIVE_KEY = 2
312} generate_method;
313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314typedef enum {
Paul Elliott33746aa2021-09-15 16:40:40 +0100315 DO_NOT_SET_LENGTHS = 0,
316 SET_LENGTHS_BEFORE_NONCE = 1,
317 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100318} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320typedef enum {
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100321 USE_NULL_TAG = 0,
322 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100323} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100324
Kusumit Ghoderaoa14ae5a2023-04-19 14:16:26 +0530325
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100326/*!
327 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100328 * \param key_type_arg Type of key passed in
329 * \param key_data The encryption / decryption key data
330 * \param alg_arg The type of algorithm used
331 * \param nonce Nonce data
332 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100333 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100334 * feed additional data in to be encrypted /
335 * decrypted. If -1, no chunking.
336 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100337 * \param data_part_len_arg If not -1, the length of chunks to feed
338 * the data in to be encrypted / decrypted. If
339 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100340 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100341 * expected here, this controls whether or not
342 * to set lengths, and in what order with
343 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100344 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100345 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100346 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100347 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100348 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100349 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350static int aead_multipart_internal_func(int key_type_arg, data_t *key_data,
351 int alg_arg,
352 data_t *nonce,
353 data_t *additional_data,
354 int ad_part_len_arg,
355 data_t *input_data,
356 int data_part_len_arg,
357 set_lengths_method_t set_lengths_method,
358 data_t *expected_output,
359 int is_encrypt,
360 int do_zero_parts)
Paul Elliottd3f82412021-06-16 16:52:21 +0100361{
362 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
363 psa_key_type_t key_type = key_type_arg;
364 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100365 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100366 unsigned char *output_data = NULL;
367 unsigned char *part_data = NULL;
368 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100369 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100370 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100371 size_t output_size = 0;
372 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100373 size_t output_length = 0;
374 size_t key_bits = 0;
375 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100376 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100377 size_t part_length = 0;
378 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100379 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100380 size_t ad_part_len = 0;
381 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100382 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100383 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
384 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
385
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100386 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100387 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 PSA_ASSERT(psa_crypto_init());
Paul Elliottd3f82412021-06-16 16:52:21 +0100390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (is_encrypt) {
392 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
393 } else {
394 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100395 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100396
397 psa_set_key_algorithm(&attributes, alg);
398 psa_set_key_type(&attributes, key_type);
399
400 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
401 &key));
402
403 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
404 key_bits = psa_get_key_bits(&attributes);
405
406 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
407
408 if (is_encrypt) {
409 /* Tag gets written at end of buffer. */
410 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
411 (input_data->len +
412 tag_length));
413 data_true_size = input_data->len;
414 } else {
415 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
416 (input_data->len -
417 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100418
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100419 /* Do not want to attempt to decrypt tag. */
420 data_true_size = input_data->len - tag_length;
421 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100422
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100423 TEST_CALLOC(output_data, output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (is_encrypt) {
426 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
427 TEST_LE_U(final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
428 } else {
429 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
430 TEST_LE_U(final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100431 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100432
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100433 TEST_CALLOC(final_data, final_output_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if (is_encrypt) {
436 status = psa_aead_encrypt_setup(&operation, key, alg);
437 } else {
438 status = psa_aead_decrypt_setup(&operation, key, alg);
439 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100440
441 /* If the operation is not supported, just skip and not fail in case the
442 * encryption involves a common limitation of cryptography hardwares and
443 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if (status == PSA_ERROR_NOT_SUPPORTED) {
445 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
446 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100447 }
448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 PSA_ASSERT(status);
Paul Elliottd3f82412021-06-16 16:52:21 +0100450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if (set_lengths_method == DO_NOT_SET_LENGTHS) {
452 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
453 } else if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
454 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
455 data_true_size));
456 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
457 } else if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
458 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottebf91632021-07-22 17:54:42 +0100459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
461 data_true_size));
Paul Elliottd3f82412021-06-16 16:52:21 +0100462 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if (ad_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100465 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100466 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100469 part_offset < additional_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 part_offset += part_length, part_count++) {
471 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100472 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 } else if (additional_data->len - part_offset < ad_part_len) {
Paul Elliotte49fe452021-09-15 16:52:11 +0100474 part_length = additional_data->len - part_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100476 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100477 }
478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 PSA_ASSERT(psa_aead_update_ad(&operation,
480 additional_data->x + part_offset,
481 part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100482
Paul Elliottd3f82412021-06-16 16:52:21 +0100483 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 } else {
Paul Elliottd3f82412021-06-16 16:52:21 +0100485 /* Pass additional data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
487 additional_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 }
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if (data_part_len_arg != -1) {
Paul Elliottd3f82412021-06-16 16:52:21 +0100491 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 data_part_len = (size_t) data_part_len_arg;
493 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
494 (size_t) data_part_len);
Paul Elliottd3f82412021-06-16 16:52:21 +0100495
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100496 TEST_CALLOC(part_data, part_data_size);
Paul Elliottd3f82412021-06-16 16:52:21 +0100497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 for (part_offset = 0, part_count = 0;
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100499 part_offset < data_true_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 part_offset += part_length, part_count++) {
501 if (do_zero_parts && (part_count & 0x01)) {
Paul Elliott329d5382021-07-22 17:10:45 +0100502 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 } else if ((data_true_size - part_offset) < data_part_len) {
504 part_length = (data_true_size - part_offset);
505 } else {
Paul Elliotte49fe452021-09-15 16:52:11 +0100506 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100507 }
508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 PSA_ASSERT(psa_aead_update(&operation,
510 (input_data->x + part_offset),
511 part_length, part_data,
512 part_data_size,
513 &output_part_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 if (output_data && output_part_length) {
516 memcpy((output_data + output_length), part_data,
517 output_part_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100518 }
519
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 output_length += output_part_length;
521 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 } else {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100523 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
525 data_true_size, output_data,
526 output_size, &output_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100527 }
528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 if (is_encrypt) {
530 PSA_ASSERT(psa_aead_finish(&operation, final_data,
531 final_output_size,
532 &output_part_length,
533 tag_buffer, tag_length,
534 &tag_size));
535 } else {
536 PSA_ASSERT(psa_aead_verify(&operation, final_data,
537 final_output_size,
538 &output_part_length,
539 (input_data->x + data_true_size),
540 tag_length));
Paul Elliottd3f82412021-06-16 16:52:21 +0100541 }
542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 if (output_data && output_part_length) {
544 memcpy((output_data + output_length), final_data,
545 output_part_length);
546 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100547
548 output_length += output_part_length;
549
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100550
551 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
552 * should be exact.*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 if (is_encrypt) {
554 TEST_EQUAL(tag_length, tag_size);
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (output_data && tag_length) {
557 memcpy((output_data + output_length), tag_buffer,
558 tag_length);
559 }
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100560
561 output_length += tag_length;
562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 TEST_EQUAL(output_length,
564 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg,
565 input_data->len));
566 TEST_LE_U(output_length,
567 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
568 } else {
569 TEST_EQUAL(output_length,
570 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg,
571 input_data->len));
572 TEST_LE_U(output_length,
573 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Paul Elliottd3f82412021-06-16 16:52:21 +0100574 }
575
Paul Elliottd3f82412021-06-16 16:52:21 +0100576
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100577 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100578 output_data, output_length);
Paul Elliottd3f82412021-06-16 16:52:21 +0100579
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100581 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100582
583exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 psa_destroy_key(key);
585 psa_aead_abort(&operation);
586 mbedtls_free(output_data);
587 mbedtls_free(part_data);
588 mbedtls_free(final_data);
589 PSA_DONE();
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 return test_ok;
Paul Elliottd3f82412021-06-16 16:52:21 +0100592}
593
Neil Armstrong4766f992022-02-28 16:23:59 +0100594/*!
595 * \brief Internal Function for MAC multipart tests.
596 * \param key_type_arg Type of key passed in
597 * \param key_data The encryption / decryption key data
598 * \param alg_arg The type of algorithm used
599 * \param input_data Data to encrypt / decrypt
600 * \param data_part_len_arg If not -1, the length of chunks to feed
601 * the data in to be encrypted / decrypted. If
602 * -1, no chunking
603 * \param expected_output Expected output
Tom Cosgrove1797b052022-12-04 17:19:59 +0000604 * \param is_verify If non-zero this is a verify operation.
Neil Armstrong4766f992022-02-28 16:23:59 +0100605 * \param do_zero_parts If non-zero, interleave zero length chunks
606 * with normal length chunks.
607 * \return int Zero on failure, non-zero on success.
608 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100609static int mac_multipart_internal_func(int key_type_arg, data_t *key_data,
610 int alg_arg,
611 data_t *input_data,
612 int data_part_len_arg,
613 data_t *expected_output,
614 int is_verify,
615 int do_zero_parts)
Neil Armstrong4766f992022-02-28 16:23:59 +0100616{
617 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
618 psa_key_type_t key_type = key_type_arg;
619 psa_algorithm_t alg = alg_arg;
620 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
621 unsigned char mac[PSA_MAC_MAX_SIZE];
622 size_t part_offset = 0;
623 size_t part_length = 0;
624 size_t data_part_len = 0;
625 size_t mac_len = 0;
626 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
627 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
628
629 int test_ok = 0;
630 size_t part_count = 0;
631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 PSA_INIT();
Neil Armstrong4766f992022-02-28 16:23:59 +0100633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (is_verify) {
635 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
636 } else {
637 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
638 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100639
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 psa_set_key_algorithm(&attributes, alg);
641 psa_set_key_type(&attributes, key_type);
Neil Armstrong4766f992022-02-28 16:23:59 +0100642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
644 &key));
Neil Armstrong4766f992022-02-28 16:23:59 +0100645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if (is_verify) {
647 status = psa_mac_verify_setup(&operation, key, alg);
648 } else {
649 status = psa_mac_sign_setup(&operation, key, alg);
650 }
Neil Armstrong4766f992022-02-28 16:23:59 +0100651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 PSA_ASSERT(status);
Neil Armstrong4766f992022-02-28 16:23:59 +0100653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (data_part_len_arg != -1) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100655 /* Pass data in parts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 data_part_len = (size_t) data_part_len_arg;
Neil Armstrong4766f992022-02-28 16:23:59 +0100657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 for (part_offset = 0, part_count = 0;
Neil Armstrong4766f992022-02-28 16:23:59 +0100659 part_offset < input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 part_offset += part_length, part_count++) {
661 if (do_zero_parts && (part_count & 0x01)) {
Neil Armstrong4766f992022-02-28 16:23:59 +0100662 part_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 } else if ((input_data->len - part_offset) < data_part_len) {
664 part_length = (input_data->len - part_offset);
665 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100666 part_length = data_part_len;
667 }
668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 PSA_ASSERT(psa_mac_update(&operation,
670 (input_data->x + part_offset),
671 part_length));
Neil Armstrong4766f992022-02-28 16:23:59 +0100672 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 } else {
Neil Armstrong4766f992022-02-28 16:23:59 +0100674 /* Pass all data in one go. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 PSA_ASSERT(psa_mac_update(&operation, input_data->x,
676 input_data->len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100677 }
678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (is_verify) {
680 PSA_ASSERT(psa_mac_verify_finish(&operation, expected_output->x,
681 expected_output->len));
682 } else {
683 PSA_ASSERT(psa_mac_sign_finish(&operation, mac,
684 PSA_MAC_MAX_SIZE, &mac_len));
Neil Armstrong4766f992022-02-28 16:23:59 +0100685
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100686 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +0100687 mac, mac_len);
Neil Armstrong4766f992022-02-28 16:23:59 +0100688 }
689
690 test_ok = 1;
691
692exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 psa_destroy_key(key);
694 psa_mac_abort(&operation);
695 PSA_DONE();
Neil Armstrong4766f992022-02-28 16:23:59 +0100696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 return test_ok;
Neil Armstrong4766f992022-02-28 16:23:59 +0100698}
699
Neil Armstrong75673ab2022-06-15 17:39:01 +0200700#if defined(PSA_WANT_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100701static void ecjpake_do_round(psa_algorithm_t alg, unsigned int primitive,
702 psa_pake_operation_t *server,
703 psa_pake_operation_t *client,
704 int client_input_first,
705 int round, int inject_error)
Neil Armstrongf983caf2022-06-15 15:27:48 +0200706{
707 unsigned char *buffer0 = NULL, *buffer1 = NULL;
708 size_t buffer_length = (
709 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
710 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
711 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200712 /* The output should be exactly this size according to the spec */
713 const size_t expected_size_key_share =
714 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
715 /* The output should be exactly this size according to the spec */
716 const size_t expected_size_zk_public =
717 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
718 /* The output can be smaller: the spec allows stripping leading zeroes */
719 const size_t max_expected_size_zk_proof =
720 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200721 size_t buffer0_off = 0;
722 size_t buffer1_off = 0;
723 size_t s_g1_len, s_g2_len, s_a_len;
724 size_t s_g1_off, s_g2_off, s_a_off;
725 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
726 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
727 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
728 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
729 size_t c_g1_len, c_g2_len, c_a_len;
730 size_t c_g1_off, c_g2_off, c_a_off;
731 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
732 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
733 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
734 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
735 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200736 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200737
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100738 TEST_CALLOC(buffer0, buffer_length);
739 TEST_CALLOC(buffer1, buffer_length);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 switch (round) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200742 case 1:
743 /* Server first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
745 buffer0 + buffer0_off,
746 512 - buffer0_off, &s_g1_len));
747 TEST_EQUAL(s_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200748 s_g1_off = buffer0_off;
749 buffer0_off += s_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
751 buffer0 + buffer0_off,
752 512 - buffer0_off, &s_x1_pk_len));
753 TEST_EQUAL(s_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200754 s_x1_pk_off = buffer0_off;
755 buffer0_off += s_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
757 buffer0 + buffer0_off,
758 512 - buffer0_off, &s_x1_pr_len));
759 TEST_LE_U(s_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200760 s_x1_pr_off = buffer0_off;
761 buffer0_off += s_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
763 buffer0 + buffer0_off,
764 512 - buffer0_off, &s_g2_len));
765 TEST_EQUAL(s_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200766 s_g2_off = buffer0_off;
767 buffer0_off += s_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
769 buffer0 + buffer0_off,
770 512 - buffer0_off, &s_x2_pk_len));
771 TEST_EQUAL(s_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200772 s_x2_pk_off = buffer0_off;
773 buffer0_off += s_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
775 buffer0 + buffer0_off,
776 512 - buffer0_off, &s_x2_pr_len));
777 TEST_LE_U(s_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200778 s_x2_pr_off = buffer0_off;
779 buffer0_off += s_x2_pr_len;
780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if (inject_error == 1) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500782 buffer0[s_x1_pr_off + 8] ^= 1;
783 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200784 expected_status = PSA_ERROR_DATA_INVALID;
785 }
786
Neil Armstrong51009d72022-09-05 17:59:54 +0200787 /*
788 * When injecting errors in inputs, the implementation is
789 * free to detect it right away of with a delay.
790 * This permits delaying the error until the end of the input
791 * sequence, if no error appears then, this will be treated
792 * as an error.
793 */
794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200796 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
798 buffer0 + s_g1_off, s_g1_len);
799 if (inject_error == 1 && status != PSA_SUCCESS) {
800 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200801 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 } else {
803 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200804 }
805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
807 buffer0 + s_x1_pk_off,
808 s_x1_pk_len);
809 if (inject_error == 1 && status != PSA_SUCCESS) {
810 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200811 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 } else {
813 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200814 }
815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
817 buffer0 + s_x1_pr_off,
818 s_x1_pr_len);
819 if (inject_error == 1 && status != PSA_SUCCESS) {
820 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200821 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 } else {
823 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200824 }
825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
827 buffer0 + s_g2_off,
828 s_g2_len);
829 if (inject_error == 1 && status != PSA_SUCCESS) {
830 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200831 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 } else {
833 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200834 }
835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
837 buffer0 + s_x2_pk_off,
838 s_x2_pk_len);
839 if (inject_error == 1 && status != PSA_SUCCESS) {
840 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200841 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 } else {
843 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200844 }
845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
847 buffer0 + s_x2_pr_off,
848 s_x2_pr_len);
849 if (inject_error == 1 && status != PSA_SUCCESS) {
850 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200851 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 } else {
853 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200854 }
855
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200856 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 if (inject_error == 1) {
858 TEST_ASSERT(
859 !"One of the last psa_pake_input() calls should have returned the expected error.");
860 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200861 }
862
863 /* Client first round Output */
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
865 buffer1 + buffer1_off,
866 512 - buffer1_off, &c_g1_len));
867 TEST_EQUAL(c_g1_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200868 c_g1_off = buffer1_off;
869 buffer1_off += c_g1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
871 buffer1 + buffer1_off,
872 512 - buffer1_off, &c_x1_pk_len));
873 TEST_EQUAL(c_x1_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200874 c_x1_pk_off = buffer1_off;
875 buffer1_off += c_x1_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
877 buffer1 + buffer1_off,
878 512 - buffer1_off, &c_x1_pr_len));
879 TEST_LE_U(c_x1_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200880 c_x1_pr_off = buffer1_off;
881 buffer1_off += c_x1_pr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
883 buffer1 + buffer1_off,
884 512 - buffer1_off, &c_g2_len));
885 TEST_EQUAL(c_g2_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200886 c_g2_off = buffer1_off;
887 buffer1_off += c_g2_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
889 buffer1 + buffer1_off,
890 512 - buffer1_off, &c_x2_pk_len));
891 TEST_EQUAL(c_x2_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200892 c_x2_pk_off = buffer1_off;
893 buffer1_off += c_x2_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
895 buffer1 + buffer1_off,
896 512 - buffer1_off, &c_x2_pr_len));
897 TEST_LE_U(c_x2_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200898 c_x2_pr_off = buffer1_off;
899 buffer1_off += c_x2_pr_len;
900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +0200902 /* Client first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
904 buffer0 + s_g1_off, s_g1_len);
905 if (inject_error == 1 && status != PSA_SUCCESS) {
906 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200907 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 } else {
909 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200910 }
911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
913 buffer0 + s_x1_pk_off,
914 s_x1_pk_len);
915 if (inject_error == 1 && status != PSA_SUCCESS) {
916 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200917 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 } else {
919 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200920 }
921
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
923 buffer0 + s_x1_pr_off,
924 s_x1_pr_len);
925 if (inject_error == 1 && status != PSA_SUCCESS) {
926 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200927 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 } else {
929 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200930 }
931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
933 buffer0 + s_g2_off,
934 s_g2_len);
935 if (inject_error == 1 && status != PSA_SUCCESS) {
936 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200937 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 } else {
939 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200940 }
941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
943 buffer0 + s_x2_pk_off,
944 s_x2_pk_len);
945 if (inject_error == 1 && status != PSA_SUCCESS) {
946 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200947 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 } else {
949 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200950 }
951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
953 buffer0 + s_x2_pr_off,
954 s_x2_pr_len);
955 if (inject_error == 1 && status != PSA_SUCCESS) {
956 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200957 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 } else {
959 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200960 }
961
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200962 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 if (inject_error == 1) {
964 TEST_ASSERT(
965 !"One of the last psa_pake_input() calls should have returned the expected error.");
966 }
Neil Armstrongf983caf2022-06-15 15:27:48 +0200967 }
968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if (inject_error == 2) {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500970 buffer1[c_x1_pr_off + 12] ^= 1;
971 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200972 expected_status = PSA_ERROR_DATA_INVALID;
973 }
974
975 /* Server first round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
977 buffer1 + c_g1_off, c_g1_len);
978 if (inject_error == 2 && status != PSA_SUCCESS) {
979 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200980 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 } else {
982 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200983 }
984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
986 buffer1 + c_x1_pk_off, c_x1_pk_len);
987 if (inject_error == 2 && status != PSA_SUCCESS) {
988 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200989 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 } else {
991 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200992 }
993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
995 buffer1 + c_x1_pr_off, c_x1_pr_len);
996 if (inject_error == 2 && status != PSA_SUCCESS) {
997 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200998 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 } else {
1000 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001001 }
1002
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1004 buffer1 + c_g2_off, c_g2_len);
1005 if (inject_error == 2 && status != PSA_SUCCESS) {
1006 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001007 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 } else {
1009 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001010 }
1011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1013 buffer1 + c_x2_pk_off, c_x2_pk_len);
1014 if (inject_error == 2 && status != PSA_SUCCESS) {
1015 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001016 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 } else {
1018 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001019 }
1020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1022 buffer1 + c_x2_pr_off, c_x2_pr_len);
1023 if (inject_error == 2 && status != PSA_SUCCESS) {
1024 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001025 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 } else {
1027 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001028 }
1029
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001030 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 if (inject_error == 2) {
1032 TEST_ASSERT(
1033 !"One of the last psa_pake_input() calls should have returned the expected error.");
1034 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001035
1036 break;
1037
1038 case 2:
1039 /* Server second round Output */
1040 buffer0_off = 0;
1041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_KEY_SHARE,
1043 buffer0 + buffer0_off,
1044 512 - buffer0_off, &s_a_len));
1045 TEST_EQUAL(s_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001046 s_a_off = buffer0_off;
1047 buffer0_off += s_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PUBLIC,
1049 buffer0 + buffer0_off,
1050 512 - buffer0_off, &s_x2s_pk_len));
1051 TEST_EQUAL(s_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001052 s_x2s_pk_off = buffer0_off;
1053 buffer0_off += s_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 PSA_ASSERT(psa_pake_output(server, PSA_PAKE_STEP_ZK_PROOF,
1055 buffer0 + buffer0_off,
1056 512 - buffer0_off, &s_x2s_pr_len));
1057 TEST_LE_U(s_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001058 s_x2s_pr_off = buffer0_off;
1059 buffer0_off += s_x2s_pr_len;
1060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if (inject_error == 3) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001062 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001063 expected_status = PSA_ERROR_DATA_INVALID;
1064 }
1065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 if (client_input_first == 1) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001067 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1069 buffer0 + s_a_off, s_a_len);
1070 if (inject_error == 3 && status != PSA_SUCCESS) {
1071 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001072 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 } else {
1074 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001075 }
1076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1078 buffer0 + s_x2s_pk_off,
1079 s_x2s_pk_len);
1080 if (inject_error == 3 && status != PSA_SUCCESS) {
1081 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001082 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 } else {
1084 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001085 }
1086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1088 buffer0 + s_x2s_pr_off,
1089 s_x2s_pr_len);
1090 if (inject_error == 3 && status != PSA_SUCCESS) {
1091 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001092 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 } else {
1094 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001095 }
1096
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001097 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 if (inject_error == 3) {
1099 TEST_ASSERT(
1100 !"One of the last psa_pake_input() calls should have returned the expected error.");
1101 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001102 }
1103
1104 /* Client second round Output */
1105 buffer1_off = 0;
1106
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_KEY_SHARE,
1108 buffer1 + buffer1_off,
1109 512 - buffer1_off, &c_a_len));
1110 TEST_EQUAL(c_a_len, expected_size_key_share);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001111 c_a_off = buffer1_off;
1112 buffer1_off += c_a_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PUBLIC,
1114 buffer1 + buffer1_off,
1115 512 - buffer1_off, &c_x2s_pk_len));
1116 TEST_EQUAL(c_x2s_pk_len, expected_size_zk_public);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001117 c_x2s_pk_off = buffer1_off;
1118 buffer1_off += c_x2s_pk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 PSA_ASSERT(psa_pake_output(client, PSA_PAKE_STEP_ZK_PROOF,
1120 buffer1 + buffer1_off,
1121 512 - buffer1_off, &c_x2s_pr_len));
1122 TEST_LE_U(c_x2s_pr_len, max_expected_size_zk_proof);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001123 c_x2s_pr_off = buffer1_off;
1124 buffer1_off += c_x2s_pr_len;
1125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 if (client_input_first == 0) {
Neil Armstrongf983caf2022-06-15 15:27:48 +02001127 /* Client second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 status = psa_pake_input(client, PSA_PAKE_STEP_KEY_SHARE,
1129 buffer0 + s_a_off, s_a_len);
1130 if (inject_error == 3 && status != PSA_SUCCESS) {
1131 TEST_EQUAL(status, expected_status);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001132 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 } else {
1134 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001135 }
1136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PUBLIC,
1138 buffer0 + s_x2s_pk_off,
1139 s_x2s_pk_len);
1140 if (inject_error == 3 && status != PSA_SUCCESS) {
1141 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001142 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 } else {
1144 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001145 }
1146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 status = psa_pake_input(client, PSA_PAKE_STEP_ZK_PROOF,
1148 buffer0 + s_x2s_pr_off,
1149 s_x2s_pr_len);
1150 if (inject_error == 3 && status != PSA_SUCCESS) {
1151 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001152 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 } else {
1154 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001155 }
1156
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001157 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 if (inject_error == 3) {
1159 TEST_ASSERT(
1160 !"One of the last psa_pake_input() calls should have returned the expected error.");
1161 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001162 }
1163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 if (inject_error == 4) {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001165 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001166 expected_status = PSA_ERROR_DATA_INVALID;
1167 }
1168
1169 /* Server second round Input */
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 status = psa_pake_input(server, PSA_PAKE_STEP_KEY_SHARE,
1171 buffer1 + c_a_off, c_a_len);
1172 if (inject_error == 4 && status != PSA_SUCCESS) {
1173 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001174 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 } else {
1176 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001177 }
1178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PUBLIC,
1180 buffer1 + c_x2s_pk_off, c_x2s_pk_len);
1181 if (inject_error == 4 && status != PSA_SUCCESS) {
1182 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001183 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 } else {
1185 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001186 }
1187
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 status = psa_pake_input(server, PSA_PAKE_STEP_ZK_PROOF,
1189 buffer1 + c_x2s_pr_off, c_x2s_pr_len);
1190 if (inject_error == 4 && status != PSA_SUCCESS) {
1191 TEST_EQUAL(status, expected_status);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001192 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 } else {
1194 TEST_EQUAL(status, PSA_SUCCESS);
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001195 }
1196
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001197 /* Error didn't trigger, make test fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 if (inject_error == 4) {
1199 TEST_ASSERT(
1200 !"One of the last psa_pake_input() calls should have returned the expected error.");
1201 }
Neil Armstrongf983caf2022-06-15 15:27:48 +02001202
1203 break;
1204
1205 }
1206
Neil Armstrongf983caf2022-06-15 15:27:48 +02001207exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 mbedtls_free(buffer0);
1209 mbedtls_free(buffer1);
Neil Armstrongf983caf2022-06-15 15:27:48 +02001210}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001211#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213typedef enum {
Valerio Setti1070aed2022-11-11 19:37:31 +01001214 INJECT_ERR_NONE = 0,
1215 INJECT_ERR_UNINITIALIZED_ACCESS,
1216 INJECT_ERR_DUPLICATE_SETUP,
1217 INJECT_ERR_INVALID_USER,
1218 INJECT_ERR_INVALID_PEER,
1219 INJECT_ERR_SET_USER,
1220 INJECT_ERR_SET_PEER,
1221 INJECT_EMPTY_IO_BUFFER,
1222 INJECT_UNKNOWN_STEP,
1223 INJECT_INVALID_FIRST_STEP,
1224 INJECT_WRONG_BUFFER_SIZE,
1225 INJECT_VALID_OPERATION_AFTER_FAILURE,
1226 INJECT_ANTICIPATE_KEY_DERIVATION_1,
1227 INJECT_ANTICIPATE_KEY_DERIVATION_2,
1228} ecjpake_injected_failure_t;
1229
Paul Elliott01885fa2023-02-09 12:07:30 +00001230#if defined(MBEDTLS_ECP_RESTARTABLE)
Paul Elliott1243f932023-02-07 11:21:10 +00001231
Paul Elliott6f600372023-02-06 18:41:05 +00001232static void interruptible_signverify_get_minmax_completes(uint32_t max_ops,
1233 psa_status_t expected_status,
1234 size_t *min_completes,
1235 size_t *max_completes)
1236{
1237
1238 /* This is slightly contrived, but we only really know that with a minimum
1239 value of max_ops that a successful operation should take more than one op
1240 to complete, and likewise that with a max_ops of
1241 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, it should complete in one go. */
1242 if (max_ops == 0 || max_ops == 1) {
Paul Elliottc86d45e2023-02-15 17:38:05 +00001243
Paul Elliott6f600372023-02-06 18:41:05 +00001244 if (expected_status == PSA_SUCCESS) {
1245 *min_completes = 2;
1246 } else {
1247 *min_completes = 1;
1248 }
1249
1250 *max_completes = PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED;
1251 } else {
1252 *min_completes = 1;
1253 *max_completes = 1;
1254 }
1255}
Paul Elliott01885fa2023-02-09 12:07:30 +00001256#endif /* MBEDTLS_ECP_RESTARTABLE */
Paul Elliott6f600372023-02-06 18:41:05 +00001257
Gilles Peskine1d25a0a2024-02-12 16:40:04 +01001258#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
1259static int rsa_test_e(mbedtls_svc_key_id_t key,
1260 size_t bits,
1261 const data_t *e_arg)
1262{
1263 uint8_t *exported = NULL;
1264 size_t exported_size =
1265 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
1266 size_t exported_length = SIZE_MAX;
1267 int ok = 0;
1268
1269 TEST_CALLOC(exported, exported_size);
1270 PSA_ASSERT(psa_export_public_key(key,
1271 exported, exported_size,
1272 &exported_length));
1273 uint8_t *p = exported;
1274 uint8_t *end = exported + exported_length;
1275 size_t len;
1276 /* RSAPublicKey ::= SEQUENCE {
1277 * modulus INTEGER, -- n
1278 * publicExponent INTEGER } -- e
1279 */
1280 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
1281 MBEDTLS_ASN1_SEQUENCE |
1282 MBEDTLS_ASN1_CONSTRUCTED));
1283 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
1284 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
1285 MBEDTLS_ASN1_INTEGER));
1286 if (len >= 1 && p[0] == 0) {
1287 ++p;
1288 --len;
1289 }
1290 if (e_arg->len == 0) {
1291 TEST_EQUAL(len, 3);
1292 TEST_EQUAL(p[0], 1);
1293 TEST_EQUAL(p[1], 0);
1294 TEST_EQUAL(p[2], 1);
1295 } else {
1296 TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len);
1297 }
1298 ok = 1;
1299
1300exit:
1301 mbedtls_free(exported);
1302 return ok;
1303}
1304#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_GENERATE */
1305
Gilles Peskinee59236f2018-01-27 23:32:46 +01001306/* END_HEADER */
1307
1308/* BEGIN_DEPENDENCIES
1309 * depends_on:MBEDTLS_PSA_CRYPTO_C
1310 * END_DEPENDENCIES
1311 */
1312
1313/* BEGIN_CASE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01001314void psa_can_do_hash()
1315{
1316 /* We can't test that this is specific to drivers until partial init has
1317 * been implemented, but we can at least test before/after full init. */
1318 TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE));
1319 PSA_INIT();
1320 TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE));
1321 PSA_DONE();
1322}
1323/* END_CASE */
1324
1325/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001326void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001327{
1328 size_t max_truncated_mac_size =
1329 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1330
1331 /* Check that the length for a truncated MAC always fits in the algorithm
1332 * encoding. The shifted mask is the maximum truncated value. The
1333 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001335}
1336/* END_CASE */
1337
1338/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001339void import_with_policy(int type_arg,
1340 int usage_arg, int alg_arg,
1341 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001342{
1343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1344 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001345 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001346 psa_key_type_t type = type_arg;
1347 psa_key_usage_t usage = usage_arg;
1348 psa_algorithm_t alg = alg_arg;
1349 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001351 psa_status_t status;
1352
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 psa_set_key_type(&attributes, type);
1356 psa_set_key_usage_flags(&attributes, usage);
1357 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 status = psa_import_key(&attributes,
1360 key_material, sizeof(key_material),
1361 &key);
1362 TEST_EQUAL(status, expected_status);
1363 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001364 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001366
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1368 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1369 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1370 mbedtls_test_update_key_usage_flags(usage));
1371 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1372 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001373
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 PSA_ASSERT(psa_destroy_key(key));
1375 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001376
1377exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001378 /*
1379 * Key attributes may have been returned by psa_get_key_attributes()
1380 * thus reset them as required.
1381 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 psa_destroy_key(key);
1385 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001386}
1387/* END_CASE */
1388
1389/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001390void import_with_data(data_t *data, int type_arg,
1391 int attr_bits_arg,
1392 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001393{
1394 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1395 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001396 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001397 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001398 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001399 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001400 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001401
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001403
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 psa_set_key_type(&attributes, type);
1405 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 status = psa_import_key(&attributes, data->x, data->len, &key);
Manuel Pégourié-Gonnard0509b582023-08-08 12:47:56 +02001408 /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED.
1409 *
1410 * This can happen with a type supported only by a driver:
1411 * - the driver sees the invalid data (for example wrong size) and thinks
1412 * "well perhaps this is a key size I don't support" so it returns
1413 * NOT_SUPPORTED which is correct at this point;
1414 * - we fallback to built-ins, which don't support this type, so return
1415 * NOT_SUPPORTED which again is correct at this point.
1416 */
1417 if (expected_status == PSA_ERROR_INVALID_ARGUMENT &&
1418 status == PSA_ERROR_NOT_SUPPORTED) {
1419 ; // OK
1420 } else {
1421 TEST_EQUAL(status, expected_status);
1422 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001424 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1428 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1429 if (attr_bits != 0) {
1430 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1431 }
1432 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 PSA_ASSERT(psa_destroy_key(key));
1435 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001436
1437exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001438 /*
1439 * Key attributes may have been returned by psa_get_key_attributes()
1440 * thus reset them as required.
1441 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001443
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 psa_destroy_key(key);
1445 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001446}
1447/* END_CASE */
1448
1449/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001450/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001451void import_large_key(int type_arg, int byte_size_arg,
1452 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001453{
1454 psa_key_type_t type = type_arg;
1455 size_t byte_size = byte_size_arg;
1456 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1457 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001458 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001459 psa_status_t status;
1460 uint8_t *buffer = NULL;
1461 size_t buffer_size = byte_size + 1;
1462 size_t n;
1463
Steven Cooreman69967ce2021-01-18 18:01:08 +01001464 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001465 * accommodate large keys due to heap size constraints */
Tom Cosgrove412a8132023-07-20 16:55:14 +01001466 TEST_CALLOC_OR_SKIP(buffer, buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001470
1471 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1473 psa_set_key_type(&attributes, type);
1474 status = psa_import_key(&attributes, buffer, byte_size, &key);
1475 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1476 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001477
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 if (status == PSA_SUCCESS) {
1479 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1480 TEST_EQUAL(psa_get_key_type(&attributes), type);
1481 TEST_EQUAL(psa_get_key_bits(&attributes),
1482 PSA_BYTES_TO_BITS(byte_size));
1483 ASSERT_NO_SLOT_NUMBER(&attributes);
1484 memset(buffer, 0, byte_size + 1);
1485 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1486 for (n = 0; n < byte_size; n++) {
1487 TEST_EQUAL(buffer[n], 'K');
1488 }
1489 for (n = byte_size; n < buffer_size; n++) {
1490 TEST_EQUAL(buffer[n], 0);
1491 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001492 }
1493
1494exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001495 /*
1496 * Key attributes may have been returned by psa_get_key_attributes()
1497 * thus reset them as required.
1498 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001500
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 psa_destroy_key(key);
1502 PSA_DONE();
1503 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001504}
1505/* END_CASE */
1506
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001507/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001508/* Import an RSA key with a valid structure (but not valid numbers
1509 * inside, beyond having sensible size and parity). This is expected to
1510 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001511void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001512{
Ronald Cron5425a212020-08-04 14:58:35 +02001513 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001514 size_t bits = bits_arg;
1515 psa_status_t expected_status = expected_status_arg;
1516 psa_status_t status;
1517 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001518 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001519 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001521 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001522 unsigned char *p;
1523 int ret;
1524 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001525 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 PSA_ASSERT(psa_crypto_init());
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001528 TEST_CALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1531 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001532 length = ret;
1533
1534 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 psa_set_key_type(&attributes, type);
1536 status = psa_import_key(&attributes, p, length, &key);
1537 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 if (status == PSA_SUCCESS) {
1540 PSA_ASSERT(psa_destroy_key(key));
1541 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001542
1543exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 mbedtls_free(buffer);
1545 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001546}
1547/* END_CASE */
1548
1549/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001550void import_export(data_t *data,
1551 int type_arg,
1552 int usage_arg, int alg_arg,
1553 int lifetime_arg,
1554 int expected_bits,
1555 int export_size_delta,
1556 int expected_export_status_arg,
1557 /*whether reexport must give the original input exactly*/
1558 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001559{
Ronald Cron5425a212020-08-04 14:58:35 +02001560 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001561 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001562 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001563 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001564 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301565 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001566 unsigned char *exported = NULL;
1567 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001568 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001569 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001570 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001571 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001572 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001573
Moran Pekercb088e72018-07-17 17:36:59 +03001574 export_size = (ptrdiff_t) data->len + export_size_delta;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001575 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 if (!canonical_input) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001577 TEST_CALLOC(reexported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 }
1579 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 psa_set_key_lifetime(&attributes, lifetime);
1582 psa_set_key_usage_flags(&attributes, usage_arg);
1583 psa_set_key_algorithm(&attributes, alg);
1584 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001585
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001586 if (PSA_KEY_TYPE_IS_DH(type) &&
1587 expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) {
Przemek Stekiel654bef02022-12-15 13:28:02 +01001588 /* Simulate that buffer is too small, by decreasing its size by 1 byte. */
1589 export_size -= 1;
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001590 }
1591
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001592 /* Import the key */
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001593 TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key),
Przemek Stekiel2e7c33d2023-04-27 12:29:45 +02001594 PSA_SUCCESS);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001595
1596 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1598 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1599 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1600 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001601
1602 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 status = psa_export_key(key, exported, export_size, &exported_length);
1604 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001605
1606 /* The exported length must be set by psa_export_key() to a value between 0
1607 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1609 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1610 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001611
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1613 export_size - exported_length));
1614 if (status != PSA_SUCCESS) {
1615 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001616 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001617 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001618
Gilles Peskineea38a922021-02-13 00:05:16 +01001619 /* Run sanity checks on the exported key. For non-canonical inputs,
1620 * this validates the canonical representations. For canonical inputs,
1621 * this doesn't directly validate the implementation, but it still helps
1622 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 if (!psa_key_lifetime_is_external(lifetime)) {
1624 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301625 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 }
Archana4d7ae1d2021-07-07 02:50:22 +05301627 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 if (canonical_input) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001630 TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001632 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1634 &key2));
1635 PSA_ASSERT(psa_export_key(key2,
1636 reexported,
1637 export_size,
1638 &reexported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001639 TEST_MEMORY_COMPARE(exported, exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001640 reexported, reexported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001642 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 TEST_LE_U(exported_length,
1644 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1645 psa_get_key_bits(&got_attributes)));
Valerio Setti1eacae82023-07-28 16:07:03 +02001646 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
1647 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
1648 } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
1649 TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1650 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001651
1652destroy:
1653 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 PSA_ASSERT(psa_destroy_key(key));
1655 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001656
1657exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001658 /*
1659 * Key attributes may have been returned by psa_get_key_attributes()
1660 * thus reset them as required.
1661 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 psa_reset_key_attributes(&got_attributes);
1663 psa_destroy_key(key);
1664 mbedtls_free(exported);
1665 mbedtls_free(reexported);
1666 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001667}
1668/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001669
Moran Pekerf709f4a2018-06-06 17:26:04 +03001670/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001671void import_export_public_key(data_t *data,
1672 int type_arg, // key pair or public key
1673 int alg_arg,
1674 int lifetime_arg,
1675 int export_size_delta,
1676 int expected_export_status_arg,
1677 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001678{
Ronald Cron5425a212020-08-04 14:58:35 +02001679 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001680 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001681 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001682 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001683 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301684 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001685 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001686 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001687 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001688 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 psa_set_key_lifetime(&attributes, lifetime);
1693 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1694 psa_set_key_algorithm(&attributes, alg);
1695 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001696
1697 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001699
Gilles Peskine49c25912018-10-29 15:15:31 +01001700 /* Export the public key */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001701 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 status = psa_export_public_key(key,
1703 exported, export_size,
1704 &exported_length);
1705 TEST_EQUAL(status, expected_export_status);
1706 if (status == PSA_SUCCESS) {
1707 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001708 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1710 bits = psa_get_key_bits(&attributes);
1711 TEST_LE_U(expected_public_key->len,
1712 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1713 TEST_LE_U(expected_public_key->len,
1714 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1715 TEST_LE_U(expected_public_key->len,
1716 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001717 TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001718 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001719 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001720exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001721 /*
1722 * Key attributes may have been returned by psa_get_key_attributes()
1723 * thus reset them as required.
1724 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001726
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 mbedtls_free(exported);
1728 psa_destroy_key(key);
1729 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001730}
1731/* END_CASE */
1732
Gilles Peskine20035e32018-02-03 22:44:14 +01001733/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001734void import_and_exercise_key(data_t *data,
1735 int type_arg,
1736 int bits_arg,
1737 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001738{
Ronald Cron5425a212020-08-04 14:58:35 +02001739 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001740 psa_key_type_t type = type_arg;
1741 size_t bits = bits_arg;
1742 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001744 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001745 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001746
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001748
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 psa_set_key_usage_flags(&attributes, usage);
1750 psa_set_key_algorithm(&attributes, alg);
1751 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001752
1753 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001755
1756 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1758 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1759 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001760
1761 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001763 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001765
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 PSA_ASSERT(psa_destroy_key(key));
1767 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001768
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001769exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001770 /*
1771 * Key attributes may have been returned by psa_get_key_attributes()
1772 * thus reset them as required.
1773 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001775
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 psa_reset_key_attributes(&attributes);
1777 psa_destroy_key(key);
1778 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001779}
1780/* END_CASE */
1781
1782/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001783void effective_key_attributes(int type_arg, int expected_type_arg,
1784 int bits_arg, int expected_bits_arg,
1785 int usage_arg, int expected_usage_arg,
1786 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001787{
Ronald Cron5425a212020-08-04 14:58:35 +02001788 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001789 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001790 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001791 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001792 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001793 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001794 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001795 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001796 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001797 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001798
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001800
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 psa_set_key_usage_flags(&attributes, usage);
1802 psa_set_key_algorithm(&attributes, alg);
1803 psa_set_key_type(&attributes, key_type);
1804 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001805
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 PSA_ASSERT(psa_generate_key(&attributes, &key));
1807 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001808
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1810 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1811 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1812 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1813 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001814
1815exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001816 /*
1817 * Key attributes may have been returned by psa_get_key_attributes()
1818 * thus reset them as required.
1819 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001821
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 psa_destroy_key(key);
1823 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001824}
1825/* END_CASE */
1826
1827/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001828void check_key_policy(int type_arg, int bits_arg,
1829 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001830{
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1832 usage_arg,
1833 mbedtls_test_update_key_usage_flags(usage_arg),
1834 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001835 goto exit;
1836}
1837/* END_CASE */
1838
1839/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001840void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001841{
1842 /* Test each valid way of initializing the object, except for `= {0}`, as
1843 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1844 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001845 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001847 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1848 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001849
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001851
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1853 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1854 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001855
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 TEST_EQUAL(psa_get_key_type(&func), 0);
1857 TEST_EQUAL(psa_get_key_type(&init), 0);
1858 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001859
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 TEST_EQUAL(psa_get_key_bits(&func), 0);
1861 TEST_EQUAL(psa_get_key_bits(&init), 0);
1862 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001863
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1865 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1866 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001867
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1869 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1870 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001871}
1872/* END_CASE */
1873
1874/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001875void mac_key_policy(int policy_usage_arg,
1876 int policy_alg_arg,
1877 int key_type_arg,
1878 data_t *key_data,
1879 int exercise_alg_arg,
1880 int expected_status_sign_arg,
1881 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001882{
Ronald Cron5425a212020-08-04 14:58:35 +02001883 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001884 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001885 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001886 psa_key_type_t key_type = key_type_arg;
1887 psa_algorithm_t policy_alg = policy_alg_arg;
1888 psa_algorithm_t exercise_alg = exercise_alg_arg;
1889 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001890 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001891 psa_status_t expected_status_sign = expected_status_sign_arg;
1892 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001893 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001894
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001896
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 psa_set_key_usage_flags(&attributes, policy_usage);
1898 psa_set_key_algorithm(&attributes, policy_alg);
1899 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001900
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1902 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001903
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1905 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001906
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1908 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001909
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001910 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001912 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1914 input, 128,
1915 mac, PSA_MAC_MAX_SIZE, &mac_len),
1916 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001917
Neil Armstrong3af9b972022-02-07 12:20:21 +01001918 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 PSA_ASSERT(psa_mac_abort(&operation));
1920 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1921 if (status == PSA_SUCCESS) {
1922 status = psa_mac_update(&operation, input, 128);
1923 if (status == PSA_SUCCESS) {
1924 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1925 &mac_len),
1926 expected_status_sign);
1927 } else {
1928 TEST_EQUAL(status, expected_status_sign);
1929 }
1930 } else {
1931 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001932 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001934
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001935 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 status = psa_mac_verify(key, exercise_alg, input, 128,
1937 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001938
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1940 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1941 } else {
1942 TEST_EQUAL(status, expected_status_verify);
1943 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001944
Neil Armstrong3af9b972022-02-07 12:20:21 +01001945 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1947 if (status == PSA_SUCCESS) {
1948 status = psa_mac_update(&operation, input, 128);
1949 if (status == PSA_SUCCESS) {
1950 status = psa_mac_verify_finish(&operation, mac, mac_len);
1951 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1952 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1953 } else {
1954 TEST_EQUAL(status, expected_status_verify);
1955 }
1956 } else {
1957 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001958 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 } else {
1960 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001961 }
1962
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001964
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 memset(mac, 0, sizeof(mac));
1966 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1967 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001968
1969exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 psa_mac_abort(&operation);
1971 psa_destroy_key(key);
1972 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001973}
1974/* END_CASE */
1975
1976/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001977void cipher_key_policy(int policy_usage_arg,
1978 int policy_alg,
1979 int key_type,
1980 data_t *key_data,
1981 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001982{
Ronald Cron5425a212020-08-04 14:58:35 +02001983 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001984 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001985 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001986 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001987 size_t output_buffer_size = 0;
1988 size_t input_buffer_size = 0;
1989 size_t output_length = 0;
1990 uint8_t *output = NULL;
1991 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001992 psa_status_t status;
1993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1995 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1996 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001997
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001998 TEST_CALLOC(input, input_buffer_size);
1999 TEST_CALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002000
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002002
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 psa_set_key_usage_flags(&attributes, policy_usage);
2004 psa_set_key_algorithm(&attributes, policy_alg);
2005 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002006
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2008 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002009
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002010 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 TEST_EQUAL(policy_usage,
2012 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002013
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002014 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
2016 output, output_buffer_size,
2017 &output_length);
2018 if (policy_alg == exercise_alg &&
2019 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2020 PSA_ASSERT(status);
2021 } else {
2022 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2023 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002024
2025 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
2027 if (policy_alg == exercise_alg &&
2028 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2029 PSA_ASSERT(status);
2030 } else {
2031 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2032 }
2033 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002034
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002035 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
2037 input, input_buffer_size,
2038 &output_length);
2039 if (policy_alg == exercise_alg &&
2040 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2041 PSA_ASSERT(status);
2042 } else {
2043 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2044 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002045
2046 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
2048 if (policy_alg == exercise_alg &&
2049 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2050 PSA_ASSERT(status);
2051 } else {
2052 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2053 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002054
2055exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 psa_cipher_abort(&operation);
2057 mbedtls_free(input);
2058 mbedtls_free(output);
2059 psa_destroy_key(key);
2060 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002061}
2062/* END_CASE */
2063
2064/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002065void aead_key_policy(int policy_usage_arg,
2066 int policy_alg,
2067 int key_type,
2068 data_t *key_data,
2069 int nonce_length_arg,
2070 int tag_length_arg,
2071 int exercise_alg,
2072 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002073{
Ronald Cron5425a212020-08-04 14:58:35 +02002074 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002075 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002076 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002077 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002078 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002079 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002081 size_t nonce_length = nonce_length_arg;
2082 unsigned char tag[16];
2083 size_t tag_length = tag_length_arg;
2084 size_t output_length;
2085
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 TEST_LE_U(nonce_length, sizeof(nonce));
2087 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002088
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002090
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 psa_set_key_usage_flags(&attributes, policy_usage);
2092 psa_set_key_algorithm(&attributes, policy_alg);
2093 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2096 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002097
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002098 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 TEST_EQUAL(policy_usage,
2100 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002101
Neil Armstrong752d8112022-02-07 14:51:11 +01002102 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 status = psa_aead_encrypt(key, exercise_alg,
2104 nonce, nonce_length,
2105 NULL, 0,
2106 NULL, 0,
2107 tag, tag_length,
2108 &output_length);
2109 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2110 TEST_EQUAL(status, expected_status);
2111 } else {
2112 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2113 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002114
Neil Armstrong752d8112022-02-07 14:51:11 +01002115 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2117 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2118 TEST_EQUAL(status, expected_status);
2119 } else {
2120 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2121 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002122
2123 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 memset(tag, 0, sizeof(tag));
2125 status = psa_aead_decrypt(key, exercise_alg,
2126 nonce, nonce_length,
2127 NULL, 0,
2128 tag, tag_length,
2129 NULL, 0,
2130 &output_length);
2131 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2132 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2133 } else if (expected_status == PSA_SUCCESS) {
2134 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2135 } else {
2136 TEST_EQUAL(status, expected_status);
2137 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002138
Neil Armstrong752d8112022-02-07 14:51:11 +01002139 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 PSA_ASSERT(psa_aead_abort(&operation));
2141 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2142 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2143 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2144 } else {
2145 TEST_EQUAL(status, expected_status);
2146 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002147
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002148exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 PSA_ASSERT(psa_aead_abort(&operation));
2150 psa_destroy_key(key);
2151 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002152}
2153/* END_CASE */
2154
2155/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002156void asymmetric_encryption_key_policy(int policy_usage_arg,
2157 int policy_alg,
2158 int key_type,
2159 data_t *key_data,
Valerio Settif202c292024-01-15 10:42:37 +01002160 int exercise_alg,
2161 int use_opaque_key)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002162{
Ronald Cron5425a212020-08-04 14:58:35 +02002163 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002164 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002165 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002166 psa_status_t status;
2167 size_t key_bits;
2168 size_t buffer_length;
2169 unsigned char *buffer = NULL;
2170 size_t output_length;
2171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002173
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 psa_set_key_usage_flags(&attributes, policy_usage);
2175 psa_set_key_algorithm(&attributes, policy_alg);
2176 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002177
Valerio Settif202c292024-01-15 10:42:37 +01002178 if (use_opaque_key) {
2179 psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
2180 PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION));
2181 }
2182
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2184 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002185
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002186 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 TEST_EQUAL(policy_usage,
2188 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002189
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2191 key_bits = psa_get_key_bits(&attributes);
2192 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2193 exercise_alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002194 TEST_CALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 status = psa_asymmetric_encrypt(key, exercise_alg,
2197 NULL, 0,
2198 NULL, 0,
2199 buffer, buffer_length,
2200 &output_length);
2201 if (policy_alg == exercise_alg &&
2202 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2203 PSA_ASSERT(status);
2204 } else {
2205 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2206 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 if (buffer_length != 0) {
2209 memset(buffer, 0, buffer_length);
2210 }
2211 status = psa_asymmetric_decrypt(key, exercise_alg,
2212 buffer, buffer_length,
2213 NULL, 0,
2214 buffer, buffer_length,
2215 &output_length);
2216 if (policy_alg == exercise_alg &&
2217 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2218 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2219 } else {
2220 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2221 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002222
2223exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002224 /*
2225 * Key attributes may have been returned by psa_get_key_attributes()
2226 * thus reset them as required.
2227 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 psa_destroy_key(key);
2231 PSA_DONE();
2232 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002233}
2234/* END_CASE */
2235
2236/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002237void asymmetric_signature_key_policy(int policy_usage_arg,
2238 int policy_alg,
2239 int key_type,
2240 data_t *key_data,
2241 int exercise_alg,
2242 int payload_length_arg,
2243 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002244{
Ronald Cron5425a212020-08-04 14:58:35 +02002245 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002246 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002247 psa_key_usage_t policy_usage = policy_usage_arg;
2248 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002249 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002251 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2252 * compatible with the policy and `payload_length_arg` is supposed to be
2253 * a valid input length to sign. If `payload_length_arg <= 0`,
2254 * `exercise_alg` is supposed to be forbidden by the policy. */
2255 int compatible_alg = payload_length_arg > 0;
2256 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002258 size_t signature_length;
2259
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002260 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002261 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 TEST_EQUAL(expected_usage,
2263 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002264
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 psa_set_key_usage_flags(&attributes, policy_usage);
2268 psa_set_key_algorithm(&attributes, policy_alg);
2269 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002270
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2272 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002273
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002275
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 status = psa_sign_hash(key, exercise_alg,
2277 payload, payload_length,
2278 signature, sizeof(signature),
2279 &signature_length);
2280 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2281 PSA_ASSERT(status);
2282 } else {
2283 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2284 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 memset(signature, 0, sizeof(signature));
2287 status = psa_verify_hash(key, exercise_alg,
2288 payload, payload_length,
2289 signature, sizeof(signature));
2290 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2291 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2292 } else {
2293 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2294 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002295
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2297 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2298 status = psa_sign_message(key, exercise_alg,
2299 payload, payload_length,
2300 signature, sizeof(signature),
2301 &signature_length);
2302 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2303 PSA_ASSERT(status);
2304 } else {
2305 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2306 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002307
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 memset(signature, 0, sizeof(signature));
2309 status = psa_verify_message(key, exercise_alg,
2310 payload, payload_length,
2311 signature, sizeof(signature));
2312 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2313 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2314 } else {
2315 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2316 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002317 }
2318
Gilles Peskined5b33222018-06-18 22:20:03 +02002319exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 psa_destroy_key(key);
2321 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002322}
2323/* END_CASE */
2324
Janos Follathba3fab92019-06-11 14:50:16 +01002325/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002326void derive_key_policy(int policy_usage,
2327 int policy_alg,
2328 int key_type,
2329 data_t *key_data,
2330 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002331{
Ronald Cron5425a212020-08-04 14:58:35 +02002332 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002333 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002334 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002335 psa_status_t status;
2336
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 psa_set_key_usage_flags(&attributes, policy_usage);
2340 psa_set_key_algorithm(&attributes, policy_alg);
2341 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2344 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002347
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2349 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2350 PSA_ASSERT(psa_key_derivation_input_bytes(
2351 &operation,
2352 PSA_KEY_DERIVATION_INPUT_SEED,
2353 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002354 }
Janos Follathba3fab92019-06-11 14:50:16 +01002355
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 status = psa_key_derivation_input_key(&operation,
2357 PSA_KEY_DERIVATION_INPUT_SECRET,
2358 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002359
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 if (policy_alg == exercise_alg &&
2361 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2362 PSA_ASSERT(status);
2363 } else {
2364 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2365 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002366
2367exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 psa_key_derivation_abort(&operation);
2369 psa_destroy_key(key);
2370 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002371}
2372/* END_CASE */
2373
2374/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002375void agreement_key_policy(int policy_usage,
2376 int policy_alg,
2377 int key_type_arg,
2378 data_t *key_data,
2379 int exercise_alg,
2380 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002381{
Ronald Cron5425a212020-08-04 14:58:35 +02002382 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002383 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002384 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002385 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002386 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002387 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002390
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 psa_set_key_usage_flags(&attributes, policy_usage);
2392 psa_set_key_algorithm(&attributes, policy_alg);
2393 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2396 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2399 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002400
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002402
2403exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 psa_key_derivation_abort(&operation);
2405 psa_destroy_key(key);
2406 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002407}
2408/* END_CASE */
2409
2410/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002411void key_policy_alg2(int key_type_arg, data_t *key_data,
2412 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002413{
Ronald Cron5425a212020-08-04 14:58:35 +02002414 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002415 psa_key_type_t key_type = key_type_arg;
2416 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2417 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2418 psa_key_usage_t usage = usage_arg;
2419 psa_algorithm_t alg = alg_arg;
2420 psa_algorithm_t alg2 = alg2_arg;
2421
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 psa_set_key_usage_flags(&attributes, usage);
2425 psa_set_key_algorithm(&attributes, alg);
2426 psa_set_key_enrollment_algorithm(&attributes, alg2);
2427 psa_set_key_type(&attributes, key_type);
2428 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2429 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002430
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002431 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 usage = mbedtls_test_update_key_usage_flags(usage);
2433 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2434 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2435 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2436 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002437
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002439 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 }
2441 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002442 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002444
2445exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002446 /*
2447 * Key attributes may have been returned by psa_get_key_attributes()
2448 * thus reset them as required.
2449 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002451
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 psa_destroy_key(key);
2453 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002454}
2455/* END_CASE */
2456
2457/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002458void raw_agreement_key_policy(int policy_usage,
2459 int policy_alg,
2460 int key_type_arg,
2461 data_t *key_data,
2462 int exercise_alg,
2463 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002464{
Ronald Cron5425a212020-08-04 14:58:35 +02002465 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002466 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002467 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002468 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002469 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002470 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002471
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002473
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 psa_set_key_usage_flags(&attributes, policy_usage);
2475 psa_set_key_algorithm(&attributes, policy_alg);
2476 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2479 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002480
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002484
2485exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 psa_key_derivation_abort(&operation);
2487 psa_destroy_key(key);
2488 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002489}
2490/* END_CASE */
2491
2492/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002493void copy_success(int source_usage_arg,
2494 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002495 int source_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 int type_arg, data_t *material,
2497 int copy_attributes,
2498 int target_usage_arg,
2499 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002500 int target_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 int expected_usage_arg,
2502 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002503{
Gilles Peskineca25db92019-04-19 11:43:08 +02002504 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2505 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002506 psa_key_usage_t expected_usage = expected_usage_arg;
2507 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002508 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302509 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2510 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002511 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2512 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002513 uint8_t *export_buffer = NULL;
2514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002516
Gilles Peskineca25db92019-04-19 11:43:08 +02002517 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2519 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2520 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2521 psa_set_key_type(&source_attributes, type_arg);
2522 psa_set_key_lifetime(&source_attributes, source_lifetime);
2523 PSA_ASSERT(psa_import_key(&source_attributes,
2524 material->x, material->len,
2525 &source_key));
2526 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002527
Gilles Peskineca25db92019-04-19 11:43:08 +02002528 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002530 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002531 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002533
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 if (target_usage_arg != -1) {
2535 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2536 }
2537 if (target_alg_arg != -1) {
2538 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2539 }
2540 if (target_alg2_arg != -1) {
2541 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2542 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002543
Archana8a180362021-07-05 02:18:48 +05302544
Gilles Peskine57ab7212019-01-28 13:03:09 +01002545 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 PSA_ASSERT(psa_copy_key(source_key,
2547 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002548
2549 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002551
2552 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2554 TEST_EQUAL(psa_get_key_type(&source_attributes),
2555 psa_get_key_type(&target_attributes));
2556 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2557 psa_get_key_bits(&target_attributes));
2558 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2559 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2560 TEST_EQUAL(expected_alg2,
2561 psa_get_key_enrollment_algorithm(&target_attributes));
2562 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002563 size_t length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002564 TEST_CALLOC(export_buffer, material->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2566 material->len, &length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002567 TEST_MEMORY_COMPARE(material->x, material->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002568 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002569 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002570
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 if (!psa_key_lifetime_is_external(target_lifetime)) {
2572 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302573 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 }
2575 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302576 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 }
Archana8a180362021-07-05 02:18:48 +05302578 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002579
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002581
2582exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002583 /*
2584 * Source and target key attributes may have been returned by
2585 * psa_get_key_attributes() thus reset them as required.
2586 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 psa_reset_key_attributes(&source_attributes);
2588 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002589
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 PSA_DONE();
2591 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002592}
2593/* END_CASE */
2594
2595/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002596void copy_fail(int source_usage_arg,
2597 int source_alg_arg, int source_alg2_arg,
2598 int source_lifetime_arg,
2599 int type_arg, data_t *material,
2600 int target_type_arg, int target_bits_arg,
2601 int target_usage_arg,
2602 int target_alg_arg, int target_alg2_arg,
2603 int target_id_arg, int target_lifetime_arg,
2604 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002605{
2606 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2607 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002608 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2609 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002611
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002613
2614 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2616 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2617 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2618 psa_set_key_type(&source_attributes, type_arg);
2619 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2620 PSA_ASSERT(psa_import_key(&source_attributes,
2621 material->x, material->len,
2622 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002623
2624 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 psa_set_key_id(&target_attributes, key_id);
2626 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2627 psa_set_key_type(&target_attributes, target_type_arg);
2628 psa_set_key_bits(&target_attributes, target_bits_arg);
2629 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2630 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2631 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002632
2633 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 TEST_EQUAL(psa_copy_key(source_key,
2635 &target_attributes, &target_key),
2636 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002637
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002639
Gilles Peskine4a644642019-05-03 17:14:08 +02002640exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 psa_reset_key_attributes(&source_attributes);
2642 psa_reset_key_attributes(&target_attributes);
2643 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002644}
2645/* END_CASE */
2646
2647/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002648void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002649{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002650 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002651 /* Test each valid way of initializing the object, except for `= {0}`, as
2652 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2653 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002654 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002656 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2657 psa_hash_operation_t zero;
2658
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002660
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002661 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2663 PSA_ERROR_BAD_STATE);
2664 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2665 PSA_ERROR_BAD_STATE);
2666 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2667 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002668
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002669 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 PSA_ASSERT(psa_hash_abort(&func));
2671 PSA_ASSERT(psa_hash_abort(&init));
2672 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002673}
2674/* END_CASE */
2675
2676/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002677void hash_setup(int alg_arg,
2678 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002679{
2680 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002681 uint8_t *output = NULL;
2682 size_t output_size = 0;
2683 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002684 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002685 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002686 psa_status_t status;
2687
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002689
Neil Armstrongedb20862022-02-07 15:47:44 +01002690 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 output_size = PSA_HASH_LENGTH(alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002692 TEST_CALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002693
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 status = psa_hash_compute(alg, NULL, 0,
2695 output, output_size, &output_length);
2696 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002697
2698 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 status = psa_hash_setup(&operation, alg);
2700 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002701
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002702 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002704
2705 /* If setup failed, reproduce the failure, so as to
2706 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 if (status != PSA_SUCCESS) {
2708 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2709 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002710
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002711 /* Now the operation object should be reusable. */
2712#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2714 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002715#endif
2716
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002717exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 mbedtls_free(output);
2719 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002720}
2721/* END_CASE */
2722
2723/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002724void hash_compute_fail(int alg_arg, data_t *input,
2725 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002726{
2727 psa_algorithm_t alg = alg_arg;
2728 uint8_t *output = NULL;
2729 size_t output_size = output_size_arg;
2730 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002731 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002732 psa_status_t expected_status = expected_status_arg;
2733 psa_status_t status;
2734
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002735 TEST_CALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002736
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002738
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002739 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 status = psa_hash_compute(alg, input->x, input->len,
2741 output, output_size, &output_length);
2742 TEST_EQUAL(status, expected_status);
2743 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002744
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002745 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 status = psa_hash_setup(&operation, alg);
2747 if (status == PSA_SUCCESS) {
2748 status = psa_hash_update(&operation, input->x, input->len);
2749 if (status == PSA_SUCCESS) {
2750 status = psa_hash_finish(&operation, output, output_size,
2751 &output_length);
2752 if (status == PSA_SUCCESS) {
2753 TEST_LE_U(output_length, output_size);
2754 } else {
2755 TEST_EQUAL(status, expected_status);
2756 }
2757 } else {
2758 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002759 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 } else {
2761 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002762 }
2763
Gilles Peskine0a749c82019-11-28 19:33:58 +01002764exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 PSA_ASSERT(psa_hash_abort(&operation));
2766 mbedtls_free(output);
2767 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002768}
2769/* END_CASE */
2770
2771/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002772void hash_compare_fail(int alg_arg, data_t *input,
2773 data_t *reference_hash,
2774 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002775{
2776 psa_algorithm_t alg = alg_arg;
2777 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002778 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002779 psa_status_t status;
2780
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002782
Neil Armstrong55a1be12022-02-07 11:23:20 +01002783 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 status = psa_hash_compare(alg, input->x, input->len,
2785 reference_hash->x, reference_hash->len);
2786 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002787
Neil Armstrong55a1be12022-02-07 11:23:20 +01002788 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 status = psa_hash_setup(&operation, alg);
2790 if (status == PSA_SUCCESS) {
2791 status = psa_hash_update(&operation, input->x, input->len);
2792 if (status == PSA_SUCCESS) {
2793 status = psa_hash_verify(&operation, reference_hash->x,
2794 reference_hash->len);
2795 TEST_EQUAL(status, expected_status);
2796 } else {
2797 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002798 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 } else {
2800 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002801 }
2802
Gilles Peskine88e08462020-01-28 20:43:00 +01002803exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 PSA_ASSERT(psa_hash_abort(&operation));
2805 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002806}
2807/* END_CASE */
2808
2809/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002810void hash_compute_compare(int alg_arg, data_t *input,
2811 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002812{
2813 psa_algorithm_t alg = alg_arg;
2814 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2815 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002816 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002817 size_t i;
2818
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002820
Neil Armstrongca30a002022-02-07 11:40:23 +01002821 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2823 output, PSA_HASH_LENGTH(alg),
2824 &output_length));
2825 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002826 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002827 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002828
Neil Armstrongca30a002022-02-07 11:40:23 +01002829 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 PSA_ASSERT(psa_hash_setup(&operation, alg));
2831 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2832 PSA_ASSERT(psa_hash_finish(&operation, output,
2833 PSA_HASH_LENGTH(alg),
2834 &output_length));
2835 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002836 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002837 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002838
2839 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2841 output, sizeof(output),
2842 &output_length));
2843 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002844 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002845 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002846
Neil Armstrongca30a002022-02-07 11:40:23 +01002847 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 PSA_ASSERT(psa_hash_setup(&operation, alg));
2849 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2850 PSA_ASSERT(psa_hash_finish(&operation, output,
2851 sizeof(output), &output_length));
2852 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002853 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002854 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002855
2856 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2858 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002859
Neil Armstrongca30a002022-02-07 11:40:23 +01002860 /* Compare with correct hash, 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_verify(&operation, output,
2864 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002865
2866 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2868 output, output_length + 1),
2869 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002870
Neil Armstrongca30a002022-02-07 11:40:23 +01002871 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 PSA_ASSERT(psa_hash_setup(&operation, alg));
2873 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2874 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2875 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002876
2877 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2879 output, output_length - 1),
2880 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002881
Neil Armstrongca30a002022-02-07 11:40:23 +01002882 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 PSA_ASSERT(psa_hash_setup(&operation, alg));
2884 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2885 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2886 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002887
Gilles Peskine0a749c82019-11-28 19:33:58 +01002888 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002889 for (i = 0; i < output_length; i++) {
2890 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002891 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002892
2893 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002894 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2895 output, output_length),
2896 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002897
2898 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 PSA_ASSERT(psa_hash_setup(&operation, alg));
2900 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2901 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2902 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002903
Gilles Peskine0a749c82019-11-28 19:33:58 +01002904 output[i] ^= 1;
2905 }
2906
2907exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002908 PSA_ASSERT(psa_hash_abort(&operation));
2909 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002910}
2911/* END_CASE */
2912
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002913/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002914void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002915{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002916 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002917 unsigned char input[] = "";
2918 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002919 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002920 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2921 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2923 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002924 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002925 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002926 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002927
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002929
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002930 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 PSA_ASSERT(psa_hash_setup(&operation, alg));
2932 ASSERT_OPERATION_IS_ACTIVE(operation);
2933 TEST_EQUAL(psa_hash_setup(&operation, alg),
2934 PSA_ERROR_BAD_STATE);
2935 ASSERT_OPERATION_IS_INACTIVE(operation);
2936 PSA_ASSERT(psa_hash_abort(&operation));
2937 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002938
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002939 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2941 PSA_ERROR_BAD_STATE);
2942 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002943
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002944 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002946 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 ASSERT_OPERATION_IS_ACTIVE(operation);
2948 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2949 PSA_ERROR_BAD_STATE);
2950 ASSERT_OPERATION_IS_INACTIVE(operation);
2951 PSA_ASSERT(psa_hash_abort(&operation));
2952 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002953
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002954 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002955 PSA_ASSERT(psa_hash_setup(&operation, alg));
2956 PSA_ASSERT(psa_hash_finish(&operation,
2957 hash, sizeof(hash), &hash_len));
2958 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2959 PSA_ERROR_BAD_STATE);
2960 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002961
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002962 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 TEST_EQUAL(psa_hash_verify(&operation,
2964 valid_hash, sizeof(valid_hash)),
2965 PSA_ERROR_BAD_STATE);
2966 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002967
2968 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 PSA_ASSERT(psa_hash_setup(&operation, alg));
2970 PSA_ASSERT(psa_hash_finish(&operation,
2971 hash, sizeof(hash), &hash_len));
2972 TEST_EQUAL(psa_hash_verify(&operation,
2973 valid_hash, sizeof(valid_hash)),
2974 PSA_ERROR_BAD_STATE);
2975 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002976
2977 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 PSA_ASSERT(psa_hash_setup(&operation, alg));
2979 ASSERT_OPERATION_IS_ACTIVE(operation);
2980 PSA_ASSERT(psa_hash_verify(&operation,
2981 valid_hash, sizeof(valid_hash)));
2982 ASSERT_OPERATION_IS_INACTIVE(operation);
2983 TEST_EQUAL(psa_hash_verify(&operation,
2984 valid_hash, sizeof(valid_hash)),
2985 PSA_ERROR_BAD_STATE);
2986 ASSERT_OPERATION_IS_INACTIVE(operation);
2987 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002988
2989 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 TEST_EQUAL(psa_hash_finish(&operation,
2991 hash, sizeof(hash), &hash_len),
2992 PSA_ERROR_BAD_STATE);
2993 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002994
2995 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 PSA_ASSERT(psa_hash_setup(&operation, alg));
2997 PSA_ASSERT(psa_hash_finish(&operation,
2998 hash, sizeof(hash), &hash_len));
2999 TEST_EQUAL(psa_hash_finish(&operation,
3000 hash, sizeof(hash), &hash_len),
3001 PSA_ERROR_BAD_STATE);
3002 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00003003
3004 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 PSA_ASSERT(psa_hash_setup(&operation, alg));
3006 PSA_ASSERT(psa_hash_verify(&operation,
3007 valid_hash, sizeof(valid_hash)));
3008 TEST_EQUAL(psa_hash_finish(&operation,
3009 hash, sizeof(hash), &hash_len),
3010 PSA_ERROR_BAD_STATE);
3011 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02003012
3013exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02003015}
3016/* END_CASE */
3017
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003018/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003019void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03003020{
3021 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02003022 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
3023 * appended to it */
3024 unsigned char hash[] = {
3025 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
3026 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
3028 };
3029 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003030 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03003031
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03003033
itayzafrir27e69452018-11-01 14:26:34 +02003034 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 PSA_ASSERT(psa_hash_setup(&operation, alg));
3036 ASSERT_OPERATION_IS_ACTIVE(operation);
3037 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
3038 PSA_ERROR_INVALID_SIGNATURE);
3039 ASSERT_OPERATION_IS_INACTIVE(operation);
3040 PSA_ASSERT(psa_hash_abort(&operation));
3041 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03003042
itayzafrir27e69452018-11-01 14:26:34 +02003043 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 PSA_ASSERT(psa_hash_setup(&operation, alg));
3045 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
3046 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03003047
itayzafrir27e69452018-11-01 14:26:34 +02003048 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 PSA_ASSERT(psa_hash_setup(&operation, alg));
3050 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
3051 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03003052
itayzafrirec93d302018-10-18 18:01:10 +03003053exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03003055}
3056/* END_CASE */
3057
Ronald Cronee414c72021-03-18 18:50:08 +01003058/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003059void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03003060{
3061 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003062 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003064 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003065 size_t hash_len;
3066
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03003068
itayzafrir58028322018-10-25 10:22:01 +03003069 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 PSA_ASSERT(psa_hash_setup(&operation, alg));
3071 TEST_EQUAL(psa_hash_finish(&operation,
3072 hash, expected_size - 1, &hash_len),
3073 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003074
3075exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003077}
3078/* END_CASE */
3079
Ronald Cronee414c72021-03-18 18:50:08 +01003080/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003081void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003082{
3083 psa_algorithm_t alg = PSA_ALG_SHA_256;
3084 unsigned char hash[PSA_HASH_MAX_SIZE];
3085 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3086 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3087 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3088 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3089 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3090 size_t hash_len;
3091
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 PSA_ASSERT(psa_crypto_init());
3093 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003094
Gilles Peskine449bd832023-01-11 14:50:10 +01003095 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3096 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3097 PSA_ASSERT(psa_hash_finish(&op_finished,
3098 hash, sizeof(hash), &hash_len));
3099 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3100 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003101
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3103 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003104
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3106 PSA_ASSERT(psa_hash_finish(&op_init,
3107 hash, sizeof(hash), &hash_len));
3108 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3109 PSA_ASSERT(psa_hash_finish(&op_finished,
3110 hash, sizeof(hash), &hash_len));
3111 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3112 PSA_ASSERT(psa_hash_finish(&op_aborted,
3113 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003114
3115exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 psa_hash_abort(&op_source);
3117 psa_hash_abort(&op_init);
3118 psa_hash_abort(&op_setup);
3119 psa_hash_abort(&op_finished);
3120 psa_hash_abort(&op_aborted);
3121 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003122}
3123/* END_CASE */
3124
Ronald Cronee414c72021-03-18 18:50:08 +01003125/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003126void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003127{
3128 psa_algorithm_t alg = PSA_ALG_SHA_256;
3129 unsigned char hash[PSA_HASH_MAX_SIZE];
3130 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3131 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3132 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3133 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3134 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3135 size_t hash_len;
3136
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003138
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3140 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3141 PSA_ASSERT(psa_hash_finish(&op_finished,
3142 hash, sizeof(hash), &hash_len));
3143 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3144 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003145
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3147 PSA_ASSERT(psa_hash_finish(&op_target,
3148 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003149
Gilles Peskine449bd832023-01-11 14:50:10 +01003150 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3151 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3152 PSA_ERROR_BAD_STATE);
3153 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3154 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003155
3156exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 psa_hash_abort(&op_target);
3158 psa_hash_abort(&op_init);
3159 psa_hash_abort(&op_setup);
3160 psa_hash_abort(&op_finished);
3161 psa_hash_abort(&op_aborted);
3162 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003163}
3164/* END_CASE */
3165
itayzafrir58028322018-10-25 10:22:01 +03003166/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003167void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003168{
Jaeden Amero252ef282019-02-15 14:05:35 +00003169 const uint8_t input[1] = { 0 };
3170
Jaeden Amero769ce272019-01-04 11:48:03 +00003171 /* Test each valid way of initializing the object, except for `= {0}`, as
3172 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3173 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003174 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003176 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3177 psa_mac_operation_t zero;
3178
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003180
Jaeden Amero252ef282019-02-15 14:05:35 +00003181 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 TEST_EQUAL(psa_mac_update(&func,
3183 input, sizeof(input)),
3184 PSA_ERROR_BAD_STATE);
3185 TEST_EQUAL(psa_mac_update(&init,
3186 input, sizeof(input)),
3187 PSA_ERROR_BAD_STATE);
3188 TEST_EQUAL(psa_mac_update(&zero,
3189 input, sizeof(input)),
3190 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003191
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003192 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 PSA_ASSERT(psa_mac_abort(&func));
3194 PSA_ASSERT(psa_mac_abort(&init));
3195 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003196}
3197/* END_CASE */
3198
3199/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003200void mac_setup(int key_type_arg,
3201 data_t *key,
3202 int alg_arg,
3203 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003204{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003205 psa_key_type_t key_type = key_type_arg;
3206 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003207 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003208 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003209 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3210#if defined(KNOWN_SUPPORTED_MAC_ALG)
3211 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3212#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003213
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003215
Gilles Peskine449bd832023-01-11 14:50:10 +01003216 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3217 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003218 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003219 }
3220 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003221
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003222 /* The operation object should be reusable. */
3223#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3225 smoke_test_key_data,
3226 sizeof(smoke_test_key_data),
3227 KNOWN_SUPPORTED_MAC_ALG,
3228 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003229 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003230 }
3231 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003232#endif
3233
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003234exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003235 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003236}
3237/* END_CASE */
3238
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003239/* 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 +01003240void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003241{
Ronald Cron5425a212020-08-04 14:58:35 +02003242 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003243 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3244 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003245 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003246 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3247 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3249 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003250 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003251 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3252 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3253 size_t sign_mac_length = 0;
3254 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3255 const uint8_t verify_mac[] = {
3256 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3257 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003258 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3259 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003260
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 PSA_ASSERT(psa_crypto_init());
3262 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3263 psa_set_key_algorithm(&attributes, alg);
3264 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003265
Gilles Peskine449bd832023-01-11 14:50:10 +01003266 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3267 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003268
Jaeden Amero252ef282019-02-15 14:05:35 +00003269 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3271 PSA_ERROR_BAD_STATE);
3272 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003273
3274 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3276 &sign_mac_length),
3277 PSA_ERROR_BAD_STATE);
3278 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003279
3280 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003281 TEST_EQUAL(psa_mac_verify_finish(&operation,
3282 verify_mac, sizeof(verify_mac)),
3283 PSA_ERROR_BAD_STATE);
3284 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003285
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003286 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3288 ASSERT_OPERATION_IS_ACTIVE(operation);
3289 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3290 PSA_ERROR_BAD_STATE);
3291 ASSERT_OPERATION_IS_INACTIVE(operation);
3292 PSA_ASSERT(psa_mac_abort(&operation));
3293 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003294
Jaeden Amero252ef282019-02-15 14:05:35 +00003295 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3297 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3298 PSA_ASSERT(psa_mac_sign_finish(&operation,
3299 sign_mac, sizeof(sign_mac),
3300 &sign_mac_length));
3301 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 update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3307 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3308 PSA_ASSERT(psa_mac_verify_finish(&operation,
3309 verify_mac, sizeof(verify_mac)));
3310 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3311 PSA_ERROR_BAD_STATE);
3312 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003313
3314 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003315 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3316 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3317 PSA_ASSERT(psa_mac_sign_finish(&operation,
3318 sign_mac, sizeof(sign_mac),
3319 &sign_mac_length));
3320 TEST_EQUAL(psa_mac_sign_finish(&operation,
3321 sign_mac, sizeof(sign_mac),
3322 &sign_mac_length),
3323 PSA_ERROR_BAD_STATE);
3324 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003325
3326 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3328 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3329 PSA_ASSERT(psa_mac_verify_finish(&operation,
3330 verify_mac, sizeof(verify_mac)));
3331 TEST_EQUAL(psa_mac_verify_finish(&operation,
3332 verify_mac, sizeof(verify_mac)),
3333 PSA_ERROR_BAD_STATE);
3334 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003335
3336 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003337 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3338 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3339 ASSERT_OPERATION_IS_ACTIVE(operation);
3340 TEST_EQUAL(psa_mac_verify_finish(&operation,
3341 verify_mac, sizeof(verify_mac)),
3342 PSA_ERROR_BAD_STATE);
3343 ASSERT_OPERATION_IS_INACTIVE(operation);
3344 PSA_ASSERT(psa_mac_abort(&operation));
3345 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003346
3347 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003348 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3349 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3350 ASSERT_OPERATION_IS_ACTIVE(operation);
3351 TEST_EQUAL(psa_mac_sign_finish(&operation,
3352 sign_mac, sizeof(sign_mac),
3353 &sign_mac_length),
3354 PSA_ERROR_BAD_STATE);
3355 ASSERT_OPERATION_IS_INACTIVE(operation);
3356 PSA_ASSERT(psa_mac_abort(&operation));
3357 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003358
Gilles Peskine449bd832023-01-11 14:50:10 +01003359 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003360
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003361exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003362 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003363}
3364/* END_CASE */
3365
3366/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003367void mac_sign_verify_multi(int key_type_arg,
3368 data_t *key_data,
3369 int alg_arg,
3370 data_t *input,
3371 int is_verify,
3372 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003373{
3374 size_t data_part_len = 0;
3375
Gilles Peskine449bd832023-01-11 14:50:10 +01003376 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003377 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003378 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003379
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 if (mac_multipart_internal_func(key_type_arg, key_data,
3381 alg_arg,
3382 input, data_part_len,
3383 expected_mac,
3384 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003385 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003387
3388 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003390
Gilles Peskine449bd832023-01-11 14:50:10 +01003391 if (mac_multipart_internal_func(key_type_arg, key_data,
3392 alg_arg,
3393 input, data_part_len,
3394 expected_mac,
3395 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003396 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003397 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003398 }
3399
3400 /* Goto is required to silence warnings about unused labels, as we
3401 * don't actually do any test assertions in this function. */
3402 goto exit;
3403}
3404/* END_CASE */
3405
3406/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003407void mac_sign(int key_type_arg,
3408 data_t *key_data,
3409 int alg_arg,
3410 data_t *input,
3411 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003412{
Ronald Cron5425a212020-08-04 14:58:35 +02003413 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003414 psa_key_type_t key_type = key_type_arg;
3415 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003416 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003417 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003418 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003419 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003421 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003422 const size_t output_sizes_to_test[] = {
3423 0,
3424 1,
3425 expected_mac->len - 1,
3426 expected_mac->len,
3427 expected_mac->len + 1,
3428 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003429
Gilles Peskine449bd832023-01-11 14:50:10 +01003430 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003431 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003432 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003433
Gilles Peskine449bd832023-01-11 14:50:10 +01003434 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003435
Gilles Peskine449bd832023-01-11 14:50:10 +01003436 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3437 psa_set_key_algorithm(&attributes, alg);
3438 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003439
Gilles Peskine449bd832023-01-11 14:50:10 +01003440 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3441 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003442
Gilles Peskine449bd832023-01-11 14:50:10 +01003443 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003444 const size_t output_size = output_sizes_to_test[i];
3445 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003446 (output_size >= expected_mac->len ? PSA_SUCCESS :
3447 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003448
Gilles Peskine449bd832023-01-11 14:50:10 +01003449 mbedtls_test_set_step(output_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003450 TEST_CALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003451
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003452 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003453 TEST_EQUAL(psa_mac_compute(key, alg,
3454 input->x, input->len,
3455 actual_mac, output_size, &mac_length),
3456 expected_status);
3457 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003458 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003459 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003460 }
3461
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 if (output_size > 0) {
3463 memset(actual_mac, 0, output_size);
3464 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003465
3466 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003467 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3468 PSA_ASSERT(psa_mac_update(&operation,
3469 input->x, input->len));
3470 TEST_EQUAL(psa_mac_sign_finish(&operation,
3471 actual_mac, output_size,
3472 &mac_length),
3473 expected_status);
3474 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003475
Gilles Peskine449bd832023-01-11 14:50:10 +01003476 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003477 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003478 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003479 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003481 actual_mac = NULL;
3482 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003483
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003484exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003485 psa_mac_abort(&operation);
3486 psa_destroy_key(key);
3487 PSA_DONE();
3488 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003489}
3490/* END_CASE */
3491
3492/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003493void mac_verify(int key_type_arg,
3494 data_t *key_data,
3495 int alg_arg,
3496 data_t *input,
3497 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003498{
Ronald Cron5425a212020-08-04 14:58:35 +02003499 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003500 psa_key_type_t key_type = key_type_arg;
3501 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003502 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003503 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003504 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003505
Gilles Peskine449bd832023-01-11 14:50:10 +01003506 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003507
Gilles Peskine449bd832023-01-11 14:50:10 +01003508 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003509
Gilles Peskine449bd832023-01-11 14:50:10 +01003510 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3511 psa_set_key_algorithm(&attributes, alg);
3512 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003513
Gilles Peskine449bd832023-01-11 14:50:10 +01003514 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3515 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003516
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003517 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003518 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3519 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003520
3521 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003522 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3523 PSA_ASSERT(psa_mac_update(&operation,
3524 input->x, input->len));
3525 PSA_ASSERT(psa_mac_verify_finish(&operation,
3526 expected_mac->x,
3527 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003528
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003529 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003530 TEST_EQUAL(psa_mac_verify(key, alg,
3531 input->x, input->len,
3532 expected_mac->x,
3533 expected_mac->len - 1),
3534 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003535
3536 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003537 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3538 PSA_ASSERT(psa_mac_update(&operation,
3539 input->x, input->len));
3540 TEST_EQUAL(psa_mac_verify_finish(&operation,
3541 expected_mac->x,
3542 expected_mac->len - 1),
3543 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003544
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003545 /* Test a MAC that's too long, one-shot case. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003546 TEST_CALLOC(perturbed_mac, expected_mac->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01003547 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3548 TEST_EQUAL(psa_mac_verify(key, alg,
3549 input->x, input->len,
3550 perturbed_mac, expected_mac->len + 1),
3551 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003552
3553 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003554 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3555 PSA_ASSERT(psa_mac_update(&operation,
3556 input->x, input->len));
3557 TEST_EQUAL(psa_mac_verify_finish(&operation,
3558 perturbed_mac,
3559 expected_mac->len + 1),
3560 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003561
3562 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003563 for (size_t i = 0; i < expected_mac->len; i++) {
3564 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003565 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003566
Gilles Peskine449bd832023-01-11 14:50:10 +01003567 TEST_EQUAL(psa_mac_verify(key, alg,
3568 input->x, input->len,
3569 perturbed_mac, expected_mac->len),
3570 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003571
Gilles Peskine449bd832023-01-11 14:50:10 +01003572 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3573 PSA_ASSERT(psa_mac_update(&operation,
3574 input->x, input->len));
3575 TEST_EQUAL(psa_mac_verify_finish(&operation,
3576 perturbed_mac,
3577 expected_mac->len),
3578 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003579 perturbed_mac[i] ^= 1;
3580 }
3581
Gilles Peskine8c9def32018-02-08 10:02:12 +01003582exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003583 psa_mac_abort(&operation);
3584 psa_destroy_key(key);
3585 PSA_DONE();
3586 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003587}
3588/* END_CASE */
3589
3590/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003591void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003592{
Jaeden Ameroab439972019-02-15 14:12:05 +00003593 const uint8_t input[1] = { 0 };
3594 unsigned char output[1] = { 0 };
3595 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003596 /* Test each valid way of initializing the object, except for `= {0}`, as
3597 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3598 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003599 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003600 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003601 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3602 psa_cipher_operation_t zero;
3603
Gilles Peskine449bd832023-01-11 14:50:10 +01003604 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003605
Jaeden Ameroab439972019-02-15 14:12:05 +00003606 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003607 TEST_EQUAL(psa_cipher_update(&func,
3608 input, sizeof(input),
3609 output, sizeof(output),
3610 &output_length),
3611 PSA_ERROR_BAD_STATE);
3612 TEST_EQUAL(psa_cipher_update(&init,
3613 input, sizeof(input),
3614 output, sizeof(output),
3615 &output_length),
3616 PSA_ERROR_BAD_STATE);
3617 TEST_EQUAL(psa_cipher_update(&zero,
3618 input, sizeof(input),
3619 output, sizeof(output),
3620 &output_length),
3621 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003622
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003623 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003624 PSA_ASSERT(psa_cipher_abort(&func));
3625 PSA_ASSERT(psa_cipher_abort(&init));
3626 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003627}
3628/* END_CASE */
3629
3630/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003631void cipher_setup(int key_type_arg,
3632 data_t *key,
3633 int alg_arg,
3634 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003635{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003636 psa_key_type_t key_type = key_type_arg;
3637 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003638 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003639 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003640 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003641#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003642 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3643#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003644
Gilles Peskine449bd832023-01-11 14:50:10 +01003645 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003646
Gilles Peskine449bd832023-01-11 14:50:10 +01003647 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3648 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003649 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003650 }
3651 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003652
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003653 /* The operation object should be reusable. */
3654#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003655 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3656 smoke_test_key_data,
3657 sizeof(smoke_test_key_data),
3658 KNOWN_SUPPORTED_CIPHER_ALG,
3659 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003660 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003661 }
3662 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003663#endif
3664
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003665exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003666 psa_cipher_abort(&operation);
3667 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003668}
3669/* END_CASE */
3670
Ronald Cronee414c72021-03-18 18:50:08 +01003671/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003672void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003673{
Ronald Cron5425a212020-08-04 14:58:35 +02003674 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003675 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3676 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003677 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003678 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003679 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003680 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003681 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003682 0xaa, 0xaa, 0xaa, 0xaa
3683 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003684 const uint8_t text[] = {
3685 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003686 0xbb, 0xbb, 0xbb, 0xbb
3687 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003688 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003689 size_t length = 0;
3690
Gilles Peskine449bd832023-01-11 14:50:10 +01003691 PSA_ASSERT(psa_crypto_init());
3692 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3693 psa_set_key_algorithm(&attributes, alg);
3694 psa_set_key_type(&attributes, key_type);
3695 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3696 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003697
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003698 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003699 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3700 ASSERT_OPERATION_IS_ACTIVE(operation);
3701 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3702 PSA_ERROR_BAD_STATE);
3703 ASSERT_OPERATION_IS_INACTIVE(operation);
3704 PSA_ASSERT(psa_cipher_abort(&operation));
3705 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003706
3707 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003708 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3709 ASSERT_OPERATION_IS_ACTIVE(operation);
3710 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3711 PSA_ERROR_BAD_STATE);
3712 ASSERT_OPERATION_IS_INACTIVE(operation);
3713 PSA_ASSERT(psa_cipher_abort(&operation));
3714 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003715
Jaeden Ameroab439972019-02-15 14:12:05 +00003716 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003717 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3718 buffer, sizeof(buffer),
3719 &length),
3720 PSA_ERROR_BAD_STATE);
3721 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003722
3723 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003724 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3725 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3726 buffer, sizeof(buffer),
3727 &length));
3728 ASSERT_OPERATION_IS_ACTIVE(operation);
3729 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3730 buffer, sizeof(buffer),
3731 &length),
3732 PSA_ERROR_BAD_STATE);
3733 ASSERT_OPERATION_IS_INACTIVE(operation);
3734 PSA_ASSERT(psa_cipher_abort(&operation));
3735 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003736
3737 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003738 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3739 PSA_ASSERT(psa_cipher_set_iv(&operation,
3740 iv, sizeof(iv)));
3741 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3742 buffer, sizeof(buffer),
3743 &length),
3744 PSA_ERROR_BAD_STATE);
3745 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003746
3747 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003748 TEST_EQUAL(psa_cipher_set_iv(&operation,
3749 iv, sizeof(iv)),
3750 PSA_ERROR_BAD_STATE);
3751 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003752
3753 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003754 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3755 PSA_ASSERT(psa_cipher_set_iv(&operation,
3756 iv, sizeof(iv)));
3757 ASSERT_OPERATION_IS_ACTIVE(operation);
3758 TEST_EQUAL(psa_cipher_set_iv(&operation,
3759 iv, sizeof(iv)),
3760 PSA_ERROR_BAD_STATE);
3761 ASSERT_OPERATION_IS_INACTIVE(operation);
3762 PSA_ASSERT(psa_cipher_abort(&operation));
3763 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003764
3765 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003766 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3767 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3768 buffer, sizeof(buffer),
3769 &length));
3770 TEST_EQUAL(psa_cipher_set_iv(&operation,
3771 iv, sizeof(iv)),
3772 PSA_ERROR_BAD_STATE);
3773 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003774
3775 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003776 TEST_EQUAL(psa_cipher_update(&operation,
3777 text, sizeof(text),
3778 buffer, sizeof(buffer),
3779 &length),
3780 PSA_ERROR_BAD_STATE);
3781 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003782
3783 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003784 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3785 ASSERT_OPERATION_IS_ACTIVE(operation);
3786 TEST_EQUAL(psa_cipher_update(&operation,
3787 text, sizeof(text),
3788 buffer, sizeof(buffer),
3789 &length),
3790 PSA_ERROR_BAD_STATE);
3791 ASSERT_OPERATION_IS_INACTIVE(operation);
3792 PSA_ASSERT(psa_cipher_abort(&operation));
3793 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003794
3795 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003796 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3797 PSA_ASSERT(psa_cipher_set_iv(&operation,
3798 iv, sizeof(iv)));
3799 PSA_ASSERT(psa_cipher_finish(&operation,
3800 buffer, sizeof(buffer), &length));
3801 TEST_EQUAL(psa_cipher_update(&operation,
3802 text, sizeof(text),
3803 buffer, sizeof(buffer),
3804 &length),
3805 PSA_ERROR_BAD_STATE);
3806 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003807
3808 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003809 TEST_EQUAL(psa_cipher_finish(&operation,
3810 buffer, sizeof(buffer), &length),
3811 PSA_ERROR_BAD_STATE);
3812 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003813
3814 /* Call finish 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));
Jaeden Ameroab439972019-02-15 14:12:05 +00003816 /* Not calling update means we are encrypting an empty buffer, which is OK
3817 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003818 ASSERT_OPERATION_IS_ACTIVE(operation);
3819 TEST_EQUAL(psa_cipher_finish(&operation,
3820 buffer, sizeof(buffer), &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 finish twice in a row. */
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_finish(&operation,
3833 buffer, sizeof(buffer), &length),
3834 PSA_ERROR_BAD_STATE);
3835 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003836
Gilles Peskine449bd832023-01-11 14:50:10 +01003837 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003838
Jaeden Ameroab439972019-02-15 14:12:05 +00003839exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003840 psa_cipher_abort(&operation);
3841 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003842}
3843/* END_CASE */
3844
3845/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003846void cipher_encrypt_fail(int alg_arg,
3847 int key_type_arg,
3848 data_t *key_data,
3849 data_t *input,
3850 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003851{
Ronald Cron5425a212020-08-04 14:58:35 +02003852 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003853 psa_status_t status;
3854 psa_key_type_t key_type = key_type_arg;
3855 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003856 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003857 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003858 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3859 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003860 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003861 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003862 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003863 size_t function_output_length;
3864 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003865 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3866
Gilles Peskine449bd832023-01-11 14:50:10 +01003867 if (PSA_ERROR_BAD_STATE != expected_status) {
3868 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003869
Gilles Peskine449bd832023-01-11 14:50:10 +01003870 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3871 psa_set_key_algorithm(&attributes, alg);
3872 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003873
Gilles Peskine449bd832023-01-11 14:50:10 +01003874 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3875 input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003876 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003877
Gilles Peskine449bd832023-01-11 14:50:10 +01003878 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3879 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003880 }
3881
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003882 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003883 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3884 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003885
Gilles Peskine449bd832023-01-11 14:50:10 +01003886 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003887
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003888 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003889 status = psa_cipher_encrypt_setup(&operation, key, alg);
3890 if (status == PSA_SUCCESS) {
3891 if (alg != PSA_ALG_ECB_NO_PADDING) {
3892 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3893 iv, iv_size,
3894 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003895 }
3896
Gilles Peskine449bd832023-01-11 14:50:10 +01003897 status = psa_cipher_update(&operation, input->x, input->len,
3898 output, output_buffer_size,
3899 &function_output_length);
3900 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003901 output_length += function_output_length;
3902
Gilles Peskine449bd832023-01-11 14:50:10 +01003903 status = psa_cipher_finish(&operation, output + output_length,
3904 output_buffer_size - output_length,
3905 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003906
Gilles Peskine449bd832023-01-11 14:50:10 +01003907 TEST_EQUAL(status, expected_status);
3908 } else {
3909 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003910 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003911 } else {
3912 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003913 }
3914
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003915exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003916 psa_cipher_abort(&operation);
3917 mbedtls_free(output);
3918 psa_destroy_key(key);
3919 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003920}
3921/* END_CASE */
3922
3923/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003924void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3925 data_t *input, int iv_length,
3926 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003927{
3928 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3929 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3930 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3931 size_t output_buffer_size = 0;
3932 unsigned char *output = NULL;
3933
Gilles Peskine449bd832023-01-11 14:50:10 +01003934 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003935 TEST_CALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003936
Gilles Peskine449bd832023-01-11 14:50:10 +01003937 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003938
Gilles Peskine449bd832023-01-11 14:50:10 +01003939 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3940 psa_set_key_algorithm(&attributes, alg);
3941 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003942
Gilles Peskine449bd832023-01-11 14:50:10 +01003943 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3944 &key));
3945 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3946 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3947 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003948
3949exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003950 psa_cipher_abort(&operation);
3951 mbedtls_free(output);
3952 psa_destroy_key(key);
3953 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003954}
3955/* END_CASE */
3956
3957/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003958void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3959 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003960{
3961 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3962 psa_key_type_t key_type = key_type_arg;
3963 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003964 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3965 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003966 unsigned char *output = NULL;
3967 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003968 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003969 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3970
Gilles Peskine449bd832023-01-11 14:50:10 +01003971 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003972
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003973 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003974 TEST_LE_U(ciphertext->len,
3975 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3976 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3977 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3978 TEST_LE_U(plaintext->len,
3979 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3980 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3981 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003982
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003983
3984 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003985 psa_set_key_usage_flags(&attributes,
3986 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3987 psa_set_key_algorithm(&attributes, alg);
3988 psa_set_key_type(&attributes, key_type);
3989 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3990 &key));
3991 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3992 plaintext->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003993 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003994
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003995 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003996 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3997 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3998 PSA_ERROR_BAD_STATE);
3999 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
4000 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
4001 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02004002
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004003 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01004004 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4005 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
4006 &length),
4007 PSA_ERROR_BAD_STATE);
4008 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
4009 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
4010 &length),
4011 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004012
Gilles Peskine286c3142022-04-20 17:09:38 +02004013 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01004014 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02004015 output_length = 0;
4016 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004017 PSA_ASSERT(psa_cipher_update(&operation,
4018 plaintext->x, plaintext->len,
4019 output, output_buffer_size,
4020 &length));
4021 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02004022 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004023 PSA_ASSERT(psa_cipher_finish(&operation,
4024 mbedtls_buffer_offset(output, output_length),
4025 output_buffer_size - output_length,
4026 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02004027 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004028 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004029 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004030
Gilles Peskine286c3142022-04-20 17:09:38 +02004031 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02004033 output_length = 0;
4034 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 PSA_ASSERT(psa_cipher_update(&operation,
4036 ciphertext->x, ciphertext->len,
4037 output, output_buffer_size,
4038 &length));
4039 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02004040 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004041 PSA_ASSERT(psa_cipher_finish(&operation,
4042 mbedtls_buffer_offset(output, output_length),
4043 output_buffer_size - output_length,
4044 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02004045 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004046 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004047 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004048
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004049 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004050 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004051 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
4052 output, output_buffer_size,
4053 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004054 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004055 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004056
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004057 /* One-shot decryption */
4058 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
4060 output, output_buffer_size,
4061 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004062 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004063 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004064
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004065exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004066 PSA_ASSERT(psa_cipher_abort(&operation));
4067 mbedtls_free(output);
4068 psa_cipher_abort(&operation);
4069 psa_destroy_key(key);
4070 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004071}
4072/* END_CASE */
4073
4074/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004075void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004076{
4077 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4078 psa_algorithm_t alg = alg_arg;
4079 psa_key_type_t key_type = key_type_arg;
4080 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4081 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4082 psa_status_t status;
4083
Gilles Peskine449bd832023-01-11 14:50:10 +01004084 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004085
Gilles Peskine449bd832023-01-11 14:50:10 +01004086 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4087 psa_set_key_algorithm(&attributes, alg);
4088 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004089
4090 /* Usage of either of these two size macros would cause divide by zero
4091 * with incorrect key types previously. Input length should be irrelevant
4092 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004093 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4094 0);
4095 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004096
4097
Gilles Peskine449bd832023-01-11 14:50:10 +01004098 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4099 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004100
4101 /* Should fail due to invalid alg type (to support invalid key type).
4102 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004103 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004104
Gilles Peskine449bd832023-01-11 14:50:10 +01004105 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004106
4107exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004108 psa_cipher_abort(&operation);
4109 psa_destroy_key(key);
4110 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004111}
4112/* END_CASE */
4113
4114/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004115void cipher_encrypt_validation(int alg_arg,
4116 int key_type_arg,
4117 data_t *key_data,
4118 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004119{
4120 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4121 psa_key_type_t key_type = key_type_arg;
4122 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004124 unsigned char *output1 = NULL;
4125 size_t output1_buffer_size = 0;
4126 size_t output1_length = 0;
4127 unsigned char *output2 = NULL;
4128 size_t output2_buffer_size = 0;
4129 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004130 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004131 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004132 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004133
Gilles Peskine449bd832023-01-11 14:50:10 +01004134 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004135
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4137 psa_set_key_algorithm(&attributes, alg);
4138 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004139
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4141 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4142 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004143 TEST_CALLOC(output1, output1_buffer_size);
4144 TEST_CALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004145
Gilles Peskine449bd832023-01-11 14:50:10 +01004146 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4147 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004148
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004149 /* The one-shot cipher encryption uses generated iv so validating
4150 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4152 output1_buffer_size, &output1_length));
4153 TEST_LE_U(output1_length,
4154 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4155 TEST_LE_U(output1_length,
4156 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004157
Gilles Peskine449bd832023-01-11 14:50:10 +01004158 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4159 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004160
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 PSA_ASSERT(psa_cipher_update(&operation,
4162 input->x, input->len,
4163 output2, output2_buffer_size,
4164 &function_output_length));
4165 TEST_LE_U(function_output_length,
4166 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4167 TEST_LE_U(function_output_length,
4168 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004169 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004170
Gilles Peskine449bd832023-01-11 14:50:10 +01004171 PSA_ASSERT(psa_cipher_finish(&operation,
4172 output2 + output2_length,
4173 output2_buffer_size - output2_length,
4174 &function_output_length));
4175 TEST_LE_U(function_output_length,
4176 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4177 TEST_LE_U(function_output_length,
4178 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004179 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004180
Gilles Peskine449bd832023-01-11 14:50:10 +01004181 PSA_ASSERT(psa_cipher_abort(&operation));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004182 TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004183 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004184
Gilles Peskine50e586b2018-06-08 14:28:46 +02004185exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004186 psa_cipher_abort(&operation);
4187 mbedtls_free(output1);
4188 mbedtls_free(output2);
4189 psa_destroy_key(key);
4190 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004191}
4192/* END_CASE */
4193
4194/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004195void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4196 data_t *key_data, data_t *iv,
4197 data_t *input,
4198 int first_part_size_arg,
4199 int output1_length_arg, int output2_length_arg,
4200 data_t *expected_output,
4201 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004202{
Ronald Cron5425a212020-08-04 14:58:35 +02004203 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004204 psa_key_type_t key_type = key_type_arg;
4205 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004206 psa_status_t status;
4207 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004208 size_t first_part_size = first_part_size_arg;
4209 size_t output1_length = output1_length_arg;
4210 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004211 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004212 size_t output_buffer_size = 0;
4213 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004214 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004215 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004216 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004217
Gilles Peskine449bd832023-01-11 14:50:10 +01004218 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004219
Gilles Peskine449bd832023-01-11 14:50:10 +01004220 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4221 psa_set_key_algorithm(&attributes, alg);
4222 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004223
Gilles Peskine449bd832023-01-11 14:50:10 +01004224 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4225 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004226
Gilles Peskine449bd832023-01-11 14:50:10 +01004227 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004228
Gilles Peskine449bd832023-01-11 14:50:10 +01004229 if (iv->len > 0) {
4230 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004231 }
4232
Gilles Peskine449bd832023-01-11 14:50:10 +01004233 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4234 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004235 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004236
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 TEST_LE_U(first_part_size, input->len);
4238 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4239 output, output_buffer_size,
4240 &function_output_length));
4241 TEST_ASSERT(function_output_length == output1_length);
4242 TEST_LE_U(function_output_length,
4243 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4244 TEST_LE_U(function_output_length,
4245 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004246 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004247
Gilles Peskine449bd832023-01-11 14:50:10 +01004248 if (first_part_size < input->len) {
4249 PSA_ASSERT(psa_cipher_update(&operation,
4250 input->x + first_part_size,
4251 input->len - first_part_size,
4252 (output_buffer_size == 0 ? NULL :
4253 output + total_output_length),
4254 output_buffer_size - total_output_length,
4255 &function_output_length));
4256 TEST_ASSERT(function_output_length == output2_length);
4257 TEST_LE_U(function_output_length,
4258 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4259 alg,
4260 input->len - first_part_size));
4261 TEST_LE_U(function_output_length,
4262 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004263 total_output_length += function_output_length;
4264 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004265
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 status = psa_cipher_finish(&operation,
4267 (output_buffer_size == 0 ? NULL :
4268 output + total_output_length),
4269 output_buffer_size - total_output_length,
4270 &function_output_length);
4271 TEST_LE_U(function_output_length,
4272 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4273 TEST_LE_U(function_output_length,
4274 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004275 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004276 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004277
Gilles Peskine449bd832023-01-11 14:50:10 +01004278 if (expected_status == PSA_SUCCESS) {
4279 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004280
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004281 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004282 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004283 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004284
4285exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 psa_cipher_abort(&operation);
4287 mbedtls_free(output);
4288 psa_destroy_key(key);
4289 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004290}
4291/* END_CASE */
4292
4293/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004294void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4295 data_t *key_data, data_t *iv,
4296 data_t *input,
4297 int first_part_size_arg,
4298 int output1_length_arg, int output2_length_arg,
4299 data_t *expected_output,
4300 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004301{
Ronald Cron5425a212020-08-04 14:58:35 +02004302 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004303 psa_key_type_t key_type = key_type_arg;
4304 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004305 psa_status_t status;
4306 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004307 size_t first_part_size = first_part_size_arg;
4308 size_t output1_length = output1_length_arg;
4309 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004310 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004311 size_t output_buffer_size = 0;
4312 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004313 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004314 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004315 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004316
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004318
Gilles Peskine449bd832023-01-11 14:50:10 +01004319 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4320 psa_set_key_algorithm(&attributes, alg);
4321 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004322
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4324 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004325
Gilles Peskine449bd832023-01-11 14:50:10 +01004326 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004327
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 if (iv->len > 0) {
4329 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004330 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004331
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4333 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004334 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004335
Gilles Peskine449bd832023-01-11 14:50:10 +01004336 TEST_LE_U(first_part_size, input->len);
4337 PSA_ASSERT(psa_cipher_update(&operation,
4338 input->x, first_part_size,
4339 output, output_buffer_size,
4340 &function_output_length));
4341 TEST_ASSERT(function_output_length == output1_length);
4342 TEST_LE_U(function_output_length,
4343 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4344 TEST_LE_U(function_output_length,
4345 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004346 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004347
Gilles Peskine449bd832023-01-11 14:50:10 +01004348 if (first_part_size < input->len) {
4349 PSA_ASSERT(psa_cipher_update(&operation,
4350 input->x + first_part_size,
4351 input->len - first_part_size,
4352 (output_buffer_size == 0 ? NULL :
4353 output + total_output_length),
4354 output_buffer_size - total_output_length,
4355 &function_output_length));
4356 TEST_ASSERT(function_output_length == output2_length);
4357 TEST_LE_U(function_output_length,
4358 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4359 alg,
4360 input->len - first_part_size));
4361 TEST_LE_U(function_output_length,
4362 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004363 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004364 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004365
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 status = psa_cipher_finish(&operation,
4367 (output_buffer_size == 0 ? NULL :
4368 output + total_output_length),
4369 output_buffer_size - total_output_length,
4370 &function_output_length);
4371 TEST_LE_U(function_output_length,
4372 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4373 TEST_LE_U(function_output_length,
4374 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004375 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004377
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 if (expected_status == PSA_SUCCESS) {
4379 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004380
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004381 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004382 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004383 }
4384
Gilles Peskine50e586b2018-06-08 14:28:46 +02004385exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 psa_cipher_abort(&operation);
4387 mbedtls_free(output);
4388 psa_destroy_key(key);
4389 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004390}
4391/* END_CASE */
4392
Gilles Peskine50e586b2018-06-08 14:28:46 +02004393/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004394void cipher_decrypt_fail(int alg_arg,
4395 int key_type_arg,
4396 data_t *key_data,
4397 data_t *iv,
4398 data_t *input_arg,
4399 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004400{
4401 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4402 psa_status_t status;
4403 psa_key_type_t key_type = key_type_arg;
4404 psa_algorithm_t alg = alg_arg;
4405 psa_status_t expected_status = expected_status_arg;
4406 unsigned char *input = NULL;
4407 size_t input_buffer_size = 0;
4408 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004409 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004410 size_t output_buffer_size = 0;
4411 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004412 size_t function_output_length;
4413 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004414 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4415
Gilles Peskine449bd832023-01-11 14:50:10 +01004416 if (PSA_ERROR_BAD_STATE != expected_status) {
4417 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004418
Gilles Peskine449bd832023-01-11 14:50:10 +01004419 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4420 psa_set_key_algorithm(&attributes, alg);
4421 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004422
Gilles Peskine449bd832023-01-11 14:50:10 +01004423 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4424 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004425 }
4426
4427 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004428 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4429 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004430 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004431 memcpy(input, iv->x, iv->len);
4432 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004433 }
4434
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004436 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004437
Neil Armstrong66a479f2022-02-07 15:41:19 +01004438 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4440 output_buffer_size, &output_length);
4441 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004442
Neil Armstrong66a479f2022-02-07 15:41:19 +01004443 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 status = psa_cipher_decrypt_setup(&operation, key, alg);
4445 if (status == PSA_SUCCESS) {
4446 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4447 input_arg->len) +
4448 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004449 TEST_CALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004450
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 if (iv->len > 0) {
4452 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004453
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 if (status != PSA_SUCCESS) {
4455 TEST_EQUAL(status, expected_status);
4456 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004457 }
4458
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 if (status == PSA_SUCCESS) {
4460 status = psa_cipher_update(&operation,
4461 input_arg->x, input_arg->len,
4462 output_multi, output_buffer_size,
4463 &function_output_length);
4464 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004465 output_length = function_output_length;
4466
Gilles Peskine449bd832023-01-11 14:50:10 +01004467 status = psa_cipher_finish(&operation,
4468 output_multi + output_length,
4469 output_buffer_size - output_length,
4470 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004471
Gilles Peskine449bd832023-01-11 14:50:10 +01004472 TEST_EQUAL(status, expected_status);
4473 } else {
4474 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004475 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004476 } else {
4477 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004478 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 } else {
4480 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004481 }
4482
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004483exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 psa_cipher_abort(&operation);
4485 mbedtls_free(input);
4486 mbedtls_free(output);
4487 mbedtls_free(output_multi);
4488 psa_destroy_key(key);
4489 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004490}
4491/* END_CASE */
4492
4493/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004494void cipher_decrypt(int alg_arg,
4495 int key_type_arg,
4496 data_t *key_data,
4497 data_t *iv,
4498 data_t *input_arg,
4499 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004500{
4501 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4502 psa_key_type_t key_type = key_type_arg;
4503 psa_algorithm_t alg = alg_arg;
4504 unsigned char *input = NULL;
4505 size_t input_buffer_size = 0;
4506 unsigned char *output = NULL;
4507 size_t output_buffer_size = 0;
4508 size_t output_length = 0;
4509 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4510
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004512
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4514 psa_set_key_algorithm(&attributes, alg);
4515 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004516
4517 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004518 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4519 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004520 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 memcpy(input, iv->x, iv->len);
4522 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004523 }
4524
Gilles Peskine449bd832023-01-11 14:50:10 +01004525 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004526 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004527
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4529 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004530
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4532 output_buffer_size, &output_length));
4533 TEST_LE_U(output_length,
4534 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4535 TEST_LE_U(output_length,
4536 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004537
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004538 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004539 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004540exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 mbedtls_free(input);
4542 mbedtls_free(output);
4543 psa_destroy_key(key);
4544 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004545}
4546/* END_CASE */
4547
4548/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004549void cipher_verify_output(int alg_arg,
4550 int key_type_arg,
4551 data_t *key_data,
4552 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004553{
Ronald Cron5425a212020-08-04 14:58:35 +02004554 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004555 psa_key_type_t key_type = key_type_arg;
4556 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004557 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004558 size_t output1_size = 0;
4559 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004560 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004561 size_t output2_size = 0;
4562 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004563 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004564
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004566
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4568 psa_set_key_algorithm(&attributes, alg);
4569 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004570
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4572 &key));
4573 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004574 TEST_CALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004575
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4577 output1, output1_size,
4578 &output1_length));
4579 TEST_LE_U(output1_length,
4580 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4581 TEST_LE_U(output1_length,
4582 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004583
4584 output2_size = output1_length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004585 TEST_CALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004586
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4588 output2, output2_size,
4589 &output2_length));
4590 TEST_LE_U(output2_length,
4591 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4592 TEST_LE_U(output2_length,
4593 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004594
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004595 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004596
4597exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004598 mbedtls_free(output1);
4599 mbedtls_free(output2);
4600 psa_destroy_key(key);
4601 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004602}
4603/* END_CASE */
4604
4605/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004606void cipher_verify_output_multipart(int alg_arg,
4607 int key_type_arg,
4608 data_t *key_data,
4609 data_t *input,
4610 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004611{
Ronald Cron5425a212020-08-04 14:58:35 +02004612 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004613 psa_key_type_t key_type = key_type_arg;
4614 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004615 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004617 size_t iv_size = 16;
4618 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004619 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004620 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004621 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004622 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004623 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004624 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004625 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004626 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4627 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004628 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004629
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004631
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4633 psa_set_key_algorithm(&attributes, alg);
4634 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004635
Gilles Peskine449bd832023-01-11 14:50:10 +01004636 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4637 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004638
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4640 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004641
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 if (alg != PSA_ALG_ECB_NO_PADDING) {
4643 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4644 iv, iv_size,
4645 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004646 }
4647
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4649 TEST_LE_U(output1_buffer_size,
4650 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004651 TEST_CALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004652
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004654
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4656 output1, output1_buffer_size,
4657 &function_output_length));
4658 TEST_LE_U(function_output_length,
4659 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4660 TEST_LE_U(function_output_length,
4661 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004662 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004663
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 PSA_ASSERT(psa_cipher_update(&operation1,
4665 input->x + first_part_size,
4666 input->len - first_part_size,
4667 output1, output1_buffer_size,
4668 &function_output_length));
4669 TEST_LE_U(function_output_length,
4670 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4671 alg,
4672 input->len - first_part_size));
4673 TEST_LE_U(function_output_length,
4674 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004675 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004676
Gilles Peskine449bd832023-01-11 14:50:10 +01004677 PSA_ASSERT(psa_cipher_finish(&operation1,
4678 output1 + output1_length,
4679 output1_buffer_size - output1_length,
4680 &function_output_length));
4681 TEST_LE_U(function_output_length,
4682 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4683 TEST_LE_U(function_output_length,
4684 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004685 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004686
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004688
Gilles Peskine048b7f02018-06-08 14:20:49 +02004689 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 TEST_LE_U(output2_buffer_size,
4691 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4692 TEST_LE_U(output2_buffer_size,
4693 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004694 TEST_CALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004695
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 if (iv_length > 0) {
4697 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4698 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004699 }
Moran Pekerded84402018-06-06 16:36:50 +03004700
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4702 output2, output2_buffer_size,
4703 &function_output_length));
4704 TEST_LE_U(function_output_length,
4705 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4706 TEST_LE_U(function_output_length,
4707 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004708 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004709
Gilles Peskine449bd832023-01-11 14:50:10 +01004710 PSA_ASSERT(psa_cipher_update(&operation2,
4711 output1 + first_part_size,
4712 output1_length - first_part_size,
4713 output2, output2_buffer_size,
4714 &function_output_length));
4715 TEST_LE_U(function_output_length,
4716 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4717 alg,
4718 output1_length - first_part_size));
4719 TEST_LE_U(function_output_length,
4720 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004721 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004722
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 PSA_ASSERT(psa_cipher_finish(&operation2,
4724 output2 + output2_length,
4725 output2_buffer_size - output2_length,
4726 &function_output_length));
4727 TEST_LE_U(function_output_length,
4728 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4729 TEST_LE_U(function_output_length,
4730 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004731 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004732
Gilles Peskine449bd832023-01-11 14:50:10 +01004733 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004734
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004735 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004736
4737exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 psa_cipher_abort(&operation1);
4739 psa_cipher_abort(&operation2);
4740 mbedtls_free(output1);
4741 mbedtls_free(output2);
4742 psa_destroy_key(key);
4743 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004744}
4745/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004746
Gilles Peskine20035e32018-02-03 22:44:14 +01004747/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004748void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4749 int alg_arg,
4750 data_t *nonce,
4751 data_t *additional_data,
4752 data_t *input_data,
4753 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004754{
Ronald Cron5425a212020-08-04 14:58:35 +02004755 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004756 psa_key_type_t key_type = key_type_arg;
4757 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004758 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004759 unsigned char *output_data = NULL;
4760 size_t output_size = 0;
4761 size_t output_length = 0;
4762 unsigned char *output_data2 = NULL;
4763 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004764 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004765 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004766 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004767
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004769
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4771 psa_set_key_algorithm(&attributes, alg);
4772 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004773
Gilles Peskine449bd832023-01-11 14:50:10 +01004774 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4775 &key));
4776 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4777 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004778
Gilles Peskine449bd832023-01-11 14:50:10 +01004779 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4780 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004781 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4782 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004783 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4784 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4785 TEST_EQUAL(output_size,
4786 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4787 TEST_LE_U(output_size,
4788 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004789 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004790 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004791
Gilles Peskine449bd832023-01-11 14:50:10 +01004792 status = psa_aead_encrypt(key, alg,
4793 nonce->x, nonce->len,
4794 additional_data->x,
4795 additional_data->len,
4796 input_data->x, input_data->len,
4797 output_data, output_size,
4798 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004799
4800 /* If the operation is not supported, just skip and not fail in case the
4801 * encryption involves a common limitation of cryptography hardwares and
4802 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 if (status == PSA_ERROR_NOT_SUPPORTED) {
4804 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4805 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004806 }
4807
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004809
Gilles Peskine449bd832023-01-11 14:50:10 +01004810 if (PSA_SUCCESS == expected_result) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004811 TEST_CALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004812
Gilles Peskine003a4a92019-05-14 16:09:40 +02004813 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4814 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 TEST_EQUAL(input_data->len,
4816 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004817
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 TEST_LE_U(input_data->len,
4819 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004820
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 TEST_EQUAL(psa_aead_decrypt(key, alg,
4822 nonce->x, nonce->len,
4823 additional_data->x,
4824 additional_data->len,
4825 output_data, output_length,
4826 output_data2, output_length,
4827 &output_length2),
4828 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004829
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004830 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004831 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004832 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004833
Gilles Peskinea1cac842018-06-11 19:33:02 +02004834exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 psa_destroy_key(key);
4836 mbedtls_free(output_data);
4837 mbedtls_free(output_data2);
4838 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004839}
4840/* END_CASE */
4841
4842/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004843void aead_encrypt(int key_type_arg, data_t *key_data,
4844 int alg_arg,
4845 data_t *nonce,
4846 data_t *additional_data,
4847 data_t *input_data,
4848 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004849{
Ronald Cron5425a212020-08-04 14:58:35 +02004850 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004851 psa_key_type_t key_type = key_type_arg;
4852 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004853 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004854 unsigned char *output_data = NULL;
4855 size_t output_size = 0;
4856 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004857 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004858 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004859
Gilles Peskine449bd832023-01-11 14:50:10 +01004860 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004861
Gilles Peskine449bd832023-01-11 14:50:10 +01004862 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4863 psa_set_key_algorithm(&attributes, alg);
4864 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004865
Gilles Peskine449bd832023-01-11 14:50:10 +01004866 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4867 &key));
4868 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4869 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4872 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004873 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4874 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004875 TEST_EQUAL(output_size,
4876 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4877 TEST_LE_U(output_size,
4878 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004879 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004880
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 status = psa_aead_encrypt(key, alg,
4882 nonce->x, nonce->len,
4883 additional_data->x, additional_data->len,
4884 input_data->x, input_data->len,
4885 output_data, output_size,
4886 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004887
Ronald Cron28a45ed2021-02-09 20:35:42 +01004888 /* If the operation is not supported, just skip and not fail in case the
4889 * encryption involves a common limitation of cryptography hardwares and
4890 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 if (status == PSA_ERROR_NOT_SUPPORTED) {
4892 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4893 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004894 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004895
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 PSA_ASSERT(status);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004897 TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004898 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004899
Gilles Peskinea1cac842018-06-11 19:33:02 +02004900exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 psa_destroy_key(key);
4902 mbedtls_free(output_data);
4903 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004904}
4905/* END_CASE */
4906
4907/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004908void aead_decrypt(int key_type_arg, data_t *key_data,
4909 int alg_arg,
4910 data_t *nonce,
4911 data_t *additional_data,
4912 data_t *input_data,
4913 data_t *expected_data,
4914 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004915{
Ronald Cron5425a212020-08-04 14:58:35 +02004916 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004917 psa_key_type_t key_type = key_type_arg;
4918 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004919 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004920 unsigned char *output_data = NULL;
4921 size_t output_size = 0;
4922 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004923 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004924 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004925 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004926
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004928
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4930 psa_set_key_algorithm(&attributes, alg);
4931 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004932
Gilles Peskine449bd832023-01-11 14:50:10 +01004933 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4934 &key));
4935 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4936 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004937
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4939 alg);
4940 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4941 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004942 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4943 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004944 TEST_EQUAL(output_size,
4945 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4946 TEST_LE_U(output_size,
4947 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004948 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004949 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004950
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 status = psa_aead_decrypt(key, alg,
4952 nonce->x, nonce->len,
4953 additional_data->x,
4954 additional_data->len,
4955 input_data->x, input_data->len,
4956 output_data, output_size,
4957 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004958
Ronald Cron28a45ed2021-02-09 20:35:42 +01004959 /* If the operation is not supported, just skip and not fail in case the
4960 * decryption involves a common limitation of cryptography hardwares and
4961 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004962 if (status == PSA_ERROR_NOT_SUPPORTED) {
4963 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4964 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004965 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004966
Gilles Peskine449bd832023-01-11 14:50:10 +01004967 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004968
Gilles Peskine449bd832023-01-11 14:50:10 +01004969 if (expected_result == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004970 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004971 output_data, output_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004973
Gilles Peskinea1cac842018-06-11 19:33:02 +02004974exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004975 psa_destroy_key(key);
4976 mbedtls_free(output_data);
4977 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004978}
4979/* END_CASE */
4980
4981/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004982void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4983 int alg_arg,
4984 data_t *nonce,
4985 data_t *additional_data,
4986 data_t *input_data,
4987 int do_set_lengths,
4988 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004989{
Paul Elliottd3f82412021-06-16 16:52:21 +01004990 size_t ad_part_len = 0;
4991 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004992 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004993
Gilles Peskine449bd832023-01-11 14:50:10 +01004994 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4995 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004996
Gilles Peskine449bd832023-01-11 14:50:10 +01004997 if (do_set_lengths) {
4998 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004999 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005000 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005001 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005003 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005004
5005 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 if (!aead_multipart_internal_func(key_type_arg, key_data,
5007 alg_arg, nonce,
5008 additional_data,
5009 ad_part_len,
5010 input_data, -1,
5011 set_lengths_method,
5012 expected_output,
5013 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005014 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005015 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005016
Gilles Peskine449bd832023-01-11 14:50:10 +01005017 /* length(0) part, length(ad_part_len) part, length(0) part... */
5018 mbedtls_test_set_step(1000 + ad_part_len);
5019
5020 if (!aead_multipart_internal_func(key_type_arg, key_data,
5021 alg_arg, nonce,
5022 additional_data,
5023 ad_part_len,
5024 input_data, -1,
5025 set_lengths_method,
5026 expected_output,
5027 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005028 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005029 }
5030 }
5031
5032 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
5033 /* Split data into length(data_part_len) parts. */
5034 mbedtls_test_set_step(2000 + data_part_len);
5035
5036 if (do_set_lengths) {
5037 if (data_part_len & 0x01) {
5038 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5039 } else {
5040 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
5041 }
5042 }
5043
5044 if (!aead_multipart_internal_func(key_type_arg, key_data,
5045 alg_arg, nonce,
5046 additional_data, -1,
5047 input_data, data_part_len,
5048 set_lengths_method,
5049 expected_output,
5050 1, 0)) {
5051 break;
5052 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005053
5054 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005056
Gilles Peskine449bd832023-01-11 14:50:10 +01005057 if (!aead_multipart_internal_func(key_type_arg, key_data,
5058 alg_arg, nonce,
5059 additional_data, -1,
5060 input_data, data_part_len,
5061 set_lengths_method,
5062 expected_output,
5063 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005064 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005065 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005066 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005067
Paul Elliott8fc45162021-06-23 16:06:01 +01005068 /* Goto is required to silence warnings about unused labels, as we
5069 * don't actually do any test assertions in this function. */
5070 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005071}
5072/* END_CASE */
5073
5074/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005075void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5076 int alg_arg,
5077 data_t *nonce,
5078 data_t *additional_data,
5079 data_t *input_data,
5080 int do_set_lengths,
5081 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005082{
Paul Elliottd3f82412021-06-16 16:52:21 +01005083 size_t ad_part_len = 0;
5084 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005085 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005086
Gilles Peskine449bd832023-01-11 14:50:10 +01005087 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005088 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005089 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005090
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 if (do_set_lengths) {
5092 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005093 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005094 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005095 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005097 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005098
Gilles Peskine449bd832023-01-11 14:50:10 +01005099 if (!aead_multipart_internal_func(key_type_arg, key_data,
5100 alg_arg, nonce,
5101 additional_data,
5102 ad_part_len,
5103 input_data, -1,
5104 set_lengths_method,
5105 expected_output,
5106 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005107 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005108 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005109
5110 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005112
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 if (!aead_multipart_internal_func(key_type_arg, key_data,
5114 alg_arg, nonce,
5115 additional_data,
5116 ad_part_len,
5117 input_data, -1,
5118 set_lengths_method,
5119 expected_output,
5120 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005121 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005123 }
5124
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005126 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005127 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005128
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 if (do_set_lengths) {
5130 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005131 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005132 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005133 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005134 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005135 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005136
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 if (!aead_multipart_internal_func(key_type_arg, key_data,
5138 alg_arg, nonce,
5139 additional_data, -1,
5140 input_data, data_part_len,
5141 set_lengths_method,
5142 expected_output,
5143 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005144 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005145 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005146
5147 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005149
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 if (!aead_multipart_internal_func(key_type_arg, key_data,
5151 alg_arg, nonce,
5152 additional_data, -1,
5153 input_data, data_part_len,
5154 set_lengths_method,
5155 expected_output,
5156 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005157 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005159 }
5160
Paul Elliott8fc45162021-06-23 16:06:01 +01005161 /* Goto is required to silence warnings about unused labels, as we
5162 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005163 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005164}
5165/* END_CASE */
5166
5167/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005168void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5169 int alg_arg,
5170 int nonce_length,
5171 int expected_nonce_length_arg,
5172 data_t *additional_data,
5173 data_t *input_data,
5174 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005175{
5176
5177 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5178 psa_key_type_t key_type = key_type_arg;
5179 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005180 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005181 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5182 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5183 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005184 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005185 size_t actual_nonce_length = 0;
5186 size_t expected_nonce_length = expected_nonce_length_arg;
5187 unsigned char *output = NULL;
5188 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005189 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005190 size_t ciphertext_size = 0;
5191 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005192 size_t tag_length = 0;
5193 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005194
Gilles Peskine449bd832023-01-11 14:50:10 +01005195 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005196
Gilles Peskine449bd832023-01-11 14:50:10 +01005197 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5198 psa_set_key_algorithm(&attributes, alg);
5199 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005200
Gilles Peskine449bd832023-01-11 14:50:10 +01005201 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5202 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005203
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005205
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005207
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005208 TEST_CALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005209
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005211
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005213
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005214 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005215
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005217
5218 /* If the operation is not supported, just skip and not fail in case the
5219 * encryption involves a common limitation of cryptography hardwares and
5220 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 if (status == PSA_ERROR_NOT_SUPPORTED) {
5222 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5223 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005224 }
5225
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005227
Gilles Peskine449bd832023-01-11 14:50:10 +01005228 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5229 nonce_length,
5230 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005231
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005233
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005235
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 if (expected_status == PSA_SUCCESS) {
5237 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5238 alg));
5239 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005240
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005242
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005244 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5246 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005247
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5249 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005250
Gilles Peskine449bd832023-01-11 14:50:10 +01005251 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5252 output, output_size,
5253 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005254
Gilles Peskine449bd832023-01-11 14:50:10 +01005255 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5256 &ciphertext_length, tag_buffer,
5257 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005258 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005259
5260exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 psa_destroy_key(key);
5262 mbedtls_free(output);
5263 mbedtls_free(ciphertext);
5264 psa_aead_abort(&operation);
5265 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005266}
5267/* END_CASE */
5268
5269/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005270void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5271 int alg_arg,
5272 int nonce_length_arg,
5273 int set_lengths_method_arg,
5274 data_t *additional_data,
5275 data_t *input_data,
5276 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005277{
5278
5279 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5280 psa_key_type_t key_type = key_type_arg;
5281 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005282 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005283 uint8_t *nonce_buffer = NULL;
5284 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5285 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5286 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005287 unsigned char *output = NULL;
5288 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005289 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005290 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005291 size_t ciphertext_size = 0;
5292 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005293 size_t tag_length = 0;
5294 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005295 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005296 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005297
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005299
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5301 psa_set_key_algorithm(&attributes, alg);
5302 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005303
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5305 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005306
Gilles Peskine449bd832023-01-11 14:50:10 +01005307 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005308
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005310
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005311 TEST_CALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005312
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005314
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005316
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005317 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005318
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005320
5321 /* If the operation is not supported, just skip and not fail in case the
5322 * encryption involves a common limitation of cryptography hardwares and
5323 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 if (status == PSA_ERROR_NOT_SUPPORTED) {
5325 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5326 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005327 }
5328
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005330
Paul Elliott4023ffd2021-09-10 16:21:22 +01005331 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005332 if (nonce_length_arg == -1) {
5333 /* Arbitrary size buffer, to test zero length valid buffer. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005334 TEST_CALLOC(nonce_buffer, 4);
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 nonce_length = 0;
5336 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005337 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005338 nonce_length = (size_t) nonce_length_arg;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005339 TEST_CALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005340
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 if (nonce_buffer) {
5342 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005343 nonce_buffer[index] = 'a' + index;
5344 }
Paul Elliott66696b52021-08-16 18:42:41 +01005345 }
Paul Elliott863864a2021-07-23 17:28:31 +01005346 }
5347
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5349 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5350 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005351 }
5352
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005354
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005356
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 if (expected_status == PSA_SUCCESS) {
5358 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5359 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5360 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005361 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005363 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005364 }
Paul Elliott863864a2021-07-23 17:28:31 +01005365
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005366 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5368 additional_data->len),
5369 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005370
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5372 output, output_size,
5373 &ciphertext_length),
5374 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005375
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5377 &ciphertext_length, tag_buffer,
5378 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5379 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005380 }
5381
5382exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 psa_destroy_key(key);
5384 mbedtls_free(output);
5385 mbedtls_free(ciphertext);
5386 mbedtls_free(nonce_buffer);
5387 psa_aead_abort(&operation);
5388 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005389}
5390/* END_CASE */
5391
5392/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005393void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005394 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005395 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005396 data_t *nonce,
5397 data_t *additional_data,
5398 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005399 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005400{
5401
5402 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5403 psa_key_type_t key_type = key_type_arg;
5404 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005405 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005406 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5407 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5408 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005409 unsigned char *output = NULL;
5410 unsigned char *ciphertext = NULL;
5411 size_t output_size = output_size_arg;
5412 size_t ciphertext_size = 0;
5413 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005414 size_t tag_length = 0;
5415 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5416
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005418
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5420 psa_set_key_algorithm(&attributes, alg);
5421 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005422
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5424 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005425
Gilles Peskine449bd832023-01-11 14:50:10 +01005426 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005427
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005428 TEST_CALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005429
Gilles Peskine449bd832023-01-11 14:50:10 +01005430 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005431
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005432 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005433
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005435
5436 /* If the operation is not supported, just skip and not fail in case the
5437 * encryption involves a common limitation of cryptography hardwares and
5438 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 if (status == PSA_ERROR_NOT_SUPPORTED) {
5440 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5441 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005442 }
5443
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005445
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5447 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005448
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005450
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5452 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005453
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 status = psa_aead_update(&operation, input_data->x, input_data->len,
5455 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005456
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005458
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005460 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005461 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5462 &ciphertext_length, tag_buffer,
5463 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005464 }
5465
5466exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 psa_destroy_key(key);
5468 mbedtls_free(output);
5469 mbedtls_free(ciphertext);
5470 psa_aead_abort(&operation);
5471 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005472}
5473/* END_CASE */
5474
Paul Elliott91b021e2021-07-23 18:52:31 +01005475/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005476void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5477 int alg_arg,
5478 int finish_ciphertext_size_arg,
5479 int tag_size_arg,
5480 data_t *nonce,
5481 data_t *additional_data,
5482 data_t *input_data,
5483 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005484{
5485
5486 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5487 psa_key_type_t key_type = key_type_arg;
5488 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005489 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005490 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5491 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5492 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005493 unsigned char *ciphertext = NULL;
5494 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005495 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005496 size_t ciphertext_size = 0;
5497 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5499 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005500 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005501
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005503
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5505 psa_set_key_algorithm(&attributes, alg);
5506 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005507
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5509 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005510
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005512
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005514
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005515 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005516
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005517 TEST_CALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005518
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005519 TEST_CALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005520
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005522
5523 /* If the operation is not supported, just skip and not fail in case the
5524 * encryption involves a common limitation of cryptography hardwares and
5525 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 if (status == PSA_ERROR_NOT_SUPPORTED) {
5527 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5528 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005529 }
5530
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005532
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005534
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5536 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005537
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5539 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005540
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5542 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005543
5544 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 status = psa_aead_finish(&operation, finish_ciphertext,
5546 finish_ciphertext_size,
5547 &ciphertext_length, tag_buffer,
5548 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005549
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005551
5552exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005553 psa_destroy_key(key);
5554 mbedtls_free(ciphertext);
5555 mbedtls_free(finish_ciphertext);
5556 mbedtls_free(tag_buffer);
5557 psa_aead_abort(&operation);
5558 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005559}
5560/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005561
5562/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005563void aead_multipart_verify(int key_type_arg, data_t *key_data,
5564 int alg_arg,
5565 data_t *nonce,
5566 data_t *additional_data,
5567 data_t *input_data,
5568 data_t *tag,
5569 int tag_usage_arg,
5570 int expected_setup_status_arg,
5571 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005572{
5573 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5574 psa_key_type_t key_type = key_type_arg;
5575 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005576 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005577 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5578 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5579 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005580 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005581 unsigned char *plaintext = NULL;
5582 unsigned char *finish_plaintext = NULL;
5583 size_t plaintext_size = 0;
5584 size_t plaintext_length = 0;
5585 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005586 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005587 unsigned char *tag_buffer = NULL;
5588 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005589
Gilles Peskine449bd832023-01-11 14:50:10 +01005590 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005591
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5593 psa_set_key_algorithm(&attributes, alg);
5594 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005595
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5597 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005598
Gilles Peskine449bd832023-01-11 14:50:10 +01005599 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005600
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5602 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005603
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005604 TEST_CALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005605
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005607
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005608 TEST_CALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005609
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005611
5612 /* If the operation is not supported, just skip and not fail in case the
5613 * encryption involves a common limitation of cryptography hardwares and
5614 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 if (status == PSA_ERROR_NOT_SUPPORTED) {
5616 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5617 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005618 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005620
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005622 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005623 }
Paul Elliott9961a662021-09-17 19:19:02 +01005624
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005626
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005628
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 status = psa_aead_set_lengths(&operation, additional_data->len,
5630 input_data->len);
5631 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005632
Gilles Peskine449bd832023-01-11 14:50:10 +01005633 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5634 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005635
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5637 input_data->len,
5638 plaintext, plaintext_size,
5639 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005640
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005642 tag_buffer = tag->x;
5643 tag_size = tag->len;
5644 }
5645
Gilles Peskine449bd832023-01-11 14:50:10 +01005646 status = psa_aead_verify(&operation, finish_plaintext,
5647 verify_plaintext_size,
5648 &plaintext_length,
5649 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005650
Gilles Peskine449bd832023-01-11 14:50:10 +01005651 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005652
5653exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005654 psa_destroy_key(key);
5655 mbedtls_free(plaintext);
5656 mbedtls_free(finish_plaintext);
5657 psa_aead_abort(&operation);
5658 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005659}
5660/* END_CASE */
5661
Paul Elliott9961a662021-09-17 19:19:02 +01005662/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005663void aead_multipart_setup(int key_type_arg, data_t *key_data,
5664 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005665{
5666 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5667 psa_key_type_t key_type = key_type_arg;
5668 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005669 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005670 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5671 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5672 psa_status_t expected_status = expected_status_arg;
5673
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005675
Gilles Peskine449bd832023-01-11 14:50:10 +01005676 psa_set_key_usage_flags(&attributes,
5677 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5678 psa_set_key_algorithm(&attributes, alg);
5679 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005680
Gilles Peskine449bd832023-01-11 14:50:10 +01005681 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5682 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005683
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005685
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005687
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005689
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005691
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005693
5694exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 psa_destroy_key(key);
5696 psa_aead_abort(&operation);
5697 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005698}
5699/* END_CASE */
5700
5701/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005702void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5703 int alg_arg,
5704 data_t *nonce,
5705 data_t *additional_data,
5706 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005707{
5708 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5709 psa_key_type_t key_type = key_type_arg;
5710 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005711 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005712 unsigned char *output_data = NULL;
5713 unsigned char *final_data = NULL;
5714 size_t output_size = 0;
5715 size_t finish_output_size = 0;
5716 size_t output_length = 0;
5717 size_t key_bits = 0;
5718 size_t tag_length = 0;
5719 size_t tag_size = 0;
5720 size_t nonce_length = 0;
5721 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5722 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5723 size_t output_part_length = 0;
5724 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5725
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005727
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 psa_set_key_usage_flags(&attributes,
5729 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5730 psa_set_key_algorithm(&attributes, alg);
5731 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005732
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5734 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005735
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5737 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005738
Gilles Peskine449bd832023-01-11 14:50:10 +01005739 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005740
Gilles Peskine449bd832023-01-11 14:50:10 +01005741 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005742
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005744
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005745 TEST_CALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005746
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005748
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005750
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005751 TEST_CALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005752
5753 /* Test all operations error without calling setup first. */
5754
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5756 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005757
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005759
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5761 PSA_AEAD_NONCE_MAX_SIZE,
5762 &nonce_length),
5763 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005764
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005766
Paul Elliott481be342021-07-16 17:38:47 +01005767 /* ------------------------------------------------------- */
5768
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5770 input_data->len),
5771 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005772
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005774
Paul Elliott481be342021-07-16 17:38:47 +01005775 /* ------------------------------------------------------- */
5776
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5778 additional_data->len),
5779 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005780
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005782
Paul Elliott481be342021-07-16 17:38:47 +01005783 /* ------------------------------------------------------- */
5784
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5786 input_data->len, output_data,
5787 output_size, &output_length),
5788 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005789
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005791
Paul Elliott481be342021-07-16 17:38:47 +01005792 /* ------------------------------------------------------- */
5793
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5795 finish_output_size,
5796 &output_part_length,
5797 tag_buffer, tag_length,
5798 &tag_size),
5799 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005800
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005802
Paul Elliott481be342021-07-16 17:38:47 +01005803 /* ------------------------------------------------------- */
5804
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5806 finish_output_size,
5807 &output_part_length,
5808 tag_buffer,
5809 tag_length),
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
5814 /* Test for double setups. */
5815
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005817
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
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 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005826
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5828 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005829
Gilles Peskine449bd832023-01-11 14:50:10 +01005830 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005831
Paul Elliott374a2be2021-07-16 17:53:40 +01005832 /* ------------------------------------------------------- */
5833
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005835
Gilles Peskine449bd832023-01-11 14:50:10 +01005836 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5837 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005838
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005840
5841 /* ------------------------------------------------------- */
5842
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005844
Gilles Peskine449bd832023-01-11 14:50:10 +01005845 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5846 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005847
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005849
Paul Elliottc23a9a02021-06-21 18:32:46 +01005850 /* Test for not setting a nonce. */
5851
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005853
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5855 additional_data->len),
5856 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005857
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005859
Paul Elliott7f628422021-09-01 12:08:29 +01005860 /* ------------------------------------------------------- */
5861
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005863
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5865 input_data->len, output_data,
5866 output_size, &output_length),
5867 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005868
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005870
Paul Elliottbdc2c682021-09-21 18:37:10 +01005871 /* ------------------------------------------------------- */
5872
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005874
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5876 finish_output_size,
5877 &output_part_length,
5878 tag_buffer, tag_length,
5879 &tag_size),
5880 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005881
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005883
5884 /* ------------------------------------------------------- */
5885
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005887
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5889 finish_output_size,
5890 &output_part_length,
5891 tag_buffer,
5892 tag_length),
5893 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005894
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005896
Paul Elliottc23a9a02021-06-21 18:32:46 +01005897 /* Test for double setting nonce. */
5898
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005900
Gilles Peskine449bd832023-01-11 14:50:10 +01005901 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005902
Gilles Peskine449bd832023-01-11 14:50:10 +01005903 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5904 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005905
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005907
Paul Elliott374a2be2021-07-16 17:53:40 +01005908 /* Test for double generating nonce. */
5909
Gilles Peskine449bd832023-01-11 14:50:10 +01005910 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005911
Gilles Peskine449bd832023-01-11 14:50:10 +01005912 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5913 PSA_AEAD_NONCE_MAX_SIZE,
5914 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005915
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5917 PSA_AEAD_NONCE_MAX_SIZE,
5918 &nonce_length),
5919 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005920
5921
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005923
5924 /* Test for generate nonce then set and vice versa */
5925
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005927
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5929 PSA_AEAD_NONCE_MAX_SIZE,
5930 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005931
Gilles Peskine449bd832023-01-11 14:50:10 +01005932 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5933 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005934
Gilles Peskine449bd832023-01-11 14:50:10 +01005935 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005936
Andrzej Kurekad837522021-12-15 15:28:49 +01005937 /* Test for generating nonce after calling set lengths */
5938
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005940
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5942 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005943
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5945 PSA_AEAD_NONCE_MAX_SIZE,
5946 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005947
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005949
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005950 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005951
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005953
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 if (operation.alg == PSA_ALG_CCM) {
5955 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5956 input_data->len),
5957 PSA_ERROR_INVALID_ARGUMENT);
5958 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5959 PSA_AEAD_NONCE_MAX_SIZE,
5960 &nonce_length),
5961 PSA_ERROR_BAD_STATE);
5962 } else {
5963 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5964 input_data->len));
5965 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5966 PSA_AEAD_NONCE_MAX_SIZE,
5967 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005968 }
5969
Gilles Peskine449bd832023-01-11 14:50:10 +01005970 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005971
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005972 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005973#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005975
Gilles Peskine449bd832023-01-11 14:50:10 +01005976 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5977 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5978 input_data->len),
5979 PSA_ERROR_INVALID_ARGUMENT);
5980 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5981 PSA_AEAD_NONCE_MAX_SIZE,
5982 &nonce_length),
5983 PSA_ERROR_BAD_STATE);
5984 } else {
5985 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5986 input_data->len));
5987 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5988 PSA_AEAD_NONCE_MAX_SIZE,
5989 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005990 }
5991
Gilles Peskine449bd832023-01-11 14:50:10 +01005992 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005993#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005994
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005995 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005996
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005998
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6000 PSA_AEAD_NONCE_MAX_SIZE,
6001 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01006002
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 if (operation.alg == PSA_ALG_CCM) {
6004 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6005 input_data->len),
6006 PSA_ERROR_INVALID_ARGUMENT);
6007 } else {
6008 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6009 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006010 }
6011
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006013
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006014 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006015 /* Test for setting nonce after calling set lengths */
6016
Gilles Peskine449bd832023-01-11 14:50:10 +01006017 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006018
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6020 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006021
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006023
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006025
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006026 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006027
Gilles Peskine449bd832023-01-11 14:50:10 +01006028 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006029
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 if (operation.alg == PSA_ALG_CCM) {
6031 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6032 input_data->len),
6033 PSA_ERROR_INVALID_ARGUMENT);
6034 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6035 PSA_ERROR_BAD_STATE);
6036 } else {
6037 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6038 input_data->len));
6039 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006040 }
6041
Gilles Peskine449bd832023-01-11 14:50:10 +01006042 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006043
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006044 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00006045#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006047
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
6049 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
6050 input_data->len),
6051 PSA_ERROR_INVALID_ARGUMENT);
6052 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6053 PSA_ERROR_BAD_STATE);
6054 } else {
6055 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6056 input_data->len));
6057 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006058 }
6059
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00006061#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006062
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006063 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006064
Gilles Peskine449bd832023-01-11 14:50:10 +01006065 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006066
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006068
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 if (operation.alg == PSA_ALG_CCM) {
6070 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6071 input_data->len),
6072 PSA_ERROR_INVALID_ARGUMENT);
6073 } else {
6074 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6075 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006076 }
6077
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006079
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006080 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006081#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006083
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 if (operation.alg == PSA_ALG_GCM) {
6085 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6086 SIZE_MAX),
6087 PSA_ERROR_INVALID_ARGUMENT);
6088 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6089 PSA_ERROR_BAD_STATE);
6090 } else if (operation.alg != PSA_ALG_CCM) {
6091 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6092 SIZE_MAX));
6093 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006094 }
6095
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006097#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006098
Tom Cosgrove1797b052022-12-04 17:19:59 +00006099 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006100#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006102
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006104
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 if (operation.alg == PSA_ALG_GCM) {
6106 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6107 SIZE_MAX),
6108 PSA_ERROR_INVALID_ARGUMENT);
6109 } else if (operation.alg != PSA_ALG_CCM) {
6110 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6111 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006112 }
6113
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006115#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006116
6117 /* ------------------------------------------------------- */
6118
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006120
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006122
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6124 PSA_AEAD_NONCE_MAX_SIZE,
6125 &nonce_length),
6126 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006127
Gilles Peskine449bd832023-01-11 14:50:10 +01006128 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006129
Paul Elliott7220cae2021-06-22 17:25:57 +01006130 /* Test for generating nonce in decrypt setup. */
6131
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006133
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6135 PSA_AEAD_NONCE_MAX_SIZE,
6136 &nonce_length),
6137 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006138
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006140
Paul Elliottc23a9a02021-06-21 18:32:46 +01006141 /* Test for setting lengths twice. */
6142
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006144
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006146
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6148 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006149
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6151 input_data->len),
6152 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006153
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006155
Andrzej Kurekad837522021-12-15 15:28:49 +01006156 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006157
Gilles Peskine449bd832023-01-11 14:50:10 +01006158 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006159
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006161
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006163
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6165 additional_data->len),
6166 PSA_ERROR_BAD_STATE);
6167 } else {
6168 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6169 additional_data->len));
6170
6171 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6172 input_data->len),
6173 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006174 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006175 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006176
6177 /* ------------------------------------------------------- */
6178
Gilles Peskine449bd832023-01-11 14:50:10 +01006179 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006180
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006182
Gilles Peskine449bd832023-01-11 14:50:10 +01006183 if (operation.alg == PSA_ALG_CCM) {
6184 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6185 input_data->len, output_data,
6186 output_size, &output_length),
6187 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006188
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 } else {
6190 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6191 input_data->len, output_data,
6192 output_size, &output_length));
6193
6194 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6195 input_data->len),
6196 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006197 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006199
6200 /* ------------------------------------------------------- */
6201
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006203
Gilles Peskine449bd832023-01-11 14:50:10 +01006204 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006205
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 if (operation.alg == PSA_ALG_CCM) {
6207 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6208 finish_output_size,
6209 &output_part_length,
6210 tag_buffer, tag_length,
6211 &tag_size));
6212 } else {
6213 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6214 finish_output_size,
6215 &output_part_length,
6216 tag_buffer, tag_length,
6217 &tag_size));
6218
6219 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6220 input_data->len),
6221 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006222 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006223 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006224
6225 /* Test for setting lengths after generating nonce + already starting data. */
6226
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006228
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6230 PSA_AEAD_NONCE_MAX_SIZE,
6231 &nonce_length));
6232 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006233
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6235 additional_data->len),
6236 PSA_ERROR_BAD_STATE);
6237 } else {
6238 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6239 additional_data->len));
6240
6241 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6242 input_data->len),
6243 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006244 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006246
6247 /* ------------------------------------------------------- */
6248
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006250
Gilles Peskine449bd832023-01-11 14:50:10 +01006251 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6252 PSA_AEAD_NONCE_MAX_SIZE,
6253 &nonce_length));
6254 if (operation.alg == PSA_ALG_CCM) {
6255 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6256 input_data->len, output_data,
6257 output_size, &output_length),
6258 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 } else {
6261 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6262 input_data->len, output_data,
6263 output_size, &output_length));
6264
6265 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6266 input_data->len),
6267 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006268 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006270
6271 /* ------------------------------------------------------- */
6272
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006274
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6276 PSA_AEAD_NONCE_MAX_SIZE,
6277 &nonce_length));
6278 if (operation.alg == PSA_ALG_CCM) {
6279 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6280 finish_output_size,
6281 &output_part_length,
6282 tag_buffer, tag_length,
6283 &tag_size));
6284 } else {
6285 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6286 finish_output_size,
6287 &output_part_length,
6288 tag_buffer, tag_length,
6289 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006290
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6292 input_data->len),
6293 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006294 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006296
Paul Elliott243080c2021-07-21 19:01:17 +01006297 /* Test for not sending any additional data or data after setting non zero
6298 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006299
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006301
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006303
Gilles Peskine449bd832023-01-11 14:50:10 +01006304 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6305 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006306
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6308 finish_output_size,
6309 &output_part_length,
6310 tag_buffer, tag_length,
6311 &tag_size),
6312 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006313
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006315
Paul Elliott243080c2021-07-21 19:01:17 +01006316 /* Test for not sending any additional data or data after setting non-zero
6317 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006318
Gilles Peskine449bd832023-01-11 14:50:10 +01006319 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006320
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006322
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6324 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006325
Gilles Peskine449bd832023-01-11 14:50:10 +01006326 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6327 finish_output_size,
6328 &output_part_length,
6329 tag_buffer,
6330 tag_length),
6331 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006332
Gilles Peskine449bd832023-01-11 14:50:10 +01006333 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006334
Paul Elliott243080c2021-07-21 19:01:17 +01006335 /* Test for not sending any additional data after setting a non-zero length
6336 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006337
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006339
Gilles Peskine449bd832023-01-11 14:50:10 +01006340 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006341
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6343 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006344
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6346 input_data->len, output_data,
6347 output_size, &output_length),
6348 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006349
Gilles Peskine449bd832023-01-11 14:50:10 +01006350 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006351
Paul Elliottf94bd992021-09-19 18:15:59 +01006352 /* Test for not sending any data after setting a non-zero length for it.*/
6353
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006355
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006357
Gilles Peskine449bd832023-01-11 14:50:10 +01006358 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6359 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006360
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6362 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006363
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6365 finish_output_size,
6366 &output_part_length,
6367 tag_buffer, tag_length,
6368 &tag_size),
6369 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006370
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006372
Paul Elliottb0450fe2021-09-01 15:06:26 +01006373 /* Test for sending too much additional data after setting lengths. */
6374
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006376
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006378
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006380
6381
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6383 additional_data->len),
6384 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006385
Gilles Peskine449bd832023-01-11 14:50:10 +01006386 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006387
Paul Elliotta2a09b02021-09-22 14:56:40 +01006388 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006389
Gilles Peskine449bd832023-01-11 14:50:10 +01006390 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006391
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006393
Gilles Peskine449bd832023-01-11 14:50:10 +01006394 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6395 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006396
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6398 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006399
Gilles Peskine449bd832023-01-11 14:50:10 +01006400 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6401 1),
6402 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006403
Gilles Peskine449bd832023-01-11 14:50:10 +01006404 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006405
Paul Elliottb0450fe2021-09-01 15:06:26 +01006406 /* Test for sending too much data after setting lengths. */
6407
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006409
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006411
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006413
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6415 input_data->len, output_data,
6416 output_size, &output_length),
6417 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006418
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006420
Paul Elliotta2a09b02021-09-22 14:56:40 +01006421 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006422
Gilles Peskine449bd832023-01-11 14:50:10 +01006423 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006424
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006426
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6428 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006429
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6431 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006432
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6434 input_data->len, output_data,
6435 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006436
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6438 1, output_data,
6439 output_size, &output_length),
6440 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006441
Gilles Peskine449bd832023-01-11 14:50:10 +01006442 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006443
Paul Elliottc23a9a02021-06-21 18:32:46 +01006444 /* Test sending additional data after data. */
6445
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006447
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006449
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 if (operation.alg != PSA_ALG_CCM) {
6451 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6452 input_data->len, output_data,
6453 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006454
Gilles Peskine449bd832023-01-11 14:50:10 +01006455 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6456 additional_data->len),
6457 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006458 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006460
Paul Elliott534d0b42021-06-22 19:15:20 +01006461 /* Test calling finish on decryption. */
6462
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006464
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006466
Gilles Peskine449bd832023-01-11 14:50:10 +01006467 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6468 finish_output_size,
6469 &output_part_length,
6470 tag_buffer, tag_length,
6471 &tag_size),
6472 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006473
Gilles Peskine449bd832023-01-11 14:50:10 +01006474 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006475
6476 /* Test calling verify on encryption. */
6477
Gilles Peskine449bd832023-01-11 14:50:10 +01006478 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006479
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006481
Gilles Peskine449bd832023-01-11 14:50:10 +01006482 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6483 finish_output_size,
6484 &output_part_length,
6485 tag_buffer,
6486 tag_length),
6487 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006488
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006490
6491
Paul Elliottc23a9a02021-06-21 18:32:46 +01006492exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006493 psa_destroy_key(key);
6494 psa_aead_abort(&operation);
6495 mbedtls_free(output_data);
6496 mbedtls_free(final_data);
6497 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006498}
6499/* END_CASE */
6500
6501/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006502void signature_size(int type_arg,
6503 int bits,
6504 int alg_arg,
6505 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006506{
6507 psa_key_type_t type = type_arg;
6508 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006510
Gilles Peskine449bd832023-01-11 14:50:10 +01006511 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006512
Gilles Peskinee59236f2018-01-27 23:32:46 +01006513exit:
6514 ;
6515}
6516/* END_CASE */
6517
6518/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006519void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6520 int alg_arg, data_t *input_data,
6521 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006522{
Ronald Cron5425a212020-08-04 14:58:35 +02006523 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006524 psa_key_type_t key_type = key_type_arg;
6525 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006526 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006527 unsigned char *signature = NULL;
6528 size_t signature_size;
6529 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006530 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006531
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006533
Gilles Peskine449bd832023-01-11 14:50:10 +01006534 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6535 psa_set_key_algorithm(&attributes, alg);
6536 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006537
Gilles Peskine449bd832023-01-11 14:50:10 +01006538 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6539 &key));
6540 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6541 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006542
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006543 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006544 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006545 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6546 key_bits, alg);
6547 TEST_ASSERT(signature_size != 0);
6548 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006549 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006550
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006551 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006552 PSA_ASSERT(psa_sign_hash(key, alg,
6553 input_data->x, input_data->len,
6554 signature, signature_size,
6555 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006556 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006557 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006558 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006559
6560exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006561 /*
6562 * Key attributes may have been returned by psa_get_key_attributes()
6563 * thus reset them as required.
6564 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006565 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006566
Gilles Peskine449bd832023-01-11 14:50:10 +01006567 psa_destroy_key(key);
6568 mbedtls_free(signature);
6569 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006570}
6571/* END_CASE */
6572
Paul Elliott712d5122022-12-07 14:03:10 +00006573/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006574/**
6575 * sign_hash_interruptible() test intentions:
6576 *
6577 * Note: This test can currently only handle ECDSA.
6578 *
6579 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006580 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006581 *
6582 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6583 * expected for different max_ops values.
6584 *
6585 * 3. Test that the number of ops done prior to start and after abort is zero
6586 * and that each successful stage completes some ops (this is not mandated by
6587 * the PSA specification, but is currently the case).
6588 *
6589 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6590 * complete() calls does not alter the number of ops returned.
6591 */
Paul Elliott712d5122022-12-07 14:03:10 +00006592void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6593 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006594 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006595{
6596 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6597 psa_key_type_t key_type = key_type_arg;
6598 psa_algorithm_t alg = alg_arg;
6599 size_t key_bits;
6600 unsigned char *signature = NULL;
6601 size_t signature_size;
6602 size_t signature_length = 0xdeadbeef;
6603 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6604 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006605 uint32_t num_ops = 0;
6606 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006607 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006608 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006609 size_t min_completes = 0;
6610 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006611
Paul Elliott712d5122022-12-07 14:03:10 +00006612 psa_sign_hash_interruptible_operation_t operation =
6613 psa_sign_hash_interruptible_operation_init();
6614
6615 PSA_ASSERT(psa_crypto_init());
6616
6617 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6618 psa_set_key_algorithm(&attributes, alg);
6619 psa_set_key_type(&attributes, key_type);
6620
6621 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6622 &key));
6623 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6624 key_bits = psa_get_key_bits(&attributes);
6625
6626 /* Allocate a buffer which has the size advertised by the
6627 * library. */
6628 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6629 key_bits, alg);
6630 TEST_ASSERT(signature_size != 0);
6631 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006632 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006633
Paul Elliott0c683352022-12-16 19:16:56 +00006634 psa_interruptible_set_max_ops(max_ops);
6635
Paul Elliott6f600372023-02-06 18:41:05 +00006636 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6637 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006638
Paul Elliott712d5122022-12-07 14:03:10 +00006639 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6640 TEST_ASSERT(num_ops_prior == 0);
6641
6642 /* Start performing the signature. */
6643 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6644 input_data->x, input_data->len));
6645
6646 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6647 TEST_ASSERT(num_ops_prior == 0);
6648
6649 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006650 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006651 status = psa_sign_hash_complete(&operation, signature, signature_size,
6652 &signature_length);
6653
Paul Elliott0c683352022-12-16 19:16:56 +00006654 num_completes++;
6655
Paul Elliott712d5122022-12-07 14:03:10 +00006656 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6657 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006658 /* We are asserting here that every complete makes progress
6659 * (completes some ops), which is true of the internal
6660 * implementation and probably any implementation, however this is
6661 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006662 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006663
Paul Elliott712d5122022-12-07 14:03:10 +00006664 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006665
6666 /* Ensure calling get_num_ops() twice still returns the same
6667 * number of ops as previously reported. */
6668 num_ops = psa_sign_hash_get_num_ops(&operation);
6669
6670 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006671 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006672 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006673
6674 TEST_ASSERT(status == PSA_SUCCESS);
6675
Paul Elliott0c683352022-12-16 19:16:56 +00006676 TEST_LE_U(min_completes, num_completes);
6677 TEST_LE_U(num_completes, max_completes);
6678
Paul Elliott712d5122022-12-07 14:03:10 +00006679 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006680 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006681 signature, signature_length);
Paul Elliott712d5122022-12-07 14:03:10 +00006682
6683 PSA_ASSERT(psa_sign_hash_abort(&operation));
6684
Paul Elliott59ad9452022-12-18 15:09:02 +00006685 num_ops = psa_sign_hash_get_num_ops(&operation);
6686 TEST_ASSERT(num_ops == 0);
6687
Paul Elliott712d5122022-12-07 14:03:10 +00006688exit:
6689
6690 /*
6691 * Key attributes may have been returned by psa_get_key_attributes()
6692 * thus reset them as required.
6693 */
6694 psa_reset_key_attributes(&attributes);
6695
6696 psa_destroy_key(key);
6697 mbedtls_free(signature);
6698 PSA_DONE();
6699}
6700/* END_CASE */
6701
Gilles Peskine20035e32018-02-03 22:44:14 +01006702/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006703void sign_hash_fail(int key_type_arg, data_t *key_data,
6704 int alg_arg, data_t *input_data,
6705 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006706{
Ronald Cron5425a212020-08-04 14:58:35 +02006707 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006708 psa_key_type_t key_type = key_type_arg;
6709 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006710 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006711 psa_status_t actual_status;
6712 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006713 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006714 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006715 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006716
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006717 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006718
Gilles Peskine449bd832023-01-11 14:50:10 +01006719 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006720
Gilles Peskine449bd832023-01-11 14:50:10 +01006721 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6722 psa_set_key_algorithm(&attributes, alg);
6723 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006724
Gilles Peskine449bd832023-01-11 14:50:10 +01006725 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6726 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006727
Gilles Peskine449bd832023-01-11 14:50:10 +01006728 actual_status = psa_sign_hash(key, alg,
6729 input_data->x, input_data->len,
6730 signature, signature_size,
6731 &signature_length);
6732 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006733 /* The value of *signature_length is unspecified on error, but
6734 * whatever it is, it should be less than signature_size, so that
6735 * if the caller tries to read *signature_length bytes without
6736 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006737 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006738
6739exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006740 psa_reset_key_attributes(&attributes);
6741 psa_destroy_key(key);
6742 mbedtls_free(signature);
6743 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006744}
6745/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006746
Paul Elliott91007972022-12-16 12:21:24 +00006747/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006748/**
6749 * sign_hash_fail_interruptible() test intentions:
6750 *
6751 * Note: This test can currently only handle ECDSA.
6752 *
6753 * 1. Test that various failure cases for interruptible sign hash fail with the
6754 * correct error codes, and at the correct point (at start or during
6755 * complete).
6756 *
6757 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6758 * expected for different max_ops values.
6759 *
6760 * 3. Test that the number of ops done prior to start and after abort is zero
6761 * and that each successful stage completes some ops (this is not mandated by
6762 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006763 *
6764 * 4. Check that calling complete() when start() fails and complete()
6765 * after completion results in a BAD_STATE error.
6766 *
6767 * 5. Check that calling start() again after start fails results in a BAD_STATE
6768 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006769 */
Paul Elliott91007972022-12-16 12:21:24 +00006770void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6771 int alg_arg, data_t *input_data,
6772 int signature_size_arg,
6773 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006774 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006775 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006776{
6777 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6778 psa_key_type_t key_type = key_type_arg;
6779 psa_algorithm_t alg = alg_arg;
6780 size_t signature_size = signature_size_arg;
6781 psa_status_t actual_status;
6782 psa_status_t expected_start_status = expected_start_status_arg;
6783 psa_status_t expected_complete_status = expected_complete_status_arg;
6784 unsigned char *signature = NULL;
6785 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006786 uint32_t num_ops = 0;
6787 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006788 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006789 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006790 size_t min_completes = 0;
6791 size_t max_completes = 0;
6792
Paul Elliott91007972022-12-16 12:21:24 +00006793 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6794 psa_sign_hash_interruptible_operation_t operation =
6795 psa_sign_hash_interruptible_operation_init();
6796
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006797 TEST_CALLOC(signature, signature_size);
Paul Elliott91007972022-12-16 12:21:24 +00006798
6799 PSA_ASSERT(psa_crypto_init());
6800
6801 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6802 psa_set_key_algorithm(&attributes, alg);
6803 psa_set_key_type(&attributes, key_type);
6804
6805 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6806 &key));
6807
Paul Elliott0c683352022-12-16 19:16:56 +00006808 psa_interruptible_set_max_ops(max_ops);
6809
Paul Elliott6f600372023-02-06 18:41:05 +00006810 interruptible_signverify_get_minmax_completes(max_ops,
6811 expected_complete_status,
6812 &min_completes,
6813 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006814
Paul Elliott91007972022-12-16 12:21:24 +00006815 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6816 TEST_ASSERT(num_ops_prior == 0);
6817
6818 /* Start performing the signature. */
6819 actual_status = psa_sign_hash_start(&operation, key, alg,
6820 input_data->x, input_data->len);
6821
6822 TEST_EQUAL(actual_status, expected_start_status);
6823
Paul Elliottc9774412023-02-06 15:14:07 +00006824 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006825 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006826 * start failed. */
6827 actual_status = psa_sign_hash_complete(&operation, signature,
6828 signature_size,
6829 &signature_length);
6830
6831 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6832
6833 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006834 actual_status = psa_sign_hash_start(&operation, key, alg,
6835 input_data->x, input_data->len);
6836
6837 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6838 }
6839
Paul Elliott91007972022-12-16 12:21:24 +00006840 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6841 TEST_ASSERT(num_ops_prior == 0);
6842
Paul Elliott91007972022-12-16 12:21:24 +00006843 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006844 do {
Paul Elliott91007972022-12-16 12:21:24 +00006845 actual_status = psa_sign_hash_complete(&operation, signature,
6846 signature_size,
6847 &signature_length);
6848
Paul Elliott0c683352022-12-16 19:16:56 +00006849 num_completes++;
6850
Paul Elliott334d7262023-01-20 17:29:41 +00006851 if (actual_status == PSA_SUCCESS ||
6852 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006853 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006854 /* We are asserting here that every complete makes progress
6855 * (completes some ops), which is true of the internal
6856 * implementation and probably any implementation, however this is
6857 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006858 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006859
Paul Elliott91007972022-12-16 12:21:24 +00006860 num_ops_prior = num_ops;
6861 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006862 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006863
Paul Elliottc9774412023-02-06 15:14:07 +00006864 TEST_EQUAL(actual_status, expected_complete_status);
6865
Paul Elliottefebad02023-02-15 16:56:45 +00006866 /* Check that another complete returns BAD_STATE. */
6867 actual_status = psa_sign_hash_complete(&operation, signature,
6868 signature_size,
6869 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006870
Paul Elliottefebad02023-02-15 16:56:45 +00006871 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006872
Paul Elliott91007972022-12-16 12:21:24 +00006873 PSA_ASSERT(psa_sign_hash_abort(&operation));
6874
Paul Elliott59ad9452022-12-18 15:09:02 +00006875 num_ops = psa_sign_hash_get_num_ops(&operation);
6876 TEST_ASSERT(num_ops == 0);
6877
Paul Elliott91007972022-12-16 12:21:24 +00006878 /* The value of *signature_length is unspecified on error, but
6879 * whatever it is, it should be less than signature_size, so that
6880 * if the caller tries to read *signature_length bytes without
6881 * checking the error code then they don't overflow a buffer. */
6882 TEST_LE_U(signature_length, signature_size);
6883
Paul Elliott0c683352022-12-16 19:16:56 +00006884 TEST_LE_U(min_completes, num_completes);
6885 TEST_LE_U(num_completes, max_completes);
6886
Paul Elliott91007972022-12-16 12:21:24 +00006887exit:
6888 psa_reset_key_attributes(&attributes);
6889 psa_destroy_key(key);
6890 mbedtls_free(signature);
6891 PSA_DONE();
6892}
6893/* END_CASE */
6894
mohammad16038cc1cee2018-03-28 01:21:33 +03006895/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006896void sign_verify_hash(int key_type_arg, data_t *key_data,
6897 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006898{
Ronald Cron5425a212020-08-04 14:58:35 +02006899 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006900 psa_key_type_t key_type = key_type_arg;
6901 psa_algorithm_t alg = alg_arg;
6902 size_t key_bits;
6903 unsigned char *signature = NULL;
6904 size_t signature_size;
6905 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006906 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006907
Gilles Peskine449bd832023-01-11 14:50:10 +01006908 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006909
Gilles Peskine449bd832023-01-11 14:50:10 +01006910 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6911 psa_set_key_algorithm(&attributes, alg);
6912 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006913
Gilles Peskine449bd832023-01-11 14:50:10 +01006914 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6915 &key));
6916 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6917 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006918
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006919 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006920 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006921 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6922 key_bits, alg);
6923 TEST_ASSERT(signature_size != 0);
6924 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006925 TEST_CALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006926
6927 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006928 PSA_ASSERT(psa_sign_hash(key, alg,
6929 input_data->x, input_data->len,
6930 signature, signature_size,
6931 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006932 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006933 TEST_LE_U(signature_length, signature_size);
6934 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006935
6936 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006937 PSA_ASSERT(psa_verify_hash(key, alg,
6938 input_data->x, input_data->len,
6939 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006940
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006942 /* Flip a bit in the input and verify that the signature is now
6943 * detected as invalid. Flip a bit at the beginning, not at the end,
6944 * because ECDSA may ignore the last few bits of the input. */
6945 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006946 TEST_EQUAL(psa_verify_hash(key, alg,
6947 input_data->x, input_data->len,
6948 signature, signature_length),
6949 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006950 }
6951
6952exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006953 /*
6954 * Key attributes may have been returned by psa_get_key_attributes()
6955 * thus reset them as required.
6956 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006957 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006958
Gilles Peskine449bd832023-01-11 14:50:10 +01006959 psa_destroy_key(key);
6960 mbedtls_free(signature);
6961 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006962}
6963/* END_CASE */
6964
Paul Elliott712d5122022-12-07 14:03:10 +00006965/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006966/**
6967 * sign_verify_hash_interruptible() test intentions:
6968 *
6969 * Note: This test can currently only handle ECDSA.
6970 *
Paul Elliott8c092052023-03-06 17:49:14 +00006971 * 1. Test that we can sign an input hash with the given keypair and then
6972 * afterwards verify that signature. This is currently the only way to test
6973 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00006974 *
6975 * 2. Test that after corrupting the hash, the verification detects an invalid
6976 * signature.
6977 *
6978 * 3. Test the number of calls to psa_sign_hash_complete() required are as
6979 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00006980 *
6981 * 4. Test that the number of ops done prior to starting signing and after abort
6982 * is zero and that each successful signing stage completes some ops (this is
6983 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00006984 */
Paul Elliott712d5122022-12-07 14:03:10 +00006985void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006986 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006987 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006988{
6989 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6990 psa_key_type_t key_type = key_type_arg;
6991 psa_algorithm_t alg = alg_arg;
6992 size_t key_bits;
6993 unsigned char *signature = NULL;
6994 size_t signature_size;
6995 size_t signature_length = 0xdeadbeef;
6996 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6997 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006998 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00006999 uint32_t num_ops = 0;
7000 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007001 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007002 size_t min_completes = 0;
7003 size_t max_completes = 0;
7004
Paul Elliott712d5122022-12-07 14:03:10 +00007005 psa_sign_hash_interruptible_operation_t sign_operation =
7006 psa_sign_hash_interruptible_operation_init();
7007 psa_verify_hash_interruptible_operation_t verify_operation =
7008 psa_verify_hash_interruptible_operation_init();
7009
7010 PSA_ASSERT(psa_crypto_init());
7011
Paul Elliott0c683352022-12-16 19:16:56 +00007012 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7013 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00007014 psa_set_key_algorithm(&attributes, alg);
7015 psa_set_key_type(&attributes, key_type);
7016
7017 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7018 &key));
7019 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7020 key_bits = psa_get_key_bits(&attributes);
7021
7022 /* Allocate a buffer which has the size advertised by the
7023 * library. */
7024 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7025 key_bits, alg);
7026 TEST_ASSERT(signature_size != 0);
7027 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007028 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00007029
Paul Elliott0c683352022-12-16 19:16:56 +00007030 psa_interruptible_set_max_ops(max_ops);
7031
Paul Elliott6f600372023-02-06 18:41:05 +00007032 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7033 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007034
Paul Elliott7c173082023-02-26 18:44:45 +00007035 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
7036 TEST_ASSERT(num_ops_prior == 0);
7037
Paul Elliott712d5122022-12-07 14:03:10 +00007038 /* Start performing the signature. */
7039 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7040 input_data->x, input_data->len));
7041
Paul Elliott7c173082023-02-26 18:44:45 +00007042 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
7043 TEST_ASSERT(num_ops_prior == 0);
7044
Paul Elliott712d5122022-12-07 14:03:10 +00007045 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007046 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007047
Paul Elliott0c683352022-12-16 19:16:56 +00007048 status = psa_sign_hash_complete(&sign_operation, signature,
7049 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00007050 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00007051
7052 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00007053
7054 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7055 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7056 /* We are asserting here that every complete makes progress
7057 * (completes some ops), which is true of the internal
7058 * implementation and probably any implementation, however this is
7059 * not mandated by the PSA specification. */
7060 TEST_ASSERT(num_ops > num_ops_prior);
7061
7062 num_ops_prior = num_ops;
7063 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007064 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007065
7066 TEST_ASSERT(status == PSA_SUCCESS);
7067
Paul Elliott0c683352022-12-16 19:16:56 +00007068 TEST_LE_U(min_completes, num_completes);
7069 TEST_LE_U(num_completes, max_completes);
7070
Paul Elliott712d5122022-12-07 14:03:10 +00007071 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7072
Paul Elliott7c173082023-02-26 18:44:45 +00007073 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7074 TEST_ASSERT(num_ops == 0);
7075
Paul Elliott712d5122022-12-07 14:03:10 +00007076 /* Check that the signature length looks sensible. */
7077 TEST_LE_U(signature_length, signature_size);
7078 TEST_ASSERT(signature_length > 0);
7079
Paul Elliott0c683352022-12-16 19:16:56 +00007080 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007081
7082 /* Start verification. */
7083 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7084 input_data->x, input_data->len,
7085 signature, signature_length));
7086
7087 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007088 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007089 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007090
7091 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007092 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007093
7094 TEST_ASSERT(status == PSA_SUCCESS);
7095
Paul Elliott0c683352022-12-16 19:16:56 +00007096 TEST_LE_U(min_completes, num_completes);
7097 TEST_LE_U(num_completes, max_completes);
7098
Paul Elliott712d5122022-12-07 14:03:10 +00007099 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7100
7101 verify_operation = psa_verify_hash_interruptible_operation_init();
7102
7103 if (input_data->len != 0) {
7104 /* Flip a bit in the input and verify that the signature is now
7105 * detected as invalid. Flip a bit at the beginning, not at the end,
7106 * because ECDSA may ignore the last few bits of the input. */
7107 input_data->x[0] ^= 1;
7108
Paul Elliott712d5122022-12-07 14:03:10 +00007109 /* Start verification. */
7110 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7111 input_data->x, input_data->len,
7112 signature, signature_length));
7113
7114 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007115 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007116 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007117 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007118
7119 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7120 }
7121
7122 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7123
7124exit:
7125 /*
7126 * Key attributes may have been returned by psa_get_key_attributes()
7127 * thus reset them as required.
7128 */
7129 psa_reset_key_attributes(&attributes);
7130
7131 psa_destroy_key(key);
7132 mbedtls_free(signature);
7133 PSA_DONE();
7134}
7135/* END_CASE */
7136
Gilles Peskine9911b022018-06-29 17:30:48 +02007137/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007138void verify_hash(int key_type_arg, data_t *key_data,
7139 int alg_arg, data_t *hash_data,
7140 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007141{
Ronald Cron5425a212020-08-04 14:58:35 +02007142 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007143 psa_key_type_t key_type = key_type_arg;
7144 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007145 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007146
Gilles Peskine449bd832023-01-11 14:50:10 +01007147 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007148
Gilles Peskine449bd832023-01-11 14:50:10 +01007149 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007150
Gilles Peskine449bd832023-01-11 14:50:10 +01007151 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7152 psa_set_key_algorithm(&attributes, alg);
7153 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007154
Gilles Peskine449bd832023-01-11 14:50:10 +01007155 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7156 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007157
Gilles Peskine449bd832023-01-11 14:50:10 +01007158 PSA_ASSERT(psa_verify_hash(key, alg,
7159 hash_data->x, hash_data->len,
7160 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007161
itayzafrir5c753392018-05-08 11:18:38 +03007162exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007163 psa_reset_key_attributes(&attributes);
7164 psa_destroy_key(key);
7165 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007166}
7167/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007168
Paul Elliott712d5122022-12-07 14:03:10 +00007169/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007170/**
7171 * verify_hash_interruptible() test intentions:
7172 *
7173 * Note: This test can currently only handle ECDSA.
7174 *
7175 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007176 * only). Given this test only does verification it can accept public keys as
7177 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007178 *
7179 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7180 * expected for different max_ops values.
7181 *
7182 * 3. Test that the number of ops done prior to start and after abort is zero
7183 * and that each successful stage completes some ops (this is not mandated by
7184 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007185 *
7186 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7187 * complete() calls does not alter the number of ops returned.
7188 *
7189 * 5. Test that after corrupting the hash, the verification detects an invalid
7190 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007191 */
Paul Elliott712d5122022-12-07 14:03:10 +00007192void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7193 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007194 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007195{
7196 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7197 psa_key_type_t key_type = key_type_arg;
7198 psa_algorithm_t alg = alg_arg;
7199 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7200 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007201 uint32_t num_ops = 0;
7202 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007203 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007204 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007205 size_t min_completes = 0;
7206 size_t max_completes = 0;
7207
Paul Elliott712d5122022-12-07 14:03:10 +00007208 psa_verify_hash_interruptible_operation_t operation =
7209 psa_verify_hash_interruptible_operation_init();
7210
7211 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7212
7213 PSA_ASSERT(psa_crypto_init());
7214
7215 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7216 psa_set_key_algorithm(&attributes, alg);
7217 psa_set_key_type(&attributes, key_type);
7218
7219 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7220 &key));
7221
Paul Elliott0c683352022-12-16 19:16:56 +00007222 psa_interruptible_set_max_ops(max_ops);
7223
Paul Elliott6f600372023-02-06 18:41:05 +00007224 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7225 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007226
Paul Elliott712d5122022-12-07 14:03:10 +00007227 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7228
7229 TEST_ASSERT(num_ops_prior == 0);
7230
7231 /* Start verification. */
7232 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7233 hash_data->x, hash_data->len,
7234 signature_data->x, signature_data->len)
7235 );
7236
7237 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7238
7239 TEST_ASSERT(num_ops_prior == 0);
7240
7241 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007242 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007243 status = psa_verify_hash_complete(&operation);
7244
Paul Elliott0c683352022-12-16 19:16:56 +00007245 num_completes++;
7246
Paul Elliott712d5122022-12-07 14:03:10 +00007247 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7248 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007249 /* We are asserting here that every complete makes progress
7250 * (completes some ops), which is true of the internal
7251 * implementation and probably any implementation, however this is
7252 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007253 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007254
Paul Elliott712d5122022-12-07 14:03:10 +00007255 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007256
7257 /* Ensure calling get_num_ops() twice still returns the same
7258 * number of ops as previously reported. */
7259 num_ops = psa_verify_hash_get_num_ops(&operation);
7260
7261 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007262 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007263 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007264
7265 TEST_ASSERT(status == PSA_SUCCESS);
7266
Paul Elliott0c683352022-12-16 19:16:56 +00007267 TEST_LE_U(min_completes, num_completes);
7268 TEST_LE_U(num_completes, max_completes);
7269
Paul Elliott712d5122022-12-07 14:03:10 +00007270 PSA_ASSERT(psa_verify_hash_abort(&operation));
7271
Paul Elliott59ad9452022-12-18 15:09:02 +00007272 num_ops = psa_verify_hash_get_num_ops(&operation);
7273 TEST_ASSERT(num_ops == 0);
7274
Paul Elliott8359c142023-02-24 18:40:10 +00007275 if (hash_data->len != 0) {
7276 /* Flip a bit in the hash and verify that the signature is now detected
7277 * as invalid. Flip a bit at the beginning, not at the end, because
7278 * ECDSA may ignore the last few bits of the input. */
7279 hash_data->x[0] ^= 1;
7280
7281 /* Start verification. */
7282 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7283 hash_data->x, hash_data->len,
7284 signature_data->x, signature_data->len));
7285
7286 /* Continue performing the signature until complete. */
7287 do {
7288 status = psa_verify_hash_complete(&operation);
7289 } while (status == PSA_OPERATION_INCOMPLETE);
7290
7291 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7292 }
7293
Paul Elliott712d5122022-12-07 14:03:10 +00007294exit:
7295 psa_reset_key_attributes(&attributes);
7296 psa_destroy_key(key);
7297 PSA_DONE();
7298}
7299/* END_CASE */
7300
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007301/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007302void verify_hash_fail(int key_type_arg, data_t *key_data,
7303 int alg_arg, data_t *hash_data,
7304 data_t *signature_data,
7305 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007306{
Ronald Cron5425a212020-08-04 14:58:35 +02007307 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007308 psa_key_type_t key_type = key_type_arg;
7309 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007310 psa_status_t actual_status;
7311 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007312 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007313
Gilles Peskine449bd832023-01-11 14:50:10 +01007314 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007315
Gilles Peskine449bd832023-01-11 14:50:10 +01007316 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7317 psa_set_key_algorithm(&attributes, alg);
7318 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007319
Gilles Peskine449bd832023-01-11 14:50:10 +01007320 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7321 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007322
Gilles Peskine449bd832023-01-11 14:50:10 +01007323 actual_status = psa_verify_hash(key, alg,
7324 hash_data->x, hash_data->len,
7325 signature_data->x, signature_data->len);
7326 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007327
7328exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007329 psa_reset_key_attributes(&attributes);
7330 psa_destroy_key(key);
7331 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007332}
7333/* END_CASE */
7334
Paul Elliott91007972022-12-16 12:21:24 +00007335/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007336/**
7337 * verify_hash_fail_interruptible() test intentions:
7338 *
7339 * Note: This test can currently only handle ECDSA.
7340 *
7341 * 1. Test that various failure cases for interruptible verify hash fail with
7342 * the correct error codes, and at the correct point (at start or during
7343 * complete).
7344 *
7345 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7346 * expected for different max_ops values.
7347 *
7348 * 3. Test that the number of ops done prior to start and after abort is zero
7349 * and that each successful stage completes some ops (this is not mandated by
7350 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007351 *
7352 * 4. Check that calling complete() when start() fails and complete()
7353 * after completion results in a BAD_STATE error.
7354 *
7355 * 5. Check that calling start() again after start fails results in a BAD_STATE
7356 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007357 */
Paul Elliott91007972022-12-16 12:21:24 +00007358void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7359 int alg_arg, data_t *hash_data,
7360 data_t *signature_data,
7361 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007362 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007363 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007364{
7365 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7366 psa_key_type_t key_type = key_type_arg;
7367 psa_algorithm_t alg = alg_arg;
7368 psa_status_t actual_status;
7369 psa_status_t expected_start_status = expected_start_status_arg;
7370 psa_status_t expected_complete_status = expected_complete_status_arg;
7371 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007372 uint32_t num_ops = 0;
7373 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007374 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007375 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007376 size_t min_completes = 0;
7377 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007378 psa_verify_hash_interruptible_operation_t operation =
7379 psa_verify_hash_interruptible_operation_init();
7380
7381 PSA_ASSERT(psa_crypto_init());
7382
7383 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7384 psa_set_key_algorithm(&attributes, alg);
7385 psa_set_key_type(&attributes, key_type);
7386
7387 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7388 &key));
7389
Paul Elliott0c683352022-12-16 19:16:56 +00007390 psa_interruptible_set_max_ops(max_ops);
7391
Paul Elliott6f600372023-02-06 18:41:05 +00007392 interruptible_signverify_get_minmax_completes(max_ops,
7393 expected_complete_status,
7394 &min_completes,
7395 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007396
Paul Elliott91007972022-12-16 12:21:24 +00007397 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7398 TEST_ASSERT(num_ops_prior == 0);
7399
7400 /* Start verification. */
7401 actual_status = psa_verify_hash_start(&operation, key, alg,
7402 hash_data->x, hash_data->len,
7403 signature_data->x,
7404 signature_data->len);
7405
7406 TEST_EQUAL(actual_status, expected_start_status);
7407
Paul Elliottc9774412023-02-06 15:14:07 +00007408 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007409 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007410 * start failed. */
7411 actual_status = psa_verify_hash_complete(&operation);
7412
7413 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7414
7415 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007416 actual_status = psa_verify_hash_start(&operation, key, alg,
7417 hash_data->x, hash_data->len,
7418 signature_data->x,
7419 signature_data->len);
7420
7421 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7422 }
7423
Paul Elliott91007972022-12-16 12:21:24 +00007424 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7425 TEST_ASSERT(num_ops_prior == 0);
7426
Paul Elliott91007972022-12-16 12:21:24 +00007427 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007428 do {
Paul Elliott91007972022-12-16 12:21:24 +00007429 actual_status = psa_verify_hash_complete(&operation);
7430
Paul Elliott0c683352022-12-16 19:16:56 +00007431 num_completes++;
7432
Paul Elliott334d7262023-01-20 17:29:41 +00007433 if (actual_status == PSA_SUCCESS ||
7434 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007435 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007436 /* We are asserting here that every complete makes progress
7437 * (completes some ops), which is true of the internal
7438 * implementation and probably any implementation, however this is
7439 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007440 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007441
Paul Elliott91007972022-12-16 12:21:24 +00007442 num_ops_prior = num_ops;
7443 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007444 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007445
Paul Elliottc9774412023-02-06 15:14:07 +00007446 TEST_EQUAL(actual_status, expected_complete_status);
7447
Paul Elliottefebad02023-02-15 16:56:45 +00007448 /* Check that another complete returns BAD_STATE. */
7449 actual_status = psa_verify_hash_complete(&operation);
7450 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007451
Paul Elliott0c683352022-12-16 19:16:56 +00007452 TEST_LE_U(min_completes, num_completes);
7453 TEST_LE_U(num_completes, max_completes);
7454
Paul Elliott91007972022-12-16 12:21:24 +00007455 PSA_ASSERT(psa_verify_hash_abort(&operation));
7456
Paul Elliott59ad9452022-12-18 15:09:02 +00007457 num_ops = psa_verify_hash_get_num_ops(&operation);
7458 TEST_ASSERT(num_ops == 0);
7459
Paul Elliott91007972022-12-16 12:21:24 +00007460exit:
7461 psa_reset_key_attributes(&attributes);
7462 psa_destroy_key(key);
7463 PSA_DONE();
7464}
7465/* END_CASE */
7466
Paul Elliott20a36062022-12-18 13:21:25 +00007467/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007468/**
7469 * interruptible_signverify_hash_state_test() test intentions:
7470 *
7471 * Note: This test can currently only handle ECDSA.
7472 *
7473 * 1. Test that calling the various interruptible sign and verify hash functions
7474 * in incorrect orders returns BAD_STATE errors.
7475 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007476void interruptible_signverify_hash_state_test(int key_type_arg,
7477 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007478{
7479 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7480 psa_key_type_t key_type = key_type_arg;
7481 psa_algorithm_t alg = alg_arg;
7482 size_t key_bits;
7483 unsigned char *signature = NULL;
7484 size_t signature_size;
7485 size_t signature_length = 0xdeadbeef;
7486 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7487 psa_sign_hash_interruptible_operation_t sign_operation =
7488 psa_sign_hash_interruptible_operation_init();
7489 psa_verify_hash_interruptible_operation_t verify_operation =
7490 psa_verify_hash_interruptible_operation_init();
7491
7492 PSA_ASSERT(psa_crypto_init());
7493
7494 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7495 PSA_KEY_USAGE_VERIFY_HASH);
7496 psa_set_key_algorithm(&attributes, alg);
7497 psa_set_key_type(&attributes, key_type);
7498
7499 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7500 &key));
7501 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7502 key_bits = psa_get_key_bits(&attributes);
7503
7504 /* Allocate a buffer which has the size advertised by the
7505 * library. */
7506 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7507 key_bits, alg);
7508 TEST_ASSERT(signature_size != 0);
7509 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007510 TEST_CALLOC(signature, signature_size);
Paul Elliott20a36062022-12-18 13:21:25 +00007511
7512 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7513
7514 /* --- Attempt completes prior to starts --- */
7515 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7516 signature_size,
7517 &signature_length),
7518 PSA_ERROR_BAD_STATE);
7519
7520 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7521
7522 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7523 PSA_ERROR_BAD_STATE);
7524
7525 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7526
7527 /* --- Aborts in all other places. --- */
7528 psa_sign_hash_abort(&sign_operation);
7529
7530 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7531 input_data->x, input_data->len));
7532
7533 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7534
7535 psa_interruptible_set_max_ops(1);
7536
7537 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7538 input_data->x, input_data->len));
7539
7540 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7541 signature_size,
7542 &signature_length),
7543 PSA_OPERATION_INCOMPLETE);
7544
7545 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7546
7547 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7548
7549 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7550 input_data->x, input_data->len));
7551
7552 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7553 signature_size,
7554 &signature_length));
7555
7556 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7557
7558 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7559
7560 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7561 input_data->x, input_data->len,
7562 signature, signature_length));
7563
7564 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7565
7566 psa_interruptible_set_max_ops(1);
7567
7568 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7569 input_data->x, input_data->len,
7570 signature, signature_length));
7571
7572 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7573 PSA_OPERATION_INCOMPLETE);
7574
7575 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7576
7577 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7578
7579 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7580 input_data->x, input_data->len,
7581 signature, signature_length));
7582
7583 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7584
7585 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7586
7587 /* --- Attempt double starts. --- */
7588
7589 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7590 input_data->x, input_data->len));
7591
7592 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7593 input_data->x, input_data->len),
7594 PSA_ERROR_BAD_STATE);
7595
7596 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7597
7598 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7599 input_data->x, input_data->len,
7600 signature, signature_length));
7601
7602 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7603 input_data->x, input_data->len,
7604 signature, signature_length),
7605 PSA_ERROR_BAD_STATE);
7606
7607 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7608
Paul Elliott76d671a2023-02-07 17:45:18 +00007609exit:
7610 /*
7611 * Key attributes may have been returned by psa_get_key_attributes()
7612 * thus reset them as required.
7613 */
7614 psa_reset_key_attributes(&attributes);
7615
7616 psa_destroy_key(key);
7617 mbedtls_free(signature);
7618 PSA_DONE();
7619}
7620/* END_CASE */
7621
7622/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007623/**
Paul Elliottc2033502023-02-26 17:09:14 +00007624 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007625 *
7626 * Note: This test can currently only handle ECDSA.
7627 *
7628 * 1. Test various edge cases in the interruptible sign and verify hash
7629 * interfaces.
7630 */
Paul Elliottc2033502023-02-26 17:09:14 +00007631void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007632 data_t *key_data, int alg_arg, data_t *input_data)
7633{
7634 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7635 psa_key_type_t key_type = key_type_arg;
7636 psa_algorithm_t alg = alg_arg;
7637 size_t key_bits;
7638 unsigned char *signature = NULL;
7639 size_t signature_size;
7640 size_t signature_length = 0xdeadbeef;
7641 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7642 uint8_t *input_buffer = NULL;
7643 psa_sign_hash_interruptible_operation_t sign_operation =
7644 psa_sign_hash_interruptible_operation_init();
7645 psa_verify_hash_interruptible_operation_t verify_operation =
7646 psa_verify_hash_interruptible_operation_init();
7647
7648 PSA_ASSERT(psa_crypto_init());
7649
7650 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7651 PSA_KEY_USAGE_VERIFY_HASH);
7652 psa_set_key_algorithm(&attributes, alg);
7653 psa_set_key_type(&attributes, key_type);
7654
7655 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7656 &key));
7657 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7658 key_bits = psa_get_key_bits(&attributes);
7659
7660 /* Allocate a buffer which has the size advertised by the
7661 * library. */
7662 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7663 key_bits, alg);
7664 TEST_ASSERT(signature_size != 0);
7665 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007666 TEST_CALLOC(signature, signature_size);
Paul Elliott76d671a2023-02-07 17:45:18 +00007667
Paul Elliott20a36062022-12-18 13:21:25 +00007668 /* --- Change function inputs mid run, to cause an error (sign only,
7669 * verify passes all inputs to start. --- */
7670
7671 psa_interruptible_set_max_ops(1);
7672
7673 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7674 input_data->x, input_data->len));
7675
7676 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7677 signature_size,
7678 &signature_length),
7679 PSA_OPERATION_INCOMPLETE);
7680
7681 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7682 0,
7683 &signature_length),
7684 PSA_ERROR_BUFFER_TOO_SMALL);
7685
Paul Elliottc9774412023-02-06 15:14:07 +00007686 /* And test that this invalidates the operation. */
7687 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7688 0,
7689 &signature_length),
7690 PSA_ERROR_BAD_STATE);
7691
Paul Elliott20a36062022-12-18 13:21:25 +00007692 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7693
Paul Elliottf9c91a72023-02-05 18:06:38 +00007694 /* Trash the hash buffer in between start and complete, to ensure
7695 * no reliance on external buffers. */
7696 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7697
Paul Elliott6c68df42023-10-23 15:33:37 +01007698 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007699
7700 memcpy(input_buffer, input_data->x, input_data->len);
7701
7702 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7703 input_buffer, input_data->len));
7704
7705 memset(input_buffer, '!', input_data->len);
7706 mbedtls_free(input_buffer);
7707 input_buffer = NULL;
7708
7709 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7710 signature_size,
7711 &signature_length));
7712
7713 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7714
Paul Elliott6c68df42023-10-23 15:33:37 +01007715 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007716
7717 memcpy(input_buffer, input_data->x, input_data->len);
7718
7719 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7720 input_buffer, input_data->len,
7721 signature, signature_length));
7722
7723 memset(input_buffer, '!', input_data->len);
7724 mbedtls_free(input_buffer);
7725 input_buffer = NULL;
7726
7727 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7728
7729 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7730
Paul Elliott20a36062022-12-18 13:21:25 +00007731exit:
7732 /*
7733 * Key attributes may have been returned by psa_get_key_attributes()
7734 * thus reset them as required.
7735 */
7736 psa_reset_key_attributes(&attributes);
7737
7738 psa_destroy_key(key);
7739 mbedtls_free(signature);
Paul Elliott6c68df42023-10-23 15:33:37 +01007740 mbedtls_free(input_buffer);
Paul Elliott20a36062022-12-18 13:21:25 +00007741 PSA_DONE();
7742}
7743/* END_CASE */
7744
Paul Elliotta4cb9092023-02-07 18:01:55 +00007745/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007746/**
Paul Elliott57702242023-02-26 20:36:10 +00007747 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007748 *
7749 * Note: This test can currently only handle ECDSA.
7750 *
7751 * 1. Test that setting max ops is reflected in both interruptible sign and
7752 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007753 * 2. Test that changing the value of max_ops to unlimited during an operation
7754 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007755 *
7756 * 3. Test that calling get_num_ops() between complete calls gives the same
7757 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007758 */
Paul Elliott57702242023-02-26 20:36:10 +00007759void interruptible_signverify_hash_ops_tests(int key_type_arg,
7760 data_t *key_data, int alg_arg,
7761 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007762{
7763 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7764 psa_key_type_t key_type = key_type_arg;
7765 psa_algorithm_t alg = alg_arg;
7766 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007767 size_t key_bits;
7768 unsigned char *signature = NULL;
7769 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007770 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007771 uint32_t num_ops = 0;
7772 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7773
Paul Elliotta4cb9092023-02-07 18:01:55 +00007774 psa_sign_hash_interruptible_operation_t sign_operation =
7775 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007776 psa_verify_hash_interruptible_operation_t verify_operation =
7777 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007778
7779 PSA_ASSERT(psa_crypto_init());
7780
7781 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7782 PSA_KEY_USAGE_VERIFY_HASH);
7783 psa_set_key_algorithm(&attributes, alg);
7784 psa_set_key_type(&attributes, key_type);
7785
Paul Elliottf1743e22023-02-15 18:44:16 +00007786 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7787 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7788 key_bits = psa_get_key_bits(&attributes);
7789
7790 /* Allocate a buffer which has the size advertised by the
7791 * library. */
7792 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7793
7794 TEST_ASSERT(signature_size != 0);
7795 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007796 TEST_CALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007797
7798 /* Check that default max ops gets set if we don't set it. */
7799 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7800 input_data->x, input_data->len));
7801
7802 TEST_EQUAL(psa_interruptible_get_max_ops(),
7803 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7804
7805 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7806
Paul Elliottf1743e22023-02-15 18:44:16 +00007807 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7808 input_data->x, input_data->len,
7809 signature, signature_size));
7810
7811 TEST_EQUAL(psa_interruptible_get_max_ops(),
7812 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7813
7814 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7815
Paul Elliotta4cb9092023-02-07 18:01:55 +00007816 /* Check that max ops gets set properly. */
7817
7818 psa_interruptible_set_max_ops(0xbeef);
7819
Paul Elliottf1743e22023-02-15 18:44:16 +00007820 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007821
Paul Elliott9e8819f2023-02-26 19:01:35 +00007822 /* --- Ensure changing the max ops mid operation works (operation should
7823 * complete successfully after setting max ops to unlimited --- */
7824 psa_interruptible_set_max_ops(1);
7825
7826 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7827 input_data->x, input_data->len));
7828
7829 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7830 signature_size,
7831 &signature_length),
7832 PSA_OPERATION_INCOMPLETE);
7833
7834 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7835
7836 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7837 signature_size,
7838 &signature_length));
7839
7840 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7841
7842 psa_interruptible_set_max_ops(1);
7843
7844 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7845 input_data->x, input_data->len,
7846 signature, signature_length));
7847
7848 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7849 PSA_OPERATION_INCOMPLETE);
7850
7851 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7852
7853 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7854
7855 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7856
Paul Elliottc1e04002023-02-26 20:27:23 +00007857 /* --- Test that not calling get_num_ops inbetween complete calls does not
7858 * result in lost ops. ---*/
7859
7860 psa_interruptible_set_max_ops(1);
7861
7862 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7863 input_data->x, input_data->len));
7864
7865 /* Continue performing the signature until complete. */
7866 do {
7867 status = psa_sign_hash_complete(&sign_operation, signature,
7868 signature_size,
7869 &signature_length);
7870
7871 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7872
7873 } while (status == PSA_OPERATION_INCOMPLETE);
7874
7875 PSA_ASSERT(status);
7876
7877 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7878
7879 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7880 input_data->x, input_data->len));
7881
7882 /* Continue performing the signature until complete. */
7883 do {
7884 status = psa_sign_hash_complete(&sign_operation, signature,
7885 signature_size,
7886 &signature_length);
7887 } while (status == PSA_OPERATION_INCOMPLETE);
7888
7889 PSA_ASSERT(status);
7890
7891 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7892
7893 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7894
7895 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7896 input_data->x, input_data->len,
7897 signature, signature_length));
7898
7899 /* Continue performing the verification until complete. */
7900 do {
7901 status = psa_verify_hash_complete(&verify_operation);
7902
7903 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7904
7905 } while (status == PSA_OPERATION_INCOMPLETE);
7906
7907 PSA_ASSERT(status);
7908
7909 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7910
7911 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7912 input_data->x, input_data->len,
7913 signature, signature_length));
7914
7915 /* Continue performing the verification until complete. */
7916 do {
7917 status = psa_verify_hash_complete(&verify_operation);
7918
7919 } while (status == PSA_OPERATION_INCOMPLETE);
7920
7921 PSA_ASSERT(status);
7922
7923 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7924
7925 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7926
Paul Elliotta4cb9092023-02-07 18:01:55 +00007927exit:
7928 /*
7929 * Key attributes may have been returned by psa_get_key_attributes()
7930 * thus reset them as required.
7931 */
7932 psa_reset_key_attributes(&attributes);
7933
7934 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007935 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007936 PSA_DONE();
7937}
7938/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007939
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007940/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007941void sign_message_deterministic(int key_type_arg,
7942 data_t *key_data,
7943 int alg_arg,
7944 data_t *input_data,
7945 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007946{
7947 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7948 psa_key_type_t key_type = key_type_arg;
7949 psa_algorithm_t alg = alg_arg;
7950 size_t key_bits;
7951 unsigned char *signature = NULL;
7952 size_t signature_size;
7953 size_t signature_length = 0xdeadbeef;
7954 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7955
Gilles Peskine449bd832023-01-11 14:50:10 +01007956 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007957
Gilles Peskine449bd832023-01-11 14:50:10 +01007958 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7959 psa_set_key_algorithm(&attributes, alg);
7960 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007961
Gilles Peskine449bd832023-01-11 14:50:10 +01007962 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7963 &key));
7964 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7965 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007966
Gilles Peskine449bd832023-01-11 14:50:10 +01007967 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7968 TEST_ASSERT(signature_size != 0);
7969 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007970 TEST_CALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007971
Gilles Peskine449bd832023-01-11 14:50:10 +01007972 PSA_ASSERT(psa_sign_message(key, alg,
7973 input_data->x, input_data->len,
7974 signature, signature_size,
7975 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007976
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01007977 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01007978 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007979
7980exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007981 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007982
Gilles Peskine449bd832023-01-11 14:50:10 +01007983 psa_destroy_key(key);
7984 mbedtls_free(signature);
7985 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007986
7987}
7988/* END_CASE */
7989
7990/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007991void sign_message_fail(int key_type_arg,
7992 data_t *key_data,
7993 int alg_arg,
7994 data_t *input_data,
7995 int signature_size_arg,
7996 int expected_status_arg)
7997{
7998 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7999 psa_key_type_t key_type = key_type_arg;
8000 psa_algorithm_t alg = alg_arg;
8001 size_t signature_size = signature_size_arg;
8002 psa_status_t actual_status;
8003 psa_status_t expected_status = expected_status_arg;
8004 unsigned char *signature = NULL;
8005 size_t signature_length = 0xdeadbeef;
8006 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8007
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008008 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008009
8010 PSA_ASSERT(psa_crypto_init());
8011
8012 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8013 psa_set_key_algorithm(&attributes, alg);
8014 psa_set_key_type(&attributes, key_type);
8015
8016 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8017 &key));
8018
8019 actual_status = psa_sign_message(key, alg,
8020 input_data->x, input_data->len,
8021 signature, signature_size,
8022 &signature_length);
8023 TEST_EQUAL(actual_status, expected_status);
8024 /* The value of *signature_length is unspecified on error, but
8025 * whatever it is, it should be less than signature_size, so that
8026 * if the caller tries to read *signature_length bytes without
8027 * checking the error code then they don't overflow a buffer. */
8028 TEST_LE_U(signature_length, signature_size);
8029
8030exit:
8031 psa_reset_key_attributes(&attributes);
8032 psa_destroy_key(key);
8033 mbedtls_free(signature);
8034 PSA_DONE();
8035}
8036/* END_CASE */
8037
8038/* BEGIN_CASE */
8039void sign_verify_message(int key_type_arg,
8040 data_t *key_data,
8041 int alg_arg,
8042 data_t *input_data)
8043{
8044 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8045 psa_key_type_t key_type = key_type_arg;
8046 psa_algorithm_t alg = alg_arg;
8047 size_t key_bits;
8048 unsigned char *signature = NULL;
8049 size_t signature_size;
8050 size_t signature_length = 0xdeadbeef;
8051 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8052
8053 PSA_ASSERT(psa_crypto_init());
8054
8055 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
8056 PSA_KEY_USAGE_VERIFY_MESSAGE);
8057 psa_set_key_algorithm(&attributes, alg);
8058 psa_set_key_type(&attributes, key_type);
8059
8060 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8061 &key));
8062 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8063 key_bits = psa_get_key_bits(&attributes);
8064
8065 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8066 TEST_ASSERT(signature_size != 0);
8067 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008068 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008069
8070 PSA_ASSERT(psa_sign_message(key, alg,
8071 input_data->x, input_data->len,
8072 signature, signature_size,
8073 &signature_length));
8074 TEST_LE_U(signature_length, signature_size);
8075 TEST_ASSERT(signature_length > 0);
8076
8077 PSA_ASSERT(psa_verify_message(key, alg,
8078 input_data->x, input_data->len,
8079 signature, signature_length));
8080
8081 if (input_data->len != 0) {
8082 /* Flip a bit in the input and verify that the signature is now
8083 * detected as invalid. Flip a bit at the beginning, not at the end,
8084 * because ECDSA may ignore the last few bits of the input. */
8085 input_data->x[0] ^= 1;
8086 TEST_EQUAL(psa_verify_message(key, alg,
8087 input_data->x, input_data->len,
8088 signature, signature_length),
8089 PSA_ERROR_INVALID_SIGNATURE);
8090 }
8091
8092exit:
8093 psa_reset_key_attributes(&attributes);
8094
8095 psa_destroy_key(key);
8096 mbedtls_free(signature);
8097 PSA_DONE();
8098}
8099/* END_CASE */
8100
8101/* BEGIN_CASE */
8102void verify_message(int key_type_arg,
8103 data_t *key_data,
8104 int alg_arg,
8105 data_t *input_data,
8106 data_t *signature_data)
8107{
8108 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8109 psa_key_type_t key_type = key_type_arg;
8110 psa_algorithm_t alg = alg_arg;
8111 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8112
8113 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8114
8115 PSA_ASSERT(psa_crypto_init());
8116
8117 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8118 psa_set_key_algorithm(&attributes, alg);
8119 psa_set_key_type(&attributes, key_type);
8120
8121 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8122 &key));
8123
8124 PSA_ASSERT(psa_verify_message(key, alg,
8125 input_data->x, input_data->len,
8126 signature_data->x, signature_data->len));
8127
8128exit:
8129 psa_reset_key_attributes(&attributes);
8130 psa_destroy_key(key);
8131 PSA_DONE();
8132}
8133/* END_CASE */
8134
8135/* BEGIN_CASE */
8136void verify_message_fail(int key_type_arg,
8137 data_t *key_data,
8138 int alg_arg,
8139 data_t *hash_data,
8140 data_t *signature_data,
8141 int expected_status_arg)
8142{
8143 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8144 psa_key_type_t key_type = key_type_arg;
8145 psa_algorithm_t alg = alg_arg;
8146 psa_status_t actual_status;
8147 psa_status_t expected_status = expected_status_arg;
8148 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8149
8150 PSA_ASSERT(psa_crypto_init());
8151
8152 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8153 psa_set_key_algorithm(&attributes, alg);
8154 psa_set_key_type(&attributes, key_type);
8155
8156 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8157 &key));
8158
8159 actual_status = psa_verify_message(key, alg,
8160 hash_data->x, hash_data->len,
8161 signature_data->x,
8162 signature_data->len);
8163 TEST_EQUAL(actual_status, expected_status);
8164
8165exit:
8166 psa_reset_key_attributes(&attributes);
8167 psa_destroy_key(key);
8168 PSA_DONE();
8169}
8170/* END_CASE */
8171
8172/* BEGIN_CASE */
8173void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008174 data_t *key_data,
8175 int alg_arg,
8176 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008177 data_t *label,
8178 int expected_output_length_arg,
8179 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008180{
Ronald Cron5425a212020-08-04 14:58:35 +02008181 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008182 psa_key_type_t key_type = key_type_arg;
8183 psa_algorithm_t alg = alg_arg;
8184 size_t expected_output_length = expected_output_length_arg;
8185 size_t key_bits;
8186 unsigned char *output = NULL;
8187 size_t output_size;
8188 size_t output_length = ~0;
8189 psa_status_t actual_status;
8190 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008191 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008192
Gilles Peskine449bd832023-01-11 14:50:10 +01008193 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008194
Gilles Peskine656896e2018-06-29 19:12:28 +02008195 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008196 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8197 psa_set_key_algorithm(&attributes, alg);
8198 psa_set_key_type(&attributes, key_type);
8199 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8200 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008201
8202 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008203 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8204 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008205
Gilles Peskine449bd832023-01-11 14:50:10 +01008206 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8207 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008208 TEST_CALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008209
8210 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008211 actual_status = psa_asymmetric_encrypt(key, alg,
8212 input_data->x, input_data->len,
8213 label->x, label->len,
8214 output, output_size,
8215 &output_length);
8216 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008217 if (actual_status == PSA_SUCCESS) {
8218 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008219 } else {
8220 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008221 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008222
Gilles Peskine68428122018-06-30 18:42:41 +02008223 /* If the label is empty, the test framework puts a non-null pointer
8224 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008225 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008226 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008227 if (output_size != 0) {
8228 memset(output, 0, output_size);
8229 }
8230 actual_status = psa_asymmetric_encrypt(key, alg,
8231 input_data->x, input_data->len,
8232 NULL, label->len,
8233 output, output_size,
8234 &output_length);
8235 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008236 if (actual_status == PSA_SUCCESS) {
8237 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008238 } else {
8239 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008240 }
Gilles Peskine68428122018-06-30 18:42:41 +02008241 }
8242
Gilles Peskine656896e2018-06-29 19:12:28 +02008243exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008244 /*
8245 * Key attributes may have been returned by psa_get_key_attributes()
8246 * thus reset them as required.
8247 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008248 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008249
Gilles Peskine449bd832023-01-11 14:50:10 +01008250 psa_destroy_key(key);
8251 mbedtls_free(output);
8252 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008253}
8254/* END_CASE */
8255
8256/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008257void asymmetric_encrypt_decrypt(int key_type_arg,
8258 data_t *key_data,
8259 int alg_arg,
8260 data_t *input_data,
8261 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008262{
Ronald Cron5425a212020-08-04 14:58:35 +02008263 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008264 psa_key_type_t key_type = key_type_arg;
8265 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008266 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008267 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008268 size_t output_size;
8269 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008270 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008271 size_t output2_size;
8272 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008273 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008274
Gilles Peskine449bd832023-01-11 14:50:10 +01008275 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008276
Gilles Peskine449bd832023-01-11 14:50:10 +01008277 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8278 psa_set_key_algorithm(&attributes, alg);
8279 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008280
Gilles Peskine449bd832023-01-11 14:50:10 +01008281 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8282 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008283
8284 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008285 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8286 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008287
Gilles Peskine449bd832023-01-11 14:50:10 +01008288 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8289 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008290 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008291
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008292 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008293 TEST_LE_U(output2_size,
8294 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8295 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008296 TEST_CALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008297
Gilles Peskineeebd7382018-06-08 18:11:54 +02008298 /* We test encryption by checking that encrypt-then-decrypt gives back
8299 * the original plaintext because of the non-optional random
8300 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008301 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8302 input_data->x, input_data->len,
8303 label->x, label->len,
8304 output, output_size,
8305 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008306 /* We don't know what ciphertext length to expect, but check that
8307 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008308 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008309
Gilles Peskine449bd832023-01-11 14:50:10 +01008310 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8311 output, output_length,
8312 label->x, label->len,
8313 output2, output2_size,
8314 &output2_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008315 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008316 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008317
8318exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008319 /*
8320 * Key attributes may have been returned by psa_get_key_attributes()
8321 * thus reset them as required.
8322 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008323 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008324
Gilles Peskine449bd832023-01-11 14:50:10 +01008325 psa_destroy_key(key);
8326 mbedtls_free(output);
8327 mbedtls_free(output2);
8328 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008329}
8330/* END_CASE */
8331
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008332/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008333void asymmetric_decrypt(int key_type_arg,
8334 data_t *key_data,
8335 int alg_arg,
8336 data_t *input_data,
8337 data_t *label,
8338 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008339{
Ronald Cron5425a212020-08-04 14:58:35 +02008340 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008341 psa_key_type_t key_type = key_type_arg;
8342 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008343 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008344 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008345 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008346 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008347 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008348
Gilles Peskine449bd832023-01-11 14:50:10 +01008349 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008350
Gilles Peskine449bd832023-01-11 14:50:10 +01008351 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8352 psa_set_key_algorithm(&attributes, alg);
8353 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008354
Gilles Peskine449bd832023-01-11 14:50:10 +01008355 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8356 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008357
Gilles Peskine449bd832023-01-11 14:50:10 +01008358 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8359 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008360
8361 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008362 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8363 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008364 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008365
Gilles Peskine449bd832023-01-11 14:50:10 +01008366 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8367 input_data->x, input_data->len,
8368 label->x, label->len,
8369 output,
8370 output_size,
8371 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008372 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008373 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008374
Gilles Peskine68428122018-06-30 18:42:41 +02008375 /* If the label is empty, the test framework puts a non-null pointer
8376 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008377 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008378 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008379 if (output_size != 0) {
8380 memset(output, 0, output_size);
8381 }
8382 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8383 input_data->x, input_data->len,
8384 NULL, label->len,
8385 output,
8386 output_size,
8387 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008388 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008389 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008390 }
8391
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008392exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008393 psa_reset_key_attributes(&attributes);
8394 psa_destroy_key(key);
8395 mbedtls_free(output);
8396 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008397}
8398/* END_CASE */
8399
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008400/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008401void asymmetric_decrypt_fail(int key_type_arg,
8402 data_t *key_data,
8403 int alg_arg,
8404 data_t *input_data,
8405 data_t *label,
8406 int output_size_arg,
8407 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008408{
Ronald Cron5425a212020-08-04 14:58:35 +02008409 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008410 psa_key_type_t key_type = key_type_arg;
8411 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008412 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008413 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008414 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008415 psa_status_t actual_status;
8416 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008417 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008418
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008419 TEST_CALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008420
Gilles Peskine449bd832023-01-11 14:50:10 +01008421 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008422
Gilles Peskine449bd832023-01-11 14:50:10 +01008423 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8424 psa_set_key_algorithm(&attributes, alg);
8425 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008426
Gilles Peskine449bd832023-01-11 14:50:10 +01008427 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8428 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008429
Gilles Peskine449bd832023-01-11 14:50:10 +01008430 actual_status = psa_asymmetric_decrypt(key, alg,
8431 input_data->x, input_data->len,
8432 label->x, label->len,
8433 output, output_size,
8434 &output_length);
8435 TEST_EQUAL(actual_status, expected_status);
8436 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008437
Gilles Peskine68428122018-06-30 18:42:41 +02008438 /* If the label is empty, the test framework puts a non-null pointer
8439 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008440 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008441 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008442 if (output_size != 0) {
8443 memset(output, 0, output_size);
8444 }
8445 actual_status = psa_asymmetric_decrypt(key, alg,
8446 input_data->x, input_data->len,
8447 NULL, label->len,
8448 output, output_size,
8449 &output_length);
8450 TEST_EQUAL(actual_status, expected_status);
8451 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008452 }
8453
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008454exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008455 psa_reset_key_attributes(&attributes);
8456 psa_destroy_key(key);
8457 mbedtls_free(output);
8458 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008459}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008460/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008461
8462/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008463void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008464{
8465 /* Test each valid way of initializing the object, except for `= {0}`, as
8466 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8467 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008468 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008469 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008470 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008471 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8472 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008473
Gilles Peskine449bd832023-01-11 14:50:10 +01008474 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008475
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008476 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008477 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8478 PSA_ERROR_BAD_STATE);
8479 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8480 PSA_ERROR_BAD_STATE);
8481 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8482 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008483
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008484 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008485 PSA_ASSERT(psa_key_derivation_abort(&func));
8486 PSA_ASSERT(psa_key_derivation_abort(&init));
8487 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008488}
8489/* END_CASE */
8490
Janos Follath16de4a42019-06-13 16:32:24 +01008491/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008492void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008493{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008494 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008495 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008496 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008497
Gilles Peskine449bd832023-01-11 14:50:10 +01008498 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008499
Gilles Peskine449bd832023-01-11 14:50:10 +01008500 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8501 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008502
8503exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008504 psa_key_derivation_abort(&operation);
8505 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008506}
8507/* END_CASE */
8508
Janos Follathaf3c2a02019-06-12 12:34:34 +01008509/* BEGIN_CASE */
Kusumit Ghoderao9ffd3972023-12-01 16:40:13 +05308510void derive_set_capacity(int alg_arg, int64_t capacity_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01008511 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008512{
8513 psa_algorithm_t alg = alg_arg;
8514 size_t capacity = capacity_arg;
8515 psa_status_t expected_status = expected_status_arg;
8516 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8517
Gilles Peskine449bd832023-01-11 14:50:10 +01008518 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008519
Gilles Peskine449bd832023-01-11 14:50:10 +01008520 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008521
Gilles Peskine449bd832023-01-11 14:50:10 +01008522 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8523 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008524
8525exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008526 psa_key_derivation_abort(&operation);
8527 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008528}
8529/* END_CASE */
8530
8531/* BEGIN_CASE */
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308532void parse_binary_string_test(data_t *input, int output)
8533{
8534 uint64_t value;
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308535 value = mbedtls_test_parse_binary_string(input);
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308536 TEST_EQUAL(value, output);
8537}
8538/* END_CASE */
8539
8540/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008541void derive_input(int alg_arg,
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308542 int step_arg1, int key_type_arg1, data_t *input1,
8543 int expected_status_arg1,
8544 int step_arg2, int key_type_arg2, data_t *input2,
8545 int expected_status_arg2,
8546 int step_arg3, int key_type_arg3, data_t *input3,
8547 int expected_status_arg3,
Gilles Peskine449bd832023-01-11 14:50:10 +01008548 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008549{
8550 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008551 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308552 uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
Gilles Peskine449bd832023-01-11 14:50:10 +01008553 psa_status_t expected_statuses[] = { expected_status_arg1,
8554 expected_status_arg2,
8555 expected_status_arg3 };
8556 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008557 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8558 MBEDTLS_SVC_KEY_ID_INIT,
8559 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008560 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8561 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8562 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008563 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008564 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008565 psa_status_t expected_output_status = expected_output_status_arg;
8566 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008567
Gilles Peskine449bd832023-01-11 14:50:10 +01008568 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008569
Gilles Peskine449bd832023-01-11 14:50:10 +01008570 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8571 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008572
Gilles Peskine449bd832023-01-11 14:50:10 +01008573 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008574
Gilles Peskine449bd832023-01-11 14:50:10 +01008575 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8576 mbedtls_test_set_step(i);
8577 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008578 /* Skip this step */
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308579 } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE &&
8580 key_types[i] != INPUT_INTEGER) {
8581 psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i]));
Gilles Peskine449bd832023-01-11 14:50:10 +01008582 PSA_ASSERT(psa_import_key(&attributes,
8583 inputs[i]->x, inputs[i]->len,
8584 &keys[i]));
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308585 if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008586 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008587 // When taking a private key as secret input, use key agreement
8588 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008589 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8590 &operation, keys[i]),
8591 expected_statuses[i]);
8592 } else {
8593 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8594 keys[i]),
8595 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008596 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008597 } else {
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308598 if (key_types[i] == INPUT_INTEGER) {
8599 TEST_EQUAL(psa_key_derivation_input_integer(
8600 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308601 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308602 expected_statuses[i]);
8603 } else {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308604 TEST_EQUAL(psa_key_derivation_input_bytes(
8605 &operation, steps[i],
8606 inputs[i]->x, inputs[i]->len),
8607 expected_statuses[i]);
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308608 }
Janos Follathaf3c2a02019-06-12 12:34:34 +01008609 }
8610 }
8611
Gilles Peskine449bd832023-01-11 14:50:10 +01008612 if (output_key_type != PSA_KEY_TYPE_NONE) {
8613 psa_reset_key_attributes(&attributes);
8614 psa_set_key_type(&attributes, output_key_type);
8615 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008616 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008617 psa_key_derivation_output_key(&attributes, &operation,
8618 &output_key);
8619 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008620 uint8_t buffer[1];
8621 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008622 psa_key_derivation_output_bytes(&operation,
8623 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008624 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008625 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008626
Janos Follathaf3c2a02019-06-12 12:34:34 +01008627exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008628 psa_key_derivation_abort(&operation);
8629 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8630 psa_destroy_key(keys[i]);
8631 }
8632 psa_destroy_key(output_key);
8633 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008634}
8635/* END_CASE */
8636
Kusumit Ghoderao42b02b92023-06-06 16:48:46 +05308637/* BEGIN_CASE*/
8638void derive_input_invalid_cost(int alg_arg, int64_t cost)
8639{
8640 psa_algorithm_t alg = alg_arg;
8641 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8642
8643 PSA_ASSERT(psa_crypto_init());
8644 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8645
8646 TEST_EQUAL(psa_key_derivation_input_integer(&operation,
8647 PSA_KEY_DERIVATION_INPUT_COST,
8648 cost),
8649 PSA_ERROR_NOT_SUPPORTED);
8650
8651exit:
8652 psa_key_derivation_abort(&operation);
8653 PSA_DONE();
8654}
8655/* END_CASE*/
8656
Janos Follathd958bb72019-07-03 15:02:16 +01008657/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008658void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008659{
Janos Follathd958bb72019-07-03 15:02:16 +01008660 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008661 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008662 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008663 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008664 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008665 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008666 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008667 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008668 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008669 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008670 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8671 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008672 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008673 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008674
Gilles Peskine449bd832023-01-11 14:50:10 +01008675 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008676
Gilles Peskine449bd832023-01-11 14:50:10 +01008677 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8678 psa_set_key_algorithm(&attributes, alg);
8679 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008680
Gilles Peskine449bd832023-01-11 14:50:10 +01008681 PSA_ASSERT(psa_import_key(&attributes,
8682 key_data, sizeof(key_data),
8683 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008684
8685 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008686 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8687 input1, input1_length,
8688 input2, input2_length,
8689 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008690 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008691 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008692
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008693 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008694 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8695 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008696
Gilles Peskine449bd832023-01-11 14:50:10 +01008697 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008698
Gilles Peskine449bd832023-01-11 14:50:10 +01008699 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8700 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008701
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008702exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008703 psa_key_derivation_abort(&operation);
8704 psa_destroy_key(key);
8705 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008706}
8707/* END_CASE */
8708
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008709/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008710void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008711{
8712 uint8_t output_buffer[16];
8713 size_t buffer_size = 16;
8714 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008715 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008716
Gilles Peskine449bd832023-01-11 14:50:10 +01008717 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8718 output_buffer, buffer_size)
8719 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008720
Gilles Peskine449bd832023-01-11 14:50:10 +01008721 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8722 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008723
Gilles Peskine449bd832023-01-11 14:50:10 +01008724 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008725
Gilles Peskine449bd832023-01-11 14:50:10 +01008726 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8727 output_buffer, buffer_size)
8728 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008729
Gilles Peskine449bd832023-01-11 14:50:10 +01008730 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8731 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008732
8733exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008734 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008735}
8736/* END_CASE */
8737
8738/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008739void derive_output(int alg_arg,
8740 int step1_arg, data_t *input1, int expected_status_arg1,
8741 int step2_arg, data_t *input2, int expected_status_arg2,
8742 int step3_arg, data_t *input3, int expected_status_arg3,
8743 int step4_arg, data_t *input4, int expected_status_arg4,
8744 data_t *key_agreement_peer_key,
8745 int requested_capacity_arg,
8746 data_t *expected_output1,
8747 data_t *expected_output2,
8748 int other_key_input_type,
8749 int key_input_type,
8750 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008751{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008752 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008753 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8754 data_t *inputs[] = { input1, input2, input3, input4 };
8755 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8756 MBEDTLS_SVC_KEY_ID_INIT,
8757 MBEDTLS_SVC_KEY_ID_INIT,
8758 MBEDTLS_SVC_KEY_ID_INIT };
8759 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8760 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008761 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008762 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008763 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008764 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008765 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008766 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008767 size_t output_buffer_size = 0;
8768 uint8_t *output_buffer = NULL;
8769 size_t expected_capacity;
8770 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008771 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8772 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8773 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8774 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008775 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008776 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008777 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008778
Gilles Peskine449bd832023-01-11 14:50:10 +01008779 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8780 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008781 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008782 }
8783 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008784 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008785 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008786 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008787 TEST_CALLOC(output_buffer, output_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008788 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008789
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008790 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008791 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8792 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8793 requested_capacity));
8794 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8795 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008796 case 0:
8797 break;
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308798 case PSA_KEY_DERIVATION_INPUT_COST:
8799 TEST_EQUAL(psa_key_derivation_input_integer(
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308800 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308801 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308802 statuses[i]);
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308803 if (statuses[i] != PSA_SUCCESS) {
8804 goto exit;
8805 }
8806 break;
8807 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Gilles Peskine1468da72019-05-29 17:35:49 +02008808 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008809 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008810 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008811 TEST_EQUAL(psa_key_derivation_input_bytes(
8812 &operation, steps[i],
8813 inputs[i]->x, inputs[i]->len),
8814 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008815
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008817 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008819 break;
8820 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8822 psa_set_key_algorithm(&attributes1, alg);
8823 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008824
Gilles Peskine449bd832023-01-11 14:50:10 +01008825 PSA_ASSERT(psa_import_key(&attributes1,
8826 inputs[i]->x, inputs[i]->len,
8827 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008828
Gilles Peskine449bd832023-01-11 14:50:10 +01008829 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8830 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8831 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8832 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008833 }
8834
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308835 TEST_EQUAL(psa_key_derivation_input_key(&operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01008836 steps[i],
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308837 keys[i]),
8838 statuses[i]);
8839
8840 if (statuses[i] != PSA_SUCCESS) {
8841 goto exit;
8842 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008843 break;
8844 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008845 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008846 break;
8847 }
8848 break;
8849 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008850 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008851 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008852 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8853 steps[i],
8854 inputs[i]->x,
8855 inputs[i]->len),
8856 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008857 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008858 case 1: // input key, type DERIVE
8859 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008860 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8861 psa_set_key_algorithm(&attributes2, alg);
8862 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008863
8864 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008865 if (other_key_input_type == 11) {
8866 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8867 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008868
Gilles Peskine449bd832023-01-11 14:50:10 +01008869 PSA_ASSERT(psa_import_key(&attributes2,
8870 inputs[i]->x, inputs[i]->len,
8871 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008872
Gilles Peskine449bd832023-01-11 14:50:10 +01008873 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8874 steps[i],
8875 keys[i]),
8876 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008877 break;
8878 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008879 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8880 psa_set_key_algorithm(&attributes3, alg);
8881 psa_set_key_type(&attributes3,
8882 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008883
Gilles Peskine449bd832023-01-11 14:50:10 +01008884 PSA_ASSERT(psa_import_key(&attributes3,
8885 inputs[i]->x, inputs[i]->len,
8886 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008887
Gilles Peskine449bd832023-01-11 14:50:10 +01008888 TEST_EQUAL(psa_key_derivation_key_agreement(
8889 &operation,
8890 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8891 keys[i], key_agreement_peer_key->x,
8892 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008893 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008894 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008895 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008896 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008897 }
8898
Gilles Peskine449bd832023-01-11 14:50:10 +01008899 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008900 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008901 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008902 break;
8903 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008904 TEST_EQUAL(psa_key_derivation_input_bytes(
8905 &operation, steps[i],
8906 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008907
Gilles Peskine449bd832023-01-11 14:50:10 +01008908 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008909 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008910 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008911 break;
8912 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008913 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008914
Gilles Peskine449bd832023-01-11 14:50:10 +01008915 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8916 &current_capacity));
8917 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008918 expected_capacity = requested_capacity;
8919
Gilles Peskine449bd832023-01-11 14:50:10 +01008920 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008921 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8922
8923 /* For output key derivation secret must be provided using
8924 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008925 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008926 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008927 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008928
Gilles Peskine449bd832023-01-11 14:50:10 +01008929 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8930 psa_set_key_algorithm(&attributes4, alg);
8931 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8932 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008933
Gilles Peskine449bd832023-01-11 14:50:10 +01008934 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8935 &derived_key), expected_status);
8936 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008937 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008938 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008939 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008940 status = psa_key_derivation_output_bytes(&operation,
8941 output_buffer, output_sizes[i]);
8942 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008943 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008944 TEST_ASSERT(status == PSA_SUCCESS ||
8945 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008946 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008947 } else if (expected_capacity == 0 ||
8948 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008949 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008950 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008951 expected_capacity = 0;
8952 continue;
8953 }
8954 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008955 PSA_ASSERT(status);
8956 if (output_sizes[i] != 0) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008957 TEST_MEMORY_COMPARE(output_buffer, output_sizes[i],
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008958 expected_outputs[i], output_sizes[i]);
Gilles Peskine449bd832023-01-11 14:50:10 +01008959 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008960 /* Check the operation status. */
8961 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008962 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8963 &current_capacity));
8964 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008965 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008966 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008967 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008968
8969exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008970 mbedtls_free(output_buffer);
8971 psa_key_derivation_abort(&operation);
8972 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8973 psa_destroy_key(keys[i]);
8974 }
8975 psa_destroy_key(derived_key);
8976 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008977}
8978/* END_CASE */
8979
8980/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008981void derive_full(int alg_arg,
8982 data_t *key_data,
8983 data_t *input1,
8984 data_t *input2,
8985 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008986{
Ronald Cron5425a212020-08-04 14:58:35 +02008987 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008988 psa_algorithm_t alg = alg_arg;
8989 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008990 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Kusumit Ghoderao9ffd3972023-12-01 16:40:13 +05308991 unsigned char output_buffer[32];
Gilles Peskined54931c2018-07-17 21:06:59 +02008992 size_t expected_capacity = requested_capacity;
8993 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008994 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008995
Gilles Peskine449bd832023-01-11 14:50:10 +01008996 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008997
Gilles Peskine449bd832023-01-11 14:50:10 +01008998 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8999 psa_set_key_algorithm(&attributes, alg);
9000 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02009001
Gilles Peskine449bd832023-01-11 14:50:10 +01009002 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9003 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02009004
Gilles Peskine449bd832023-01-11 14:50:10 +01009005 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
9006 input1->x, input1->len,
9007 input2->x, input2->len,
9008 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01009009 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009010 }
Janos Follath47f27ed2019-06-25 13:24:52 +01009011
Gilles Peskine449bd832023-01-11 14:50:10 +01009012 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9013 &current_capacity));
9014 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02009015
9016 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009017 while (current_capacity > 0) {
9018 size_t read_size = sizeof(output_buffer);
9019 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02009020 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01009021 }
9022 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9023 output_buffer,
9024 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02009025 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01009026 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
9027 &current_capacity));
9028 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02009029 }
9030
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009031 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009032 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
9033 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02009034
Gilles Peskine449bd832023-01-11 14:50:10 +01009035 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02009036
9037exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009038 psa_key_derivation_abort(&operation);
9039 psa_destroy_key(key);
9040 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02009041}
9042/* END_CASE */
9043
Stephan Koch78109f52023-04-12 14:19:36 +02009044/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01009045void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
9046 int derivation_step,
9047 int capacity, int expected_capacity_status_arg,
9048 data_t *expected_output,
9049 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009050{
9051 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
9052 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04009053 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009054 uint8_t *output_buffer = NULL;
9055 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04009056 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
9057 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
9058 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009059
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009060 TEST_CALLOC(output_buffer, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009061 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009062
Gilles Peskine449bd832023-01-11 14:50:10 +01009063 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9064 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
9065 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009066
Gilles Peskine449bd832023-01-11 14:50:10 +01009067 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
9068 step, input->x, input->len),
9069 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009070
Gilles Peskine449bd832023-01-11 14:50:10 +01009071 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009072 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009073 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009074
Gilles Peskine449bd832023-01-11 14:50:10 +01009075 status = psa_key_derivation_output_bytes(&operation, output_buffer,
9076 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009077
Gilles Peskine449bd832023-01-11 14:50:10 +01009078 TEST_EQUAL(status, expected_output_status);
9079 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009080 TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009081 expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009082 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009083
9084exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009085 mbedtls_free(output_buffer);
9086 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009087 PSA_DONE();
9088}
9089/* END_CASE */
9090
Janos Follathe60c9052019-07-03 13:51:30 +01009091/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009092void derive_key_exercise(int alg_arg,
9093 data_t *key_data,
9094 data_t *input1,
9095 data_t *input2,
9096 int derived_type_arg,
9097 int derived_bits_arg,
9098 int derived_usage_arg,
9099 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009100{
Ronald Cron5425a212020-08-04 14:58:35 +02009101 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9102 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009103 psa_algorithm_t alg = alg_arg;
9104 psa_key_type_t derived_type = derived_type_arg;
9105 size_t derived_bits = derived_bits_arg;
9106 psa_key_usage_t derived_usage = derived_usage_arg;
9107 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009108 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009109 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009110 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009111 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009112
Gilles Peskine449bd832023-01-11 14:50:10 +01009113 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009114
Gilles Peskine449bd832023-01-11 14:50:10 +01009115 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9116 psa_set_key_algorithm(&attributes, alg);
9117 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9118 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9119 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009120
9121 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009122 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9123 input1->x, input1->len,
9124 input2->x, input2->len,
9125 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009126 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009127 }
Janos Follathe60c9052019-07-03 13:51:30 +01009128
Gilles Peskine449bd832023-01-11 14:50:10 +01009129 psa_set_key_usage_flags(&attributes, derived_usage);
9130 psa_set_key_algorithm(&attributes, derived_alg);
9131 psa_set_key_type(&attributes, derived_type);
9132 psa_set_key_bits(&attributes, derived_bits);
9133 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9134 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009135
9136 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009137 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9138 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9139 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009140
9141 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009142 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009143 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009144 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009145
9146exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009147 /*
9148 * Key attributes may have been returned by psa_get_key_attributes()
9149 * thus reset them as required.
9150 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009151 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009152
Gilles Peskine449bd832023-01-11 14:50:10 +01009153 psa_key_derivation_abort(&operation);
9154 psa_destroy_key(base_key);
9155 psa_destroy_key(derived_key);
9156 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009157}
9158/* END_CASE */
9159
Janos Follath42fd8882019-07-03 14:17:09 +01009160/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009161void derive_key_export(int alg_arg,
9162 data_t *key_data,
9163 data_t *input1,
9164 data_t *input2,
9165 int bytes1_arg,
9166 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009167{
Ronald Cron5425a212020-08-04 14:58:35 +02009168 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9169 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009170 psa_algorithm_t alg = alg_arg;
9171 size_t bytes1 = bytes1_arg;
9172 size_t bytes2 = bytes2_arg;
9173 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009174 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009175 uint8_t *output_buffer = NULL;
9176 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009177 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9178 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009179 size_t length;
9180
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009181 TEST_CALLOC(output_buffer, capacity);
9182 TEST_CALLOC(export_buffer, capacity);
Gilles Peskine449bd832023-01-11 14:50:10 +01009183 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009184
Gilles Peskine449bd832023-01-11 14:50:10 +01009185 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9186 psa_set_key_algorithm(&base_attributes, alg);
9187 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9188 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9189 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009190
9191 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009192 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9193 input1->x, input1->len,
9194 input2->x, input2->len,
9195 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009196 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009197 }
Janos Follath42fd8882019-07-03 14:17:09 +01009198
Gilles Peskine449bd832023-01-11 14:50:10 +01009199 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9200 output_buffer,
9201 capacity));
9202 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009203
9204 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009205 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9206 input1->x, input1->len,
9207 input2->x, input2->len,
9208 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009209 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009210 }
Janos Follath42fd8882019-07-03 14:17:09 +01009211
Gilles Peskine449bd832023-01-11 14:50:10 +01009212 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9213 psa_set_key_algorithm(&derived_attributes, 0);
9214 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9215 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9216 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9217 &derived_key));
9218 PSA_ASSERT(psa_export_key(derived_key,
9219 export_buffer, bytes1,
9220 &length));
9221 TEST_EQUAL(length, bytes1);
9222 PSA_ASSERT(psa_destroy_key(derived_key));
9223 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9224 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9225 &derived_key));
9226 PSA_ASSERT(psa_export_key(derived_key,
9227 export_buffer + bytes1, bytes2,
9228 &length));
9229 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009230
9231 /* Compare the outputs from the two runs. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009232 TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009233 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009234
9235exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009236 mbedtls_free(output_buffer);
9237 mbedtls_free(export_buffer);
9238 psa_key_derivation_abort(&operation);
9239 psa_destroy_key(base_key);
9240 psa_destroy_key(derived_key);
9241 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009242}
9243/* END_CASE */
9244
9245/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009246void derive_key_type(int alg_arg,
9247 data_t *key_data,
9248 data_t *input1,
9249 data_t *input2,
9250 int key_type_arg, int bits_arg,
9251 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009252{
9253 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9254 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9255 const psa_algorithm_t alg = alg_arg;
9256 const psa_key_type_t key_type = key_type_arg;
9257 const size_t bits = bits_arg;
9258 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9259 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009260 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009261 uint8_t *export_buffer = NULL;
9262 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9263 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9264 size_t export_length;
9265
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009266 TEST_CALLOC(export_buffer, export_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01009267 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009268
Gilles Peskine449bd832023-01-11 14:50:10 +01009269 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9270 psa_set_key_algorithm(&base_attributes, alg);
9271 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9272 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9273 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009274
Gilles Peskine449bd832023-01-11 14:50:10 +01009275 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009276 &operation, base_key, alg,
9277 input1->x, input1->len,
9278 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009279 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009280 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009281 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009282
Gilles Peskine449bd832023-01-11 14:50:10 +01009283 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9284 psa_set_key_algorithm(&derived_attributes, 0);
9285 psa_set_key_type(&derived_attributes, key_type);
9286 psa_set_key_bits(&derived_attributes, bits);
9287 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9288 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009289
Gilles Peskine449bd832023-01-11 14:50:10 +01009290 PSA_ASSERT(psa_export_key(derived_key,
9291 export_buffer, export_buffer_size,
9292 &export_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009293 TEST_MEMORY_COMPARE(export_buffer, export_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009294 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009295
9296exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009297 mbedtls_free(export_buffer);
9298 psa_key_derivation_abort(&operation);
9299 psa_destroy_key(base_key);
9300 psa_destroy_key(derived_key);
9301 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009302}
9303/* END_CASE */
9304
9305/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009306void derive_key(int alg_arg,
9307 data_t *key_data, data_t *input1, data_t *input2,
9308 int type_arg, int bits_arg,
9309 int expected_status_arg,
9310 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009311{
Ronald Cron5425a212020-08-04 14:58:35 +02009312 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9313 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009314 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009315 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009316 size_t bits = bits_arg;
9317 psa_status_t expected_status = expected_status_arg;
9318 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9319 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9320 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9321
Gilles Peskine449bd832023-01-11 14:50:10 +01009322 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009323
Gilles Peskine449bd832023-01-11 14:50:10 +01009324 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9325 psa_set_key_algorithm(&base_attributes, alg);
9326 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9327 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9328 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009329
Gilles Peskine449bd832023-01-11 14:50:10 +01009330 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9331 input1->x, input1->len,
9332 input2->x, input2->len,
9333 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009334 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009335 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009336
Gilles Peskine449bd832023-01-11 14:50:10 +01009337 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9338 psa_set_key_algorithm(&derived_attributes, 0);
9339 psa_set_key_type(&derived_attributes, type);
9340 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009341
9342 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009343 psa_key_derivation_output_key(&derived_attributes,
9344 &operation,
9345 &derived_key);
9346 if (is_large_output > 0) {
9347 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9348 }
9349 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009350
9351exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009352 psa_key_derivation_abort(&operation);
9353 psa_destroy_key(base_key);
9354 psa_destroy_key(derived_key);
9355 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009356}
9357/* END_CASE */
9358
9359/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009360void key_agreement_setup(int alg_arg,
9361 int our_key_type_arg, int our_key_alg_arg,
9362 data_t *our_key_data, data_t *peer_key_data,
9363 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009364{
Ronald Cron5425a212020-08-04 14:58:35 +02009365 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009366 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009367 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009368 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009369 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009370 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009371 psa_status_t expected_status = expected_status_arg;
9372 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009373
Gilles Peskine449bd832023-01-11 14:50:10 +01009374 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009375
Gilles Peskine449bd832023-01-11 14:50:10 +01009376 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9377 psa_set_key_algorithm(&attributes, our_key_alg);
9378 psa_set_key_type(&attributes, our_key_type);
9379 PSA_ASSERT(psa_import_key(&attributes,
9380 our_key_data->x, our_key_data->len,
9381 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009382
Gilles Peskine77f40d82019-04-11 21:27:06 +02009383 /* The tests currently include inputs that should fail at either step.
9384 * Test cases that fail at the setup step should be changed to call
9385 * key_derivation_setup instead, and this function should be renamed
9386 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009387 status = psa_key_derivation_setup(&operation, alg);
9388 if (status == PSA_SUCCESS) {
9389 TEST_EQUAL(psa_key_derivation_key_agreement(
9390 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9391 our_key,
9392 peer_key_data->x, peer_key_data->len),
9393 expected_status);
9394 } else {
9395 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009396 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009397
9398exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009399 psa_key_derivation_abort(&operation);
9400 psa_destroy_key(our_key);
9401 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009402}
9403/* END_CASE */
9404
9405/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009406void raw_key_agreement(int alg_arg,
9407 int our_key_type_arg, data_t *our_key_data,
9408 data_t *peer_key_data,
9409 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009410{
Ronald Cron5425a212020-08-04 14:58:35 +02009411 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009412 psa_algorithm_t alg = alg_arg;
9413 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009414 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009415 unsigned char *output = NULL;
9416 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009417 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009418
Gilles Peskine449bd832023-01-11 14:50:10 +01009419 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009420
Gilles Peskine449bd832023-01-11 14:50:10 +01009421 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9422 psa_set_key_algorithm(&attributes, alg);
9423 psa_set_key_type(&attributes, our_key_type);
9424 PSA_ASSERT(psa_import_key(&attributes,
9425 our_key_data->x, our_key_data->len,
9426 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009427
Gilles Peskine449bd832023-01-11 14:50:10 +01009428 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9429 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009430
Gilles Peskine992bee82022-04-13 23:25:52 +02009431 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009432 TEST_LE_U(expected_output->len,
9433 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9434 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9435 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009436
9437 /* Good case with exact output size */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009438 TEST_CALLOC(output, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009439 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9440 peer_key_data->x, peer_key_data->len,
9441 output, expected_output->len,
9442 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009443 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009444 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009445 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009446 output = NULL;
9447 output_length = ~0;
9448
9449 /* Larger buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009450 TEST_CALLOC(output, expected_output->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009451 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9452 peer_key_data->x, peer_key_data->len,
9453 output, expected_output->len + 1,
9454 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009455 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009456 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009457 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009458 output = NULL;
9459 output_length = ~0;
9460
9461 /* Buffer too small */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009462 TEST_CALLOC(output, expected_output->len - 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009463 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9464 peer_key_data->x, peer_key_data->len,
9465 output, expected_output->len - 1,
9466 &output_length),
9467 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009468 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009469 TEST_LE_U(output_length, expected_output->len - 1);
9470 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009471 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009472
9473exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009474 mbedtls_free(output);
9475 psa_destroy_key(our_key);
9476 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009477}
9478/* END_CASE */
9479
9480/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009481void key_agreement_capacity(int alg_arg,
9482 int our_key_type_arg, data_t *our_key_data,
9483 data_t *peer_key_data,
9484 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009485{
Ronald Cron5425a212020-08-04 14:58:35 +02009486 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009487 psa_algorithm_t alg = alg_arg;
9488 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009489 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009490 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009491 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009492 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009493
Gilles Peskine449bd832023-01-11 14:50:10 +01009494 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009495
Gilles Peskine449bd832023-01-11 14:50:10 +01009496 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9497 psa_set_key_algorithm(&attributes, alg);
9498 psa_set_key_type(&attributes, our_key_type);
9499 PSA_ASSERT(psa_import_key(&attributes,
9500 our_key_data->x, our_key_data->len,
9501 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009502
Gilles Peskine449bd832023-01-11 14:50:10 +01009503 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9504 PSA_ASSERT(psa_key_derivation_key_agreement(
9505 &operation,
9506 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9507 peer_key_data->x, peer_key_data->len));
9508 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009509 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009510 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9511 PSA_KEY_DERIVATION_INPUT_INFO,
9512 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009513 }
Gilles Peskine59685592018-09-18 12:11:34 +02009514
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009515 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009516 PSA_ASSERT(psa_key_derivation_get_capacity(
9517 &operation, &actual_capacity));
9518 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009519
Gilles Peskinebf491972018-10-25 22:36:12 +02009520 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009521 while (actual_capacity > sizeof(output)) {
9522 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9523 output, sizeof(output)));
9524 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009525 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009526 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9527 output, actual_capacity));
9528 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9529 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009530
Gilles Peskine59685592018-09-18 12:11:34 +02009531exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009532 psa_key_derivation_abort(&operation);
9533 psa_destroy_key(our_key);
9534 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009535}
9536/* END_CASE */
9537
Valerio Settiad819672023-12-29 12:14:41 +01009538/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9539void ecc_conversion_functions(int grp_id_arg, int psa_family_arg, int bits_arg)
Valerio Settibf999cb2023-12-28 17:48:13 +01009540{
9541 mbedtls_ecp_group_id grp_id = grp_id_arg;
Valerio Settiad819672023-12-29 12:14:41 +01009542 psa_ecc_family_t ecc_family = psa_family_arg;
9543 size_t bits = bits_arg;
9544 size_t bits_tmp;
Valerio Settibf999cb2023-12-28 17:48:13 +01009545
Valerio Settiad819672023-12-29 12:14:41 +01009546 TEST_EQUAL(ecc_family, mbedtls_ecc_group_to_psa(grp_id, &bits_tmp));
9547 TEST_EQUAL(bits, bits_tmp);
Valerio Settiac739522024-01-04 10:22:01 +01009548 TEST_EQUAL(grp_id, mbedtls_ecc_group_from_psa(ecc_family, bits));
Valerio Settibf999cb2023-12-28 17:48:13 +01009549}
9550/* END_CASE */
9551
Valerio Settiac739522024-01-04 10:22:01 +01009552/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9553void ecc_conversion_functions_fail()
9554{
9555 size_t bits;
9556
Valerio Settidb6e0292024-01-05 10:15:45 +01009557 /* Invalid legacy curve identifiers. */
9558 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_MAX, &bits));
9559 TEST_EQUAL(0, bits);
Valerio Settiac739522024-01-04 10:22:01 +01009560 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_NONE, &bits));
9561 TEST_EQUAL(0, bits);
9562
9563 /* Invalid PSA EC family. */
9564 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(0, 192));
9565 /* Invalid bit-size for a valid EC family. */
9566 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_SECP_R1, 512));
9567
9568 /* Twisted-Edward curves are not supported yet. */
9569 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9570 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255));
9571 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9572 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 448));
9573}
9574/* END_CASE */
9575
9576
Valerio Settibf999cb2023-12-28 17:48:13 +01009577/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009578void key_agreement_output(int alg_arg,
9579 int our_key_type_arg, data_t *our_key_data,
9580 data_t *peer_key_data,
9581 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009582{
Ronald Cron5425a212020-08-04 14:58:35 +02009583 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009584 psa_algorithm_t alg = alg_arg;
9585 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009586 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009587 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009588 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009589
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009590 TEST_CALLOC(actual_output, MAX(expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009591 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009592
Gilles Peskine449bd832023-01-11 14:50:10 +01009593 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009594
Gilles Peskine449bd832023-01-11 14:50:10 +01009595 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9596 psa_set_key_algorithm(&attributes, alg);
9597 psa_set_key_type(&attributes, our_key_type);
9598 PSA_ASSERT(psa_import_key(&attributes,
9599 our_key_data->x, our_key_data->len,
9600 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009601
Gilles Peskine449bd832023-01-11 14:50:10 +01009602 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9603 PSA_ASSERT(psa_key_derivation_key_agreement(
9604 &operation,
9605 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9606 peer_key_data->x, peer_key_data->len));
9607 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009608 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009609 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9610 PSA_KEY_DERIVATION_INPUT_INFO,
9611 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009612 }
Gilles Peskine59685592018-09-18 12:11:34 +02009613
Gilles Peskine449bd832023-01-11 14:50:10 +01009614 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9615 actual_output,
9616 expected_output1->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009617 TEST_MEMORY_COMPARE(actual_output, expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009618 expected_output1->x, expected_output1->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009619 if (expected_output2->len != 0) {
9620 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9621 actual_output,
9622 expected_output2->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009623 TEST_MEMORY_COMPARE(actual_output, expected_output2->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009624 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009625 }
Gilles Peskine59685592018-09-18 12:11:34 +02009626
9627exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009628 psa_key_derivation_abort(&operation);
9629 psa_destroy_key(our_key);
9630 PSA_DONE();
9631 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009632}
9633/* END_CASE */
9634
9635/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009636void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009637{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009638 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009639 unsigned char *output = NULL;
9640 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009641 size_t i;
9642 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009643
Gilles Peskine449bd832023-01-11 14:50:10 +01009644 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009645
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009646 TEST_CALLOC(output, bytes);
9647 TEST_CALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009648
Gilles Peskine449bd832023-01-11 14:50:10 +01009649 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009650
Gilles Peskinea50d7392018-06-21 10:22:13 +02009651 /* Run several times, to ensure that every output byte will be
9652 * nonzero at least once with overwhelming probability
9653 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009654 for (run = 0; run < 10; run++) {
9655 if (bytes != 0) {
9656 memset(output, 0, bytes);
9657 }
9658 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009659
Gilles Peskine449bd832023-01-11 14:50:10 +01009660 for (i = 0; i < bytes; i++) {
9661 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009662 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009663 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009664 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009665 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009666
9667 /* Check that every byte was changed to nonzero at least once. This
9668 * validates that psa_generate_random is overwriting every byte of
9669 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009670 for (i = 0; i < bytes; i++) {
9671 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009672 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009673
9674exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009675 PSA_DONE();
9676 mbedtls_free(output);
9677 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009678}
9679/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009680
9681/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009682void generate_key(int type_arg,
9683 int bits_arg,
9684 int usage_arg,
9685 int alg_arg,
9686 int expected_status_arg,
9687 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009688{
Ronald Cron5425a212020-08-04 14:58:35 +02009689 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009690 psa_key_type_t type = type_arg;
9691 psa_key_usage_t usage = usage_arg;
9692 size_t bits = bits_arg;
9693 psa_algorithm_t alg = alg_arg;
9694 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009695 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009696 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009697
Gilles Peskine449bd832023-01-11 14:50:10 +01009698 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009699
Gilles Peskine449bd832023-01-11 14:50:10 +01009700 psa_set_key_usage_flags(&attributes, usage);
9701 psa_set_key_algorithm(&attributes, alg);
9702 psa_set_key_type(&attributes, type);
9703 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009704
9705 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009706 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009707
Gilles Peskine449bd832023-01-11 14:50:10 +01009708 if (is_large_key > 0) {
9709 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9710 }
9711 TEST_EQUAL(status, expected_status);
9712 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009713 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009714 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009715
9716 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009717 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9718 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9719 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009720
Gilles Peskine818ca122018-06-20 18:16:48 +02009721 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009722 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009723 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009724 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009725
9726exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009727 /*
9728 * Key attributes may have been returned by psa_get_key_attributes()
9729 * thus reset them as required.
9730 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009731 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009732
Gilles Peskine449bd832023-01-11 14:50:10 +01009733 psa_destroy_key(key);
9734 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009735}
9736/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009737
Valerio Setti19fec542023-07-25 12:31:50 +02009738/* 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 +01009739void generate_key_rsa(int bits_arg,
9740 data_t *e_arg,
9741 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009742{
Ronald Cron5425a212020-08-04 14:58:35 +02009743 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009744 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009745 size_t bits = bits_arg;
9746 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9747 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9748 psa_status_t expected_status = expected_status_arg;
9749 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009750 uint8_t *e_read_buffer = NULL;
9751 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009752 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009753 size_t e_read_length = SIZE_MAX;
9754
Gilles Peskine449bd832023-01-11 14:50:10 +01009755 if (e_arg->len == 0 ||
9756 (e_arg->len == 3 &&
9757 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009758 is_default_public_exponent = 1;
9759 e_read_size = 0;
9760 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009761 TEST_CALLOC(e_read_buffer, e_read_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009762
Gilles Peskine449bd832023-01-11 14:50:10 +01009763 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009764
Gilles Peskine449bd832023-01-11 14:50:10 +01009765 psa_set_key_usage_flags(&attributes, usage);
9766 psa_set_key_algorithm(&attributes, alg);
9767 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9768 e_arg->x, e_arg->len));
9769 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009770
9771 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009772 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9773 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009774 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009775 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009776
9777 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009778 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9779 TEST_EQUAL(psa_get_key_type(&attributes), type);
9780 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009781 psa_status_t status = psa_get_key_domain_parameters(&attributes,
9782 e_read_buffer, e_read_size,
9783 &e_read_length);
9784
9785
9786#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
9787 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
9788 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +01009789 if (is_default_public_exponent) {
9790 TEST_EQUAL(e_read_length, 0);
9791 } else {
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009792 TEST_EQUAL(status, PSA_SUCCESS);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009793 TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009794 }
Pengyu Lv9e976f32023-12-06 16:58:05 +08009795#else
Pengyu Lv9e976f32023-12-06 16:58:05 +08009796 (void) is_default_public_exponent;
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009797 TEST_EQUAL(status, PSA_ERROR_NOT_SUPPORTED);
Pengyu Lv9e976f32023-12-06 16:58:05 +08009798#endif
Gilles Peskinee56e8782019-04-26 17:34:02 +02009799
9800 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009801 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009802 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009803 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009804
Gilles Peskine1d25a0a2024-02-12 16:40:04 +01009805 TEST_ASSERT(rsa_test_e(key, bits, e_arg));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009806
9807exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009808 /*
9809 * Key attributes may have been returned by psa_get_key_attributes() or
9810 * set by psa_set_key_domain_parameters() thus reset them as required.
9811 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009812 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009813
Gilles Peskine449bd832023-01-11 14:50:10 +01009814 psa_destroy_key(key);
9815 PSA_DONE();
9816 mbedtls_free(e_read_buffer);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009817}
9818/* END_CASE */
9819
Darryl Greend49a4992018-06-18 17:27:26 +01009820/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009821void persistent_key_load_key_from_storage(data_t *data,
9822 int type_arg, int bits_arg,
9823 int usage_flags_arg, int alg_arg,
9824 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009825{
Gilles Peskine449bd832023-01-11 14:50:10 +01009826 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009827 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009828 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9829 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009830 psa_key_type_t type = type_arg;
9831 size_t bits = bits_arg;
9832 psa_key_usage_t usage_flags = usage_flags_arg;
9833 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009834 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009835 unsigned char *first_export = NULL;
9836 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009837 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Sergeybef1f632023-03-06 15:25:06 -07009838 size_t first_exported_length = 0;
Darryl Greend49a4992018-06-18 17:27:26 +01009839 size_t second_exported_length;
9840
Gilles Peskine449bd832023-01-11 14:50:10 +01009841 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009842 TEST_CALLOC(first_export, export_size);
9843 TEST_CALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009844 }
Darryl Greend49a4992018-06-18 17:27:26 +01009845
Gilles Peskine449bd832023-01-11 14:50:10 +01009846 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009847
Gilles Peskine449bd832023-01-11 14:50:10 +01009848 psa_set_key_id(&attributes, key_id);
9849 psa_set_key_usage_flags(&attributes, usage_flags);
9850 psa_set_key_algorithm(&attributes, alg);
9851 psa_set_key_type(&attributes, type);
9852 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009853
Gilles Peskine449bd832023-01-11 14:50:10 +01009854 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009855 case IMPORT_KEY:
9856 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009857 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9858 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009859 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009860
Darryl Green0c6575a2018-11-07 16:05:30 +00009861 case GENERATE_KEY:
9862 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009863 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009864 break;
9865
9866 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009867#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009868 {
9869 /* Create base key */
9870 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9871 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9872 psa_set_key_usage_flags(&base_attributes,
9873 PSA_KEY_USAGE_DERIVE);
9874 psa_set_key_algorithm(&base_attributes, derive_alg);
9875 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9876 PSA_ASSERT(psa_import_key(&base_attributes,
9877 data->x, data->len,
9878 &base_key));
9879 /* Derive a key. */
9880 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9881 PSA_ASSERT(psa_key_derivation_input_key(
9882 &operation,
9883 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9884 PSA_ASSERT(psa_key_derivation_input_bytes(
9885 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9886 NULL, 0));
9887 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9888 &operation,
9889 &key));
9890 PSA_ASSERT(psa_key_derivation_abort(&operation));
9891 PSA_ASSERT(psa_destroy_key(base_key));
9892 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9893 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009894#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009895 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009896#endif
9897 break;
9898
9899 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01009900 TEST_FAIL("generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009901 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009902 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009903 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009904
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009905 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009906 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9907 PSA_ASSERT(psa_export_key(key,
9908 first_export, export_size,
9909 &first_exported_length));
9910 if (generation_method == IMPORT_KEY) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009911 TEST_MEMORY_COMPARE(data->x, data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009912 first_export, first_exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01009913 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009914 }
Darryl Greend49a4992018-06-18 17:27:26 +01009915
9916 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009917 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009918 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009919 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009920
Darryl Greend49a4992018-06-18 17:27:26 +01009921 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009922 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9923 TEST_ASSERT(mbedtls_svc_key_id_equal(
9924 psa_get_key_id(&attributes), key_id));
9925 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9926 PSA_KEY_LIFETIME_PERSISTENT);
9927 TEST_EQUAL(psa_get_key_type(&attributes), type);
9928 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9929 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9930 mbedtls_test_update_key_usage_flags(usage_flags));
9931 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009932
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009933 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009934 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9935 PSA_ASSERT(psa_export_key(key,
9936 second_export, export_size,
9937 &second_exported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009938 TEST_MEMORY_COMPARE(first_export, first_exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009939 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009940 }
9941
9942 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009943 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009944 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009945 }
Darryl Greend49a4992018-06-18 17:27:26 +01009946
9947exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009948 /*
9949 * Key attributes may have been returned by psa_get_key_attributes()
9950 * thus reset them as required.
9951 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009952 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009953
Gilles Peskine449bd832023-01-11 14:50:10 +01009954 mbedtls_free(first_export);
9955 mbedtls_free(second_export);
9956 psa_key_derivation_abort(&operation);
9957 psa_destroy_key(base_key);
9958 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009959 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009960}
9961/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009962
Neil Armstronga557cb82022-06-10 08:58:32 +02009963/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009964void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9965 int primitive_arg, int hash_arg, int role_arg,
9966 int test_input, data_t *pw_data,
9967 int inj_err_type_arg,
9968 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009969{
9970 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9971 psa_pake_operation_t operation = psa_pake_operation_init();
9972 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009973 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009974 psa_key_type_t key_type_pw = key_type_pw_arg;
9975 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009976 psa_algorithm_t hash_alg = hash_arg;
9977 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009978 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9979 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009980 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9981 psa_status_t expected_error = expected_error_arg;
9982 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009983 unsigned char *output_buffer = NULL;
9984 size_t output_len = 0;
9985
Gilles Peskine449bd832023-01-11 14:50:10 +01009986 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009987
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009988 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009989 PSA_PAKE_STEP_KEY_SHARE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009990 TEST_CALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009991
Gilles Peskine449bd832023-01-11 14:50:10 +01009992 if (pw_data->len > 0) {
9993 psa_set_key_usage_flags(&attributes, key_usage_pw);
9994 psa_set_key_algorithm(&attributes, alg);
9995 psa_set_key_type(&attributes, key_type_pw);
9996 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9997 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009998 }
9999
Gilles Peskine449bd832023-01-11 14:50:10 +010010000 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10001 psa_pake_cs_set_primitive(&cipher_suite, primitive);
10002 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010003
Gilles Peskine449bd832023-01-11 14:50:10 +010010004 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +020010005
Gilles Peskine449bd832023-01-11 14:50:10 +010010006 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
10007 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10008 expected_error);
10009 PSA_ASSERT(psa_pake_abort(&operation));
10010 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10011 expected_error);
10012 PSA_ASSERT(psa_pake_abort(&operation));
10013 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
10014 expected_error);
10015 PSA_ASSERT(psa_pake_abort(&operation));
10016 TEST_EQUAL(psa_pake_set_role(&operation, role),
10017 expected_error);
10018 PSA_ASSERT(psa_pake_abort(&operation));
10019 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10020 NULL, 0, NULL),
10021 expected_error);
10022 PSA_ASSERT(psa_pake_abort(&operation));
10023 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
10024 expected_error);
10025 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010026 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010027 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010028
Gilles Peskine449bd832023-01-11 14:50:10 +010010029 status = psa_pake_setup(&operation, &cipher_suite);
10030 if (status != PSA_SUCCESS) {
10031 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010032 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010033 }
10034
Gilles Peskine449bd832023-01-11 14:50:10 +010010035 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
10036 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
10037 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010038 goto exit;
10039 }
10040
Gilles Peskine449bd832023-01-11 14:50:10 +010010041 status = psa_pake_set_role(&operation, role);
10042 if (status != PSA_SUCCESS) {
10043 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010044 goto exit;
10045 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010046
Gilles Peskine449bd832023-01-11 14:50:10 +010010047 if (pw_data->len > 0) {
10048 status = psa_pake_set_password_key(&operation, key);
10049 if (status != PSA_SUCCESS) {
10050 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010051 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010052 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010053 }
10054
Gilles Peskine449bd832023-01-11 14:50:10 +010010055 if (inj_err_type == INJECT_ERR_INVALID_USER) {
10056 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10057 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010058 goto exit;
10059 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010060
Gilles Peskine449bd832023-01-11 14:50:10 +010010061 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
10062 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10063 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010064 goto exit;
10065 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010066
Gilles Peskine449bd832023-01-11 14:50:10 +010010067 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010068 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010069 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
10070 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010071 goto exit;
10072 }
10073
Gilles Peskine449bd832023-01-11 14:50:10 +010010074 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010075 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010076 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
10077 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010078 goto exit;
10079 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010080
Gilles Peskine449bd832023-01-11 14:50:10 +010010081 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
10082 PSA_PAKE_STEP_KEY_SHARE);
10083 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
10084 PSA_PAKE_STEP_ZK_PUBLIC);
10085 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
10086 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010087
Gilles Peskine449bd832023-01-11 14:50:10 +010010088 if (test_input) {
10089 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10090 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
10091 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010092 goto exit;
10093 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010094
Gilles Peskine449bd832023-01-11 14:50:10 +010010095 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10096 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10097 output_buffer, size_zk_proof),
10098 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010099 goto exit;
10100 }
10101
Gilles Peskine449bd832023-01-11 14:50:10 +010010102 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10103 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
10104 output_buffer, size_zk_proof),
10105 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010106 goto exit;
10107 }
10108
Gilles Peskine449bd832023-01-11 14:50:10 +010010109 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
10110 output_buffer, size_key_share);
10111 if (status != PSA_SUCCESS) {
10112 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010113 goto exit;
10114 }
10115
Gilles Peskine449bd832023-01-11 14:50:10 +010010116 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10117 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10118 output_buffer, size_zk_public + 1),
10119 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010120 goto exit;
10121 }
10122
Gilles Peskine449bd832023-01-11 14:50:10 +010010123 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010124 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010125 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10126 output_buffer, size_zk_public + 1);
10127 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10128 output_buffer, size_zk_public),
10129 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010130 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010131 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010132 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010133 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10134 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10135 NULL, 0, NULL),
10136 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010137 goto exit;
10138 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010139
Gilles Peskine449bd832023-01-11 14:50:10 +010010140 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10141 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10142 output_buffer, buf_size, &output_len),
10143 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010144 goto exit;
10145 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010146
Gilles Peskine449bd832023-01-11 14:50:10 +010010147 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10148 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10149 output_buffer, buf_size, &output_len),
10150 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010151 goto exit;
10152 }
10153
Gilles Peskine449bd832023-01-11 14:50:10 +010010154 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10155 output_buffer, buf_size, &output_len);
10156 if (status != PSA_SUCCESS) {
10157 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010158 goto exit;
10159 }
10160
Gilles Peskine449bd832023-01-11 14:50:10 +010010161 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010162
Gilles Peskine449bd832023-01-11 14:50:10 +010010163 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10164 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10165 output_buffer, size_zk_public - 1, &output_len),
10166 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010167 goto exit;
10168 }
10169
Gilles Peskine449bd832023-01-11 14:50:10 +010010170 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010171 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010172 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10173 output_buffer, size_zk_public - 1, &output_len);
10174 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10175 output_buffer, buf_size, &output_len),
10176 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010177 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010178 }
10179 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010180
10181exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010182 PSA_ASSERT(psa_destroy_key(key));
10183 PSA_ASSERT(psa_pake_abort(&operation));
10184 mbedtls_free(output_buffer);
10185 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010186}
10187/* END_CASE */
10188
Neil Armstronga557cb82022-06-10 08:58:32 +020010189/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010190void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10191 int client_input_first, int inject_error,
10192 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010193{
10194 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10195 psa_pake_operation_t server = psa_pake_operation_init();
10196 psa_pake_operation_t client = psa_pake_operation_init();
10197 psa_algorithm_t alg = alg_arg;
10198 psa_algorithm_t hash_alg = hash_arg;
10199 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10200 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10201
Gilles Peskine449bd832023-01-11 14:50:10 +010010202 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010203
Gilles Peskine449bd832023-01-11 14:50:10 +010010204 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10205 psa_set_key_algorithm(&attributes, alg);
10206 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10207 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10208 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010209
Gilles Peskine449bd832023-01-11 14:50:10 +010010210 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10211 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10212 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010213
10214
Gilles Peskine449bd832023-01-11 14:50:10 +010010215 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10216 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010217
Gilles Peskine449bd832023-01-11 14:50:10 +010010218 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10219 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010220
Gilles Peskine449bd832023-01-11 14:50:10 +010010221 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10222 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010223
Gilles Peskine449bd832023-01-11 14:50:10 +010010224 ecjpake_do_round(alg, primitive_arg, &server, &client,
10225 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010226
Gilles Peskine449bd832023-01-11 14:50:10 +010010227 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010228 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010229 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010230
Gilles Peskine449bd832023-01-11 14:50:10 +010010231 ecjpake_do_round(alg, primitive_arg, &server, &client,
10232 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010233
10234exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010235 psa_destroy_key(key);
10236 psa_pake_abort(&server);
10237 psa_pake_abort(&client);
10238 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010239}
10240/* END_CASE */
10241
10242/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010243void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10244 int derive_alg_arg, data_t *pw_data,
10245 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010246{
10247 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10248 psa_pake_operation_t server = psa_pake_operation_init();
10249 psa_pake_operation_t client = psa_pake_operation_init();
10250 psa_algorithm_t alg = alg_arg;
10251 psa_algorithm_t hash_alg = hash_arg;
10252 psa_algorithm_t derive_alg = derive_alg_arg;
10253 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10254 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10255 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010256 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010257 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010258 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010259 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010260
Gilles Peskine449bd832023-01-11 14:50:10 +010010261 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010262
Gilles Peskine449bd832023-01-11 14:50:10 +010010263 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10264 psa_set_key_algorithm(&attributes, alg);
10265 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10266 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10267 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010268
Gilles Peskine449bd832023-01-11 14:50:10 +010010269 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10270 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10271 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010272
Neil Armstrong1e855602022-06-15 11:32:11 +020010273 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010274 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10275 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010276
Gilles Peskine449bd832023-01-11 14:50:10 +010010277 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10278 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10279 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10280 PSA_KEY_DERIVATION_INPUT_SEED,
10281 (const uint8_t *) "", 0));
10282 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10283 PSA_KEY_DERIVATION_INPUT_SEED,
10284 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010285 }
10286
Gilles Peskine449bd832023-01-11 14:50:10 +010010287 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10288 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010289
Gilles Peskine449bd832023-01-11 14:50:10 +010010290 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10291 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010292
Gilles Peskine449bd832023-01-11 14:50:10 +010010293 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10294 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010295
Gilles Peskine449bd832023-01-11 14:50:10 +010010296 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10297 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10298 PSA_ERROR_BAD_STATE);
10299 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10300 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010301 goto exit;
10302 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010303
Neil Armstrongf983caf2022-06-15 15:27:48 +020010304 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010305 ecjpake_do_round(alg, primitive_arg, &server, &client,
10306 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010307
Gilles Peskine449bd832023-01-11 14:50:10 +010010308 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
10309 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10310 PSA_ERROR_BAD_STATE);
10311 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10312 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010313 goto exit;
10314 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010315
Neil Armstrongf983caf2022-06-15 15:27:48 +020010316 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010317 ecjpake_do_round(alg, primitive_arg, &server, &client,
10318 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010319
Gilles Peskine449bd832023-01-11 14:50:10 +010010320 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10321 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010322
10323exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010324 psa_key_derivation_abort(&server_derive);
10325 psa_key_derivation_abort(&client_derive);
10326 psa_destroy_key(key);
10327 psa_pake_abort(&server);
10328 psa_pake_abort(&client);
10329 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010330}
10331/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010332
10333/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010334void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010335{
10336 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10337 const size_t bits = 256;
10338 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010339 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010340 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010341 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010342
10343 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10344 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010345 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10346 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10347 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10348 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010349 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010350 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10351 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010352
10353 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010354 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10355 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10356 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10357 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10358 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10359 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010360
10361 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010362 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10363 PSA_PAKE_OUTPUT_MAX_SIZE);
10364 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10365 PSA_PAKE_OUTPUT_MAX_SIZE);
10366 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10367 PSA_PAKE_OUTPUT_MAX_SIZE);
10368 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10369 PSA_PAKE_INPUT_MAX_SIZE);
10370 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10371 PSA_PAKE_INPUT_MAX_SIZE);
10372 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10373 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010374}
10375/* END_CASE */