blob: 889a3b0081ec3a3ac5e68b01c116dd6d456e7b62 [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 Peskinee59236f2018-01-27 23:32:46 +01001258/* END_HEADER */
1259
1260/* BEGIN_DEPENDENCIES
1261 * depends_on:MBEDTLS_PSA_CRYPTO_C
1262 * END_DEPENDENCIES
1263 */
1264
1265/* BEGIN_CASE */
Manuel Pégourié-Gonnard7abdf7e2023-03-09 11:17:43 +01001266void psa_can_do_hash()
1267{
1268 /* We can't test that this is specific to drivers until partial init has
1269 * been implemented, but we can at least test before/after full init. */
1270 TEST_EQUAL(0, psa_can_do_hash(PSA_ALG_NONE));
1271 PSA_INIT();
1272 TEST_EQUAL(1, psa_can_do_hash(PSA_ALG_NONE));
1273 PSA_DONE();
1274}
1275/* END_CASE */
1276
1277/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001278void static_checks()
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001279{
1280 size_t max_truncated_mac_size =
1281 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1282
1283 /* Check that the length for a truncated MAC always fits in the algorithm
1284 * encoding. The shifted mask is the maximum truncated value. The
1285 * untruncated algorithm may be one byte larger. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 TEST_LE_U(PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size);
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001287}
1288/* END_CASE */
1289
1290/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001291void import_with_policy(int type_arg,
1292 int usage_arg, int alg_arg,
1293 int expected_status_arg)
Gilles Peskine6edfa292019-07-31 15:53:45 +02001294{
1295 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1296 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001297 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001298 psa_key_type_t type = type_arg;
1299 psa_key_usage_t usage = usage_arg;
1300 psa_algorithm_t alg = alg_arg;
1301 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 const uint8_t key_material[16] = { 0 };
Gilles Peskine6edfa292019-07-31 15:53:45 +02001303 psa_status_t status;
1304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 PSA_ASSERT(psa_crypto_init());
Gilles Peskine6edfa292019-07-31 15:53:45 +02001306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 psa_set_key_type(&attributes, type);
1308 psa_set_key_usage_flags(&attributes, usage);
1309 psa_set_key_algorithm(&attributes, alg);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 status = psa_import_key(&attributes,
1312 key_material, sizeof(key_material),
1313 &key);
1314 TEST_EQUAL(status, expected_status);
1315 if (status != PSA_SUCCESS) {
Gilles Peskine6edfa292019-07-31 15:53:45 +02001316 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 }
Gilles Peskine6edfa292019-07-31 15:53:45 +02001318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1320 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1321 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes),
1322 mbedtls_test_update_key_usage_flags(usage));
1323 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
1324 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001325
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 PSA_ASSERT(psa_destroy_key(key));
1327 test_operations_on_invalid_key(key);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001328
1329exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001330 /*
1331 * Key attributes may have been returned by psa_get_key_attributes()
1332 * thus reset them as required.
1333 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 psa_destroy_key(key);
1337 PSA_DONE();
Gilles Peskine6edfa292019-07-31 15:53:45 +02001338}
1339/* END_CASE */
1340
1341/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001342void import_with_data(data_t *data, int type_arg,
1343 int attr_bits_arg,
1344 int expected_status_arg)
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001345{
1346 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1347 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001348 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001349 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001350 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001351 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001352 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001355
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 psa_set_key_type(&attributes, type);
1357 psa_set_key_bits(&attributes, attr_bits);
Gilles Peskine6edfa292019-07-31 15:53:45 +02001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 status = psa_import_key(&attributes, data->x, data->len, &key);
Manuel Pégourié-Gonnard0509b582023-08-08 12:47:56 +02001360 /* When expecting INVALID_ARGUMENT, also accept NOT_SUPPORTED.
1361 *
1362 * This can happen with a type supported only by a driver:
1363 * - the driver sees the invalid data (for example wrong size) and thinks
1364 * "well perhaps this is a key size I don't support" so it returns
1365 * NOT_SUPPORTED which is correct at this point;
1366 * - we fallback to built-ins, which don't support this type, so return
1367 * NOT_SUPPORTED which again is correct at this point.
1368 */
1369 if (expected_status == PSA_ERROR_INVALID_ARGUMENT &&
1370 status == PSA_ERROR_NOT_SUPPORTED) {
1371 ; // OK
1372 } else {
1373 TEST_EQUAL(status, expected_status);
1374 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 if (status != PSA_SUCCESS) {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001376 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001378
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1380 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1381 if (attr_bits != 0) {
1382 TEST_EQUAL(attr_bits, psa_get_key_bits(&got_attributes));
1383 }
1384 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001385
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 PSA_ASSERT(psa_destroy_key(key));
1387 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001388
1389exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001390 /*
1391 * Key attributes may have been returned by psa_get_key_attributes()
1392 * thus reset them as required.
1393 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 psa_destroy_key(key);
1397 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001398}
1399/* END_CASE */
1400
1401/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001402/* Construct and attempt to import a large unstructured key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001403void import_large_key(int type_arg, int byte_size_arg,
1404 int expected_status_arg)
Gilles Peskinec744d992019-07-30 17:26:54 +02001405{
1406 psa_key_type_t type = type_arg;
1407 size_t byte_size = byte_size_arg;
1408 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1409 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001410 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001411 psa_status_t status;
1412 uint8_t *buffer = NULL;
1413 size_t buffer_size = byte_size + 1;
1414 size_t n;
1415
Steven Cooreman69967ce2021-01-18 18:01:08 +01001416 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001417 * accommodate large keys due to heap size constraints */
Tom Cosgrove412a8132023-07-20 16:55:14 +01001418 TEST_CALLOC_OR_SKIP(buffer, buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 memset(buffer, 'K', byte_size);
Gilles Peskinec744d992019-07-30 17:26:54 +02001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02001422
1423 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1425 psa_set_key_type(&attributes, type);
1426 status = psa_import_key(&attributes, buffer, byte_size, &key);
1427 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
1428 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 if (status == PSA_SUCCESS) {
1431 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1432 TEST_EQUAL(psa_get_key_type(&attributes), type);
1433 TEST_EQUAL(psa_get_key_bits(&attributes),
1434 PSA_BYTES_TO_BITS(byte_size));
1435 ASSERT_NO_SLOT_NUMBER(&attributes);
1436 memset(buffer, 0, byte_size + 1);
1437 PSA_ASSERT(psa_export_key(key, buffer, byte_size, &n));
1438 for (n = 0; n < byte_size; n++) {
1439 TEST_EQUAL(buffer[n], 'K');
1440 }
1441 for (n = byte_size; n < buffer_size; n++) {
1442 TEST_EQUAL(buffer[n], 0);
1443 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001444 }
1445
1446exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001447 /*
1448 * Key attributes may have been returned by psa_get_key_attributes()
1449 * thus reset them as required.
1450 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001452
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 psa_destroy_key(key);
1454 PSA_DONE();
1455 mbedtls_free(buffer);
Gilles Peskinec744d992019-07-30 17:26:54 +02001456}
1457/* END_CASE */
1458
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001459/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001460/* Import an RSA key with a valid structure (but not valid numbers
1461 * inside, beyond having sensible size and parity). This is expected to
1462 * fail for large keys. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001463void import_rsa_made_up(int bits_arg, int keypair, int expected_status_arg)
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001464{
Ronald Cron5425a212020-08-04 14:58:35 +02001465 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001466 size_t bits = bits_arg;
1467 psa_status_t expected_status = expected_status_arg;
1468 psa_status_t status;
1469 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001470 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001471 size_t buffer_size = /* Slight overapproximations */
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001473 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001474 unsigned char *p;
1475 int ret;
1476 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001477 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 PSA_ASSERT(psa_crypto_init());
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001480 TEST_CALLOC(buffer, buffer_size);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001481
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 TEST_ASSERT((ret = construct_fake_rsa_key(buffer, buffer_size, &p,
1483 bits, keypair)) >= 0);
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001484 length = ret;
1485
1486 /* Try importing the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 psa_set_key_type(&attributes, type);
1488 status = psa_import_key(&attributes, p, length, &key);
1489 TEST_EQUAL(status, expected_status);
Gilles Peskine76b29a72019-05-28 14:08:50 +02001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if (status == PSA_SUCCESS) {
1492 PSA_ASSERT(psa_destroy_key(key));
1493 }
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001494
1495exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 mbedtls_free(buffer);
1497 PSA_DONE();
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001498}
1499/* END_CASE */
1500
1501/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001502void import_export(data_t *data,
1503 int type_arg,
1504 int usage_arg, int alg_arg,
1505 int lifetime_arg,
1506 int expected_bits,
1507 int export_size_delta,
1508 int expected_export_status_arg,
1509 /*whether reexport must give the original input exactly*/
1510 int canonical_input)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001511{
Ronald Cron5425a212020-08-04 14:58:35 +02001512 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001513 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001514 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001515 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001516 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301517 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001518 unsigned char *exported = NULL;
1519 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001520 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001521 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001522 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001523 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001524 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001525
Moran Pekercb088e72018-07-17 17:36:59 +03001526 export_size = (ptrdiff_t) data->len + export_size_delta;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001527 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 if (!canonical_input) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001529 TEST_CALLOC(reexported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 }
1531 PSA_ASSERT(psa_crypto_init());
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 psa_set_key_lifetime(&attributes, lifetime);
1534 psa_set_key_usage_flags(&attributes, usage_arg);
1535 psa_set_key_algorithm(&attributes, alg);
1536 psa_set_key_type(&attributes, type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07001537
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001538 if (PSA_KEY_TYPE_IS_DH(type) &&
1539 expected_export_status == PSA_ERROR_BUFFER_TOO_SMALL) {
Przemek Stekiel654bef02022-12-15 13:28:02 +01001540 /* Simulate that buffer is too small, by decreasing its size by 1 byte. */
1541 export_size -= 1;
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001542 }
1543
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001544 /* Import the key */
Przemek Stekiel1d9c2b62022-12-01 15:01:39 +01001545 TEST_EQUAL(psa_import_key(&attributes, data->x, data->len, &key),
Przemek Stekiel2e7c33d2023-04-27 12:29:45 +02001546 PSA_SUCCESS);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001547
1548 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1550 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1551 TEST_EQUAL(psa_get_key_bits(&got_attributes), (size_t) expected_bits);
1552 ASSERT_NO_SLOT_NUMBER(&got_attributes);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001553
1554 /* Export the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 status = psa_export_key(key, exported, export_size, &exported_length);
1556 TEST_EQUAL(status, expected_export_status);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001557
1558 /* The exported length must be set by psa_export_key() to a value between 0
1559 * and export_size. On errors, the exported length must be 0. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 TEST_ASSERT(exported_length != INVALID_EXPORT_LENGTH);
1561 TEST_ASSERT(status == PSA_SUCCESS || exported_length == 0);
1562 TEST_LE_U(exported_length, export_size);
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001563
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 TEST_ASSERT(mem_is_char(exported + exported_length, 0,
1565 export_size - exported_length));
1566 if (status != PSA_SUCCESS) {
1567 TEST_EQUAL(exported_length, 0);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001568 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001569 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001570
Gilles Peskineea38a922021-02-13 00:05:16 +01001571 /* Run sanity checks on the exported key. For non-canonical inputs,
1572 * this validates the canonical representations. For canonical inputs,
1573 * this doesn't directly validate the implementation, but it still helps
1574 * by cross-validating the test data with the sanity check code. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 if (!psa_key_lifetime_is_external(lifetime)) {
1576 if (!mbedtls_test_psa_exercise_key(key, usage_arg, 0)) {
Archana4d7ae1d2021-07-07 02:50:22 +05301577 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 }
Archana4d7ae1d2021-07-07 02:50:22 +05301579 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 if (canonical_input) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001582 TEST_MEMORY_COMPARE(data->x, data->len, exported, exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 } else {
Ronald Cron5425a212020-08-04 14:58:35 +02001584 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 PSA_ASSERT(psa_import_key(&attributes, exported, exported_length,
1586 &key2));
1587 PSA_ASSERT(psa_export_key(key2,
1588 reexported,
1589 export_size,
1590 &reexported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001591 TEST_MEMORY_COMPARE(exported, exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001592 reexported, reexported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 PSA_ASSERT(psa_destroy_key(key2));
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001594 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 TEST_LE_U(exported_length,
1596 PSA_EXPORT_KEY_OUTPUT_SIZE(type,
1597 psa_get_key_bits(&got_attributes)));
Valerio Setti1eacae82023-07-28 16:07:03 +02001598 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
1599 TEST_LE_U(exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE);
1600 } else if (PSA_KEY_TYPE_IS_PUBLIC_KEY(type)) {
1601 TEST_LE_U(exported_length, PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
1602 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001603
1604destroy:
1605 /* Destroy the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 PSA_ASSERT(psa_destroy_key(key));
1607 test_operations_on_invalid_key(key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001608
1609exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001610 /*
1611 * Key attributes may have been returned by psa_get_key_attributes()
1612 * thus reset them as required.
1613 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 psa_reset_key_attributes(&got_attributes);
1615 psa_destroy_key(key);
1616 mbedtls_free(exported);
1617 mbedtls_free(reexported);
1618 PSA_DONE();
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001619}
1620/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001621
Moran Pekerf709f4a2018-06-06 17:26:04 +03001622/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001623void import_export_public_key(data_t *data,
1624 int type_arg, // key pair or public key
1625 int alg_arg,
1626 int lifetime_arg,
1627 int export_size_delta,
1628 int expected_export_status_arg,
1629 data_t *expected_public_key)
Moran Pekerf709f4a2018-06-06 17:26:04 +03001630{
Ronald Cron5425a212020-08-04 14:58:35 +02001631 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001632 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001633 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001634 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001635 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301636 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001637 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001638 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001639 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001640 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 PSA_ASSERT(psa_crypto_init());
Moran Pekerf709f4a2018-06-06 17:26:04 +03001643
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 psa_set_key_lifetime(&attributes, lifetime);
1645 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT);
1646 psa_set_key_algorithm(&attributes, alg);
1647 psa_set_key_type(&attributes, type);
Moran Pekerf709f4a2018-06-06 17:26:04 +03001648
1649 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Moran Pekerf709f4a2018-06-06 17:26:04 +03001651
Gilles Peskine49c25912018-10-29 15:15:31 +01001652 /* Export the public key */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001653 TEST_CALLOC(exported, export_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 status = psa_export_public_key(key,
1655 exported, export_size,
1656 &exported_length);
1657 TEST_EQUAL(status, expected_export_status);
1658 if (status == PSA_SUCCESS) {
1659 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001660 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1662 bits = psa_get_key_bits(&attributes);
1663 TEST_LE_U(expected_public_key->len,
1664 PSA_EXPORT_KEY_OUTPUT_SIZE(public_type, bits));
1665 TEST_LE_U(expected_public_key->len,
1666 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(public_type, bits));
1667 TEST_LE_U(expected_public_key->len,
1668 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001669 TEST_MEMORY_COMPARE(expected_public_key->x, expected_public_key->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01001670 exported, exported_length);
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001671 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001672exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001673 /*
1674 * Key attributes may have been returned by psa_get_key_attributes()
1675 * thus reset them as required.
1676 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001678
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 mbedtls_free(exported);
1680 psa_destroy_key(key);
1681 PSA_DONE();
Moran Pekerf709f4a2018-06-06 17:26:04 +03001682}
1683/* END_CASE */
1684
Gilles Peskine20035e32018-02-03 22:44:14 +01001685/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001686void import_and_exercise_key(data_t *data,
1687 int type_arg,
1688 int bits_arg,
1689 int alg_arg)
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001690{
Ronald Cron5425a212020-08-04 14:58:35 +02001691 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001692 psa_key_type_t type = type_arg;
1693 size_t bits = bits_arg;
1694 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise(type, alg);
Gilles Peskine4747d192019-04-17 15:05:45 +02001696 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001697 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001700
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 psa_set_key_usage_flags(&attributes, usage);
1702 psa_set_key_algorithm(&attributes, alg);
1703 psa_set_key_type(&attributes, type);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001704
1705 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len, &key));
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001707
1708 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
1710 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
1711 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001712
1713 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02001715 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 }
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001717
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 PSA_ASSERT(psa_destroy_key(key));
1719 test_operations_on_invalid_key(key);
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001720
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001721exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001722 /*
1723 * Key attributes may have been returned by psa_get_key_attributes()
1724 * thus reset them as required.
1725 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001727
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 psa_reset_key_attributes(&attributes);
1729 psa_destroy_key(key);
1730 PSA_DONE();
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001731}
1732/* END_CASE */
1733
1734/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001735void effective_key_attributes(int type_arg, int expected_type_arg,
1736 int bits_arg, int expected_bits_arg,
1737 int usage_arg, int expected_usage_arg,
1738 int alg_arg, int expected_alg_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001739{
Ronald Cron5425a212020-08-04 14:58:35 +02001740 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001741 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001742 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001743 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001744 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001745 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001746 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001747 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001748 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001749 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001750
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001752
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 psa_set_key_usage_flags(&attributes, usage);
1754 psa_set_key_algorithm(&attributes, alg);
1755 psa_set_key_type(&attributes, key_type);
1756 psa_set_key_bits(&attributes, bits);
Gilles Peskined5b33222018-06-18 22:20:03 +02001757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 PSA_ASSERT(psa_generate_key(&attributes, &key));
1759 psa_reset_key_attributes(&attributes);
Gilles Peskined5b33222018-06-18 22:20:03 +02001760
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
1762 TEST_EQUAL(psa_get_key_type(&attributes), expected_key_type);
1763 TEST_EQUAL(psa_get_key_bits(&attributes), expected_bits);
1764 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
1765 TEST_EQUAL(psa_get_key_algorithm(&attributes), expected_alg);
Gilles Peskined5b33222018-06-18 22:20:03 +02001766
1767exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001768 /*
1769 * Key attributes may have been returned by psa_get_key_attributes()
1770 * thus reset them as required.
1771 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001773
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 psa_destroy_key(key);
1775 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02001776}
1777/* END_CASE */
1778
1779/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001780void check_key_policy(int type_arg, int bits_arg,
1781 int usage_arg, int alg_arg)
Gilles Peskine06c28892019-11-26 18:07:46 +01001782{
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 test_effective_key_attributes(type_arg, type_arg, bits_arg, bits_arg,
1784 usage_arg,
1785 mbedtls_test_update_key_usage_flags(usage_arg),
1786 alg_arg, alg_arg);
Gilles Peskine06c28892019-11-26 18:07:46 +01001787 goto exit;
1788}
1789/* END_CASE */
1790
1791/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001792void key_attributes_init()
Jaeden Amero70261c52019-01-04 11:47:20 +00001793{
1794 /* Test each valid way of initializing the object, except for `= {0}`, as
1795 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1796 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001797 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 psa_key_attributes_t func = psa_key_attributes_init();
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001799 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1800 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 memset(&zero, 0, sizeof(zero));
Jaeden Amero70261c52019-01-04 11:47:20 +00001803
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 TEST_EQUAL(psa_get_key_lifetime(&func), PSA_KEY_LIFETIME_VOLATILE);
1805 TEST_EQUAL(psa_get_key_lifetime(&init), PSA_KEY_LIFETIME_VOLATILE);
1806 TEST_EQUAL(psa_get_key_lifetime(&zero), PSA_KEY_LIFETIME_VOLATILE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001807
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 TEST_EQUAL(psa_get_key_type(&func), 0);
1809 TEST_EQUAL(psa_get_key_type(&init), 0);
1810 TEST_EQUAL(psa_get_key_type(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001811
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 TEST_EQUAL(psa_get_key_bits(&func), 0);
1813 TEST_EQUAL(psa_get_key_bits(&init), 0);
1814 TEST_EQUAL(psa_get_key_bits(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001815
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 TEST_EQUAL(psa_get_key_usage_flags(&func), 0);
1817 TEST_EQUAL(psa_get_key_usage_flags(&init), 0);
1818 TEST_EQUAL(psa_get_key_usage_flags(&zero), 0);
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001819
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 TEST_EQUAL(psa_get_key_algorithm(&func), 0);
1821 TEST_EQUAL(psa_get_key_algorithm(&init), 0);
1822 TEST_EQUAL(psa_get_key_algorithm(&zero), 0);
Jaeden Amero70261c52019-01-04 11:47:20 +00001823}
1824/* END_CASE */
1825
1826/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001827void mac_key_policy(int policy_usage_arg,
1828 int policy_alg_arg,
1829 int key_type_arg,
1830 data_t *key_data,
1831 int exercise_alg_arg,
1832 int expected_status_sign_arg,
1833 int expected_status_verify_arg)
Gilles Peskined5b33222018-06-18 22:20:03 +02001834{
Ronald Cron5425a212020-08-04 14:58:35 +02001835 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001836 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001837 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001838 psa_key_type_t key_type = key_type_arg;
1839 psa_algorithm_t policy_alg = policy_alg_arg;
1840 psa_algorithm_t exercise_alg = exercise_alg_arg;
1841 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001842 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001843 psa_status_t expected_status_sign = expected_status_sign_arg;
1844 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001845 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 PSA_ASSERT(psa_crypto_init());
Gilles Peskined5b33222018-06-18 22:20:03 +02001848
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 psa_set_key_usage_flags(&attributes, policy_usage);
1850 psa_set_key_algorithm(&attributes, policy_alg);
1851 psa_set_key_type(&attributes, key_type);
Gilles Peskined5b33222018-06-18 22:20:03 +02001852
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1854 &key));
Gilles Peskined5b33222018-06-18 22:20:03 +02001855
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
1857 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001858
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1860 TEST_EQUAL(status, expected_status_sign);
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001861
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001862 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 uint8_t input[128] = { 0 };
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001864 size_t mac_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 TEST_EQUAL(psa_mac_compute(key, exercise_alg,
1866 input, 128,
1867 mac, PSA_MAC_MAX_SIZE, &mac_len),
1868 expected_status_sign);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001869
Neil Armstrong3af9b972022-02-07 12:20:21 +01001870 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 PSA_ASSERT(psa_mac_abort(&operation));
1872 status = psa_mac_sign_setup(&operation, key, exercise_alg);
1873 if (status == PSA_SUCCESS) {
1874 status = psa_mac_update(&operation, input, 128);
1875 if (status == PSA_SUCCESS) {
1876 TEST_EQUAL(psa_mac_sign_finish(&operation, mac, PSA_MAC_MAX_SIZE,
1877 &mac_len),
1878 expected_status_sign);
1879 } else {
1880 TEST_EQUAL(status, expected_status_sign);
1881 }
1882 } else {
1883 TEST_EQUAL(status, expected_status_sign);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001884 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 PSA_ASSERT(psa_mac_abort(&operation));
Neil Armstrong3af9b972022-02-07 12:20:21 +01001886
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001887 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 status = psa_mac_verify(key, exercise_alg, input, 128,
1889 mac, mac_len);
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001890
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1892 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1893 } else {
1894 TEST_EQUAL(status, expected_status_verify);
1895 }
Gilles Peskinefe11b722018-12-18 00:24:04 +01001896
Neil Armstrong3af9b972022-02-07 12:20:21 +01001897 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1899 if (status == PSA_SUCCESS) {
1900 status = psa_mac_update(&operation, input, 128);
1901 if (status == PSA_SUCCESS) {
1902 status = psa_mac_verify_finish(&operation, mac, mac_len);
1903 if (expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS) {
1904 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
1905 } else {
1906 TEST_EQUAL(status, expected_status_verify);
1907 }
1908 } else {
1909 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001910 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 } else {
1912 TEST_EQUAL(status, expected_status_verify);
Neil Armstrong3af9b972022-02-07 12:20:21 +01001913 }
1914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 psa_mac_abort(&operation);
Gilles Peskined5b33222018-06-18 22:20:03 +02001916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 memset(mac, 0, sizeof(mac));
1918 status = psa_mac_verify_setup(&operation, key, exercise_alg);
1919 TEST_EQUAL(status, expected_status_verify);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001920
1921exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 psa_mac_abort(&operation);
1923 psa_destroy_key(key);
1924 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001925}
1926/* END_CASE */
1927
1928/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001929void cipher_key_policy(int policy_usage_arg,
1930 int policy_alg,
1931 int key_type,
1932 data_t *key_data,
1933 int exercise_alg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001934{
Ronald Cron5425a212020-08-04 14:58:35 +02001935 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001936 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001937 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001938 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001939 size_t output_buffer_size = 0;
1940 size_t input_buffer_size = 0;
1941 size_t output_length = 0;
1942 uint8_t *output = NULL;
1943 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001944 psa_status_t status;
1945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(exercise_alg);
1947 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, exercise_alg,
1948 input_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001949
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001950 TEST_CALLOC(input, input_buffer_size);
1951 TEST_CALLOC(output, output_buffer_size);
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001952
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 psa_set_key_usage_flags(&attributes, policy_usage);
1956 psa_set_key_algorithm(&attributes, policy_alg);
1957 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001958
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
1960 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001961
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001962 /* Check if no key usage flag implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 TEST_EQUAL(policy_usage,
1964 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001965
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001966 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 status = psa_cipher_encrypt(key, exercise_alg, input, input_buffer_size,
1968 output, output_buffer_size,
1969 &output_length);
1970 if (policy_alg == exercise_alg &&
1971 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1972 PSA_ASSERT(status);
1973 } else {
1974 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1975 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001976
1977 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 status = psa_cipher_encrypt_setup(&operation, key, exercise_alg);
1979 if (policy_alg == exercise_alg &&
1980 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
1981 PSA_ASSERT(status);
1982 } else {
1983 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1984 }
1985 psa_cipher_abort(&operation);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001986
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001987 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 status = psa_cipher_decrypt(key, exercise_alg, output, output_buffer_size,
1989 input, input_buffer_size,
1990 &output_length);
1991 if (policy_alg == exercise_alg &&
1992 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
1993 PSA_ASSERT(status);
1994 } else {
1995 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
1996 }
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001997
1998 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 status = psa_cipher_decrypt_setup(&operation, key, exercise_alg);
2000 if (policy_alg == exercise_alg &&
2001 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2002 PSA_ASSERT(status);
2003 } else {
2004 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2005 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002006
2007exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 psa_cipher_abort(&operation);
2009 mbedtls_free(input);
2010 mbedtls_free(output);
2011 psa_destroy_key(key);
2012 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002013}
2014/* END_CASE */
2015
2016/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002017void aead_key_policy(int policy_usage_arg,
2018 int policy_alg,
2019 int key_type,
2020 data_t *key_data,
2021 int nonce_length_arg,
2022 int tag_length_arg,
2023 int exercise_alg,
2024 int expected_status_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002025{
Ronald Cron5425a212020-08-04 14:58:35 +02002026 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002027 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002028 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002029 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002030 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002031 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 unsigned char nonce[16] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002033 size_t nonce_length = nonce_length_arg;
2034 unsigned char tag[16];
2035 size_t tag_length = tag_length_arg;
2036 size_t output_length;
2037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 TEST_LE_U(nonce_length, sizeof(nonce));
2039 TEST_LE_U(tag_length, sizeof(tag));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002040
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002042
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 psa_set_key_usage_flags(&attributes, policy_usage);
2044 psa_set_key_algorithm(&attributes, policy_alg);
2045 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002046
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2048 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002049
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002050 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 TEST_EQUAL(policy_usage,
2052 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002053
Neil Armstrong752d8112022-02-07 14:51:11 +01002054 /* Encrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 status = psa_aead_encrypt(key, exercise_alg,
2056 nonce, nonce_length,
2057 NULL, 0,
2058 NULL, 0,
2059 tag, tag_length,
2060 &output_length);
2061 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2062 TEST_EQUAL(status, expected_status);
2063 } else {
2064 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2065 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002066
Neil Armstrong752d8112022-02-07 14:51:11 +01002067 /* Encrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 status = psa_aead_encrypt_setup(&operation, key, exercise_alg);
2069 if ((policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2070 TEST_EQUAL(status, expected_status);
2071 } else {
2072 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2073 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002074
2075 /* Decrypt check, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 memset(tag, 0, sizeof(tag));
2077 status = psa_aead_decrypt(key, exercise_alg,
2078 nonce, nonce_length,
2079 NULL, 0,
2080 tag, tag_length,
2081 NULL, 0,
2082 &output_length);
2083 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2084 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2085 } else if (expected_status == PSA_SUCCESS) {
2086 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2087 } else {
2088 TEST_EQUAL(status, expected_status);
2089 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002090
Neil Armstrong752d8112022-02-07 14:51:11 +01002091 /* Decrypt check, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 PSA_ASSERT(psa_aead_abort(&operation));
2093 status = psa_aead_decrypt_setup(&operation, key, exercise_alg);
2094 if ((policy_usage & PSA_KEY_USAGE_DECRYPT) == 0) {
2095 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2096 } else {
2097 TEST_EQUAL(status, expected_status);
2098 }
Neil Armstrong752d8112022-02-07 14:51:11 +01002099
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002100exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 PSA_ASSERT(psa_aead_abort(&operation));
2102 psa_destroy_key(key);
2103 PSA_DONE();
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002104}
2105/* END_CASE */
2106
2107/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002108void asymmetric_encryption_key_policy(int policy_usage_arg,
2109 int policy_alg,
2110 int key_type,
2111 data_t *key_data,
Valerio Settif202c292024-01-15 10:42:37 +01002112 int exercise_alg,
2113 int use_opaque_key)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002114{
Ronald Cron5425a212020-08-04 14:58:35 +02002115 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002116 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002117 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002118 psa_status_t status;
2119 size_t key_bits;
2120 size_t buffer_length;
2121 unsigned char *buffer = NULL;
2122 size_t output_length;
2123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 psa_set_key_usage_flags(&attributes, policy_usage);
2127 psa_set_key_algorithm(&attributes, policy_alg);
2128 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002129
Valerio Settif202c292024-01-15 10:42:37 +01002130 if (use_opaque_key) {
2131 psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
2132 PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION));
2133 }
2134
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2136 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002137
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002138 /* Check if no key usage implication is done */
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 TEST_EQUAL(policy_usage,
2140 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002141
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
2143 key_bits = psa_get_key_bits(&attributes);
2144 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits,
2145 exercise_alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002146 TEST_CALLOC(buffer, buffer_length);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002147
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 status = psa_asymmetric_encrypt(key, exercise_alg,
2149 NULL, 0,
2150 NULL, 0,
2151 buffer, buffer_length,
2152 &output_length);
2153 if (policy_alg == exercise_alg &&
2154 (policy_usage & PSA_KEY_USAGE_ENCRYPT) != 0) {
2155 PSA_ASSERT(status);
2156 } else {
2157 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2158 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 if (buffer_length != 0) {
2161 memset(buffer, 0, buffer_length);
2162 }
2163 status = psa_asymmetric_decrypt(key, exercise_alg,
2164 buffer, buffer_length,
2165 NULL, 0,
2166 buffer, buffer_length,
2167 &output_length);
2168 if (policy_alg == exercise_alg &&
2169 (policy_usage & PSA_KEY_USAGE_DECRYPT) != 0) {
2170 TEST_EQUAL(status, PSA_ERROR_INVALID_PADDING);
2171 } else {
2172 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2173 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002174
2175exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002176 /*
2177 * Key attributes may have been returned by psa_get_key_attributes()
2178 * thus reset them as required.
2179 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002181
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 psa_destroy_key(key);
2183 PSA_DONE();
2184 mbedtls_free(buffer);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002185}
2186/* END_CASE */
2187
2188/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002189void asymmetric_signature_key_policy(int policy_usage_arg,
2190 int policy_alg,
2191 int key_type,
2192 data_t *key_data,
2193 int exercise_alg,
2194 int payload_length_arg,
2195 int expected_usage_arg)
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002196{
Ronald Cron5425a212020-08-04 14:58:35 +02002197 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002198 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002199 psa_key_usage_t policy_usage = policy_usage_arg;
2200 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002201 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 unsigned char payload[PSA_HASH_MAX_SIZE] = { 1 };
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002203 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2204 * compatible with the policy and `payload_length_arg` is supposed to be
2205 * a valid input length to sign. If `payload_length_arg <= 0`,
2206 * `exercise_alg` is supposed to be forbidden by the policy. */
2207 int compatible_alg = payload_length_arg > 0;
2208 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = { 0 };
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002210 size_t signature_length;
2211
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002212 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002213 in the expected usage flags. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 TEST_EQUAL(expected_usage,
2215 mbedtls_test_update_key_usage_flags(policy_usage));
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 PSA_ASSERT(psa_crypto_init());
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 psa_set_key_usage_flags(&attributes, policy_usage);
2220 psa_set_key_algorithm(&attributes, policy_alg);
2221 psa_set_key_type(&attributes, key_type);
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002222
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2224 &key));
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002225
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 TEST_EQUAL(psa_get_key_usage_flags(&attributes), expected_usage);
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 status = psa_sign_hash(key, exercise_alg,
2229 payload, payload_length,
2230 signature, sizeof(signature),
2231 &signature_length);
2232 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_HASH) != 0) {
2233 PSA_ASSERT(status);
2234 } else {
2235 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2236 }
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002237
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 memset(signature, 0, sizeof(signature));
2239 status = psa_verify_hash(key, exercise_alg,
2240 payload, payload_length,
2241 signature, sizeof(signature));
2242 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_HASH) != 0) {
2243 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2244 } else {
2245 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2246 }
Gilles Peskined5b33222018-06-18 22:20:03 +02002247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 if (PSA_ALG_IS_SIGN_HASH(exercise_alg) &&
2249 PSA_ALG_IS_HASH(PSA_ALG_SIGN_GET_HASH(exercise_alg))) {
2250 status = psa_sign_message(key, exercise_alg,
2251 payload, payload_length,
2252 signature, sizeof(signature),
2253 &signature_length);
2254 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE) != 0) {
2255 PSA_ASSERT(status);
2256 } else {
2257 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2258 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002259
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 memset(signature, 0, sizeof(signature));
2261 status = psa_verify_message(key, exercise_alg,
2262 payload, payload_length,
2263 signature, sizeof(signature));
2264 if (compatible_alg && (expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE) != 0) {
2265 TEST_EQUAL(status, PSA_ERROR_INVALID_SIGNATURE);
2266 } else {
2267 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2268 }
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002269 }
2270
Gilles Peskined5b33222018-06-18 22:20:03 +02002271exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 psa_destroy_key(key);
2273 PSA_DONE();
Gilles Peskined5b33222018-06-18 22:20:03 +02002274}
2275/* END_CASE */
2276
Janos Follathba3fab92019-06-11 14:50:16 +01002277/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002278void derive_key_policy(int policy_usage,
2279 int policy_alg,
2280 int key_type,
2281 data_t *key_data,
2282 int exercise_alg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02002283{
Ronald Cron5425a212020-08-04 14:58:35 +02002284 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002285 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002286 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002287 psa_status_t status;
2288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02002290
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 psa_set_key_usage_flags(&attributes, policy_usage);
2292 psa_set_key_algorithm(&attributes, policy_alg);
2293 psa_set_key_type(&attributes, key_type);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002294
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2296 &key));
Gilles Peskineea0fb492018-07-12 17:17:20 +02002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
Janos Follathba3fab92019-06-11 14:50:16 +01002299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 if (PSA_ALG_IS_TLS12_PRF(exercise_alg) ||
2301 PSA_ALG_IS_TLS12_PSK_TO_MS(exercise_alg)) {
2302 PSA_ASSERT(psa_key_derivation_input_bytes(
2303 &operation,
2304 PSA_KEY_DERIVATION_INPUT_SEED,
2305 (const uint8_t *) "", 0));
Janos Follath0c1ed842019-06-28 13:35:36 +01002306 }
Janos Follathba3fab92019-06-11 14:50:16 +01002307
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 status = psa_key_derivation_input_key(&operation,
2309 PSA_KEY_DERIVATION_INPUT_SECRET,
2310 key);
Janos Follathba3fab92019-06-11 14:50:16 +01002311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 if (policy_alg == exercise_alg &&
2313 (policy_usage & PSA_KEY_USAGE_DERIVE) != 0) {
2314 PSA_ASSERT(status);
2315 } else {
2316 TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
2317 }
Gilles Peskineea0fb492018-07-12 17:17:20 +02002318
2319exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 psa_key_derivation_abort(&operation);
2321 psa_destroy_key(key);
2322 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02002323}
2324/* END_CASE */
2325
2326/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002327void agreement_key_policy(int policy_usage,
2328 int policy_alg,
2329 int key_type_arg,
2330 data_t *key_data,
2331 int exercise_alg,
2332 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02002333{
Ronald Cron5425a212020-08-04 14:58:35 +02002334 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002335 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002336 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002337 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002338 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002339 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02002342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 psa_set_key_usage_flags(&attributes, policy_usage);
2344 psa_set_key_algorithm(&attributes, policy_alg);
2345 psa_set_key_type(&attributes, key_type);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2348 &key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02002349
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 PSA_ASSERT(psa_key_derivation_setup(&operation, exercise_alg));
2351 status = mbedtls_test_psa_key_agreement_with_self(&operation, key);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002352
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 TEST_EQUAL(status, expected_status);
Gilles Peskine01d718c2018-09-18 12:01:02 +02002354
2355exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 psa_key_derivation_abort(&operation);
2357 psa_destroy_key(key);
2358 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02002359}
2360/* END_CASE */
2361
2362/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002363void key_policy_alg2(int key_type_arg, data_t *key_data,
2364 int usage_arg, int alg_arg, int alg2_arg)
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002365{
Ronald Cron5425a212020-08-04 14:58:35 +02002366 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002367 psa_key_type_t key_type = key_type_arg;
2368 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2369 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2370 psa_key_usage_t usage = usage_arg;
2371 psa_algorithm_t alg = alg_arg;
2372 psa_algorithm_t alg2 = alg2_arg;
2373
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002375
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 psa_set_key_usage_flags(&attributes, usage);
2377 psa_set_key_algorithm(&attributes, alg);
2378 psa_set_key_enrollment_algorithm(&attributes, alg2);
2379 psa_set_key_type(&attributes, key_type);
2380 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2381 &key));
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002382
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002383 /* Update the usage flags to obtain implicit usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 usage = mbedtls_test_update_key_usage_flags(usage);
2385 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
2386 TEST_EQUAL(psa_get_key_usage_flags(&got_attributes), usage);
2387 TEST_EQUAL(psa_get_key_algorithm(&got_attributes), alg);
2388 TEST_EQUAL(psa_get_key_enrollment_algorithm(&got_attributes), alg2);
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002391 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 }
2393 if (!mbedtls_test_psa_exercise_key(key, usage, alg2)) {
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002394 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 }
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002396
2397exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002398 /*
2399 * Key attributes may have been returned by psa_get_key_attributes()
2400 * thus reset them as required.
2401 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002403
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 psa_destroy_key(key);
2405 PSA_DONE();
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002406}
2407/* END_CASE */
2408
2409/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002410void raw_agreement_key_policy(int policy_usage,
2411 int policy_alg,
2412 int key_type_arg,
2413 data_t *key_data,
2414 int exercise_alg,
2415 int expected_status_arg)
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002416{
Ronald Cron5425a212020-08-04 14:58:35 +02002417 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002418 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002419 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002420 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002421 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002422 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 PSA_ASSERT(psa_crypto_init());
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002425
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 psa_set_key_usage_flags(&attributes, policy_usage);
2427 psa_set_key_algorithm(&attributes, policy_alg);
2428 psa_set_key_type(&attributes, key_type);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002429
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
2431 &key));
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002432
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 status = mbedtls_test_psa_raw_key_agreement_with_self(exercise_alg, key);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002434
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 TEST_EQUAL(status, expected_status);
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002436
2437exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 psa_key_derivation_abort(&operation);
2439 psa_destroy_key(key);
2440 PSA_DONE();
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002441}
2442/* END_CASE */
2443
2444/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002445void copy_success(int source_usage_arg,
2446 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002447 int source_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 int type_arg, data_t *material,
2449 int copy_attributes,
2450 int target_usage_arg,
2451 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4ea4ad02022-12-04 15:11:00 +01002452 int target_lifetime_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 int expected_usage_arg,
2454 int expected_alg_arg, int expected_alg2_arg)
Gilles Peskine57ab7212019-01-28 13:03:09 +01002455{
Gilles Peskineca25db92019-04-19 11:43:08 +02002456 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2457 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002458 psa_key_usage_t expected_usage = expected_usage_arg;
2459 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002460 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302461 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2462 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002463 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2464 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002465 uint8_t *export_buffer = NULL;
2466
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 PSA_ASSERT(psa_crypto_init());
Gilles Peskine57ab7212019-01-28 13:03:09 +01002468
Gilles Peskineca25db92019-04-19 11:43:08 +02002469 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2471 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2472 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2473 psa_set_key_type(&source_attributes, type_arg);
2474 psa_set_key_lifetime(&source_attributes, source_lifetime);
2475 PSA_ASSERT(psa_import_key(&source_attributes,
2476 material->x, material->len,
2477 &source_key));
2478 PSA_ASSERT(psa_get_key_attributes(source_key, &source_attributes));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002479
Gilles Peskineca25db92019-04-19 11:43:08 +02002480 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 if (copy_attributes) {
Gilles Peskineca25db92019-04-19 11:43:08 +02002482 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002483 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 psa_set_key_lifetime(&target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 if (target_usage_arg != -1) {
2487 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2488 }
2489 if (target_alg_arg != -1) {
2490 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2491 }
2492 if (target_alg2_arg != -1) {
2493 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
2494 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002495
Archana8a180362021-07-05 02:18:48 +05302496
Gilles Peskine57ab7212019-01-28 13:03:09 +01002497 /* Copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 PSA_ASSERT(psa_copy_key(source_key,
2499 &target_attributes, &target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002500
2501 /* Destroy the source to ensure that this doesn't affect the target. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002503
2504 /* Test that the target slot has the expected content and policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 PSA_ASSERT(psa_get_key_attributes(target_key, &target_attributes));
2506 TEST_EQUAL(psa_get_key_type(&source_attributes),
2507 psa_get_key_type(&target_attributes));
2508 TEST_EQUAL(psa_get_key_bits(&source_attributes),
2509 psa_get_key_bits(&target_attributes));
2510 TEST_EQUAL(expected_usage, psa_get_key_usage_flags(&target_attributes));
2511 TEST_EQUAL(expected_alg, psa_get_key_algorithm(&target_attributes));
2512 TEST_EQUAL(expected_alg2,
2513 psa_get_key_enrollment_algorithm(&target_attributes));
2514 if (expected_usage & PSA_KEY_USAGE_EXPORT) {
Gilles Peskine57ab7212019-01-28 13:03:09 +01002515 size_t length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002516 TEST_CALLOC(export_buffer, material->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 PSA_ASSERT(psa_export_key(target_key, export_buffer,
2518 material->len, &length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002519 TEST_MEMORY_COMPARE(material->x, material->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002520 export_buffer, length);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002521 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002522
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 if (!psa_key_lifetime_is_external(target_lifetime)) {
2524 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg)) {
Archana8a180362021-07-05 02:18:48 +05302525 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 }
2527 if (!mbedtls_test_psa_exercise_key(target_key, expected_usage, expected_alg2)) {
Archana8a180362021-07-05 02:18:48 +05302528 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 }
Archana8a180362021-07-05 02:18:48 +05302530 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002531
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 PSA_ASSERT(psa_destroy_key(target_key));
Gilles Peskine57ab7212019-01-28 13:03:09 +01002533
2534exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002535 /*
2536 * Source and target key attributes may have been returned by
2537 * psa_get_key_attributes() thus reset them as required.
2538 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 psa_reset_key_attributes(&source_attributes);
2540 psa_reset_key_attributes(&target_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002541
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 PSA_DONE();
2543 mbedtls_free(export_buffer);
Gilles Peskine57ab7212019-01-28 13:03:09 +01002544}
2545/* END_CASE */
2546
2547/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002548void copy_fail(int source_usage_arg,
2549 int source_alg_arg, int source_alg2_arg,
2550 int source_lifetime_arg,
2551 int type_arg, data_t *material,
2552 int target_type_arg, int target_bits_arg,
2553 int target_usage_arg,
2554 int target_alg_arg, int target_alg2_arg,
2555 int target_id_arg, int target_lifetime_arg,
2556 int expected_status_arg)
Gilles Peskine4a644642019-05-03 17:14:08 +02002557{
2558 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2559 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002560 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2561 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, target_id_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 PSA_ASSERT(psa_crypto_init());
Gilles Peskine4a644642019-05-03 17:14:08 +02002565
2566 /* Prepare the source key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 psa_set_key_usage_flags(&source_attributes, source_usage_arg);
2568 psa_set_key_algorithm(&source_attributes, source_alg_arg);
2569 psa_set_key_enrollment_algorithm(&source_attributes, source_alg2_arg);
2570 psa_set_key_type(&source_attributes, type_arg);
2571 psa_set_key_lifetime(&source_attributes, source_lifetime_arg);
2572 PSA_ASSERT(psa_import_key(&source_attributes,
2573 material->x, material->len,
2574 &source_key));
Gilles Peskine4a644642019-05-03 17:14:08 +02002575
2576 /* Prepare the target attributes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 psa_set_key_id(&target_attributes, key_id);
2578 psa_set_key_lifetime(&target_attributes, target_lifetime_arg);
2579 psa_set_key_type(&target_attributes, target_type_arg);
2580 psa_set_key_bits(&target_attributes, target_bits_arg);
2581 psa_set_key_usage_flags(&target_attributes, target_usage_arg);
2582 psa_set_key_algorithm(&target_attributes, target_alg_arg);
2583 psa_set_key_enrollment_algorithm(&target_attributes, target_alg2_arg);
Gilles Peskine4a644642019-05-03 17:14:08 +02002584
2585 /* Try to copy the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 TEST_EQUAL(psa_copy_key(source_key,
2587 &target_attributes, &target_key),
2588 expected_status_arg);
Gilles Peskine76b29a72019-05-28 14:08:50 +02002589
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 PSA_ASSERT(psa_destroy_key(source_key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02002591
Gilles Peskine4a644642019-05-03 17:14:08 +02002592exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 psa_reset_key_attributes(&source_attributes);
2594 psa_reset_key_attributes(&target_attributes);
2595 PSA_DONE();
Gilles Peskine4a644642019-05-03 17:14:08 +02002596}
2597/* END_CASE */
2598
2599/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002600void hash_operation_init()
Jaeden Amero6a25b412019-01-04 11:47:44 +00002601{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002602 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002603 /* Test each valid way of initializing the object, except for `= {0}`, as
2604 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2605 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002606 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 psa_hash_operation_t func = psa_hash_operation_init();
Jaeden Amero6a25b412019-01-04 11:47:44 +00002608 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2609 psa_hash_operation_t zero;
2610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 memset(&zero, 0, sizeof(zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002612
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002613 /* A freshly-initialized hash operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 TEST_EQUAL(psa_hash_update(&func, input, sizeof(input)),
2615 PSA_ERROR_BAD_STATE);
2616 TEST_EQUAL(psa_hash_update(&init, input, sizeof(input)),
2617 PSA_ERROR_BAD_STATE);
2618 TEST_EQUAL(psa_hash_update(&zero, input, sizeof(input)),
2619 PSA_ERROR_BAD_STATE);
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002620
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002621 /* A default hash operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 PSA_ASSERT(psa_hash_abort(&func));
2623 PSA_ASSERT(psa_hash_abort(&init));
2624 PSA_ASSERT(psa_hash_abort(&zero));
Jaeden Amero6a25b412019-01-04 11:47:44 +00002625}
2626/* END_CASE */
2627
2628/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002629void hash_setup(int alg_arg,
2630 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002631{
2632 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002633 uint8_t *output = NULL;
2634 size_t output_size = 0;
2635 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002636 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002637 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002638 psa_status_t status;
2639
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002641
Neil Armstrongedb20862022-02-07 15:47:44 +01002642 /* Hash Setup, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 output_size = PSA_HASH_LENGTH(alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002644 TEST_CALLOC(output, output_size);
Neil Armstrongedb20862022-02-07 15:47:44 +01002645
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 status = psa_hash_compute(alg, NULL, 0,
2647 output, output_size, &output_length);
2648 TEST_EQUAL(status, expected_status);
Neil Armstrongedb20862022-02-07 15:47:44 +01002649
2650 /* Hash Setup, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 status = psa_hash_setup(&operation, alg);
2652 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002653
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002654 /* Whether setup succeeded or failed, abort must succeed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002656
2657 /* If setup failed, reproduce the failure, so as to
2658 * test the resulting state of the operation object. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 if (status != PSA_SUCCESS) {
2660 TEST_EQUAL(psa_hash_setup(&operation, alg), status);
2661 }
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002662
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002663 /* Now the operation object should be reusable. */
2664#if defined(KNOWN_SUPPORTED_HASH_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 PSA_ASSERT(psa_hash_setup(&operation, KNOWN_SUPPORTED_HASH_ALG));
2666 PSA_ASSERT(psa_hash_abort(&operation));
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002667#endif
2668
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002669exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 mbedtls_free(output);
2671 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002672}
2673/* END_CASE */
2674
2675/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002676void hash_compute_fail(int alg_arg, data_t *input,
2677 int output_size_arg, int expected_status_arg)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002678{
2679 psa_algorithm_t alg = alg_arg;
2680 uint8_t *output = NULL;
2681 size_t output_size = output_size_arg;
2682 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002683 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002684 psa_status_t expected_status = expected_status_arg;
2685 psa_status_t status;
2686
Tom Cosgrove05b2a872023-07-21 11:31:13 +01002687 TEST_CALLOC(output, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002688
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002690
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002691 /* Hash Compute, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 status = psa_hash_compute(alg, input->x, input->len,
2693 output, output_size, &output_length);
2694 TEST_EQUAL(status, expected_status);
2695 TEST_LE_U(output_length, output_size);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002696
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002697 /* Hash Compute, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 status = psa_hash_setup(&operation, alg);
2699 if (status == PSA_SUCCESS) {
2700 status = psa_hash_update(&operation, input->x, input->len);
2701 if (status == PSA_SUCCESS) {
2702 status = psa_hash_finish(&operation, output, output_size,
2703 &output_length);
2704 if (status == PSA_SUCCESS) {
2705 TEST_LE_U(output_length, output_size);
2706 } else {
2707 TEST_EQUAL(status, expected_status);
2708 }
2709 } else {
2710 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002711 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 } else {
2713 TEST_EQUAL(status, expected_status);
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002714 }
2715
Gilles Peskine0a749c82019-11-28 19:33:58 +01002716exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 PSA_ASSERT(psa_hash_abort(&operation));
2718 mbedtls_free(output);
2719 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002720}
2721/* END_CASE */
2722
2723/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002724void hash_compare_fail(int alg_arg, data_t *input,
2725 data_t *reference_hash,
2726 int expected_status_arg)
Gilles Peskine88e08462020-01-28 20:43:00 +01002727{
2728 psa_algorithm_t alg = alg_arg;
2729 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002730 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002731 psa_status_t status;
2732
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 PSA_ASSERT(psa_crypto_init());
Gilles Peskine88e08462020-01-28 20:43:00 +01002734
Neil Armstrong55a1be12022-02-07 11:23:20 +01002735 /* Hash Compare, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 status = psa_hash_compare(alg, input->x, input->len,
2737 reference_hash->x, reference_hash->len);
2738 TEST_EQUAL(status, expected_status);
Gilles Peskine88e08462020-01-28 20:43:00 +01002739
Neil Armstrong55a1be12022-02-07 11:23:20 +01002740 /* Hash Compare, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 status = psa_hash_setup(&operation, alg);
2742 if (status == PSA_SUCCESS) {
2743 status = psa_hash_update(&operation, input->x, input->len);
2744 if (status == PSA_SUCCESS) {
2745 status = psa_hash_verify(&operation, reference_hash->x,
2746 reference_hash->len);
2747 TEST_EQUAL(status, expected_status);
2748 } else {
2749 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002750 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 } else {
2752 TEST_EQUAL(status, expected_status);
Neil Armstrong55a1be12022-02-07 11:23:20 +01002753 }
2754
Gilles Peskine88e08462020-01-28 20:43:00 +01002755exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 PSA_ASSERT(psa_hash_abort(&operation));
2757 PSA_DONE();
Gilles Peskine88e08462020-01-28 20:43:00 +01002758}
2759/* END_CASE */
2760
2761/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002762void hash_compute_compare(int alg_arg, data_t *input,
2763 data_t *expected_output)
Gilles Peskine0a749c82019-11-28 19:33:58 +01002764{
2765 psa_algorithm_t alg = alg_arg;
2766 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2767 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002768 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002769 size_t i;
2770
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0a749c82019-11-28 19:33:58 +01002772
Neil Armstrongca30a002022-02-07 11:40:23 +01002773 /* Compute with tight buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2775 output, PSA_HASH_LENGTH(alg),
2776 &output_length));
2777 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002778 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002779 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002780
Neil Armstrongca30a002022-02-07 11:40:23 +01002781 /* Compute with tight buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 PSA_ASSERT(psa_hash_setup(&operation, alg));
2783 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2784 PSA_ASSERT(psa_hash_finish(&operation, output,
2785 PSA_HASH_LENGTH(alg),
2786 &output_length));
2787 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002788 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002789 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002790
2791 /* Compute with larger buffer, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002792 PSA_ASSERT(psa_hash_compute(alg, input->x, input->len,
2793 output, sizeof(output),
2794 &output_length));
2795 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002796 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002797 expected_output->x, expected_output->len);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002798
Neil Armstrongca30a002022-02-07 11:40:23 +01002799 /* Compute with larger buffer, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 PSA_ASSERT(psa_hash_setup(&operation, alg));
2801 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2802 PSA_ASSERT(psa_hash_finish(&operation, output,
2803 sizeof(output), &output_length));
2804 TEST_EQUAL(output_length, PSA_HASH_LENGTH(alg));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01002805 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01002806 expected_output->x, expected_output->len);
Neil Armstrongca30a002022-02-07 11:40:23 +01002807
2808 /* Compare with correct hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 PSA_ASSERT(psa_hash_compare(alg, input->x, input->len,
2810 output, output_length));
Gilles Peskine0a749c82019-11-28 19:33:58 +01002811
Neil Armstrongca30a002022-02-07 11:40:23 +01002812 /* Compare with correct hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 PSA_ASSERT(psa_hash_setup(&operation, alg));
2814 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2815 PSA_ASSERT(psa_hash_verify(&operation, output,
2816 output_length));
Neil Armstrongca30a002022-02-07 11:40:23 +01002817
2818 /* Compare with trailing garbage, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2820 output, output_length + 1),
2821 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002822
Neil Armstrongca30a002022-02-07 11:40:23 +01002823 /* Compare with trailing garbage, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 PSA_ASSERT(psa_hash_setup(&operation, alg));
2825 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2826 TEST_EQUAL(psa_hash_verify(&operation, output, output_length + 1),
2827 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002828
2829 /* Compare with truncated hash, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2831 output, output_length - 1),
2832 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002833
Neil Armstrongca30a002022-02-07 11:40:23 +01002834 /* Compare with truncated hash, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 PSA_ASSERT(psa_hash_setup(&operation, alg));
2836 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2837 TEST_EQUAL(psa_hash_verify(&operation, output, output_length - 1),
2838 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002839
Gilles Peskine0a749c82019-11-28 19:33:58 +01002840 /* Compare with corrupted value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 for (i = 0; i < output_length; i++) {
2842 mbedtls_test_set_step(i);
Gilles Peskine0a749c82019-11-28 19:33:58 +01002843 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002844
2845 /* One-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 TEST_EQUAL(psa_hash_compare(alg, input->x, input->len,
2847 output, output_length),
2848 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002849
2850 /* Multi-Part */
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 PSA_ASSERT(psa_hash_setup(&operation, alg));
2852 PSA_ASSERT(psa_hash_update(&operation, input->x, input->len));
2853 TEST_EQUAL(psa_hash_verify(&operation, output, output_length),
2854 PSA_ERROR_INVALID_SIGNATURE);
Neil Armstrongca30a002022-02-07 11:40:23 +01002855
Gilles Peskine0a749c82019-11-28 19:33:58 +01002856 output[i] ^= 1;
2857 }
2858
2859exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 PSA_ASSERT(psa_hash_abort(&operation));
2861 PSA_DONE();
Gilles Peskine0a749c82019-11-28 19:33:58 +01002862}
2863/* END_CASE */
2864
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002865/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002866void hash_bad_order()
itayzafrirf86548d2018-11-01 10:44:32 +02002867{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002868 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002869 unsigned char input[] = "";
2870 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002871 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002872 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2873 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
2875 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002876 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002877 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002878 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002879
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 PSA_ASSERT(psa_crypto_init());
itayzafrirf86548d2018-11-01 10:44:32 +02002881
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002882 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 PSA_ASSERT(psa_hash_setup(&operation, alg));
2884 ASSERT_OPERATION_IS_ACTIVE(operation);
2885 TEST_EQUAL(psa_hash_setup(&operation, alg),
2886 PSA_ERROR_BAD_STATE);
2887 ASSERT_OPERATION_IS_INACTIVE(operation);
2888 PSA_ASSERT(psa_hash_abort(&operation));
2889 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002890
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002891 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2893 PSA_ERROR_BAD_STATE);
2894 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002895
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002896 /* Check that update calls abort on error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 PSA_ASSERT(psa_hash_setup(&operation, alg));
Dave Rodgman6f710582021-06-24 18:14:52 +01002898 operation.id = UINT_MAX;
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 ASSERT_OPERATION_IS_ACTIVE(operation);
2900 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2901 PSA_ERROR_BAD_STATE);
2902 ASSERT_OPERATION_IS_INACTIVE(operation);
2903 PSA_ASSERT(psa_hash_abort(&operation));
2904 ASSERT_OPERATION_IS_INACTIVE(operation);
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002905
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002906 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 PSA_ASSERT(psa_hash_setup(&operation, alg));
2908 PSA_ASSERT(psa_hash_finish(&operation,
2909 hash, sizeof(hash), &hash_len));
2910 TEST_EQUAL(psa_hash_update(&operation, input, sizeof(input)),
2911 PSA_ERROR_BAD_STATE);
2912 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002913
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002914 /* Call verify without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 TEST_EQUAL(psa_hash_verify(&operation,
2916 valid_hash, sizeof(valid_hash)),
2917 PSA_ERROR_BAD_STATE);
2918 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002919
2920 /* Call verify after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 PSA_ASSERT(psa_hash_setup(&operation, alg));
2922 PSA_ASSERT(psa_hash_finish(&operation,
2923 hash, sizeof(hash), &hash_len));
2924 TEST_EQUAL(psa_hash_verify(&operation,
2925 valid_hash, sizeof(valid_hash)),
2926 PSA_ERROR_BAD_STATE);
2927 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002928
2929 /* Call verify twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 PSA_ASSERT(psa_hash_setup(&operation, alg));
2931 ASSERT_OPERATION_IS_ACTIVE(operation);
2932 PSA_ASSERT(psa_hash_verify(&operation,
2933 valid_hash, sizeof(valid_hash)));
2934 ASSERT_OPERATION_IS_INACTIVE(operation);
2935 TEST_EQUAL(psa_hash_verify(&operation,
2936 valid_hash, sizeof(valid_hash)),
2937 PSA_ERROR_BAD_STATE);
2938 ASSERT_OPERATION_IS_INACTIVE(operation);
2939 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002940
2941 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 TEST_EQUAL(psa_hash_finish(&operation,
2943 hash, sizeof(hash), &hash_len),
2944 PSA_ERROR_BAD_STATE);
2945 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002946
2947 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 PSA_ASSERT(psa_hash_setup(&operation, alg));
2949 PSA_ASSERT(psa_hash_finish(&operation,
2950 hash, sizeof(hash), &hash_len));
2951 TEST_EQUAL(psa_hash_finish(&operation,
2952 hash, sizeof(hash), &hash_len),
2953 PSA_ERROR_BAD_STATE);
2954 PSA_ASSERT(psa_hash_abort(&operation));
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002955
2956 /* Call finish after calling verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 PSA_ASSERT(psa_hash_setup(&operation, alg));
2958 PSA_ASSERT(psa_hash_verify(&operation,
2959 valid_hash, sizeof(valid_hash)));
2960 TEST_EQUAL(psa_hash_finish(&operation,
2961 hash, sizeof(hash), &hash_len),
2962 PSA_ERROR_BAD_STATE);
2963 PSA_ASSERT(psa_hash_abort(&operation));
itayzafrirf86548d2018-11-01 10:44:32 +02002964
2965exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 PSA_DONE();
itayzafrirf86548d2018-11-01 10:44:32 +02002967}
2968/* END_CASE */
2969
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002970/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002971void hash_verify_bad_args()
itayzafrirec93d302018-10-18 18:01:10 +03002972{
2973 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002974 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2975 * appended to it */
2976 unsigned char hash[] = {
2977 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2978 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb
2980 };
2981 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00002982 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002983
Gilles Peskine449bd832023-01-11 14:50:10 +01002984 PSA_ASSERT(psa_crypto_init());
itayzafrirec93d302018-10-18 18:01:10 +03002985
itayzafrir27e69452018-11-01 14:26:34 +02002986 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 PSA_ASSERT(psa_hash_setup(&operation, alg));
2988 ASSERT_OPERATION_IS_ACTIVE(operation);
2989 TEST_EQUAL(psa_hash_verify(&operation, hash, expected_size - 1),
2990 PSA_ERROR_INVALID_SIGNATURE);
2991 ASSERT_OPERATION_IS_INACTIVE(operation);
2992 PSA_ASSERT(psa_hash_abort(&operation));
2993 ASSERT_OPERATION_IS_INACTIVE(operation);
itayzafrirec93d302018-10-18 18:01:10 +03002994
itayzafrir27e69452018-11-01 14:26:34 +02002995 /* psa_hash_verify with a non-matching hash */
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 PSA_ASSERT(psa_hash_setup(&operation, alg));
2997 TEST_EQUAL(psa_hash_verify(&operation, hash + 1, expected_size),
2998 PSA_ERROR_INVALID_SIGNATURE);
itayzafrirec93d302018-10-18 18:01:10 +03002999
itayzafrir27e69452018-11-01 14:26:34 +02003000 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 PSA_ASSERT(psa_hash_setup(&operation, alg));
3002 TEST_EQUAL(psa_hash_verify(&operation, hash, sizeof(hash)),
3003 PSA_ERROR_INVALID_SIGNATURE);
itayzafrir4271df92018-10-24 18:16:19 +03003004
itayzafrirec93d302018-10-18 18:01:10 +03003005exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 PSA_DONE();
itayzafrirec93d302018-10-18 18:01:10 +03003007}
3008/* END_CASE */
3009
Ronald Cronee414c72021-03-18 18:50:08 +01003010/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003011void hash_finish_bad_args()
itayzafrir58028322018-10-25 10:22:01 +03003012{
3013 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003014 unsigned char hash[PSA_HASH_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 size_t expected_size = PSA_HASH_LENGTH(alg);
Jaeden Amero6a25b412019-01-04 11:47:44 +00003016 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003017 size_t hash_len;
3018
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 PSA_ASSERT(psa_crypto_init());
itayzafrir58028322018-10-25 10:22:01 +03003020
itayzafrir58028322018-10-25 10:22:01 +03003021 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 PSA_ASSERT(psa_hash_setup(&operation, alg));
3023 TEST_EQUAL(psa_hash_finish(&operation,
3024 hash, expected_size - 1, &hash_len),
3025 PSA_ERROR_BUFFER_TOO_SMALL);
itayzafrir58028322018-10-25 10:22:01 +03003026
3027exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 PSA_DONE();
itayzafrir58028322018-10-25 10:22:01 +03003029}
3030/* END_CASE */
3031
Ronald Cronee414c72021-03-18 18:50:08 +01003032/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003033void hash_clone_source_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003034{
3035 psa_algorithm_t alg = PSA_ALG_SHA_256;
3036 unsigned char hash[PSA_HASH_MAX_SIZE];
3037 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3038 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3039 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3040 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3041 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3042 size_t hash_len;
3043
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 PSA_ASSERT(psa_crypto_init());
3045 PSA_ASSERT(psa_hash_setup(&op_source, alg));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003046
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3048 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3049 PSA_ASSERT(psa_hash_finish(&op_finished,
3050 hash, sizeof(hash), &hash_len));
3051 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3052 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003053
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 TEST_EQUAL(psa_hash_clone(&op_source, &op_setup),
3055 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003056
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 PSA_ASSERT(psa_hash_clone(&op_source, &op_init));
3058 PSA_ASSERT(psa_hash_finish(&op_init,
3059 hash, sizeof(hash), &hash_len));
3060 PSA_ASSERT(psa_hash_clone(&op_source, &op_finished));
3061 PSA_ASSERT(psa_hash_finish(&op_finished,
3062 hash, sizeof(hash), &hash_len));
3063 PSA_ASSERT(psa_hash_clone(&op_source, &op_aborted));
3064 PSA_ASSERT(psa_hash_finish(&op_aborted,
3065 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003066
3067exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 psa_hash_abort(&op_source);
3069 psa_hash_abort(&op_init);
3070 psa_hash_abort(&op_setup);
3071 psa_hash_abort(&op_finished);
3072 psa_hash_abort(&op_aborted);
3073 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003074}
3075/* END_CASE */
3076
Ronald Cronee414c72021-03-18 18:50:08 +01003077/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003078void hash_clone_target_state()
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003079{
3080 psa_algorithm_t alg = PSA_ALG_SHA_256;
3081 unsigned char hash[PSA_HASH_MAX_SIZE];
3082 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3083 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3084 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3085 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3086 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3087 size_t hash_len;
3088
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 PSA_ASSERT(psa_crypto_init());
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003090
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 PSA_ASSERT(psa_hash_setup(&op_setup, alg));
3092 PSA_ASSERT(psa_hash_setup(&op_finished, alg));
3093 PSA_ASSERT(psa_hash_finish(&op_finished,
3094 hash, sizeof(hash), &hash_len));
3095 PSA_ASSERT(psa_hash_setup(&op_aborted, alg));
3096 PSA_ASSERT(psa_hash_abort(&op_aborted));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003097
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 PSA_ASSERT(psa_hash_clone(&op_setup, &op_target));
3099 PSA_ASSERT(psa_hash_finish(&op_target,
3100 hash, sizeof(hash), &hash_len));
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003101
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 TEST_EQUAL(psa_hash_clone(&op_init, &op_target), PSA_ERROR_BAD_STATE);
3103 TEST_EQUAL(psa_hash_clone(&op_finished, &op_target),
3104 PSA_ERROR_BAD_STATE);
3105 TEST_EQUAL(psa_hash_clone(&op_aborted, &op_target),
3106 PSA_ERROR_BAD_STATE);
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003107
3108exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 psa_hash_abort(&op_target);
3110 psa_hash_abort(&op_init);
3111 psa_hash_abort(&op_setup);
3112 psa_hash_abort(&op_finished);
3113 psa_hash_abort(&op_aborted);
3114 PSA_DONE();
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003115}
3116/* END_CASE */
3117
itayzafrir58028322018-10-25 10:22:01 +03003118/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003119void mac_operation_init()
Jaeden Amero769ce272019-01-04 11:48:03 +00003120{
Jaeden Amero252ef282019-02-15 14:05:35 +00003121 const uint8_t input[1] = { 0 };
3122
Jaeden Amero769ce272019-01-04 11:48:03 +00003123 /* Test each valid way of initializing the object, except for `= {0}`, as
3124 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3125 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003126 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 psa_mac_operation_t func = psa_mac_operation_init();
Jaeden Amero769ce272019-01-04 11:48:03 +00003128 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3129 psa_mac_operation_t zero;
3130
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 memset(&zero, 0, sizeof(zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003132
Jaeden Amero252ef282019-02-15 14:05:35 +00003133 /* A freshly-initialized MAC operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 TEST_EQUAL(psa_mac_update(&func,
3135 input, sizeof(input)),
3136 PSA_ERROR_BAD_STATE);
3137 TEST_EQUAL(psa_mac_update(&init,
3138 input, sizeof(input)),
3139 PSA_ERROR_BAD_STATE);
3140 TEST_EQUAL(psa_mac_update(&zero,
3141 input, sizeof(input)),
3142 PSA_ERROR_BAD_STATE);
Jaeden Amero252ef282019-02-15 14:05:35 +00003143
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003144 /* A default MAC operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 PSA_ASSERT(psa_mac_abort(&func));
3146 PSA_ASSERT(psa_mac_abort(&init));
3147 PSA_ASSERT(psa_mac_abort(&zero));
Jaeden Amero769ce272019-01-04 11:48:03 +00003148}
3149/* END_CASE */
3150
3151/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003152void mac_setup(int key_type_arg,
3153 data_t *key,
3154 int alg_arg,
3155 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003156{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003157 psa_key_type_t key_type = key_type_arg;
3158 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003159 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003160 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003161 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3162#if defined(KNOWN_SUPPORTED_MAC_ALG)
3163 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3164#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003165
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003167
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 if (!exercise_mac_setup(key_type, key->x, key->len, alg,
3169 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003170 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003171 }
3172 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003173
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003174 /* The operation object should be reusable. */
3175#if defined(KNOWN_SUPPORTED_MAC_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003176 if (!exercise_mac_setup(KNOWN_SUPPORTED_MAC_KEY_TYPE,
3177 smoke_test_key_data,
3178 sizeof(smoke_test_key_data),
3179 KNOWN_SUPPORTED_MAC_ALG,
3180 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003181 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 }
3183 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003184#endif
3185
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003186exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003188}
3189/* END_CASE */
3190
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003191/* 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 +01003192void mac_bad_order()
Jaeden Amero252ef282019-02-15 14:05:35 +00003193{
Ronald Cron5425a212020-08-04 14:58:35 +02003194 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003195 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3196 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003197 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003198 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3199 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa
3201 };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003202 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003203 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3204 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3205 size_t sign_mac_length = 0;
3206 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3207 const uint8_t verify_mac[] = {
3208 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3209 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6
3211 };
Jaeden Amero252ef282019-02-15 14:05:35 +00003212
Gilles Peskine449bd832023-01-11 14:50:10 +01003213 PSA_ASSERT(psa_crypto_init());
3214 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
3215 psa_set_key_algorithm(&attributes, alg);
3216 psa_set_key_type(&attributes, key_type);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003217
Gilles Peskine449bd832023-01-11 14:50:10 +01003218 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3219 &key));
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003220
Jaeden Amero252ef282019-02-15 14:05:35 +00003221 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003222 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3223 PSA_ERROR_BAD_STATE);
3224 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003225
3226 /* Call sign finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003227 TEST_EQUAL(psa_mac_sign_finish(&operation, sign_mac, sizeof(sign_mac),
3228 &sign_mac_length),
3229 PSA_ERROR_BAD_STATE);
3230 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003231
3232 /* Call verify finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 TEST_EQUAL(psa_mac_verify_finish(&operation,
3234 verify_mac, sizeof(verify_mac)),
3235 PSA_ERROR_BAD_STATE);
3236 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003237
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003238 /* Call setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3240 ASSERT_OPERATION_IS_ACTIVE(operation);
3241 TEST_EQUAL(psa_mac_sign_setup(&operation, key, alg),
3242 PSA_ERROR_BAD_STATE);
3243 ASSERT_OPERATION_IS_INACTIVE(operation);
3244 PSA_ASSERT(psa_mac_abort(&operation));
3245 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003246
Jaeden Amero252ef282019-02-15 14:05:35 +00003247 /* Call update after sign finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3249 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3250 PSA_ASSERT(psa_mac_sign_finish(&operation,
3251 sign_mac, sizeof(sign_mac),
3252 &sign_mac_length));
3253 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3254 PSA_ERROR_BAD_STATE);
3255 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003256
3257 /* Call update after verify finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003258 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3259 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3260 PSA_ASSERT(psa_mac_verify_finish(&operation,
3261 verify_mac, sizeof(verify_mac)));
3262 TEST_EQUAL(psa_mac_update(&operation, input, sizeof(input)),
3263 PSA_ERROR_BAD_STATE);
3264 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003265
3266 /* Call sign finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3268 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3269 PSA_ASSERT(psa_mac_sign_finish(&operation,
3270 sign_mac, sizeof(sign_mac),
3271 &sign_mac_length));
3272 TEST_EQUAL(psa_mac_sign_finish(&operation,
3273 sign_mac, sizeof(sign_mac),
3274 &sign_mac_length),
3275 PSA_ERROR_BAD_STATE);
3276 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003277
3278 /* Call verify finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3280 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3281 PSA_ASSERT(psa_mac_verify_finish(&operation,
3282 verify_mac, sizeof(verify_mac)));
3283 TEST_EQUAL(psa_mac_verify_finish(&operation,
3284 verify_mac, sizeof(verify_mac)),
3285 PSA_ERROR_BAD_STATE);
3286 PSA_ASSERT(psa_mac_abort(&operation));
Jaeden Amero252ef282019-02-15 14:05:35 +00003287
3288 /* Setup sign but try verify. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003289 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3290 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3291 ASSERT_OPERATION_IS_ACTIVE(operation);
3292 TEST_EQUAL(psa_mac_verify_finish(&operation,
3293 verify_mac, sizeof(verify_mac)),
3294 PSA_ERROR_BAD_STATE);
3295 ASSERT_OPERATION_IS_INACTIVE(operation);
3296 PSA_ASSERT(psa_mac_abort(&operation));
3297 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero252ef282019-02-15 14:05:35 +00003298
3299 /* Setup verify but try sign. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003300 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3301 PSA_ASSERT(psa_mac_update(&operation, input, sizeof(input)));
3302 ASSERT_OPERATION_IS_ACTIVE(operation);
3303 TEST_EQUAL(psa_mac_sign_finish(&operation,
3304 sign_mac, sizeof(sign_mac),
3305 &sign_mac_length),
3306 PSA_ERROR_BAD_STATE);
3307 ASSERT_OPERATION_IS_INACTIVE(operation);
3308 PSA_ASSERT(psa_mac_abort(&operation));
3309 ASSERT_OPERATION_IS_INACTIVE(operation);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003310
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003312
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003313exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003314 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003315}
3316/* END_CASE */
3317
3318/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003319void mac_sign_verify_multi(int key_type_arg,
3320 data_t *key_data,
3321 int alg_arg,
3322 data_t *input,
3323 int is_verify,
3324 data_t *expected_mac)
Neil Armstrong4766f992022-02-28 16:23:59 +01003325{
3326 size_t data_part_len = 0;
3327
Gilles Peskine449bd832023-01-11 14:50:10 +01003328 for (data_part_len = 1; data_part_len <= input->len; data_part_len++) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003329 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 mbedtls_test_set_step(2000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003331
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 if (mac_multipart_internal_func(key_type_arg, key_data,
3333 alg_arg,
3334 input, data_part_len,
3335 expected_mac,
3336 is_verify, 0) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003337 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003338 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003339
3340 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 mbedtls_test_set_step(3000 + data_part_len);
Neil Armstrong4766f992022-02-28 16:23:59 +01003342
Gilles Peskine449bd832023-01-11 14:50:10 +01003343 if (mac_multipart_internal_func(key_type_arg, key_data,
3344 alg_arg,
3345 input, data_part_len,
3346 expected_mac,
3347 is_verify, 1) == 0) {
Neil Armstrong4766f992022-02-28 16:23:59 +01003348 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 }
Neil Armstrong4766f992022-02-28 16:23:59 +01003350 }
3351
3352 /* Goto is required to silence warnings about unused labels, as we
3353 * don't actually do any test assertions in this function. */
3354 goto exit;
3355}
3356/* END_CASE */
3357
3358/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003359void mac_sign(int key_type_arg,
3360 data_t *key_data,
3361 int alg_arg,
3362 data_t *input,
3363 data_t *expected_mac)
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003364{
Ronald Cron5425a212020-08-04 14:58:35 +02003365 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003366 psa_key_type_t key_type = key_type_arg;
3367 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003368 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003369 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003370 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003371 size_t mac_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01003372 PSA_MAC_LENGTH(key_type, PSA_BYTES_TO_BITS(key_data->len), alg);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003373 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003374 const size_t output_sizes_to_test[] = {
3375 0,
3376 1,
3377 expected_mac->len - 1,
3378 expected_mac->len,
3379 expected_mac->len + 1,
3380 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003381
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 TEST_LE_U(mac_buffer_size, PSA_MAC_MAX_SIZE);
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003383 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 TEST_ASSERT(expected_mac->len == mac_buffer_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003385
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003387
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
3389 psa_set_key_algorithm(&attributes, alg);
3390 psa_set_key_type(&attributes, key_type);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003391
Gilles Peskine449bd832023-01-11 14:50:10 +01003392 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3393 &key));
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003394
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 for (size_t i = 0; i < ARRAY_LENGTH(output_sizes_to_test); i++) {
Gilles Peskine8b356b52020-08-25 23:44:59 +02003396 const size_t output_size = output_sizes_to_test[i];
3397 psa_status_t expected_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01003398 (output_size >= expected_mac->len ? PSA_SUCCESS :
3399 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003400
Gilles Peskine449bd832023-01-11 14:50:10 +01003401 mbedtls_test_set_step(output_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003402 TEST_CALLOC(actual_mac, output_size);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003403
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003404 /* Calculate the MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003405 TEST_EQUAL(psa_mac_compute(key, alg,
3406 input->x, input->len,
3407 actual_mac, output_size, &mac_length),
3408 expected_status);
3409 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003410 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003411 actual_mac, mac_length);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003412 }
3413
Gilles Peskine449bd832023-01-11 14:50:10 +01003414 if (output_size > 0) {
3415 memset(actual_mac, 0, output_size);
3416 }
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003417
3418 /* Calculate the MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003419 PSA_ASSERT(psa_mac_sign_setup(&operation, key, alg));
3420 PSA_ASSERT(psa_mac_update(&operation,
3421 input->x, input->len));
3422 TEST_EQUAL(psa_mac_sign_finish(&operation,
3423 actual_mac, output_size,
3424 &mac_length),
3425 expected_status);
3426 PSA_ASSERT(psa_mac_abort(&operation));
Gilles Peskine8b356b52020-08-25 23:44:59 +02003427
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 if (expected_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003429 TEST_MEMORY_COMPARE(expected_mac->x, expected_mac->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003430 actual_mac, mac_length);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003431 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003432 mbedtls_free(actual_mac);
Gilles Peskine8b356b52020-08-25 23:44:59 +02003433 actual_mac = NULL;
3434 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003435
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003436exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003437 psa_mac_abort(&operation);
3438 psa_destroy_key(key);
3439 PSA_DONE();
3440 mbedtls_free(actual_mac);
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003441}
3442/* END_CASE */
3443
3444/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003445void mac_verify(int key_type_arg,
3446 data_t *key_data,
3447 int alg_arg,
3448 data_t *input,
3449 data_t *expected_mac)
Gilles Peskine8c9def32018-02-08 10:02:12 +01003450{
Ronald Cron5425a212020-08-04 14:58:35 +02003451 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003452 psa_key_type_t key_type = key_type_arg;
3453 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003454 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003455 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003456 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003457
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 TEST_LE_U(expected_mac->len, PSA_MAC_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02003459
Gilles Peskine449bd832023-01-11 14:50:10 +01003460 PSA_ASSERT(psa_crypto_init());
Gilles Peskine8c9def32018-02-08 10:02:12 +01003461
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
3463 psa_set_key_algorithm(&attributes, alg);
3464 psa_set_key_type(&attributes, key_type);
mohammad16036df908f2018-04-02 08:34:15 -07003465
Gilles Peskine449bd832023-01-11 14:50:10 +01003466 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3467 &key));
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003468
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003469 /* Verify correct MAC, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003470 PSA_ASSERT(psa_mac_verify(key, alg, input->x, input->len,
3471 expected_mac->x, expected_mac->len));
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003472
3473 /* Verify correct MAC, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003474 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3475 PSA_ASSERT(psa_mac_update(&operation,
3476 input->x, input->len));
3477 PSA_ASSERT(psa_mac_verify_finish(&operation,
3478 expected_mac->x,
3479 expected_mac->len));
Gilles Peskine8c9def32018-02-08 10:02:12 +01003480
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003481 /* Test a MAC that's too short, one-shot case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003482 TEST_EQUAL(psa_mac_verify(key, alg,
3483 input->x, input->len,
3484 expected_mac->x,
3485 expected_mac->len - 1),
3486 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003487
3488 /* Test a MAC that's too short, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003489 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3490 PSA_ASSERT(psa_mac_update(&operation,
3491 input->x, input->len));
3492 TEST_EQUAL(psa_mac_verify_finish(&operation,
3493 expected_mac->x,
3494 expected_mac->len - 1),
3495 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003496
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003497 /* Test a MAC that's too long, one-shot case. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003498 TEST_CALLOC(perturbed_mac, expected_mac->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01003499 memcpy(perturbed_mac, expected_mac->x, expected_mac->len);
3500 TEST_EQUAL(psa_mac_verify(key, alg,
3501 input->x, input->len,
3502 perturbed_mac, expected_mac->len + 1),
3503 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003504
3505 /* Test a MAC that's too long, multi-part case. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003506 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3507 PSA_ASSERT(psa_mac_update(&operation,
3508 input->x, input->len));
3509 TEST_EQUAL(psa_mac_verify_finish(&operation,
3510 perturbed_mac,
3511 expected_mac->len + 1),
3512 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003513
3514 /* Test changing one byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003515 for (size_t i = 0; i < expected_mac->len; i++) {
3516 mbedtls_test_set_step(i);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003517 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003518
Gilles Peskine449bd832023-01-11 14:50:10 +01003519 TEST_EQUAL(psa_mac_verify(key, alg,
3520 input->x, input->len,
3521 perturbed_mac, expected_mac->len),
3522 PSA_ERROR_INVALID_SIGNATURE);
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003523
Gilles Peskine449bd832023-01-11 14:50:10 +01003524 PSA_ASSERT(psa_mac_verify_setup(&operation, key, alg));
3525 PSA_ASSERT(psa_mac_update(&operation,
3526 input->x, input->len));
3527 TEST_EQUAL(psa_mac_verify_finish(&operation,
3528 perturbed_mac,
3529 expected_mac->len),
3530 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003531 perturbed_mac[i] ^= 1;
3532 }
3533
Gilles Peskine8c9def32018-02-08 10:02:12 +01003534exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003535 psa_mac_abort(&operation);
3536 psa_destroy_key(key);
3537 PSA_DONE();
3538 mbedtls_free(perturbed_mac);
Gilles Peskine8c9def32018-02-08 10:02:12 +01003539}
3540/* END_CASE */
3541
3542/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003543void cipher_operation_init()
Jaeden Amero5bae2272019-01-04 11:48:27 +00003544{
Jaeden Ameroab439972019-02-15 14:12:05 +00003545 const uint8_t input[1] = { 0 };
3546 unsigned char output[1] = { 0 };
3547 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003548 /* Test each valid way of initializing the object, except for `= {0}`, as
3549 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3550 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003551 * to suppress the Clang warning for the test. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003552 psa_cipher_operation_t func = psa_cipher_operation_init();
Jaeden Amero5bae2272019-01-04 11:48:27 +00003553 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3554 psa_cipher_operation_t zero;
3555
Gilles Peskine449bd832023-01-11 14:50:10 +01003556 memset(&zero, 0, sizeof(zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003557
Jaeden Ameroab439972019-02-15 14:12:05 +00003558 /* A freshly-initialized cipher operation should not be usable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003559 TEST_EQUAL(psa_cipher_update(&func,
3560 input, sizeof(input),
3561 output, sizeof(output),
3562 &output_length),
3563 PSA_ERROR_BAD_STATE);
3564 TEST_EQUAL(psa_cipher_update(&init,
3565 input, sizeof(input),
3566 output, sizeof(output),
3567 &output_length),
3568 PSA_ERROR_BAD_STATE);
3569 TEST_EQUAL(psa_cipher_update(&zero,
3570 input, sizeof(input),
3571 output, sizeof(output),
3572 &output_length),
3573 PSA_ERROR_BAD_STATE);
Jaeden Ameroab439972019-02-15 14:12:05 +00003574
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003575 /* A default cipher operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003576 PSA_ASSERT(psa_cipher_abort(&func));
3577 PSA_ASSERT(psa_cipher_abort(&init));
3578 PSA_ASSERT(psa_cipher_abort(&zero));
Jaeden Amero5bae2272019-01-04 11:48:27 +00003579}
3580/* END_CASE */
3581
3582/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003583void cipher_setup(int key_type_arg,
3584 data_t *key,
3585 int alg_arg,
3586 int expected_status_arg)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003587{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003588 psa_key_type_t key_type = key_type_arg;
3589 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003590 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003591 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003592 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003593#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003594 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3595#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003596
Gilles Peskine449bd832023-01-11 14:50:10 +01003597 PSA_ASSERT(psa_crypto_init());
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003598
Gilles Peskine449bd832023-01-11 14:50:10 +01003599 if (!exercise_cipher_setup(key_type, key->x, key->len, alg,
3600 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003601 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003602 }
3603 TEST_EQUAL(status, expected_status);
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003604
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003605 /* The operation object should be reusable. */
3606#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskine449bd832023-01-11 14:50:10 +01003607 if (!exercise_cipher_setup(KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3608 smoke_test_key_data,
3609 sizeof(smoke_test_key_data),
3610 KNOWN_SUPPORTED_CIPHER_ALG,
3611 &operation, &status)) {
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003612 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003613 }
3614 TEST_EQUAL(status, PSA_SUCCESS);
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003615#endif
3616
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003617exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003618 psa_cipher_abort(&operation);
3619 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003620}
3621/* END_CASE */
3622
Ronald Cronee414c72021-03-18 18:50:08 +01003623/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003624void cipher_bad_order()
Jaeden Ameroab439972019-02-15 14:12:05 +00003625{
Ronald Cron5425a212020-08-04 14:58:35 +02003626 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003627 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3628 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003629 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003630 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003631 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003632 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003633 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
Gilles Peskine449bd832023-01-11 14:50:10 +01003634 0xaa, 0xaa, 0xaa, 0xaa
3635 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003636 const uint8_t text[] = {
3637 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
Gilles Peskine449bd832023-01-11 14:50:10 +01003638 0xbb, 0xbb, 0xbb, 0xbb
3639 };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003640 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003641 size_t length = 0;
3642
Gilles Peskine449bd832023-01-11 14:50:10 +01003643 PSA_ASSERT(psa_crypto_init());
3644 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3645 psa_set_key_algorithm(&attributes, alg);
3646 psa_set_key_type(&attributes, key_type);
3647 PSA_ASSERT(psa_import_key(&attributes, key_data, sizeof(key_data),
3648 &key));
Jaeden Ameroab439972019-02-15 14:12:05 +00003649
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003650 /* Call encrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003651 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3652 ASSERT_OPERATION_IS_ACTIVE(operation);
3653 TEST_EQUAL(psa_cipher_encrypt_setup(&operation, key, alg),
3654 PSA_ERROR_BAD_STATE);
3655 ASSERT_OPERATION_IS_INACTIVE(operation);
3656 PSA_ASSERT(psa_cipher_abort(&operation));
3657 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003658
3659 /* Call decrypt setup twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003660 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3661 ASSERT_OPERATION_IS_ACTIVE(operation);
3662 TEST_EQUAL(psa_cipher_decrypt_setup(&operation, key, alg),
3663 PSA_ERROR_BAD_STATE);
3664 ASSERT_OPERATION_IS_INACTIVE(operation);
3665 PSA_ASSERT(psa_cipher_abort(&operation));
3666 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003667
Jaeden Ameroab439972019-02-15 14:12:05 +00003668 /* Generate an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003669 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3670 buffer, sizeof(buffer),
3671 &length),
3672 PSA_ERROR_BAD_STATE);
3673 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003674
3675 /* Generate an IV twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003676 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3677 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3678 buffer, sizeof(buffer),
3679 &length));
3680 ASSERT_OPERATION_IS_ACTIVE(operation);
3681 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3682 buffer, sizeof(buffer),
3683 &length),
3684 PSA_ERROR_BAD_STATE);
3685 ASSERT_OPERATION_IS_INACTIVE(operation);
3686 PSA_ASSERT(psa_cipher_abort(&operation));
3687 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003688
3689 /* Generate an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003690 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3691 PSA_ASSERT(psa_cipher_set_iv(&operation,
3692 iv, sizeof(iv)));
3693 TEST_EQUAL(psa_cipher_generate_iv(&operation,
3694 buffer, sizeof(buffer),
3695 &length),
3696 PSA_ERROR_BAD_STATE);
3697 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003698
3699 /* Set an IV without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003700 TEST_EQUAL(psa_cipher_set_iv(&operation,
3701 iv, sizeof(iv)),
3702 PSA_ERROR_BAD_STATE);
3703 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003704
3705 /* Set an IV after it's already set. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003706 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3707 PSA_ASSERT(psa_cipher_set_iv(&operation,
3708 iv, sizeof(iv)));
3709 ASSERT_OPERATION_IS_ACTIVE(operation);
3710 TEST_EQUAL(psa_cipher_set_iv(&operation,
3711 iv, sizeof(iv)),
3712 PSA_ERROR_BAD_STATE);
3713 ASSERT_OPERATION_IS_INACTIVE(operation);
3714 PSA_ASSERT(psa_cipher_abort(&operation));
3715 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003716
3717 /* Set an IV after it's already generated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003718 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3719 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3720 buffer, sizeof(buffer),
3721 &length));
3722 TEST_EQUAL(psa_cipher_set_iv(&operation,
3723 iv, sizeof(iv)),
3724 PSA_ERROR_BAD_STATE);
3725 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003726
3727 /* Call update without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003728 TEST_EQUAL(psa_cipher_update(&operation,
3729 text, sizeof(text),
3730 buffer, sizeof(buffer),
3731 &length),
3732 PSA_ERROR_BAD_STATE);
3733 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003734
3735 /* Call update without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003736 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3737 ASSERT_OPERATION_IS_ACTIVE(operation);
3738 TEST_EQUAL(psa_cipher_update(&operation,
3739 text, sizeof(text),
3740 buffer, sizeof(buffer),
3741 &length),
3742 PSA_ERROR_BAD_STATE);
3743 ASSERT_OPERATION_IS_INACTIVE(operation);
3744 PSA_ASSERT(psa_cipher_abort(&operation));
3745 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003746
3747 /* Call update after finish. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003748 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3749 PSA_ASSERT(psa_cipher_set_iv(&operation,
3750 iv, sizeof(iv)));
3751 PSA_ASSERT(psa_cipher_finish(&operation,
3752 buffer, sizeof(buffer), &length));
3753 TEST_EQUAL(psa_cipher_update(&operation,
3754 text, sizeof(text),
3755 buffer, sizeof(buffer),
3756 &length),
3757 PSA_ERROR_BAD_STATE);
3758 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003759
3760 /* Call finish without calling setup beforehand. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003761 TEST_EQUAL(psa_cipher_finish(&operation,
3762 buffer, sizeof(buffer), &length),
3763 PSA_ERROR_BAD_STATE);
3764 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003765
3766 /* Call finish without an IV where an IV is required. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003767 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Jaeden Ameroab439972019-02-15 14:12:05 +00003768 /* Not calling update means we are encrypting an empty buffer, which is OK
3769 * for cipher modes with padding. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003770 ASSERT_OPERATION_IS_ACTIVE(operation);
3771 TEST_EQUAL(psa_cipher_finish(&operation,
3772 buffer, sizeof(buffer), &length),
3773 PSA_ERROR_BAD_STATE);
3774 ASSERT_OPERATION_IS_INACTIVE(operation);
3775 PSA_ASSERT(psa_cipher_abort(&operation));
3776 ASSERT_OPERATION_IS_INACTIVE(operation);
Jaeden Ameroab439972019-02-15 14:12:05 +00003777
3778 /* Call finish twice in a row. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003779 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3780 PSA_ASSERT(psa_cipher_set_iv(&operation,
3781 iv, sizeof(iv)));
3782 PSA_ASSERT(psa_cipher_finish(&operation,
3783 buffer, sizeof(buffer), &length));
3784 TEST_EQUAL(psa_cipher_finish(&operation,
3785 buffer, sizeof(buffer), &length),
3786 PSA_ERROR_BAD_STATE);
3787 PSA_ASSERT(psa_cipher_abort(&operation));
Jaeden Ameroab439972019-02-15 14:12:05 +00003788
Gilles Peskine449bd832023-01-11 14:50:10 +01003789 PSA_ASSERT(psa_destroy_key(key));
Gilles Peskine76b29a72019-05-28 14:08:50 +02003790
Jaeden Ameroab439972019-02-15 14:12:05 +00003791exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003792 psa_cipher_abort(&operation);
3793 PSA_DONE();
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003794}
3795/* END_CASE */
3796
3797/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003798void cipher_encrypt_fail(int alg_arg,
3799 int key_type_arg,
3800 data_t *key_data,
3801 data_t *input,
3802 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02003803{
Ronald Cron5425a212020-08-04 14:58:35 +02003804 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003805 psa_status_t status;
3806 psa_key_type_t key_type = key_type_arg;
3807 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003808 psa_status_t expected_status = expected_status_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01003809 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = { 0 };
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003810 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3811 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003812 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003813 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003814 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003815 size_t function_output_length;
3816 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003817 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3818
Gilles Peskine449bd832023-01-11 14:50:10 +01003819 if (PSA_ERROR_BAD_STATE != expected_status) {
3820 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003821
Gilles Peskine449bd832023-01-11 14:50:10 +01003822 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3823 psa_set_key_algorithm(&attributes, alg);
3824 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003825
Gilles Peskine449bd832023-01-11 14:50:10 +01003826 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3827 input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003828 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003829
Gilles Peskine449bd832023-01-11 14:50:10 +01003830 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3831 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003832 }
3833
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003834 /* Encrypt, one-shot */
Gilles Peskine449bd832023-01-11 14:50:10 +01003835 status = psa_cipher_encrypt(key, alg, input->x, input->len, output,
3836 output_buffer_size, &output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003837
Gilles Peskine449bd832023-01-11 14:50:10 +01003838 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003839
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003840 /* Encrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01003841 status = psa_cipher_encrypt_setup(&operation, key, alg);
3842 if (status == PSA_SUCCESS) {
3843 if (alg != PSA_ALG_ECB_NO_PADDING) {
3844 PSA_ASSERT(psa_cipher_generate_iv(&operation,
3845 iv, iv_size,
3846 &iv_length));
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003847 }
3848
Gilles Peskine449bd832023-01-11 14:50:10 +01003849 status = psa_cipher_update(&operation, input->x, input->len,
3850 output, output_buffer_size,
3851 &function_output_length);
3852 if (status == PSA_SUCCESS) {
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003853 output_length += function_output_length;
3854
Gilles Peskine449bd832023-01-11 14:50:10 +01003855 status = psa_cipher_finish(&operation, output + output_length,
3856 output_buffer_size - output_length,
3857 &function_output_length);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003858
Gilles Peskine449bd832023-01-11 14:50:10 +01003859 TEST_EQUAL(status, expected_status);
3860 } else {
3861 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003862 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003863 } else {
3864 TEST_EQUAL(status, expected_status);
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003865 }
3866
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003867exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003868 psa_cipher_abort(&operation);
3869 mbedtls_free(output);
3870 psa_destroy_key(key);
3871 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003872}
3873/* END_CASE */
3874
3875/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003876void cipher_encrypt_validate_iv_length(int alg, int key_type, data_t *key_data,
3877 data_t *input, int iv_length,
3878 int expected_result)
Mateusz Starzyked71e922021-10-21 10:04:57 +02003879{
3880 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3881 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3882 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3883 size_t output_buffer_size = 0;
3884 unsigned char *output = NULL;
3885
Gilles Peskine449bd832023-01-11 14:50:10 +01003886 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003887 TEST_CALLOC(output, output_buffer_size);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003888
Gilles Peskine449bd832023-01-11 14:50:10 +01003889 PSA_ASSERT(psa_crypto_init());
Mateusz Starzyked71e922021-10-21 10:04:57 +02003890
Gilles Peskine449bd832023-01-11 14:50:10 +01003891 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
3892 psa_set_key_algorithm(&attributes, alg);
3893 psa_set_key_type(&attributes, key_type);
Mateusz Starzyked71e922021-10-21 10:04:57 +02003894
Gilles Peskine449bd832023-01-11 14:50:10 +01003895 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3896 &key));
3897 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3898 TEST_EQUAL(expected_result, psa_cipher_set_iv(&operation, output,
3899 iv_length));
Mateusz Starzyked71e922021-10-21 10:04:57 +02003900
3901exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003902 psa_cipher_abort(&operation);
3903 mbedtls_free(output);
3904 psa_destroy_key(key);
3905 PSA_DONE();
Mateusz Starzyked71e922021-10-21 10:04:57 +02003906}
3907/* END_CASE */
3908
3909/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003910void cipher_alg_without_iv(int alg_arg, int key_type_arg, data_t *key_data,
3911 data_t *plaintext, data_t *ciphertext)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003912{
3913 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3914 psa_key_type_t key_type = key_type_arg;
3915 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003916 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3917 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003918 unsigned char *output = NULL;
3919 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003920 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003921 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3922
Gilles Peskine449bd832023-01-11 14:50:10 +01003923 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003924
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003925 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01003926 TEST_LE_U(ciphertext->len,
3927 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len));
3928 TEST_LE_U(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext->len),
3929 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(plaintext->len));
3930 TEST_LE_U(plaintext->len,
3931 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len));
3932 TEST_LE_U(PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext->len),
3933 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(ciphertext->len));
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003934
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003935
3936 /* Set up key and output buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003937 psa_set_key_usage_flags(&attributes,
3938 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
3939 psa_set_key_algorithm(&attributes, alg);
3940 psa_set_key_type(&attributes, key_type);
3941 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
3942 &key));
3943 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg,
3944 plaintext->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01003945 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003946
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003947 /* set_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003948 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3949 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3950 PSA_ERROR_BAD_STATE);
3951 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3952 TEST_EQUAL(psa_cipher_set_iv(&operation, iv, sizeof(iv)),
3953 PSA_ERROR_BAD_STATE);
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003954
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003955 /* generate_iv() is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +01003956 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
3957 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3958 &length),
3959 PSA_ERROR_BAD_STATE);
3960 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
3961 TEST_EQUAL(psa_cipher_generate_iv(&operation, iv, sizeof(iv),
3962 &length),
3963 PSA_ERROR_BAD_STATE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003964
Gilles Peskine286c3142022-04-20 17:09:38 +02003965 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003966 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003967 output_length = 0;
3968 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003969 PSA_ASSERT(psa_cipher_update(&operation,
3970 plaintext->x, plaintext->len,
3971 output, output_buffer_size,
3972 &length));
3973 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003974 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003975 PSA_ASSERT(psa_cipher_finish(&operation,
3976 mbedtls_buffer_offset(output, output_length),
3977 output_buffer_size - output_length,
3978 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003979 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003980 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003981 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003982
Gilles Peskine286c3142022-04-20 17:09:38 +02003983 /* Multipart encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01003984 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine286c3142022-04-20 17:09:38 +02003985 output_length = 0;
3986 length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003987 PSA_ASSERT(psa_cipher_update(&operation,
3988 ciphertext->x, ciphertext->len,
3989 output, output_buffer_size,
3990 &length));
3991 TEST_LE_U(length, output_buffer_size);
Gilles Peskine286c3142022-04-20 17:09:38 +02003992 output_length += length;
Gilles Peskine449bd832023-01-11 14:50:10 +01003993 PSA_ASSERT(psa_cipher_finish(&operation,
3994 mbedtls_buffer_offset(output, output_length),
3995 output_buffer_size - output_length,
3996 &length));
Gilles Peskine286c3142022-04-20 17:09:38 +02003997 output_length += length;
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01003998 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01003999 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004000
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004001 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004002 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004003 PSA_ASSERT(psa_cipher_encrypt(key, alg, plaintext->x, plaintext->len,
4004 output, output_buffer_size,
4005 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004006 TEST_MEMORY_COMPARE(ciphertext->x, ciphertext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004007 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004008
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004009 /* One-shot decryption */
4010 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004011 PSA_ASSERT(psa_cipher_decrypt(key, alg, ciphertext->x, ciphertext->len,
4012 output, output_buffer_size,
4013 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004014 TEST_MEMORY_COMPARE(plaintext->x, plaintext->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004015 output, output_length);
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004016
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004017exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004018 PSA_ASSERT(psa_cipher_abort(&operation));
4019 mbedtls_free(output);
4020 psa_cipher_abort(&operation);
4021 psa_destroy_key(key);
4022 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004023}
4024/* END_CASE */
4025
4026/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004027void cipher_bad_key(int alg_arg, int key_type_arg, data_t *key_data)
Paul Elliotta417f562021-07-14 12:31:21 +01004028{
4029 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4030 psa_algorithm_t alg = alg_arg;
4031 psa_key_type_t key_type = key_type_arg;
4032 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4033 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4034 psa_status_t status;
4035
Gilles Peskine449bd832023-01-11 14:50:10 +01004036 PSA_ASSERT(psa_crypto_init());
Paul Elliotta417f562021-07-14 12:31:21 +01004037
Gilles Peskine449bd832023-01-11 14:50:10 +01004038 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4039 psa_set_key_algorithm(&attributes, alg);
4040 psa_set_key_type(&attributes, key_type);
Paul Elliotta417f562021-07-14 12:31:21 +01004041
4042 /* Usage of either of these two size macros would cause divide by zero
4043 * with incorrect key types previously. Input length should be irrelevant
4044 * here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 TEST_EQUAL(PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, 16),
4046 0);
4047 TEST_EQUAL(PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, 16), 0);
Paul Elliotta417f562021-07-14 12:31:21 +01004048
4049
Gilles Peskine449bd832023-01-11 14:50:10 +01004050 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4051 &key));
Paul Elliotta417f562021-07-14 12:31:21 +01004052
4053 /* Should fail due to invalid alg type (to support invalid key type).
4054 * Encrypt or decrypt will end up in the same place. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 status = psa_cipher_encrypt_setup(&operation, key, alg);
Paul Elliotta417f562021-07-14 12:31:21 +01004056
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 TEST_EQUAL(status, PSA_ERROR_INVALID_ARGUMENT);
Paul Elliotta417f562021-07-14 12:31:21 +01004058
4059exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004060 psa_cipher_abort(&operation);
4061 psa_destroy_key(key);
4062 PSA_DONE();
Paul Elliotta417f562021-07-14 12:31:21 +01004063}
4064/* END_CASE */
4065
4066/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004067void cipher_encrypt_validation(int alg_arg,
4068 int key_type_arg,
4069 data_t *key_data,
4070 data_t *input)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004071{
4072 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4073 psa_key_type_t key_type = key_type_arg;
4074 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004075 size_t iv_size = PSA_CIPHER_IV_LENGTH(key_type, alg);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004076 unsigned char *output1 = NULL;
4077 size_t output1_buffer_size = 0;
4078 size_t output1_length = 0;
4079 unsigned char *output2 = NULL;
4080 size_t output2_buffer_size = 0;
4081 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004082 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004083 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004084 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004085
Gilles Peskine449bd832023-01-11 14:50:10 +01004086 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004087
Gilles Peskine449bd832023-01-11 14:50:10 +01004088 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4089 psa_set_key_algorithm(&attributes, alg);
4090 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004091
Gilles Peskine449bd832023-01-11 14:50:10 +01004092 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4093 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4094 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004095 TEST_CALLOC(output1, output1_buffer_size);
4096 TEST_CALLOC(output2, output2_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004097
Gilles Peskine449bd832023-01-11 14:50:10 +01004098 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4099 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004100
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004101 /* The one-shot cipher encryption uses generated iv so validating
4102 the output is not possible. Validating with multipart encryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004103 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len, output1,
4104 output1_buffer_size, &output1_length));
4105 TEST_LE_U(output1_length,
4106 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4107 TEST_LE_U(output1_length,
4108 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004109
Gilles Peskine449bd832023-01-11 14:50:10 +01004110 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
4111 PSA_ASSERT(psa_cipher_set_iv(&operation, output1, iv_size));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004112
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 PSA_ASSERT(psa_cipher_update(&operation,
4114 input->x, input->len,
4115 output2, output2_buffer_size,
4116 &function_output_length));
4117 TEST_LE_U(function_output_length,
4118 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len));
4119 TEST_LE_U(function_output_length,
4120 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004121 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004122
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 PSA_ASSERT(psa_cipher_finish(&operation,
4124 output2 + output2_length,
4125 output2_buffer_size - output2_length,
4126 &function_output_length));
4127 TEST_LE_U(function_output_length,
4128 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4129 TEST_LE_U(function_output_length,
4130 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004131 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004132
Gilles Peskine449bd832023-01-11 14:50:10 +01004133 PSA_ASSERT(psa_cipher_abort(&operation));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004134 TEST_MEMORY_COMPARE(output1 + iv_size, output1_length - iv_size,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004135 output2, output2_length);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004136
Gilles Peskine50e586b2018-06-08 14:28:46 +02004137exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004138 psa_cipher_abort(&operation);
4139 mbedtls_free(output1);
4140 mbedtls_free(output2);
4141 psa_destroy_key(key);
4142 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004143}
4144/* END_CASE */
4145
4146/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004147void cipher_encrypt_multipart(int alg_arg, int key_type_arg,
4148 data_t *key_data, data_t *iv,
4149 data_t *input,
4150 int first_part_size_arg,
4151 int output1_length_arg, int output2_length_arg,
4152 data_t *expected_output,
4153 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004154{
Ronald Cron5425a212020-08-04 14:58:35 +02004155 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004156 psa_key_type_t key_type = key_type_arg;
4157 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004158 psa_status_t status;
4159 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004160 size_t first_part_size = first_part_size_arg;
4161 size_t output1_length = output1_length_arg;
4162 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004163 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004164 size_t output_buffer_size = 0;
4165 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004166 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004167 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004168 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004169
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004171
Gilles Peskine449bd832023-01-11 14:50:10 +01004172 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4173 psa_set_key_algorithm(&attributes, alg);
4174 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004175
Gilles Peskine449bd832023-01-11 14:50:10 +01004176 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4177 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004178
Gilles Peskine449bd832023-01-11 14:50:10 +01004179 PSA_ASSERT(psa_cipher_encrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004180
Gilles Peskine449bd832023-01-11 14:50:10 +01004181 if (iv->len > 0) {
4182 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004183 }
4184
Gilles Peskine449bd832023-01-11 14:50:10 +01004185 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4186 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004187 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004188
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 TEST_LE_U(first_part_size, input->len);
4190 PSA_ASSERT(psa_cipher_update(&operation, input->x, first_part_size,
4191 output, output_buffer_size,
4192 &function_output_length));
4193 TEST_ASSERT(function_output_length == output1_length);
4194 TEST_LE_U(function_output_length,
4195 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4196 TEST_LE_U(function_output_length,
4197 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004198 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004199
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 if (first_part_size < input->len) {
4201 PSA_ASSERT(psa_cipher_update(&operation,
4202 input->x + first_part_size,
4203 input->len - first_part_size,
4204 (output_buffer_size == 0 ? NULL :
4205 output + total_output_length),
4206 output_buffer_size - total_output_length,
4207 &function_output_length));
4208 TEST_ASSERT(function_output_length == output2_length);
4209 TEST_LE_U(function_output_length,
4210 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4211 alg,
4212 input->len - first_part_size));
4213 TEST_LE_U(function_output_length,
4214 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004215 total_output_length += function_output_length;
4216 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004217
Gilles Peskine449bd832023-01-11 14:50:10 +01004218 status = psa_cipher_finish(&operation,
4219 (output_buffer_size == 0 ? NULL :
4220 output + total_output_length),
4221 output_buffer_size - total_output_length,
4222 &function_output_length);
4223 TEST_LE_U(function_output_length,
4224 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4225 TEST_LE_U(function_output_length,
4226 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004227 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004228 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004229
Gilles Peskine449bd832023-01-11 14:50:10 +01004230 if (expected_status == PSA_SUCCESS) {
4231 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004232
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004233 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004234 output, total_output_length);
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004235 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004236
4237exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004238 psa_cipher_abort(&operation);
4239 mbedtls_free(output);
4240 psa_destroy_key(key);
4241 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004242}
4243/* END_CASE */
4244
4245/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004246void cipher_decrypt_multipart(int alg_arg, int key_type_arg,
4247 data_t *key_data, data_t *iv,
4248 data_t *input,
4249 int first_part_size_arg,
4250 int output1_length_arg, int output2_length_arg,
4251 data_t *expected_output,
4252 int expected_status_arg)
Gilles Peskine50e586b2018-06-08 14:28:46 +02004253{
Ronald Cron5425a212020-08-04 14:58:35 +02004254 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004255 psa_key_type_t key_type = key_type_arg;
4256 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004257 psa_status_t status;
4258 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004259 size_t first_part_size = first_part_size_arg;
4260 size_t output1_length = output1_length_arg;
4261 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004262 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004263 size_t output_buffer_size = 0;
4264 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004265 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004266 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004267 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004268
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 PSA_ASSERT(psa_crypto_init());
Gilles Peskine50e586b2018-06-08 14:28:46 +02004270
Gilles Peskine449bd832023-01-11 14:50:10 +01004271 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4272 psa_set_key_algorithm(&attributes, alg);
4273 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004274
Gilles Peskine449bd832023-01-11 14:50:10 +01004275 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4276 &key));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004277
Gilles Peskine449bd832023-01-11 14:50:10 +01004278 PSA_ASSERT(psa_cipher_decrypt_setup(&operation, key, alg));
Gilles Peskine50e586b2018-06-08 14:28:46 +02004279
Gilles Peskine449bd832023-01-11 14:50:10 +01004280 if (iv->len > 0) {
4281 PSA_ASSERT(psa_cipher_set_iv(&operation, iv->x, iv->len));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004282 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004283
Gilles Peskine449bd832023-01-11 14:50:10 +01004284 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input->len) +
4285 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004286 TEST_CALLOC(output, output_buffer_size);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004287
Gilles Peskine449bd832023-01-11 14:50:10 +01004288 TEST_LE_U(first_part_size, input->len);
4289 PSA_ASSERT(psa_cipher_update(&operation,
4290 input->x, first_part_size,
4291 output, output_buffer_size,
4292 &function_output_length));
4293 TEST_ASSERT(function_output_length == output1_length);
4294 TEST_LE_U(function_output_length,
4295 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4296 TEST_LE_U(function_output_length,
4297 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004298 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004299
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 if (first_part_size < input->len) {
4301 PSA_ASSERT(psa_cipher_update(&operation,
4302 input->x + first_part_size,
4303 input->len - first_part_size,
4304 (output_buffer_size == 0 ? NULL :
4305 output + total_output_length),
4306 output_buffer_size - total_output_length,
4307 &function_output_length));
4308 TEST_ASSERT(function_output_length == output2_length);
4309 TEST_LE_U(function_output_length,
4310 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4311 alg,
4312 input->len - first_part_size));
4313 TEST_LE_U(function_output_length,
4314 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004315 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004316 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004317
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 status = psa_cipher_finish(&operation,
4319 (output_buffer_size == 0 ? NULL :
4320 output + total_output_length),
4321 output_buffer_size - total_output_length,
4322 &function_output_length);
4323 TEST_LE_U(function_output_length,
4324 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4325 TEST_LE_U(function_output_length,
4326 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004327 total_output_length += function_output_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 TEST_EQUAL(status, expected_status);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004329
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 if (expected_status == PSA_SUCCESS) {
4331 PSA_ASSERT(psa_cipher_abort(&operation));
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004332
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004333 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004334 output, total_output_length);
Gilles Peskine50e586b2018-06-08 14:28:46 +02004335 }
4336
Gilles Peskine50e586b2018-06-08 14:28:46 +02004337exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004338 psa_cipher_abort(&operation);
4339 mbedtls_free(output);
4340 psa_destroy_key(key);
4341 PSA_DONE();
Gilles Peskine50e586b2018-06-08 14:28:46 +02004342}
4343/* END_CASE */
4344
Gilles Peskine50e586b2018-06-08 14:28:46 +02004345/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004346void cipher_decrypt_fail(int alg_arg,
4347 int key_type_arg,
4348 data_t *key_data,
4349 data_t *iv,
4350 data_t *input_arg,
4351 int expected_status_arg)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004352{
4353 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4354 psa_status_t status;
4355 psa_key_type_t key_type = key_type_arg;
4356 psa_algorithm_t alg = alg_arg;
4357 psa_status_t expected_status = expected_status_arg;
4358 unsigned char *input = NULL;
4359 size_t input_buffer_size = 0;
4360 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004361 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004362 size_t output_buffer_size = 0;
4363 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004364 size_t function_output_length;
4365 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004366 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4367
Gilles Peskine449bd832023-01-11 14:50:10 +01004368 if (PSA_ERROR_BAD_STATE != expected_status) {
4369 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004370
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4372 psa_set_key_algorithm(&attributes, alg);
4373 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004374
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4376 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004377 }
4378
4379 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004380 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4381 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004382 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004383 memcpy(input, iv->x, iv->len);
4384 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004385 }
4386
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004388 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004389
Neil Armstrong66a479f2022-02-07 15:41:19 +01004390 /* Decrypt, one-short */
Gilles Peskine449bd832023-01-11 14:50:10 +01004391 status = psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4392 output_buffer_size, &output_length);
4393 TEST_EQUAL(status, expected_status);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004394
Neil Armstrong66a479f2022-02-07 15:41:19 +01004395 /* Decrypt, multi-part */
Gilles Peskine449bd832023-01-11 14:50:10 +01004396 status = psa_cipher_decrypt_setup(&operation, key, alg);
4397 if (status == PSA_SUCCESS) {
4398 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg,
4399 input_arg->len) +
4400 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004401 TEST_CALLOC(output_multi, output_buffer_size);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004402
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 if (iv->len > 0) {
4404 status = psa_cipher_set_iv(&operation, iv->x, iv->len);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004405
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 if (status != PSA_SUCCESS) {
4407 TEST_EQUAL(status, expected_status);
4408 }
Neil Armstrong66a479f2022-02-07 15:41:19 +01004409 }
4410
Gilles Peskine449bd832023-01-11 14:50:10 +01004411 if (status == PSA_SUCCESS) {
4412 status = psa_cipher_update(&operation,
4413 input_arg->x, input_arg->len,
4414 output_multi, output_buffer_size,
4415 &function_output_length);
4416 if (status == PSA_SUCCESS) {
Neil Armstrong66a479f2022-02-07 15:41:19 +01004417 output_length = function_output_length;
4418
Gilles Peskine449bd832023-01-11 14:50:10 +01004419 status = psa_cipher_finish(&operation,
4420 output_multi + output_length,
4421 output_buffer_size - output_length,
4422 &function_output_length);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004423
Gilles Peskine449bd832023-01-11 14:50:10 +01004424 TEST_EQUAL(status, expected_status);
4425 } else {
4426 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004427 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004428 } else {
4429 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004430 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004431 } else {
4432 TEST_EQUAL(status, expected_status);
Neil Armstrong66a479f2022-02-07 15:41:19 +01004433 }
4434
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004435exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004436 psa_cipher_abort(&operation);
4437 mbedtls_free(input);
4438 mbedtls_free(output);
4439 mbedtls_free(output_multi);
4440 psa_destroy_key(key);
4441 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004442}
4443/* END_CASE */
4444
4445/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004446void cipher_decrypt(int alg_arg,
4447 int key_type_arg,
4448 data_t *key_data,
4449 data_t *iv,
4450 data_t *input_arg,
4451 data_t *expected_output)
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004452{
4453 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4454 psa_key_type_t key_type = key_type_arg;
4455 psa_algorithm_t alg = alg_arg;
4456 unsigned char *input = NULL;
4457 size_t input_buffer_size = 0;
4458 unsigned char *output = NULL;
4459 size_t output_buffer_size = 0;
4460 size_t output_length = 0;
4461 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4462
Gilles Peskine449bd832023-01-11 14:50:10 +01004463 PSA_ASSERT(psa_crypto_init());
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004464
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4466 psa_set_key_algorithm(&attributes, alg);
4467 psa_set_key_type(&attributes, key_type);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004468
4469 /* Allocate input buffer and copy the iv and the plaintext */
Gilles Peskine449bd832023-01-11 14:50:10 +01004470 input_buffer_size = ((size_t) input_arg->len + (size_t) iv->len);
4471 if (input_buffer_size > 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004472 TEST_CALLOC(input, input_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01004473 memcpy(input, iv->x, iv->len);
4474 memcpy(input + iv->len, input_arg->x, input_arg->len);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004475 }
4476
Gilles Peskine449bd832023-01-11 14:50:10 +01004477 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004478 TEST_CALLOC(output, output_buffer_size);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004479
Gilles Peskine449bd832023-01-11 14:50:10 +01004480 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4481 &key));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004482
Gilles Peskine449bd832023-01-11 14:50:10 +01004483 PSA_ASSERT(psa_cipher_decrypt(key, alg, input, input_buffer_size, output,
4484 output_buffer_size, &output_length));
4485 TEST_LE_U(output_length,
4486 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_buffer_size));
4487 TEST_LE_U(output_length,
4488 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_buffer_size));
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004489
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004490 TEST_MEMORY_COMPARE(expected_output->x, expected_output->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004491 output, output_length);
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004492exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 mbedtls_free(input);
4494 mbedtls_free(output);
4495 psa_destroy_key(key);
4496 PSA_DONE();
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004497}
4498/* END_CASE */
4499
4500/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004501void cipher_verify_output(int alg_arg,
4502 int key_type_arg,
4503 data_t *key_data,
4504 data_t *input)
mohammad1603d7d7ba52018-03-12 18:51:53 +02004505{
Ronald Cron5425a212020-08-04 14:58:35 +02004506 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004507 psa_key_type_t key_type = key_type_arg;
4508 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004509 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004510 size_t output1_size = 0;
4511 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004512 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004513 size_t output2_size = 0;
4514 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004515 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004516
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 PSA_ASSERT(psa_crypto_init());
mohammad1603d7d7ba52018-03-12 18:51:53 +02004518
Gilles Peskine449bd832023-01-11 14:50:10 +01004519 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4520 psa_set_key_algorithm(&attributes, alg);
4521 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004522
Gilles Peskine449bd832023-01-11 14:50:10 +01004523 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4524 &key));
4525 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004526 TEST_CALLOC(output1, output1_size);
Moran Pekerded84402018-06-06 16:36:50 +03004527
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 PSA_ASSERT(psa_cipher_encrypt(key, alg, input->x, input->len,
4529 output1, output1_size,
4530 &output1_length));
4531 TEST_LE_U(output1_length,
4532 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len));
4533 TEST_LE_U(output1_length,
4534 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Moran Pekerded84402018-06-06 16:36:50 +03004535
4536 output2_size = output1_length;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004537 TEST_CALLOC(output2, output2_size);
Moran Pekerded84402018-06-06 16:36:50 +03004538
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 PSA_ASSERT(psa_cipher_decrypt(key, alg, output1, output1_length,
4540 output2, output2_size,
4541 &output2_length));
4542 TEST_LE_U(output2_length,
4543 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4544 TEST_LE_U(output2_length,
4545 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Moran Pekerded84402018-06-06 16:36:50 +03004546
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004547 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
Moran Pekerded84402018-06-06 16:36:50 +03004548
4549exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 mbedtls_free(output1);
4551 mbedtls_free(output2);
4552 psa_destroy_key(key);
4553 PSA_DONE();
Moran Pekerded84402018-06-06 16:36:50 +03004554}
4555/* END_CASE */
4556
4557/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004558void cipher_verify_output_multipart(int alg_arg,
4559 int key_type_arg,
4560 data_t *key_data,
4561 data_t *input,
4562 int first_part_size_arg)
Moran Pekerded84402018-06-06 16:36:50 +03004563{
Ronald Cron5425a212020-08-04 14:58:35 +02004564 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004565 psa_key_type_t key_type = key_type_arg;
4566 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004567 size_t first_part_size = first_part_size_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004568 unsigned char iv[16] = { 0 };
Moran Pekerded84402018-06-06 16:36:50 +03004569 size_t iv_size = 16;
4570 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004571 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004572 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004573 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004574 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004575 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004576 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004577 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004578 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4579 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004580 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 PSA_ASSERT(psa_crypto_init());
Moran Pekerded84402018-06-06 16:36:50 +03004583
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4585 psa_set_key_algorithm(&attributes, alg);
4586 psa_set_key_type(&attributes, key_type);
Moran Pekered346952018-07-05 15:22:45 +03004587
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4589 &key));
Moran Pekerded84402018-06-06 16:36:50 +03004590
Gilles Peskine449bd832023-01-11 14:50:10 +01004591 PSA_ASSERT(psa_cipher_encrypt_setup(&operation1, key, alg));
4592 PSA_ASSERT(psa_cipher_decrypt_setup(&operation2, key, alg));
Moran Pekerded84402018-06-06 16:36:50 +03004593
Gilles Peskine449bd832023-01-11 14:50:10 +01004594 if (alg != PSA_ALG_ECB_NO_PADDING) {
4595 PSA_ASSERT(psa_cipher_generate_iv(&operation1,
4596 iv, iv_size,
4597 &iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004598 }
4599
Gilles Peskine449bd832023-01-11 14:50:10 +01004600 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input->len);
4601 TEST_LE_U(output1_buffer_size,
4602 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004603 TEST_CALLOC(output1, output1_buffer_size);
Moran Pekerded84402018-06-06 16:36:50 +03004604
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 TEST_LE_U(first_part_size, input->len);
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004606
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 PSA_ASSERT(psa_cipher_update(&operation1, input->x, first_part_size,
4608 output1, output1_buffer_size,
4609 &function_output_length));
4610 TEST_LE_U(function_output_length,
4611 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4612 TEST_LE_U(function_output_length,
4613 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004614 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004615
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 PSA_ASSERT(psa_cipher_update(&operation1,
4617 input->x + first_part_size,
4618 input->len - first_part_size,
4619 output1, output1_buffer_size,
4620 &function_output_length));
4621 TEST_LE_U(function_output_length,
4622 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4623 alg,
4624 input->len - first_part_size));
4625 TEST_LE_U(function_output_length,
4626 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input->len - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004627 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004628
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 PSA_ASSERT(psa_cipher_finish(&operation1,
4630 output1 + output1_length,
4631 output1_buffer_size - output1_length,
4632 &function_output_length));
4633 TEST_LE_U(function_output_length,
4634 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4635 TEST_LE_U(function_output_length,
4636 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004637 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004638
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 PSA_ASSERT(psa_cipher_abort(&operation1));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004640
Gilles Peskine048b7f02018-06-08 14:20:49 +02004641 output2_buffer_size = output1_length;
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 TEST_LE_U(output2_buffer_size,
4643 PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, output1_length));
4644 TEST_LE_U(output2_buffer_size,
4645 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(output1_length));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004646 TEST_CALLOC(output2, output2_buffer_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004647
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 if (iv_length > 0) {
4649 PSA_ASSERT(psa_cipher_set_iv(&operation2,
4650 iv, iv_length));
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004651 }
Moran Pekerded84402018-06-06 16:36:50 +03004652
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 PSA_ASSERT(psa_cipher_update(&operation2, output1, first_part_size,
4654 output2, output2_buffer_size,
4655 &function_output_length));
4656 TEST_LE_U(function_output_length,
4657 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, first_part_size));
4658 TEST_LE_U(function_output_length,
4659 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004660 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004661
Gilles Peskine449bd832023-01-11 14:50:10 +01004662 PSA_ASSERT(psa_cipher_update(&operation2,
4663 output1 + first_part_size,
4664 output1_length - first_part_size,
4665 output2, output2_buffer_size,
4666 &function_output_length));
4667 TEST_LE_U(function_output_length,
4668 PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type,
4669 alg,
4670 output1_length - first_part_size));
4671 TEST_LE_U(function_output_length,
4672 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(output1_length - first_part_size));
Gilles Peskine048b7f02018-06-08 14:20:49 +02004673 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004674
Gilles Peskine449bd832023-01-11 14:50:10 +01004675 PSA_ASSERT(psa_cipher_finish(&operation2,
4676 output2 + output2_length,
4677 output2_buffer_size - output2_length,
4678 &function_output_length));
4679 TEST_LE_U(function_output_length,
4680 PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg));
4681 TEST_LE_U(function_output_length,
4682 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE);
Gilles Peskine048b7f02018-06-08 14:20:49 +02004683 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004684
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 PSA_ASSERT(psa_cipher_abort(&operation2));
mohammad1603d7d7ba52018-03-12 18:51:53 +02004686
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004687 TEST_MEMORY_COMPARE(input->x, input->len, output2, output2_length);
mohammad1603d7d7ba52018-03-12 18:51:53 +02004688
4689exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 psa_cipher_abort(&operation1);
4691 psa_cipher_abort(&operation2);
4692 mbedtls_free(output1);
4693 mbedtls_free(output2);
4694 psa_destroy_key(key);
4695 PSA_DONE();
mohammad1603d7d7ba52018-03-12 18:51:53 +02004696}
4697/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004698
Gilles Peskine20035e32018-02-03 22:44:14 +01004699/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004700void aead_encrypt_decrypt(int key_type_arg, data_t *key_data,
4701 int alg_arg,
4702 data_t *nonce,
4703 data_t *additional_data,
4704 data_t *input_data,
4705 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004706{
Ronald Cron5425a212020-08-04 14:58:35 +02004707 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004708 psa_key_type_t key_type = key_type_arg;
4709 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004710 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004711 unsigned char *output_data = NULL;
4712 size_t output_size = 0;
4713 size_t output_length = 0;
4714 unsigned char *output_data2 = NULL;
4715 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004716 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004717 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004718 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004719
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004721
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
4723 psa_set_key_algorithm(&attributes, alg);
4724 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004725
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4727 &key));
4728 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4729 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004730
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4732 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004733 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4734 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4736 expected_result != PSA_ERROR_NOT_SUPPORTED) {
4737 TEST_EQUAL(output_size,
4738 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4739 TEST_LE_U(output_size,
4740 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004741 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004742 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004743
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 status = psa_aead_encrypt(key, alg,
4745 nonce->x, nonce->len,
4746 additional_data->x,
4747 additional_data->len,
4748 input_data->x, input_data->len,
4749 output_data, output_size,
4750 &output_length);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004751
4752 /* If the operation is not supported, just skip and not fail in case the
4753 * encryption involves a common limitation of cryptography hardwares and
4754 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 if (status == PSA_ERROR_NOT_SUPPORTED) {
4756 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4757 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremanf49478b2021-02-15 15:19:25 +01004758 }
4759
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004761
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 if (PSA_SUCCESS == expected_result) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004763 TEST_CALLOC(output_data2, output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004764
Gilles Peskine003a4a92019-05-14 16:09:40 +02004765 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4766 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 TEST_EQUAL(input_data->len,
4768 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, output_length));
Gilles Peskine003a4a92019-05-14 16:09:40 +02004769
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 TEST_LE_U(input_data->len,
4771 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(output_length));
gabor-mezei-armceface22021-01-21 12:26:17 +01004772
Gilles Peskine449bd832023-01-11 14:50:10 +01004773 TEST_EQUAL(psa_aead_decrypt(key, alg,
4774 nonce->x, nonce->len,
4775 additional_data->x,
4776 additional_data->len,
4777 output_data, output_length,
4778 output_data2, output_length,
4779 &output_length2),
4780 expected_result);
Gilles Peskine2d277862018-06-18 15:41:12 +02004781
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004782 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004783 output_data2, output_length2);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004784 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004785
Gilles Peskinea1cac842018-06-11 19:33:02 +02004786exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004787 psa_destroy_key(key);
4788 mbedtls_free(output_data);
4789 mbedtls_free(output_data2);
4790 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004791}
4792/* END_CASE */
4793
4794/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004795void aead_encrypt(int key_type_arg, data_t *key_data,
4796 int alg_arg,
4797 data_t *nonce,
4798 data_t *additional_data,
4799 data_t *input_data,
4800 data_t *expected_result)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004801{
Ronald Cron5425a212020-08-04 14:58:35 +02004802 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004803 psa_key_type_t key_type = key_type_arg;
4804 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004805 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004806 unsigned char *output_data = NULL;
4807 size_t output_size = 0;
4808 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004809 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004810 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004811
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004813
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
4815 psa_set_key_algorithm(&attributes, alg);
4816 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004817
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4819 &key));
4820 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4821 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004822
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 output_size = input_data->len + PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4824 alg);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004825 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4826 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 TEST_EQUAL(output_size,
4828 PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4829 TEST_LE_U(output_size,
4830 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(input_data->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004831 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004832
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 status = psa_aead_encrypt(key, alg,
4834 nonce->x, nonce->len,
4835 additional_data->x, additional_data->len,
4836 input_data->x, input_data->len,
4837 output_data, output_size,
4838 &output_length);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004839
Ronald Cron28a45ed2021-02-09 20:35:42 +01004840 /* If the operation is not supported, just skip and not fail in case the
4841 * encryption involves a common limitation of cryptography hardwares and
4842 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004843 if (status == PSA_ERROR_NOT_SUPPORTED) {
4844 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4845 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004846 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004847
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 PSA_ASSERT(status);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004849 TEST_MEMORY_COMPARE(expected_result->x, expected_result->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004850 output_data, output_length);
Gilles Peskine2d277862018-06-18 15:41:12 +02004851
Gilles Peskinea1cac842018-06-11 19:33:02 +02004852exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004853 psa_destroy_key(key);
4854 mbedtls_free(output_data);
4855 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004856}
4857/* END_CASE */
4858
4859/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004860void aead_decrypt(int key_type_arg, data_t *key_data,
4861 int alg_arg,
4862 data_t *nonce,
4863 data_t *additional_data,
4864 data_t *input_data,
4865 data_t *expected_data,
4866 int expected_result_arg)
Gilles Peskinea1cac842018-06-11 19:33:02 +02004867{
Ronald Cron5425a212020-08-04 14:58:35 +02004868 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004869 psa_key_type_t key_type = key_type_arg;
4870 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004871 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004872 unsigned char *output_data = NULL;
4873 size_t output_size = 0;
4874 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004875 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004876 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004877 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004878
Gilles Peskine449bd832023-01-11 14:50:10 +01004879 PSA_ASSERT(psa_crypto_init());
Gilles Peskinea1cac842018-06-11 19:33:02 +02004880
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
4882 psa_set_key_algorithm(&attributes, alg);
4883 psa_set_key_type(&attributes, key_type);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004884
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
4886 &key));
4887 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
4888 key_bits = psa_get_key_bits(&attributes);
Bence Szépkútiec174e22021-03-19 18:46:15 +01004889
Gilles Peskine449bd832023-01-11 14:50:10 +01004890 output_size = input_data->len - PSA_AEAD_TAG_LENGTH(key_type, key_bits,
4891 alg);
4892 if (expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4893 expected_result != PSA_ERROR_NOT_SUPPORTED) {
Bence Szépkútiec174e22021-03-19 18:46:15 +01004894 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4895 * should be exact. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 TEST_EQUAL(output_size,
4897 PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, input_data->len));
4898 TEST_LE_U(output_size,
4899 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(input_data->len));
Bence Szépkútiec174e22021-03-19 18:46:15 +01004900 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01004901 TEST_CALLOC(output_data, output_size);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004902
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 status = psa_aead_decrypt(key, alg,
4904 nonce->x, nonce->len,
4905 additional_data->x,
4906 additional_data->len,
4907 input_data->x, input_data->len,
4908 output_data, output_size,
4909 &output_length);
Steven Cooremand588ea12021-01-11 19:36:04 +01004910
Ronald Cron28a45ed2021-02-09 20:35:42 +01004911 /* If the operation is not supported, just skip and not fail in case the
4912 * decryption involves a common limitation of cryptography hardwares and
4913 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004914 if (status == PSA_ERROR_NOT_SUPPORTED) {
4915 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
4916 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Steven Cooremand588ea12021-01-11 19:36:04 +01004917 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004918
Gilles Peskine449bd832023-01-11 14:50:10 +01004919 TEST_EQUAL(status, expected_result);
Gilles Peskinea1cac842018-06-11 19:33:02 +02004920
Gilles Peskine449bd832023-01-11 14:50:10 +01004921 if (expected_result == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01004922 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01004923 output_data, output_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 }
Gilles Peskinea1cac842018-06-11 19:33:02 +02004925
Gilles Peskinea1cac842018-06-11 19:33:02 +02004926exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 psa_destroy_key(key);
4928 mbedtls_free(output_data);
4929 PSA_DONE();
Gilles Peskinea1cac842018-06-11 19:33:02 +02004930}
4931/* END_CASE */
4932
4933/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004934void aead_multipart_encrypt(int key_type_arg, data_t *key_data,
4935 int alg_arg,
4936 data_t *nonce,
4937 data_t *additional_data,
4938 data_t *input_data,
4939 int do_set_lengths,
4940 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01004941{
Paul Elliottd3f82412021-06-16 16:52:21 +01004942 size_t ad_part_len = 0;
4943 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004944 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004945
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
4947 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01004948
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 if (do_set_lengths) {
4950 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004951 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004952 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004953 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01004954 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004955 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004956
4957 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004958 if (!aead_multipart_internal_func(key_type_arg, key_data,
4959 alg_arg, nonce,
4960 additional_data,
4961 ad_part_len,
4962 input_data, -1,
4963 set_lengths_method,
4964 expected_output,
4965 1, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004966 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004967 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004968
Gilles Peskine449bd832023-01-11 14:50:10 +01004969 /* length(0) part, length(ad_part_len) part, length(0) part... */
4970 mbedtls_test_set_step(1000 + ad_part_len);
4971
4972 if (!aead_multipart_internal_func(key_type_arg, key_data,
4973 alg_arg, nonce,
4974 additional_data,
4975 ad_part_len,
4976 input_data, -1,
4977 set_lengths_method,
4978 expected_output,
4979 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004980 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004981 }
4982 }
4983
4984 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
4985 /* Split data into length(data_part_len) parts. */
4986 mbedtls_test_set_step(2000 + data_part_len);
4987
4988 if (do_set_lengths) {
4989 if (data_part_len & 0x01) {
4990 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4991 } else {
4992 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
4993 }
4994 }
4995
4996 if (!aead_multipart_internal_func(key_type_arg, key_data,
4997 alg_arg, nonce,
4998 additional_data, -1,
4999 input_data, data_part_len,
5000 set_lengths_method,
5001 expected_output,
5002 1, 0)) {
5003 break;
5004 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005005
5006 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005007 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005008
Gilles Peskine449bd832023-01-11 14:50:10 +01005009 if (!aead_multipart_internal_func(key_type_arg, key_data,
5010 alg_arg, nonce,
5011 additional_data, -1,
5012 input_data, data_part_len,
5013 set_lengths_method,
5014 expected_output,
5015 1, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005016 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005017 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005018 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005019
Paul Elliott8fc45162021-06-23 16:06:01 +01005020 /* Goto is required to silence warnings about unused labels, as we
5021 * don't actually do any test assertions in this function. */
5022 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005023}
5024/* END_CASE */
5025
5026/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005027void aead_multipart_decrypt(int key_type_arg, data_t *key_data,
5028 int alg_arg,
5029 data_t *nonce,
5030 data_t *additional_data,
5031 data_t *input_data,
5032 int do_set_lengths,
5033 data_t *expected_output)
Paul Elliott0023e0a2021-04-27 10:06:22 +01005034{
Paul Elliottd3f82412021-06-16 16:52:21 +01005035 size_t ad_part_len = 0;
5036 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005037 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005038
Gilles Peskine449bd832023-01-11 14:50:10 +01005039 for (ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005040 /* Split ad into length(ad_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005041 mbedtls_test_set_step(ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005042
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 if (do_set_lengths) {
5044 if (ad_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005045 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005047 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005049 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005050
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 if (!aead_multipart_internal_func(key_type_arg, key_data,
5052 alg_arg, nonce,
5053 additional_data,
5054 ad_part_len,
5055 input_data, -1,
5056 set_lengths_method,
5057 expected_output,
5058 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005059 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005061
5062 /* length(0) part, length(ad_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005063 mbedtls_test_set_step(1000 + ad_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005064
Gilles Peskine449bd832023-01-11 14:50:10 +01005065 if (!aead_multipart_internal_func(key_type_arg, key_data,
5066 alg_arg, nonce,
5067 additional_data,
5068 ad_part_len,
5069 input_data, -1,
5070 set_lengths_method,
5071 expected_output,
5072 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005073 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005075 }
5076
Gilles Peskine449bd832023-01-11 14:50:10 +01005077 for (data_part_len = 1; data_part_len <= input_data->len; data_part_len++) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005078 /* Split data into length(data_part_len) parts. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005079 mbedtls_test_set_step(2000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005080
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 if (do_set_lengths) {
5082 if (data_part_len & 0x01) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005083 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 } else {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005085 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005087 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005088
Gilles Peskine449bd832023-01-11 14:50:10 +01005089 if (!aead_multipart_internal_func(key_type_arg, key_data,
5090 alg_arg, nonce,
5091 additional_data, -1,
5092 input_data, data_part_len,
5093 set_lengths_method,
5094 expected_output,
5095 0, 0)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005096 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005097 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005098
5099 /* length(0) part, length(data_part_len) part, length(0) part... */
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 mbedtls_test_set_step(3000 + data_part_len);
Paul Elliott32f46ba2021-09-23 18:24:36 +01005101
Gilles Peskine449bd832023-01-11 14:50:10 +01005102 if (!aead_multipart_internal_func(key_type_arg, key_data,
5103 alg_arg, nonce,
5104 additional_data, -1,
5105 input_data, data_part_len,
5106 set_lengths_method,
5107 expected_output,
5108 0, 1)) {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005109 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005110 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005111 }
5112
Paul Elliott8fc45162021-06-23 16:06:01 +01005113 /* Goto is required to silence warnings about unused labels, as we
5114 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005115 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005116}
5117/* END_CASE */
5118
5119/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005120void aead_multipart_generate_nonce(int key_type_arg, data_t *key_data,
5121 int alg_arg,
5122 int nonce_length,
5123 int expected_nonce_length_arg,
5124 data_t *additional_data,
5125 data_t *input_data,
5126 int expected_status_arg)
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005127{
5128
5129 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5130 psa_key_type_t key_type = key_type_arg;
5131 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005132 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005133 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5134 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5135 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005136 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005137 size_t actual_nonce_length = 0;
5138 size_t expected_nonce_length = expected_nonce_length_arg;
5139 unsigned char *output = NULL;
5140 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005141 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005142 size_t ciphertext_size = 0;
5143 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005144 size_t tag_length = 0;
5145 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005146
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 PSA_ASSERT(psa_crypto_init());
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005148
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5150 psa_set_key_algorithm(&attributes, alg);
5151 psa_set_key_type(&attributes, key_type);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005152
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5154 &key));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005155
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005157
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005159
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005160 TEST_CALLOC(output, output_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005161
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005163
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005165
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005166 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005167
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005169
5170 /* If the operation is not supported, just skip and not fail in case the
5171 * encryption involves a common limitation of cryptography hardwares and
5172 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 if (status == PSA_ERROR_NOT_SUPPORTED) {
5174 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5175 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005176 }
5177
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 PSA_ASSERT(status);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005179
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 status = psa_aead_generate_nonce(&operation, nonce_buffer,
5181 nonce_length,
5182 &actual_nonce_length);
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005183
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 TEST_EQUAL(status, expected_status);
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005185
Gilles Peskine449bd832023-01-11 14:50:10 +01005186 TEST_EQUAL(actual_nonce_length, expected_nonce_length);
Paul Elliottd85f5472021-07-16 18:20:16 +01005187
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 if (expected_status == PSA_SUCCESS) {
5189 TEST_EQUAL(actual_nonce_length, PSA_AEAD_NONCE_LENGTH(key_type,
5190 alg));
5191 }
Paul Elliott88ecbe12021-09-22 17:23:03 +01005192
Gilles Peskine449bd832023-01-11 14:50:10 +01005193 TEST_LE_U(actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE);
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005194
Gilles Peskine449bd832023-01-11 14:50:10 +01005195 if (expected_status == PSA_SUCCESS) {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005196 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005197 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5198 input_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005199
Gilles Peskine449bd832023-01-11 14:50:10 +01005200 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5201 additional_data->len));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005202
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5204 output, output_size,
5205 &ciphertext_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005206
Gilles Peskine449bd832023-01-11 14:50:10 +01005207 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5208 &ciphertext_length, tag_buffer,
5209 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005210 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005211
5212exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 psa_destroy_key(key);
5214 mbedtls_free(output);
5215 mbedtls_free(ciphertext);
5216 psa_aead_abort(&operation);
5217 PSA_DONE();
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005218}
5219/* END_CASE */
5220
5221/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005222void aead_multipart_set_nonce(int key_type_arg, data_t *key_data,
5223 int alg_arg,
5224 int nonce_length_arg,
5225 int set_lengths_method_arg,
5226 data_t *additional_data,
5227 data_t *input_data,
5228 int expected_status_arg)
Paul Elliott863864a2021-07-23 17:28:31 +01005229{
5230
5231 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5232 psa_key_type_t key_type = key_type_arg;
5233 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005234 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005235 uint8_t *nonce_buffer = NULL;
5236 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5237 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5238 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005239 unsigned char *output = NULL;
5240 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005241 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005242 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005243 size_t ciphertext_size = 0;
5244 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005245 size_t tag_length = 0;
5246 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005247 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005248 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005249
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 PSA_ASSERT(psa_crypto_init());
Paul Elliott863864a2021-07-23 17:28:31 +01005251
Gilles Peskine449bd832023-01-11 14:50:10 +01005252 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5253 psa_set_key_algorithm(&attributes, alg);
5254 psa_set_key_type(&attributes, key_type);
Paul Elliott863864a2021-07-23 17:28:31 +01005255
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5257 &key));
Paul Elliott863864a2021-07-23 17:28:31 +01005258
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott863864a2021-07-23 17:28:31 +01005260
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott863864a2021-07-23 17:28:31 +01005262
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005263 TEST_CALLOC(output, output_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005264
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005266
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 TEST_LE_U(ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliott863864a2021-07-23 17:28:31 +01005268
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005269 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott863864a2021-07-23 17:28:31 +01005270
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott863864a2021-07-23 17:28:31 +01005272
5273 /* If the operation is not supported, just skip and not fail in case the
5274 * encryption involves a common limitation of cryptography hardwares and
5275 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 if (status == PSA_ERROR_NOT_SUPPORTED) {
5277 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5278 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce_length_arg);
Paul Elliott863864a2021-07-23 17:28:31 +01005279 }
5280
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 PSA_ASSERT(status);
Paul Elliott863864a2021-07-23 17:28:31 +01005282
Paul Elliott4023ffd2021-09-10 16:21:22 +01005283 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 if (nonce_length_arg == -1) {
5285 /* Arbitrary size buffer, to test zero length valid buffer. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005286 TEST_CALLOC(nonce_buffer, 4);
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 nonce_length = 0;
5288 } else {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005289 /* If length is zero, then this will return NULL. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005290 nonce_length = (size_t) nonce_length_arg;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005291 TEST_CALLOC(nonce_buffer, nonce_length);
Paul Elliott66696b52021-08-16 18:42:41 +01005292
Gilles Peskine449bd832023-01-11 14:50:10 +01005293 if (nonce_buffer) {
5294 for (index = 0; index < nonce_length - 1; ++index) {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005295 nonce_buffer[index] = 'a' + index;
5296 }
Paul Elliott66696b52021-08-16 18:42:41 +01005297 }
Paul Elliott863864a2021-07-23 17:28:31 +01005298 }
5299
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 if (set_lengths_method == SET_LENGTHS_BEFORE_NONCE) {
5301 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5302 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005303 }
5304
Gilles Peskine449bd832023-01-11 14:50:10 +01005305 status = psa_aead_set_nonce(&operation, nonce_buffer, nonce_length);
Paul Elliott863864a2021-07-23 17:28:31 +01005306
Gilles Peskine449bd832023-01-11 14:50:10 +01005307 TEST_EQUAL(status, expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005308
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 if (expected_status == PSA_SUCCESS) {
5310 if (set_lengths_method == SET_LENGTHS_AFTER_NONCE) {
5311 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5312 input_data->len));
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005313 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005314 if (operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS) {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005315 expected_status = PSA_ERROR_BAD_STATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 }
Paul Elliott863864a2021-07-23 17:28:31 +01005317
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005318 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5320 additional_data->len),
5321 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005322
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 TEST_EQUAL(psa_aead_update(&operation, input_data->x, input_data->len,
5324 output, output_size,
5325 &ciphertext_length),
5326 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005327
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 TEST_EQUAL(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5329 &ciphertext_length, tag_buffer,
5330 PSA_AEAD_TAG_MAX_SIZE, &tag_length),
5331 expected_status);
Paul Elliott863864a2021-07-23 17:28:31 +01005332 }
5333
5334exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 psa_destroy_key(key);
5336 mbedtls_free(output);
5337 mbedtls_free(ciphertext);
5338 mbedtls_free(nonce_buffer);
5339 psa_aead_abort(&operation);
5340 PSA_DONE();
Paul Elliott863864a2021-07-23 17:28:31 +01005341}
5342/* END_CASE */
5343
5344/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005345void aead_multipart_update_buffer_test(int key_type_arg, data_t *key_data,
Paul Elliott43fbda62021-07-23 18:30:59 +01005346 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005347 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005348 data_t *nonce,
5349 data_t *additional_data,
5350 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 int expected_status_arg)
Paul Elliott43fbda62021-07-23 18:30:59 +01005352{
5353
5354 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5355 psa_key_type_t key_type = key_type_arg;
5356 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005357 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005358 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5359 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5360 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005361 unsigned char *output = NULL;
5362 unsigned char *ciphertext = NULL;
5363 size_t output_size = output_size_arg;
5364 size_t ciphertext_size = 0;
5365 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005366 size_t tag_length = 0;
5367 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5368
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 PSA_ASSERT(psa_crypto_init());
Paul Elliott43fbda62021-07-23 18:30:59 +01005370
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5372 psa_set_key_algorithm(&attributes, alg);
5373 psa_set_key_type(&attributes, key_type);
Paul Elliott43fbda62021-07-23 18:30:59 +01005374
Gilles Peskine449bd832023-01-11 14:50:10 +01005375 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5376 &key));
Paul Elliott43fbda62021-07-23 18:30:59 +01005377
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott43fbda62021-07-23 18:30:59 +01005379
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005380 TEST_CALLOC(output, output_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005381
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005383
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005384 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott43fbda62021-07-23 18:30:59 +01005385
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott43fbda62021-07-23 18:30:59 +01005387
5388 /* If the operation is not supported, just skip and not fail in case the
5389 * encryption involves a common limitation of cryptography hardwares and
5390 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 if (status == PSA_ERROR_NOT_SUPPORTED) {
5392 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5393 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott43fbda62021-07-23 18:30:59 +01005394 }
5395
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 PSA_ASSERT(status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005397
Gilles Peskine449bd832023-01-11 14:50:10 +01005398 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5399 input_data->len));
Paul Elliott47b9a142021-10-07 15:04:57 +01005400
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005402
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5404 additional_data->len));
Paul Elliott43fbda62021-07-23 18:30:59 +01005405
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 status = psa_aead_update(&operation, input_data->x, input_data->len,
5407 output, output_size, &ciphertext_length);
Paul Elliott43fbda62021-07-23 18:30:59 +01005408
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 TEST_EQUAL(status, expected_status);
Paul Elliott43fbda62021-07-23 18:30:59 +01005410
Gilles Peskine449bd832023-01-11 14:50:10 +01005411 if (expected_status == PSA_SUCCESS) {
Paul Elliott43fbda62021-07-23 18:30:59 +01005412 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005413 PSA_ASSERT(psa_aead_finish(&operation, ciphertext, ciphertext_size,
5414 &ciphertext_length, tag_buffer,
5415 PSA_AEAD_TAG_MAX_SIZE, &tag_length));
Paul Elliott43fbda62021-07-23 18:30:59 +01005416 }
5417
5418exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 psa_destroy_key(key);
5420 mbedtls_free(output);
5421 mbedtls_free(ciphertext);
5422 psa_aead_abort(&operation);
5423 PSA_DONE();
Paul Elliott43fbda62021-07-23 18:30:59 +01005424}
5425/* END_CASE */
5426
Paul Elliott91b021e2021-07-23 18:52:31 +01005427/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005428void aead_multipart_finish_buffer_test(int key_type_arg, data_t *key_data,
5429 int alg_arg,
5430 int finish_ciphertext_size_arg,
5431 int tag_size_arg,
5432 data_t *nonce,
5433 data_t *additional_data,
5434 data_t *input_data,
5435 int expected_status_arg)
Paul Elliott91b021e2021-07-23 18:52:31 +01005436{
5437
5438 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5439 psa_key_type_t key_type = key_type_arg;
5440 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005441 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005442 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5443 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5444 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005445 unsigned char *ciphertext = NULL;
5446 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005447 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005448 size_t ciphertext_size = 0;
5449 size_t ciphertext_length = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 size_t finish_ciphertext_size = (size_t) finish_ciphertext_size_arg;
5451 size_t tag_size = (size_t) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005452 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005453
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 PSA_ASSERT(psa_crypto_init());
Paul Elliott91b021e2021-07-23 18:52:31 +01005455
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
5457 psa_set_key_algorithm(&attributes, alg);
5458 psa_set_key_type(&attributes, key_type);
Paul Elliott91b021e2021-07-23 18:52:31 +01005459
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5461 &key));
Paul Elliott91b021e2021-07-23 18:52:31 +01005462
Gilles Peskine449bd832023-01-11 14:50:10 +01005463 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott91b021e2021-07-23 18:52:31 +01005464
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005466
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005467 TEST_CALLOC(ciphertext, ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005468
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005469 TEST_CALLOC(finish_ciphertext, finish_ciphertext_size);
Paul Elliott91b021e2021-07-23 18:52:31 +01005470
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005471 TEST_CALLOC(tag_buffer, tag_size);
Paul Elliott719c1322021-09-13 18:27:22 +01005472
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott91b021e2021-07-23 18:52:31 +01005474
5475 /* If the operation is not supported, just skip and not fail in case the
5476 * encryption involves a common limitation of cryptography hardwares and
5477 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 if (status == PSA_ERROR_NOT_SUPPORTED) {
5479 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5480 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott91b021e2021-07-23 18:52:31 +01005481 }
5482
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 PSA_ASSERT(status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005484
Gilles Peskine449bd832023-01-11 14:50:10 +01005485 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005486
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5488 input_data->len));
Paul Elliott76bda482021-10-07 17:07:23 +01005489
Gilles Peskine449bd832023-01-11 14:50:10 +01005490 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5491 additional_data->len));
Paul Elliott91b021e2021-07-23 18:52:31 +01005492
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 PSA_ASSERT(psa_aead_update(&operation, input_data->x, input_data->len,
5494 ciphertext, ciphertext_size, &ciphertext_length));
Paul Elliott91b021e2021-07-23 18:52:31 +01005495
5496 /* Ensure we can still complete operation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 status = psa_aead_finish(&operation, finish_ciphertext,
5498 finish_ciphertext_size,
5499 &ciphertext_length, tag_buffer,
5500 tag_size, &tag_length);
Paul Elliott91b021e2021-07-23 18:52:31 +01005501
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 TEST_EQUAL(status, expected_status);
Paul Elliott91b021e2021-07-23 18:52:31 +01005503
5504exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005505 psa_destroy_key(key);
5506 mbedtls_free(ciphertext);
5507 mbedtls_free(finish_ciphertext);
5508 mbedtls_free(tag_buffer);
5509 psa_aead_abort(&operation);
5510 PSA_DONE();
Paul Elliott91b021e2021-07-23 18:52:31 +01005511}
5512/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005513
5514/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005515void aead_multipart_verify(int key_type_arg, data_t *key_data,
5516 int alg_arg,
5517 data_t *nonce,
5518 data_t *additional_data,
5519 data_t *input_data,
5520 data_t *tag,
5521 int tag_usage_arg,
5522 int expected_setup_status_arg,
5523 int expected_status_arg)
Paul Elliott9961a662021-09-17 19:19:02 +01005524{
5525 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5526 psa_key_type_t key_type = key_type_arg;
5527 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005528 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005529 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5530 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5531 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005532 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005533 unsigned char *plaintext = NULL;
5534 unsigned char *finish_plaintext = NULL;
5535 size_t plaintext_size = 0;
5536 size_t plaintext_length = 0;
5537 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005538 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005539 unsigned char *tag_buffer = NULL;
5540 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005541
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 PSA_ASSERT(psa_crypto_init());
Paul Elliott9961a662021-09-17 19:19:02 +01005543
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
5545 psa_set_key_algorithm(&attributes, alg);
5546 psa_set_key_type(&attributes, key_type);
Paul Elliott9961a662021-09-17 19:19:02 +01005547
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5549 &key));
Paul Elliott9961a662021-09-17 19:19:02 +01005550
Gilles Peskine449bd832023-01-11 14:50:10 +01005551 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
Paul Elliott9961a662021-09-17 19:19:02 +01005552
Gilles Peskine449bd832023-01-11 14:50:10 +01005553 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg,
5554 input_data->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005555
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005556 TEST_CALLOC(plaintext, plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005557
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005559
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005560 TEST_CALLOC(finish_plaintext, verify_plaintext_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005561
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott9961a662021-09-17 19:19:02 +01005563
5564 /* If the operation is not supported, just skip and not fail in case the
5565 * encryption involves a common limitation of cryptography hardwares and
5566 * an alternative implementation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 if (status == PSA_ERROR_NOT_SUPPORTED) {
5568 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192(key_type, key_data->len * 8);
5569 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE(alg, nonce->len);
Paul Elliott9961a662021-09-17 19:19:02 +01005570 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 TEST_EQUAL(status, expected_setup_status);
Andrzej Kurekf8816012021-12-19 17:00:12 +01005572
Gilles Peskine449bd832023-01-11 14:50:10 +01005573 if (status != PSA_SUCCESS) {
Andrzej Kurekf8816012021-12-19 17:00:12 +01005574 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 }
Paul Elliott9961a662021-09-17 19:19:02 +01005576
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 PSA_ASSERT(status);
Paul Elliott9961a662021-09-17 19:19:02 +01005578
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005580
Gilles Peskine449bd832023-01-11 14:50:10 +01005581 status = psa_aead_set_lengths(&operation, additional_data->len,
5582 input_data->len);
5583 PSA_ASSERT(status);
Paul Elliottfec6f372021-10-06 17:15:02 +01005584
Gilles Peskine449bd832023-01-11 14:50:10 +01005585 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
5586 additional_data->len));
Paul Elliott9961a662021-09-17 19:19:02 +01005587
Gilles Peskine449bd832023-01-11 14:50:10 +01005588 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
5589 input_data->len,
5590 plaintext, plaintext_size,
5591 &plaintext_length));
Paul Elliott9961a662021-09-17 19:19:02 +01005592
Gilles Peskine449bd832023-01-11 14:50:10 +01005593 if (tag_usage == USE_GIVEN_TAG) {
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005594 tag_buffer = tag->x;
5595 tag_size = tag->len;
5596 }
5597
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 status = psa_aead_verify(&operation, finish_plaintext,
5599 verify_plaintext_size,
5600 &plaintext_length,
5601 tag_buffer, tag_size);
Paul Elliott9961a662021-09-17 19:19:02 +01005602
Gilles Peskine449bd832023-01-11 14:50:10 +01005603 TEST_EQUAL(status, expected_status);
Paul Elliott9961a662021-09-17 19:19:02 +01005604
5605exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 psa_destroy_key(key);
5607 mbedtls_free(plaintext);
5608 mbedtls_free(finish_plaintext);
5609 psa_aead_abort(&operation);
5610 PSA_DONE();
Paul Elliott9961a662021-09-17 19:19:02 +01005611}
5612/* END_CASE */
5613
Paul Elliott9961a662021-09-17 19:19:02 +01005614/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005615void aead_multipart_setup(int key_type_arg, data_t *key_data,
5616 int alg_arg, int expected_status_arg)
Paul Elliott5221ef62021-09-19 17:33:03 +01005617{
5618 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5619 psa_key_type_t key_type = key_type_arg;
5620 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005621 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005622 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5623 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5624 psa_status_t expected_status = expected_status_arg;
5625
Gilles Peskine449bd832023-01-11 14:50:10 +01005626 PSA_ASSERT(psa_crypto_init());
Paul Elliott5221ef62021-09-19 17:33:03 +01005627
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 psa_set_key_usage_flags(&attributes,
5629 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5630 psa_set_key_algorithm(&attributes, alg);
5631 psa_set_key_type(&attributes, key_type);
Paul Elliott5221ef62021-09-19 17:33:03 +01005632
Gilles Peskine449bd832023-01-11 14:50:10 +01005633 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5634 &key));
Paul Elliott5221ef62021-09-19 17:33:03 +01005635
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 status = psa_aead_encrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005637
Gilles Peskine449bd832023-01-11 14:50:10 +01005638 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005639
Gilles Peskine449bd832023-01-11 14:50:10 +01005640 psa_aead_abort(&operation);
Paul Elliott5221ef62021-09-19 17:33:03 +01005641
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 status = psa_aead_decrypt_setup(&operation, key, alg);
Paul Elliott5221ef62021-09-19 17:33:03 +01005643
Gilles Peskine449bd832023-01-11 14:50:10 +01005644 TEST_EQUAL(status, expected_status);
Paul Elliott5221ef62021-09-19 17:33:03 +01005645
5646exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 psa_destroy_key(key);
5648 psa_aead_abort(&operation);
5649 PSA_DONE();
Paul Elliott5221ef62021-09-19 17:33:03 +01005650}
5651/* END_CASE */
5652
5653/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005654void aead_multipart_state_test(int key_type_arg, data_t *key_data,
5655 int alg_arg,
5656 data_t *nonce,
5657 data_t *additional_data,
5658 data_t *input_data)
Paul Elliottc23a9a02021-06-21 18:32:46 +01005659{
5660 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5661 psa_key_type_t key_type = key_type_arg;
5662 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005663 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005664 unsigned char *output_data = NULL;
5665 unsigned char *final_data = NULL;
5666 size_t output_size = 0;
5667 size_t finish_output_size = 0;
5668 size_t output_length = 0;
5669 size_t key_bits = 0;
5670 size_t tag_length = 0;
5671 size_t tag_size = 0;
5672 size_t nonce_length = 0;
5673 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5674 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5675 size_t output_part_length = 0;
5676 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5677
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 PSA_ASSERT(psa_crypto_init());
Paul Elliottc23a9a02021-06-21 18:32:46 +01005679
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 psa_set_key_usage_flags(&attributes,
5681 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
5682 psa_set_key_algorithm(&attributes, alg);
5683 psa_set_key_type(&attributes, key_type);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005684
Gilles Peskine449bd832023-01-11 14:50:10 +01005685 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
5686 &key));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005687
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
5689 key_bits = psa_get_key_bits(&attributes);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005690
Gilles Peskine449bd832023-01-11 14:50:10 +01005691 tag_length = PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005692
Gilles Peskine449bd832023-01-11 14:50:10 +01005693 TEST_LE_U(tag_length, PSA_AEAD_TAG_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005694
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_data->len);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005696
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005697 TEST_CALLOC(output_data, output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005698
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005700
Gilles Peskine449bd832023-01-11 14:50:10 +01005701 TEST_LE_U(finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005702
Tom Cosgrove05b2a872023-07-21 11:31:13 +01005703 TEST_CALLOC(final_data, finish_output_size);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005704
5705 /* Test all operations error without calling setup first. */
5706
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5708 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005709
Gilles Peskine449bd832023-01-11 14:50:10 +01005710 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005711
Gilles Peskine449bd832023-01-11 14:50:10 +01005712 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5713 PSA_AEAD_NONCE_MAX_SIZE,
5714 &nonce_length),
5715 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005716
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005718
Paul Elliott481be342021-07-16 17:38:47 +01005719 /* ------------------------------------------------------- */
5720
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
5722 input_data->len),
5723 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005724
Gilles Peskine449bd832023-01-11 14:50:10 +01005725 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005726
Paul Elliott481be342021-07-16 17:38:47 +01005727 /* ------------------------------------------------------- */
5728
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5730 additional_data->len),
5731 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005732
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005734
Paul Elliott481be342021-07-16 17:38:47 +01005735 /* ------------------------------------------------------- */
5736
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5738 input_data->len, output_data,
5739 output_size, &output_length),
5740 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005741
Gilles Peskine449bd832023-01-11 14:50:10 +01005742 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005743
Paul Elliott481be342021-07-16 17:38:47 +01005744 /* ------------------------------------------------------- */
5745
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5747 finish_output_size,
5748 &output_part_length,
5749 tag_buffer, tag_length,
5750 &tag_size),
5751 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005752
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005754
Paul Elliott481be342021-07-16 17:38:47 +01005755 /* ------------------------------------------------------- */
5756
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5758 finish_output_size,
5759 &output_part_length,
5760 tag_buffer,
5761 tag_length),
5762 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005763
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005765
5766 /* Test for double setups. */
5767
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005769
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5771 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005772
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005774
Paul Elliott481be342021-07-16 17:38:47 +01005775 /* ------------------------------------------------------- */
5776
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005778
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5780 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005781
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005783
Paul Elliott374a2be2021-07-16 17:53:40 +01005784 /* ------------------------------------------------------- */
5785
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005787
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 TEST_EQUAL(psa_aead_decrypt_setup(&operation, key, alg),
5789 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005790
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005792
5793 /* ------------------------------------------------------- */
5794
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005796
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 TEST_EQUAL(psa_aead_encrypt_setup(&operation, key, alg),
5798 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005799
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005801
Paul Elliottc23a9a02021-06-21 18:32:46 +01005802 /* Test for not setting a nonce. */
5803
Gilles Peskine449bd832023-01-11 14:50:10 +01005804 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005805
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
5807 additional_data->len),
5808 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005809
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01005811
Paul Elliott7f628422021-09-01 12:08:29 +01005812 /* ------------------------------------------------------- */
5813
Gilles Peskine449bd832023-01-11 14:50:10 +01005814 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott7f628422021-09-01 12:08:29 +01005815
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
5817 input_data->len, output_data,
5818 output_size, &output_length),
5819 PSA_ERROR_BAD_STATE);
Paul Elliott7f628422021-09-01 12:08:29 +01005820
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 psa_aead_abort(&operation);
Paul Elliott7f628422021-09-01 12:08:29 +01005822
Paul Elliottbdc2c682021-09-21 18:37:10 +01005823 /* ------------------------------------------------------- */
5824
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005826
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 TEST_EQUAL(psa_aead_finish(&operation, final_data,
5828 finish_output_size,
5829 &output_part_length,
5830 tag_buffer, tag_length,
5831 &tag_size),
5832 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005833
Gilles Peskine449bd832023-01-11 14:50:10 +01005834 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005835
5836 /* ------------------------------------------------------- */
5837
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottbdc2c682021-09-21 18:37:10 +01005839
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 TEST_EQUAL(psa_aead_verify(&operation, final_data,
5841 finish_output_size,
5842 &output_part_length,
5843 tag_buffer,
5844 tag_length),
5845 PSA_ERROR_BAD_STATE);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005846
Gilles Peskine449bd832023-01-11 14:50:10 +01005847 psa_aead_abort(&operation);
Paul Elliottbdc2c682021-09-21 18:37:10 +01005848
Paul Elliottc23a9a02021-06-21 18:32:46 +01005849 /* Test for double setting nonce. */
5850
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005852
Gilles Peskine449bd832023-01-11 14:50:10 +01005853 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01005854
Gilles Peskine449bd832023-01-11 14:50:10 +01005855 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->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 Elliott374a2be2021-07-16 17:53:40 +01005860 /* Test for double generating nonce. */
5861
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005863
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5865 PSA_AEAD_NONCE_MAX_SIZE,
5866 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005867
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5869 PSA_AEAD_NONCE_MAX_SIZE,
5870 &nonce_length),
5871 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005872
5873
Gilles Peskine449bd832023-01-11 14:50:10 +01005874 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005875
5876 /* Test for generate nonce then set and vice versa */
5877
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01005879
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5881 PSA_AEAD_NONCE_MAX_SIZE,
5882 &nonce_length));
Paul Elliott374a2be2021-07-16 17:53:40 +01005883
Gilles Peskine449bd832023-01-11 14:50:10 +01005884 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5885 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01005886
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01005888
Andrzej Kurekad837522021-12-15 15:28:49 +01005889 /* Test for generating nonce after calling set lengths */
5890
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005892
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5894 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005895
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5897 PSA_AEAD_NONCE_MAX_SIZE,
5898 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005899
Gilles Peskine449bd832023-01-11 14:50:10 +01005900 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005901
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005902 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005903
Gilles Peskine449bd832023-01-11 14:50:10 +01005904 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005905
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 if (operation.alg == PSA_ALG_CCM) {
5907 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5908 input_data->len),
5909 PSA_ERROR_INVALID_ARGUMENT);
5910 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5911 PSA_AEAD_NONCE_MAX_SIZE,
5912 &nonce_length),
5913 PSA_ERROR_BAD_STATE);
5914 } else {
5915 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5916 input_data->len));
5917 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5918 PSA_AEAD_NONCE_MAX_SIZE,
5919 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005920 }
5921
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005923
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005924 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmand26d7442023-02-11 17:14:54 +00005925#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005927
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
5929 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
5930 input_data->len),
5931 PSA_ERROR_INVALID_ARGUMENT);
5932 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
5933 PSA_AEAD_NONCE_MAX_SIZE,
5934 &nonce_length),
5935 PSA_ERROR_BAD_STATE);
5936 } else {
5937 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
5938 input_data->len));
5939 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5940 PSA_AEAD_NONCE_MAX_SIZE,
5941 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005942 }
5943
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 psa_aead_abort(&operation);
Dave Rodgmand26d7442023-02-11 17:14:54 +00005945#endif
Andrzej Kurekad837522021-12-15 15:28:49 +01005946
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005947 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005948
Gilles Peskine449bd832023-01-11 14:50:10 +01005949 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01005950
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
5952 PSA_AEAD_NONCE_MAX_SIZE,
5953 &nonce_length));
Andrzej Kurekad837522021-12-15 15:28:49 +01005954
Gilles Peskine449bd832023-01-11 14:50:10 +01005955 if (operation.alg == PSA_ALG_CCM) {
5956 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5957 input_data->len),
5958 PSA_ERROR_INVALID_ARGUMENT);
5959 } else {
5960 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5961 input_data->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01005962 }
5963
Gilles Peskine449bd832023-01-11 14:50:10 +01005964 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01005965
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005966 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005967 /* Test for setting nonce after calling set lengths */
5968
Gilles Peskine449bd832023-01-11 14:50:10 +01005969 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005970
Gilles Peskine449bd832023-01-11 14:50:10 +01005971 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
5972 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005973
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005975
Gilles Peskine449bd832023-01-11 14:50:10 +01005976 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005977
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005978 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005979
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005981
Gilles Peskine449bd832023-01-11 14:50:10 +01005982 if (operation.alg == PSA_ALG_CCM) {
5983 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
5984 input_data->len),
5985 PSA_ERROR_INVALID_ARGUMENT);
5986 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
5987 PSA_ERROR_BAD_STATE);
5988 } else {
5989 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
5990 input_data->len));
5991 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005992 }
5993
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 psa_aead_abort(&operation);
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005995
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005996 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Dave Rodgmana4763632023-02-11 18:36:23 +00005997#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005999
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 if (operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM) {
6001 TEST_EQUAL(psa_aead_set_lengths(&operation, SIZE_MAX,
6002 input_data->len),
6003 PSA_ERROR_INVALID_ARGUMENT);
6004 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6005 PSA_ERROR_BAD_STATE);
6006 } else {
6007 PSA_ASSERT(psa_aead_set_lengths(&operation, SIZE_MAX,
6008 input_data->len));
6009 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006010 }
6011
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 psa_aead_abort(&operation);
Dave Rodgmana4763632023-02-11 18:36:23 +00006013#endif
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006014
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006015 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006016
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_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006020
Gilles Peskine449bd832023-01-11 14:50:10 +01006021 if (operation.alg == PSA_ALG_CCM) {
6022 TEST_EQUAL(psa_aead_set_lengths(&operation, UINT32_MAX,
6023 input_data->len),
6024 PSA_ERROR_INVALID_ARGUMENT);
6025 } else {
6026 PSA_ASSERT(psa_aead_set_lengths(&operation, UINT32_MAX,
6027 input_data->len));
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006028 }
6029
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006031
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006032 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Dave Rodgman91e83212023-02-11 20:07:43 +00006033#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006034 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006035
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 if (operation.alg == PSA_ALG_GCM) {
6037 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6038 SIZE_MAX),
6039 PSA_ERROR_INVALID_ARGUMENT);
6040 TEST_EQUAL(psa_aead_set_nonce(&operation, nonce->x, nonce->len),
6041 PSA_ERROR_BAD_STATE);
6042 } else if (operation.alg != PSA_ALG_CCM) {
6043 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6044 SIZE_MAX));
6045 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006046 }
6047
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 psa_aead_abort(&operation);
Dave Rodgman91e83212023-02-11 20:07:43 +00006049#endif
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006050
Tom Cosgrove1797b052022-12-04 17:19:59 +00006051 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Dave Rodgman641288b2023-02-11 22:02:04 +00006052#if SIZE_MAX > UINT32_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006054
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006056
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 if (operation.alg == PSA_ALG_GCM) {
6058 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6059 SIZE_MAX),
6060 PSA_ERROR_INVALID_ARGUMENT);
6061 } else if (operation.alg != PSA_ALG_CCM) {
6062 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6063 SIZE_MAX));
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006064 }
6065
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 psa_aead_abort(&operation);
Dave Rodgman641288b2023-02-11 22:02:04 +00006067#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006068
6069 /* ------------------------------------------------------- */
6070
Gilles Peskine449bd832023-01-11 14:50:10 +01006071 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott374a2be2021-07-16 17:53:40 +01006072
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott374a2be2021-07-16 17:53:40 +01006074
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6076 PSA_AEAD_NONCE_MAX_SIZE,
6077 &nonce_length),
6078 PSA_ERROR_BAD_STATE);
Paul Elliott374a2be2021-07-16 17:53:40 +01006079
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 psa_aead_abort(&operation);
Paul Elliott374a2be2021-07-16 17:53:40 +01006081
Paul Elliott7220cae2021-06-22 17:25:57 +01006082 /* Test for generating nonce in decrypt setup. */
6083
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott7220cae2021-06-22 17:25:57 +01006085
Gilles Peskine449bd832023-01-11 14:50:10 +01006086 TEST_EQUAL(psa_aead_generate_nonce(&operation, nonce_buffer,
6087 PSA_AEAD_NONCE_MAX_SIZE,
6088 &nonce_length),
6089 PSA_ERROR_BAD_STATE);
Paul Elliott7220cae2021-06-22 17:25:57 +01006090
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 psa_aead_abort(&operation);
Paul Elliott7220cae2021-06-22 17:25:57 +01006092
Paul Elliottc23a9a02021-06-21 18:32:46 +01006093 /* Test for setting lengths twice. */
6094
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006096
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006098
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6100 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006101
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6103 input_data->len),
6104 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006105
Gilles Peskine449bd832023-01-11 14:50:10 +01006106 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006107
Andrzej Kurekad837522021-12-15 15:28:49 +01006108 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006109
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006111
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006113
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 if (operation.alg == PSA_ALG_CCM) {
Paul Elliottf94bd992021-09-19 18:15:59 +01006115
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6117 additional_data->len),
6118 PSA_ERROR_BAD_STATE);
6119 } else {
6120 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6121 additional_data->len));
6122
6123 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6124 input_data->len),
6125 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006126 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006127 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006128
6129 /* ------------------------------------------------------- */
6130
Gilles Peskine449bd832023-01-11 14:50:10 +01006131 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006132
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006134
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 if (operation.alg == PSA_ALG_CCM) {
6136 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6137 input_data->len, output_data,
6138 output_size, &output_length),
6139 PSA_ERROR_BAD_STATE);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006140
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 } else {
6142 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6143 input_data->len, output_data,
6144 output_size, &output_length));
6145
6146 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6147 input_data->len),
6148 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006149 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006151
6152 /* ------------------------------------------------------- */
6153
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006155
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Andrzej Kurekad837522021-12-15 15:28:49 +01006157
Gilles Peskine449bd832023-01-11 14:50:10 +01006158 if (operation.alg == PSA_ALG_CCM) {
6159 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6160 finish_output_size,
6161 &output_part_length,
6162 tag_buffer, tag_length,
6163 &tag_size));
6164 } else {
6165 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6166 finish_output_size,
6167 &output_part_length,
6168 tag_buffer, tag_length,
6169 &tag_size));
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);
Andrzej Kurekad837522021-12-15 15:28:49 +01006176
6177 /* Test for setting lengths after generating nonce + already starting data. */
6178
Gilles Peskine449bd832023-01-11 14:50:10 +01006179 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006180
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6182 PSA_AEAD_NONCE_MAX_SIZE,
6183 &nonce_length));
6184 if (operation.alg == PSA_ALG_CCM) {
Andrzej Kurekad837522021-12-15 15:28:49 +01006185
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6187 additional_data->len),
6188 PSA_ERROR_BAD_STATE);
6189 } else {
6190 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6191 additional_data->len));
6192
6193 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6194 input_data->len),
6195 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006196 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006198
6199 /* ------------------------------------------------------- */
6200
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006202
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6204 PSA_AEAD_NONCE_MAX_SIZE,
6205 &nonce_length));
6206 if (operation.alg == PSA_ALG_CCM) {
6207 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6208 input_data->len, output_data,
6209 output_size, &output_length),
6210 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006211
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 } else {
6213 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6214 input_data->len, output_data,
6215 output_size, &output_length));
6216
6217 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6218 input_data->len),
6219 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006220 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 psa_aead_abort(&operation);
Andrzej Kurekad837522021-12-15 15:28:49 +01006222
6223 /* ------------------------------------------------------- */
6224
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Andrzej Kurekad837522021-12-15 15:28:49 +01006226
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 PSA_ASSERT(psa_aead_generate_nonce(&operation, nonce_buffer,
6228 PSA_AEAD_NONCE_MAX_SIZE,
6229 &nonce_length));
6230 if (operation.alg == PSA_ALG_CCM) {
6231 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6232 finish_output_size,
6233 &output_part_length,
6234 tag_buffer, tag_length,
6235 &tag_size));
6236 } else {
6237 PSA_ASSERT(psa_aead_finish(&operation, final_data,
6238 finish_output_size,
6239 &output_part_length,
6240 tag_buffer, tag_length,
6241 &tag_size));
Andrzej Kurekad837522021-12-15 15:28:49 +01006242
Gilles Peskine449bd832023-01-11 14:50:10 +01006243 TEST_EQUAL(psa_aead_set_lengths(&operation, additional_data->len,
6244 input_data->len),
6245 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006246 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006248
Paul Elliott243080c2021-07-21 19:01:17 +01006249 /* Test for not sending any additional data or data after setting non zero
6250 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006251
Gilles Peskine449bd832023-01-11 14:50:10 +01006252 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006253
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6257 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006258
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6260 finish_output_size,
6261 &output_part_length,
6262 tag_buffer, tag_length,
6263 &tag_size),
6264 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006265
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006267
Paul Elliott243080c2021-07-21 19:01:17 +01006268 /* Test for not sending any additional data or data after setting non-zero
6269 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006270
Gilles Peskine449bd832023-01-11 14:50:10 +01006271 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006272
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006274
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6276 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006277
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6279 finish_output_size,
6280 &output_part_length,
6281 tag_buffer,
6282 tag_length),
6283 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006284
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006286
Paul Elliott243080c2021-07-21 19:01:17 +01006287 /* Test for not sending any additional data after setting a non-zero length
6288 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006289
Gilles Peskine449bd832023-01-11 14:50:10 +01006290 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006291
Gilles Peskine449bd832023-01-11 14:50:10 +01006292 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006293
Gilles Peskine449bd832023-01-11 14:50:10 +01006294 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6295 input_data->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006296
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6298 input_data->len, output_data,
6299 output_size, &output_length),
6300 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006301
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006303
Paul Elliottf94bd992021-09-19 18:15:59 +01006304 /* Test for not sending any data after setting a non-zero length for it.*/
6305
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottf94bd992021-09-19 18:15:59 +01006307
Gilles Peskine449bd832023-01-11 14:50:10 +01006308 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006309
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6311 input_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006312
Gilles Peskine449bd832023-01-11 14:50:10 +01006313 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6314 additional_data->len));
Paul Elliottf94bd992021-09-19 18:15:59 +01006315
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6317 finish_output_size,
6318 &output_part_length,
6319 tag_buffer, tag_length,
6320 &tag_size),
6321 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottf94bd992021-09-19 18:15:59 +01006322
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 psa_aead_abort(&operation);
Paul Elliottf94bd992021-09-19 18:15:59 +01006324
Paul Elliottb0450fe2021-09-01 15:06:26 +01006325 /* Test for sending too much additional data after setting lengths. */
6326
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006328
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006330
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006332
6333
Gilles Peskine449bd832023-01-11 14:50:10 +01006334 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6335 additional_data->len),
6336 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006337
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006339
Paul Elliotta2a09b02021-09-22 14:56:40 +01006340 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006341
Gilles Peskine449bd832023-01-11 14:50:10 +01006342 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006343
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006345
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6347 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006348
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6350 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006351
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6353 1),
6354 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006355
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006357
Paul Elliottb0450fe2021-09-01 15:06:26 +01006358 /* Test for sending too much data after setting lengths. */
6359
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006361
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006363
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 PSA_ASSERT(psa_aead_set_lengths(&operation, 0, 0));
Paul Elliottb0450fe2021-09-01 15:06:26 +01006365
Gilles Peskine449bd832023-01-11 14:50:10 +01006366 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6367 input_data->len, output_data,
6368 output_size, &output_length),
6369 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006370
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 psa_aead_abort(&operation);
Paul Elliottb0450fe2021-09-01 15:06:26 +01006372
Paul Elliotta2a09b02021-09-22 14:56:40 +01006373 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006374
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006376
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006378
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 PSA_ASSERT(psa_aead_set_lengths(&operation, additional_data->len,
6380 input_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006381
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 PSA_ASSERT(psa_aead_update_ad(&operation, additional_data->x,
6383 additional_data->len));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006384
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6386 input_data->len, output_data,
6387 output_size, &output_length));
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006388
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 TEST_EQUAL(psa_aead_update(&operation, input_data->x,
6390 1, output_data,
6391 output_size, &output_length),
6392 PSA_ERROR_INVALID_ARGUMENT);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006393
Gilles Peskine449bd832023-01-11 14:50:10 +01006394 psa_aead_abort(&operation);
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006395
Paul Elliottc23a9a02021-06-21 18:32:46 +01006396 /* Test sending additional data after data. */
6397
Gilles Peskine449bd832023-01-11 14:50:10 +01006398 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006399
Gilles Peskine449bd832023-01-11 14:50:10 +01006400 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006401
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 if (operation.alg != PSA_ALG_CCM) {
6403 PSA_ASSERT(psa_aead_update(&operation, input_data->x,
6404 input_data->len, output_data,
6405 output_size, &output_length));
Paul Elliottc23a9a02021-06-21 18:32:46 +01006406
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 TEST_EQUAL(psa_aead_update_ad(&operation, additional_data->x,
6408 additional_data->len),
6409 PSA_ERROR_BAD_STATE);
Andrzej Kurekad837522021-12-15 15:28:49 +01006410 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 psa_aead_abort(&operation);
Paul Elliottc23a9a02021-06-21 18:32:46 +01006412
Paul Elliott534d0b42021-06-22 19:15:20 +01006413 /* Test calling finish on decryption. */
6414
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 PSA_ASSERT(psa_aead_decrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006416
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006418
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 TEST_EQUAL(psa_aead_finish(&operation, final_data,
6420 finish_output_size,
6421 &output_part_length,
6422 tag_buffer, tag_length,
6423 &tag_size),
6424 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006425
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006427
6428 /* Test calling verify on encryption. */
6429
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 PSA_ASSERT(psa_aead_encrypt_setup(&operation, key, alg));
Paul Elliott534d0b42021-06-22 19:15:20 +01006431
Gilles Peskine449bd832023-01-11 14:50:10 +01006432 PSA_ASSERT(psa_aead_set_nonce(&operation, nonce->x, nonce->len));
Paul Elliott534d0b42021-06-22 19:15:20 +01006433
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 TEST_EQUAL(psa_aead_verify(&operation, final_data,
6435 finish_output_size,
6436 &output_part_length,
6437 tag_buffer,
6438 tag_length),
6439 PSA_ERROR_BAD_STATE);
Paul Elliott534d0b42021-06-22 19:15:20 +01006440
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 psa_aead_abort(&operation);
Paul Elliott534d0b42021-06-22 19:15:20 +01006442
6443
Paul Elliottc23a9a02021-06-21 18:32:46 +01006444exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 psa_destroy_key(key);
6446 psa_aead_abort(&operation);
6447 mbedtls_free(output_data);
6448 mbedtls_free(final_data);
6449 PSA_DONE();
Paul Elliottc23a9a02021-06-21 18:32:46 +01006450}
6451/* END_CASE */
6452
6453/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006454void signature_size(int type_arg,
6455 int bits,
6456 int alg_arg,
6457 int expected_size_arg)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006458{
6459 psa_key_type_t type = type_arg;
6460 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01006461 size_t actual_size = PSA_SIGN_OUTPUT_SIZE(type, bits, alg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006462
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 TEST_EQUAL(actual_size, (size_t) expected_size_arg);
Gilles Peskine841b14b2019-11-26 17:37:37 +01006464
Gilles Peskinee59236f2018-01-27 23:32:46 +01006465exit:
6466 ;
6467}
6468/* END_CASE */
6469
6470/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006471void sign_hash_deterministic(int key_type_arg, data_t *key_data,
6472 int alg_arg, data_t *input_data,
6473 data_t *output_data)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006474{
Ronald Cron5425a212020-08-04 14:58:35 +02006475 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006476 psa_key_type_t key_type = key_type_arg;
6477 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006478 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006479 unsigned char *signature = NULL;
6480 size_t signature_size;
6481 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006482 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006483
Gilles Peskine449bd832023-01-11 14:50:10 +01006484 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006485
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6487 psa_set_key_algorithm(&attributes, alg);
6488 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006489
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6491 &key));
6492 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6493 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine20035e32018-02-03 22:44:14 +01006494
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006495 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006496 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6498 key_bits, alg);
6499 TEST_ASSERT(signature_size != 0);
6500 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006501 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006502
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006503 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 PSA_ASSERT(psa_sign_hash(key, alg,
6505 input_data->x, input_data->len,
6506 signature, signature_size,
6507 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006508 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006509 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006510 signature, signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01006511
6512exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006513 /*
6514 * Key attributes may have been returned by psa_get_key_attributes()
6515 * thus reset them as required.
6516 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006517 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006518
Gilles Peskine449bd832023-01-11 14:50:10 +01006519 psa_destroy_key(key);
6520 mbedtls_free(signature);
6521 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006522}
6523/* END_CASE */
6524
Paul Elliott712d5122022-12-07 14:03:10 +00006525/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006526/**
6527 * sign_hash_interruptible() test intentions:
6528 *
6529 * Note: This test can currently only handle ECDSA.
6530 *
6531 * 1. Test interruptible sign hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00006532 * and private keys / keypairs only).
Paul Elliottc7f68822023-02-24 17:37:04 +00006533 *
6534 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6535 * expected for different max_ops values.
6536 *
6537 * 3. Test that the number of ops done prior to start and after abort is zero
6538 * and that each successful stage completes some ops (this is not mandated by
6539 * the PSA specification, but is currently the case).
6540 *
6541 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
6542 * complete() calls does not alter the number of ops returned.
6543 */
Paul Elliott712d5122022-12-07 14:03:10 +00006544void sign_hash_interruptible(int key_type_arg, data_t *key_data,
6545 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006546 data_t *output_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006547{
6548 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6549 psa_key_type_t key_type = key_type_arg;
6550 psa_algorithm_t alg = alg_arg;
6551 size_t key_bits;
6552 unsigned char *signature = NULL;
6553 size_t signature_size;
6554 size_t signature_length = 0xdeadbeef;
6555 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6556 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006557 uint32_t num_ops = 0;
6558 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00006559 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006560 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006561 size_t min_completes = 0;
6562 size_t max_completes = 0;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006563
Paul Elliott712d5122022-12-07 14:03:10 +00006564 psa_sign_hash_interruptible_operation_t operation =
6565 psa_sign_hash_interruptible_operation_init();
6566
6567 PSA_ASSERT(psa_crypto_init());
6568
6569 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6570 psa_set_key_algorithm(&attributes, alg);
6571 psa_set_key_type(&attributes, key_type);
6572
6573 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6574 &key));
6575 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6576 key_bits = psa_get_key_bits(&attributes);
6577
6578 /* Allocate a buffer which has the size advertised by the
6579 * library. */
6580 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6581 key_bits, alg);
6582 TEST_ASSERT(signature_size != 0);
6583 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006584 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006585
Paul Elliott0c683352022-12-16 19:16:56 +00006586 psa_interruptible_set_max_ops(max_ops);
6587
Paul Elliott6f600372023-02-06 18:41:05 +00006588 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6589 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006590
Paul Elliott712d5122022-12-07 14:03:10 +00006591 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6592 TEST_ASSERT(num_ops_prior == 0);
6593
6594 /* Start performing the signature. */
6595 PSA_ASSERT(psa_sign_hash_start(&operation, key, alg,
6596 input_data->x, input_data->len));
6597
6598 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6599 TEST_ASSERT(num_ops_prior == 0);
6600
6601 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006602 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006603 status = psa_sign_hash_complete(&operation, signature, signature_size,
6604 &signature_length);
6605
Paul Elliott0c683352022-12-16 19:16:56 +00006606 num_completes++;
6607
Paul Elliott712d5122022-12-07 14:03:10 +00006608 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
6609 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006610 /* We are asserting here that every complete makes progress
6611 * (completes some ops), which is true of the internal
6612 * implementation and probably any implementation, however this is
6613 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00006614 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006615
Paul Elliott712d5122022-12-07 14:03:10 +00006616 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00006617
6618 /* Ensure calling get_num_ops() twice still returns the same
6619 * number of ops as previously reported. */
6620 num_ops = psa_sign_hash_get_num_ops(&operation);
6621
6622 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00006623 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006624 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00006625
6626 TEST_ASSERT(status == PSA_SUCCESS);
6627
Paul Elliott0c683352022-12-16 19:16:56 +00006628 TEST_LE_U(min_completes, num_completes);
6629 TEST_LE_U(num_completes, max_completes);
6630
Paul Elliott712d5122022-12-07 14:03:10 +00006631 /* Verify that the signature is what is expected. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01006632 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01006633 signature, signature_length);
Paul Elliott712d5122022-12-07 14:03:10 +00006634
6635 PSA_ASSERT(psa_sign_hash_abort(&operation));
6636
Paul Elliott59ad9452022-12-18 15:09:02 +00006637 num_ops = psa_sign_hash_get_num_ops(&operation);
6638 TEST_ASSERT(num_ops == 0);
6639
Paul Elliott712d5122022-12-07 14:03:10 +00006640exit:
6641
6642 /*
6643 * Key attributes may have been returned by psa_get_key_attributes()
6644 * thus reset them as required.
6645 */
6646 psa_reset_key_attributes(&attributes);
6647
6648 psa_destroy_key(key);
6649 mbedtls_free(signature);
6650 PSA_DONE();
6651}
6652/* END_CASE */
6653
Gilles Peskine20035e32018-02-03 22:44:14 +01006654/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006655void sign_hash_fail(int key_type_arg, data_t *key_data,
6656 int alg_arg, data_t *input_data,
6657 int signature_size_arg, int expected_status_arg)
Gilles Peskine20035e32018-02-03 22:44:14 +01006658{
Ronald Cron5425a212020-08-04 14:58:35 +02006659 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006660 psa_key_type_t key_type = key_type_arg;
6661 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006662 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006663 psa_status_t actual_status;
6664 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006665 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006666 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006668
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006669 TEST_CALLOC(signature, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006670
Gilles Peskine449bd832023-01-11 14:50:10 +01006671 PSA_ASSERT(psa_crypto_init());
Gilles Peskine20035e32018-02-03 22:44:14 +01006672
Gilles Peskine449bd832023-01-11 14:50:10 +01006673 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6674 psa_set_key_algorithm(&attributes, alg);
6675 psa_set_key_type(&attributes, key_type);
mohammad1603a97cb8c2018-03-28 03:46:26 -07006676
Gilles Peskine449bd832023-01-11 14:50:10 +01006677 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6678 &key));
Gilles Peskine20035e32018-02-03 22:44:14 +01006679
Gilles Peskine449bd832023-01-11 14:50:10 +01006680 actual_status = psa_sign_hash(key, alg,
6681 input_data->x, input_data->len,
6682 signature, signature_size,
6683 &signature_length);
6684 TEST_EQUAL(actual_status, expected_status);
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006685 /* The value of *signature_length is unspecified on error, but
6686 * whatever it is, it should be less than signature_size, so that
6687 * if the caller tries to read *signature_length bytes without
6688 * checking the error code then they don't overflow a buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 TEST_LE_U(signature_length, signature_size);
Gilles Peskine20035e32018-02-03 22:44:14 +01006690
6691exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006692 psa_reset_key_attributes(&attributes);
6693 psa_destroy_key(key);
6694 mbedtls_free(signature);
6695 PSA_DONE();
Gilles Peskine20035e32018-02-03 22:44:14 +01006696}
6697/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006698
Paul Elliott91007972022-12-16 12:21:24 +00006699/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006700/**
6701 * sign_hash_fail_interruptible() test intentions:
6702 *
6703 * Note: This test can currently only handle ECDSA.
6704 *
6705 * 1. Test that various failure cases for interruptible sign hash fail with the
6706 * correct error codes, and at the correct point (at start or during
6707 * complete).
6708 *
6709 * 2. Test the number of calls to psa_sign_hash_complete() required are as
6710 * expected for different max_ops values.
6711 *
6712 * 3. Test that the number of ops done prior to start and after abort is zero
6713 * and that each successful stage completes some ops (this is not mandated by
6714 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00006715 *
6716 * 4. Check that calling complete() when start() fails and complete()
6717 * after completion results in a BAD_STATE error.
6718 *
6719 * 5. Check that calling start() again after start fails results in a BAD_STATE
6720 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00006721 */
Paul Elliott91007972022-12-16 12:21:24 +00006722void sign_hash_fail_interruptible(int key_type_arg, data_t *key_data,
6723 int alg_arg, data_t *input_data,
6724 int signature_size_arg,
6725 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00006726 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006727 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00006728{
6729 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6730 psa_key_type_t key_type = key_type_arg;
6731 psa_algorithm_t alg = alg_arg;
6732 size_t signature_size = signature_size_arg;
6733 psa_status_t actual_status;
6734 psa_status_t expected_start_status = expected_start_status_arg;
6735 psa_status_t expected_complete_status = expected_complete_status_arg;
6736 unsigned char *signature = NULL;
6737 size_t signature_length = 0xdeadbeef;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006738 uint32_t num_ops = 0;
6739 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00006740 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006741 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006742 size_t min_completes = 0;
6743 size_t max_completes = 0;
6744
Paul Elliott91007972022-12-16 12:21:24 +00006745 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6746 psa_sign_hash_interruptible_operation_t operation =
6747 psa_sign_hash_interruptible_operation_init();
6748
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006749 TEST_CALLOC(signature, signature_size);
Paul Elliott91007972022-12-16 12:21:24 +00006750
6751 PSA_ASSERT(psa_crypto_init());
6752
6753 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
6754 psa_set_key_algorithm(&attributes, alg);
6755 psa_set_key_type(&attributes, key_type);
6756
6757 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6758 &key));
6759
Paul Elliott0c683352022-12-16 19:16:56 +00006760 psa_interruptible_set_max_ops(max_ops);
6761
Paul Elliott6f600372023-02-06 18:41:05 +00006762 interruptible_signverify_get_minmax_completes(max_ops,
6763 expected_complete_status,
6764 &min_completes,
6765 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006766
Paul Elliott91007972022-12-16 12:21:24 +00006767 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6768 TEST_ASSERT(num_ops_prior == 0);
6769
6770 /* Start performing the signature. */
6771 actual_status = psa_sign_hash_start(&operation, key, alg,
6772 input_data->x, input_data->len);
6773
6774 TEST_EQUAL(actual_status, expected_start_status);
6775
Paul Elliottc9774412023-02-06 15:14:07 +00006776 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00006777 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00006778 * start failed. */
6779 actual_status = psa_sign_hash_complete(&operation, signature,
6780 signature_size,
6781 &signature_length);
6782
6783 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6784
6785 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00006786 actual_status = psa_sign_hash_start(&operation, key, alg,
6787 input_data->x, input_data->len);
6788
6789 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
6790 }
6791
Paul Elliott91007972022-12-16 12:21:24 +00006792 num_ops_prior = psa_sign_hash_get_num_ops(&operation);
6793 TEST_ASSERT(num_ops_prior == 0);
6794
Paul Elliott91007972022-12-16 12:21:24 +00006795 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006796 do {
Paul Elliott91007972022-12-16 12:21:24 +00006797 actual_status = psa_sign_hash_complete(&operation, signature,
6798 signature_size,
6799 &signature_length);
6800
Paul Elliott0c683352022-12-16 19:16:56 +00006801 num_completes++;
6802
Paul Elliott334d7262023-01-20 17:29:41 +00006803 if (actual_status == PSA_SUCCESS ||
6804 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00006805 num_ops = psa_sign_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00006806 /* We are asserting here that every complete makes progress
6807 * (completes some ops), which is true of the internal
6808 * implementation and probably any implementation, however this is
6809 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00006810 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00006811
Paul Elliott91007972022-12-16 12:21:24 +00006812 num_ops_prior = num_ops;
6813 }
Paul Elliottedfc8832023-01-20 17:13:10 +00006814 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00006815
Paul Elliottc9774412023-02-06 15:14:07 +00006816 TEST_EQUAL(actual_status, expected_complete_status);
6817
Paul Elliottefebad02023-02-15 16:56:45 +00006818 /* Check that another complete returns BAD_STATE. */
6819 actual_status = psa_sign_hash_complete(&operation, signature,
6820 signature_size,
6821 &signature_length);
Paul Elliottc9774412023-02-06 15:14:07 +00006822
Paul Elliottefebad02023-02-15 16:56:45 +00006823 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00006824
Paul Elliott91007972022-12-16 12:21:24 +00006825 PSA_ASSERT(psa_sign_hash_abort(&operation));
6826
Paul Elliott59ad9452022-12-18 15:09:02 +00006827 num_ops = psa_sign_hash_get_num_ops(&operation);
6828 TEST_ASSERT(num_ops == 0);
6829
Paul Elliott91007972022-12-16 12:21:24 +00006830 /* The value of *signature_length is unspecified on error, but
6831 * whatever it is, it should be less than signature_size, so that
6832 * if the caller tries to read *signature_length bytes without
6833 * checking the error code then they don't overflow a buffer. */
6834 TEST_LE_U(signature_length, signature_size);
6835
Paul Elliott0c683352022-12-16 19:16:56 +00006836 TEST_LE_U(min_completes, num_completes);
6837 TEST_LE_U(num_completes, max_completes);
6838
Paul Elliott91007972022-12-16 12:21:24 +00006839exit:
6840 psa_reset_key_attributes(&attributes);
6841 psa_destroy_key(key);
6842 mbedtls_free(signature);
6843 PSA_DONE();
6844}
6845/* END_CASE */
6846
mohammad16038cc1cee2018-03-28 01:21:33 +03006847/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01006848void sign_verify_hash(int key_type_arg, data_t *key_data,
6849 int alg_arg, data_t *input_data)
Gilles Peskine9911b022018-06-29 17:30:48 +02006850{
Ronald Cron5425a212020-08-04 14:58:35 +02006851 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006852 psa_key_type_t key_type = key_type_arg;
6853 psa_algorithm_t alg = alg_arg;
6854 size_t key_bits;
6855 unsigned char *signature = NULL;
6856 size_t signature_size;
6857 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006858 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006859
Gilles Peskine449bd832023-01-11 14:50:10 +01006860 PSA_ASSERT(psa_crypto_init());
Gilles Peskine9911b022018-06-29 17:30:48 +02006861
Gilles Peskine449bd832023-01-11 14:50:10 +01006862 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
6863 psa_set_key_algorithm(&attributes, alg);
6864 psa_set_key_type(&attributes, key_type);
Gilles Peskine9911b022018-06-29 17:30:48 +02006865
Gilles Peskine449bd832023-01-11 14:50:10 +01006866 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6867 &key));
6868 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6869 key_bits = psa_get_key_bits(&attributes);
Gilles Peskine9911b022018-06-29 17:30:48 +02006870
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006871 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006872 * library. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006873 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6874 key_bits, alg);
6875 TEST_ASSERT(signature_size != 0);
6876 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006877 TEST_CALLOC(signature, signature_size);
Gilles Peskine9911b022018-06-29 17:30:48 +02006878
6879 /* Perform the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006880 PSA_ASSERT(psa_sign_hash(key, alg,
6881 input_data->x, input_data->len,
6882 signature, signature_size,
6883 &signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006884 /* Check that the signature length looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006885 TEST_LE_U(signature_length, signature_size);
6886 TEST_ASSERT(signature_length > 0);
Gilles Peskine9911b022018-06-29 17:30:48 +02006887
6888 /* Use the library to verify that the signature is correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006889 PSA_ASSERT(psa_verify_hash(key, alg,
6890 input_data->x, input_data->len,
6891 signature, signature_length));
Gilles Peskine9911b022018-06-29 17:30:48 +02006892
Gilles Peskine449bd832023-01-11 14:50:10 +01006893 if (input_data->len != 0) {
Gilles Peskine9911b022018-06-29 17:30:48 +02006894 /* Flip a bit in the input and verify that the signature is now
6895 * detected as invalid. Flip a bit at the beginning, not at the end,
6896 * because ECDSA may ignore the last few bits of the input. */
6897 input_data->x[0] ^= 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006898 TEST_EQUAL(psa_verify_hash(key, alg,
6899 input_data->x, input_data->len,
6900 signature, signature_length),
6901 PSA_ERROR_INVALID_SIGNATURE);
Gilles Peskine9911b022018-06-29 17:30:48 +02006902 }
6903
6904exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006905 /*
6906 * Key attributes may have been returned by psa_get_key_attributes()
6907 * thus reset them as required.
6908 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006909 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006910
Gilles Peskine449bd832023-01-11 14:50:10 +01006911 psa_destroy_key(key);
6912 mbedtls_free(signature);
6913 PSA_DONE();
Gilles Peskine9911b022018-06-29 17:30:48 +02006914}
6915/* END_CASE */
6916
Paul Elliott712d5122022-12-07 14:03:10 +00006917/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00006918/**
6919 * sign_verify_hash_interruptible() test intentions:
6920 *
6921 * Note: This test can currently only handle ECDSA.
6922 *
Paul Elliott8c092052023-03-06 17:49:14 +00006923 * 1. Test that we can sign an input hash with the given keypair and then
6924 * afterwards verify that signature. This is currently the only way to test
6925 * non deterministic ECDSA, but this test can also handle deterministic.
Paul Elliottc7f68822023-02-24 17:37:04 +00006926 *
6927 * 2. Test that after corrupting the hash, the verification detects an invalid
6928 * signature.
6929 *
6930 * 3. Test the number of calls to psa_sign_hash_complete() required are as
6931 * expected for different max_ops values.
Paul Elliott7c173082023-02-26 18:44:45 +00006932 *
6933 * 4. Test that the number of ops done prior to starting signing and after abort
6934 * is zero and that each successful signing stage completes some ops (this is
6935 * not mandated by the PSA specification, but is currently the case).
Paul Elliottc7f68822023-02-24 17:37:04 +00006936 */
Paul Elliott712d5122022-12-07 14:03:10 +00006937void sign_verify_hash_interruptible(int key_type_arg, data_t *key_data,
Paul Elliott0c683352022-12-16 19:16:56 +00006938 int alg_arg, data_t *input_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00006939 int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00006940{
6941 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6942 psa_key_type_t key_type = key_type_arg;
6943 psa_algorithm_t alg = alg_arg;
6944 size_t key_bits;
6945 unsigned char *signature = NULL;
6946 size_t signature_size;
6947 size_t signature_length = 0xdeadbeef;
6948 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6949 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00006950 uint32_t max_ops = max_ops_arg;
Paul Elliott7c173082023-02-26 18:44:45 +00006951 uint32_t num_ops = 0;
6952 uint32_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00006953 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00006954 size_t min_completes = 0;
6955 size_t max_completes = 0;
6956
Paul Elliott712d5122022-12-07 14:03:10 +00006957 psa_sign_hash_interruptible_operation_t sign_operation =
6958 psa_sign_hash_interruptible_operation_init();
6959 psa_verify_hash_interruptible_operation_t verify_operation =
6960 psa_verify_hash_interruptible_operation_init();
6961
6962 PSA_ASSERT(psa_crypto_init());
6963
Paul Elliott0c683352022-12-16 19:16:56 +00006964 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
6965 PSA_KEY_USAGE_VERIFY_HASH);
Paul Elliott712d5122022-12-07 14:03:10 +00006966 psa_set_key_algorithm(&attributes, alg);
6967 psa_set_key_type(&attributes, key_type);
6968
6969 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
6970 &key));
6971 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
6972 key_bits = psa_get_key_bits(&attributes);
6973
6974 /* Allocate a buffer which has the size advertised by the
6975 * library. */
6976 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
6977 key_bits, alg);
6978 TEST_ASSERT(signature_size != 0);
6979 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01006980 TEST_CALLOC(signature, signature_size);
Paul Elliott712d5122022-12-07 14:03:10 +00006981
Paul Elliott0c683352022-12-16 19:16:56 +00006982 psa_interruptible_set_max_ops(max_ops);
6983
Paul Elliott6f600372023-02-06 18:41:05 +00006984 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
6985 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00006986
Paul Elliott7c173082023-02-26 18:44:45 +00006987 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6988 TEST_ASSERT(num_ops_prior == 0);
6989
Paul Elliott712d5122022-12-07 14:03:10 +00006990 /* Start performing the signature. */
6991 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
6992 input_data->x, input_data->len));
6993
Paul Elliott7c173082023-02-26 18:44:45 +00006994 num_ops_prior = psa_sign_hash_get_num_ops(&sign_operation);
6995 TEST_ASSERT(num_ops_prior == 0);
6996
Paul Elliott712d5122022-12-07 14:03:10 +00006997 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00006998 do {
Paul Elliott712d5122022-12-07 14:03:10 +00006999
Paul Elliott0c683352022-12-16 19:16:56 +00007000 status = psa_sign_hash_complete(&sign_operation, signature,
7001 signature_size,
Paul Elliott712d5122022-12-07 14:03:10 +00007002 &signature_length);
Paul Elliott0c683352022-12-16 19:16:56 +00007003
7004 num_completes++;
Paul Elliott7c173082023-02-26 18:44:45 +00007005
7006 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7007 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7008 /* We are asserting here that every complete makes progress
7009 * (completes some ops), which is true of the internal
7010 * implementation and probably any implementation, however this is
7011 * not mandated by the PSA specification. */
7012 TEST_ASSERT(num_ops > num_ops_prior);
7013
7014 num_ops_prior = num_ops;
7015 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007016 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007017
7018 TEST_ASSERT(status == PSA_SUCCESS);
7019
Paul Elliott0c683352022-12-16 19:16:56 +00007020 TEST_LE_U(min_completes, num_completes);
7021 TEST_LE_U(num_completes, max_completes);
7022
Paul Elliott712d5122022-12-07 14:03:10 +00007023 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7024
Paul Elliott7c173082023-02-26 18:44:45 +00007025 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7026 TEST_ASSERT(num_ops == 0);
7027
Paul Elliott712d5122022-12-07 14:03:10 +00007028 /* Check that the signature length looks sensible. */
7029 TEST_LE_U(signature_length, signature_size);
7030 TEST_ASSERT(signature_length > 0);
7031
Paul Elliott0c683352022-12-16 19:16:56 +00007032 num_completes = 0;
Paul Elliott712d5122022-12-07 14:03:10 +00007033
7034 /* Start verification. */
7035 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7036 input_data->x, input_data->len,
7037 signature, signature_length));
7038
7039 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007040 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007041 status = psa_verify_hash_complete(&verify_operation);
Paul Elliott0c683352022-12-16 19:16:56 +00007042
7043 num_completes++;
Paul Elliottedfc8832023-01-20 17:13:10 +00007044 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007045
7046 TEST_ASSERT(status == PSA_SUCCESS);
7047
Paul Elliott0c683352022-12-16 19:16:56 +00007048 TEST_LE_U(min_completes, num_completes);
7049 TEST_LE_U(num_completes, max_completes);
7050
Paul Elliott712d5122022-12-07 14:03:10 +00007051 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7052
7053 verify_operation = psa_verify_hash_interruptible_operation_init();
7054
7055 if (input_data->len != 0) {
7056 /* Flip a bit in the input and verify that the signature is now
7057 * detected as invalid. Flip a bit at the beginning, not at the end,
7058 * because ECDSA may ignore the last few bits of the input. */
7059 input_data->x[0] ^= 1;
7060
Paul Elliott712d5122022-12-07 14:03:10 +00007061 /* Start verification. */
7062 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7063 input_data->x, input_data->len,
7064 signature, signature_length));
7065
7066 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007067 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007068 status = psa_verify_hash_complete(&verify_operation);
Paul Elliottedfc8832023-01-20 17:13:10 +00007069 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007070
7071 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7072 }
7073
7074 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7075
7076exit:
7077 /*
7078 * Key attributes may have been returned by psa_get_key_attributes()
7079 * thus reset them as required.
7080 */
7081 psa_reset_key_attributes(&attributes);
7082
7083 psa_destroy_key(key);
7084 mbedtls_free(signature);
7085 PSA_DONE();
7086}
7087/* END_CASE */
7088
Gilles Peskine9911b022018-06-29 17:30:48 +02007089/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007090void verify_hash(int key_type_arg, data_t *key_data,
7091 int alg_arg, data_t *hash_data,
7092 data_t *signature_data)
itayzafrir5c753392018-05-08 11:18:38 +03007093{
Ronald Cron5425a212020-08-04 14:58:35 +02007094 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007095 psa_key_type_t key_type = key_type_arg;
7096 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007097 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03007098
Gilles Peskine449bd832023-01-11 14:50:10 +01007099 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
Gilles Peskine69c12672018-06-28 00:07:19 +02007100
Gilles Peskine449bd832023-01-11 14:50:10 +01007101 PSA_ASSERT(psa_crypto_init());
itayzafrir5c753392018-05-08 11:18:38 +03007102
Gilles Peskine449bd832023-01-11 14:50:10 +01007103 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7104 psa_set_key_algorithm(&attributes, alg);
7105 psa_set_key_type(&attributes, key_type);
itayzafrir5c753392018-05-08 11:18:38 +03007106
Gilles Peskine449bd832023-01-11 14:50:10 +01007107 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7108 &key));
itayzafrir5c753392018-05-08 11:18:38 +03007109
Gilles Peskine449bd832023-01-11 14:50:10 +01007110 PSA_ASSERT(psa_verify_hash(key, alg,
7111 hash_data->x, hash_data->len,
7112 signature_data->x, signature_data->len));
Gilles Peskine0627f982019-11-26 19:12:16 +01007113
itayzafrir5c753392018-05-08 11:18:38 +03007114exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007115 psa_reset_key_attributes(&attributes);
7116 psa_destroy_key(key);
7117 PSA_DONE();
itayzafrir5c753392018-05-08 11:18:38 +03007118}
7119/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007120
Paul Elliott712d5122022-12-07 14:03:10 +00007121/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007122/**
7123 * verify_hash_interruptible() test intentions:
7124 *
7125 * Note: This test can currently only handle ECDSA.
7126 *
7127 * 1. Test interruptible verify hash with known outcomes (deterministic ECDSA
Paul Elliott8c092052023-03-06 17:49:14 +00007128 * only). Given this test only does verification it can accept public keys as
7129 * well as private keys / keypairs.
Paul Elliottc7f68822023-02-24 17:37:04 +00007130 *
7131 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7132 * expected for different max_ops values.
7133 *
7134 * 3. Test that the number of ops done prior to start and after abort is zero
7135 * and that each successful stage completes some ops (this is not mandated by
7136 * the PSA specification, but is currently the case).
Paul Elliott8359c142023-02-24 18:40:10 +00007137 *
7138 * 4. Test that calling psa_sign_hash_get_num_ops() multiple times between
7139 * complete() calls does not alter the number of ops returned.
7140 *
7141 * 5. Test that after corrupting the hash, the verification detects an invalid
7142 * signature.
Paul Elliottc7f68822023-02-24 17:37:04 +00007143 */
Paul Elliott712d5122022-12-07 14:03:10 +00007144void verify_hash_interruptible(int key_type_arg, data_t *key_data,
7145 int alg_arg, data_t *hash_data,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007146 data_t *signature_data, int max_ops_arg)
Paul Elliott712d5122022-12-07 14:03:10 +00007147{
7148 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7149 psa_key_type_t key_type = key_type_arg;
7150 psa_algorithm_t alg = alg_arg;
7151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7152 psa_status_t status = PSA_OPERATION_INCOMPLETE;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007153 uint32_t num_ops = 0;
7154 uint32_t max_ops = max_ops_arg;
Paul Elliott712d5122022-12-07 14:03:10 +00007155 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007156 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007157 size_t min_completes = 0;
7158 size_t max_completes = 0;
7159
Paul Elliott712d5122022-12-07 14:03:10 +00007160 psa_verify_hash_interruptible_operation_t operation =
7161 psa_verify_hash_interruptible_operation_init();
7162
7163 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
7164
7165 PSA_ASSERT(psa_crypto_init());
7166
7167 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7168 psa_set_key_algorithm(&attributes, alg);
7169 psa_set_key_type(&attributes, key_type);
7170
7171 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7172 &key));
7173
Paul Elliott0c683352022-12-16 19:16:56 +00007174 psa_interruptible_set_max_ops(max_ops);
7175
Paul Elliott6f600372023-02-06 18:41:05 +00007176 interruptible_signverify_get_minmax_completes(max_ops, PSA_SUCCESS,
7177 &min_completes, &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007178
Paul Elliott712d5122022-12-07 14:03:10 +00007179 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7180
7181 TEST_ASSERT(num_ops_prior == 0);
7182
7183 /* Start verification. */
7184 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7185 hash_data->x, hash_data->len,
7186 signature_data->x, signature_data->len)
7187 );
7188
7189 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7190
7191 TEST_ASSERT(num_ops_prior == 0);
7192
7193 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007194 do {
Paul Elliott712d5122022-12-07 14:03:10 +00007195 status = psa_verify_hash_complete(&operation);
7196
Paul Elliott0c683352022-12-16 19:16:56 +00007197 num_completes++;
7198
Paul Elliott712d5122022-12-07 14:03:10 +00007199 if (status == PSA_SUCCESS || status == PSA_OPERATION_INCOMPLETE) {
7200 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007201 /* We are asserting here that every complete makes progress
7202 * (completes some ops), which is true of the internal
7203 * implementation and probably any implementation, however this is
7204 * not mandated by the PSA specification. */
Paul Elliott712d5122022-12-07 14:03:10 +00007205 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007206
Paul Elliott712d5122022-12-07 14:03:10 +00007207 num_ops_prior = num_ops;
Paul Elliottf8e5b562023-02-19 18:43:45 +00007208
7209 /* Ensure calling get_num_ops() twice still returns the same
7210 * number of ops as previously reported. */
7211 num_ops = psa_verify_hash_get_num_ops(&operation);
7212
7213 TEST_EQUAL(num_ops, num_ops_prior);
Paul Elliott712d5122022-12-07 14:03:10 +00007214 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007215 } while (status == PSA_OPERATION_INCOMPLETE);
Paul Elliott712d5122022-12-07 14:03:10 +00007216
7217 TEST_ASSERT(status == PSA_SUCCESS);
7218
Paul Elliott0c683352022-12-16 19:16:56 +00007219 TEST_LE_U(min_completes, num_completes);
7220 TEST_LE_U(num_completes, max_completes);
7221
Paul Elliott712d5122022-12-07 14:03:10 +00007222 PSA_ASSERT(psa_verify_hash_abort(&operation));
7223
Paul Elliott59ad9452022-12-18 15:09:02 +00007224 num_ops = psa_verify_hash_get_num_ops(&operation);
7225 TEST_ASSERT(num_ops == 0);
7226
Paul Elliott8359c142023-02-24 18:40:10 +00007227 if (hash_data->len != 0) {
7228 /* Flip a bit in the hash and verify that the signature is now detected
7229 * as invalid. Flip a bit at the beginning, not at the end, because
7230 * ECDSA may ignore the last few bits of the input. */
7231 hash_data->x[0] ^= 1;
7232
7233 /* Start verification. */
7234 PSA_ASSERT(psa_verify_hash_start(&operation, key, alg,
7235 hash_data->x, hash_data->len,
7236 signature_data->x, signature_data->len));
7237
7238 /* Continue performing the signature until complete. */
7239 do {
7240 status = psa_verify_hash_complete(&operation);
7241 } while (status == PSA_OPERATION_INCOMPLETE);
7242
7243 TEST_ASSERT(status == PSA_ERROR_INVALID_SIGNATURE);
7244 }
7245
Paul Elliott712d5122022-12-07 14:03:10 +00007246exit:
7247 psa_reset_key_attributes(&attributes);
7248 psa_destroy_key(key);
7249 PSA_DONE();
7250}
7251/* END_CASE */
7252
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007253/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007254void verify_hash_fail(int key_type_arg, data_t *key_data,
7255 int alg_arg, data_t *hash_data,
7256 data_t *signature_data,
7257 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007258{
Ronald Cron5425a212020-08-04 14:58:35 +02007259 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007260 psa_key_type_t key_type = key_type_arg;
7261 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007262 psa_status_t actual_status;
7263 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007264 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007265
Gilles Peskine449bd832023-01-11 14:50:10 +01007266 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007267
Gilles Peskine449bd832023-01-11 14:50:10 +01007268 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7269 psa_set_key_algorithm(&attributes, alg);
7270 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007271
Gilles Peskine449bd832023-01-11 14:50:10 +01007272 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7273 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007274
Gilles Peskine449bd832023-01-11 14:50:10 +01007275 actual_status = psa_verify_hash(key, alg,
7276 hash_data->x, hash_data->len,
7277 signature_data->x, signature_data->len);
7278 TEST_EQUAL(actual_status, expected_status);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007279
7280exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007281 psa_reset_key_attributes(&attributes);
7282 psa_destroy_key(key);
7283 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007284}
7285/* END_CASE */
7286
Paul Elliott91007972022-12-16 12:21:24 +00007287/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007288/**
7289 * verify_hash_fail_interruptible() test intentions:
7290 *
7291 * Note: This test can currently only handle ECDSA.
7292 *
7293 * 1. Test that various failure cases for interruptible verify hash fail with
7294 * the correct error codes, and at the correct point (at start or during
7295 * complete).
7296 *
7297 * 2. Test the number of calls to psa_verify_hash_complete() required are as
7298 * expected for different max_ops values.
7299 *
7300 * 3. Test that the number of ops done prior to start and after abort is zero
7301 * and that each successful stage completes some ops (this is not mandated by
7302 * the PSA specification, but is currently the case).
Paul Elliott587e7802023-02-27 09:53:08 +00007303 *
7304 * 4. Check that calling complete() when start() fails and complete()
7305 * after completion results in a BAD_STATE error.
7306 *
7307 * 5. Check that calling start() again after start fails results in a BAD_STATE
7308 * error.
Paul Elliottc7f68822023-02-24 17:37:04 +00007309 */
Paul Elliott91007972022-12-16 12:21:24 +00007310void verify_hash_fail_interruptible(int key_type_arg, data_t *key_data,
7311 int alg_arg, data_t *hash_data,
7312 data_t *signature_data,
7313 int expected_start_status_arg,
Paul Elliott0c683352022-12-16 19:16:56 +00007314 int expected_complete_status_arg,
Paul Elliottab7c5c82023-02-03 15:49:42 +00007315 int max_ops_arg)
Paul Elliott91007972022-12-16 12:21:24 +00007316{
7317 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7318 psa_key_type_t key_type = key_type_arg;
7319 psa_algorithm_t alg = alg_arg;
7320 psa_status_t actual_status;
7321 psa_status_t expected_start_status = expected_start_status_arg;
7322 psa_status_t expected_complete_status = expected_complete_status_arg;
7323 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottab7c5c82023-02-03 15:49:42 +00007324 uint32_t num_ops = 0;
7325 uint32_t max_ops = max_ops_arg;
Paul Elliott91007972022-12-16 12:21:24 +00007326 size_t num_ops_prior = 0;
Paul Elliott0c683352022-12-16 19:16:56 +00007327 size_t num_completes = 0;
Paul Elliott97ac7d92023-01-23 18:09:06 +00007328 size_t min_completes = 0;
7329 size_t max_completes = 0;
Paul Elliott91007972022-12-16 12:21:24 +00007330 psa_verify_hash_interruptible_operation_t operation =
7331 psa_verify_hash_interruptible_operation_init();
7332
7333 PSA_ASSERT(psa_crypto_init());
7334
7335 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
7336 psa_set_key_algorithm(&attributes, alg);
7337 psa_set_key_type(&attributes, key_type);
7338
7339 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7340 &key));
7341
Paul Elliott0c683352022-12-16 19:16:56 +00007342 psa_interruptible_set_max_ops(max_ops);
7343
Paul Elliott6f600372023-02-06 18:41:05 +00007344 interruptible_signverify_get_minmax_completes(max_ops,
7345 expected_complete_status,
7346 &min_completes,
7347 &max_completes);
Paul Elliott97ac7d92023-01-23 18:09:06 +00007348
Paul Elliott91007972022-12-16 12:21:24 +00007349 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7350 TEST_ASSERT(num_ops_prior == 0);
7351
7352 /* Start verification. */
7353 actual_status = psa_verify_hash_start(&operation, key, alg,
7354 hash_data->x, hash_data->len,
7355 signature_data->x,
7356 signature_data->len);
7357
7358 TEST_EQUAL(actual_status, expected_start_status);
7359
Paul Elliottc9774412023-02-06 15:14:07 +00007360 if (expected_start_status != PSA_SUCCESS) {
Paul Elliottde7c31e2023-03-01 14:37:48 +00007361 /* Emulate poor application code, and call complete anyway, even though
Paul Elliott587e7802023-02-27 09:53:08 +00007362 * start failed. */
7363 actual_status = psa_verify_hash_complete(&operation);
7364
7365 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7366
7367 /* Test that calling start again after failure also causes BAD_STATE. */
Paul Elliottc9774412023-02-06 15:14:07 +00007368 actual_status = psa_verify_hash_start(&operation, key, alg,
7369 hash_data->x, hash_data->len,
7370 signature_data->x,
7371 signature_data->len);
7372
7373 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
7374 }
7375
Paul Elliott91007972022-12-16 12:21:24 +00007376 num_ops_prior = psa_verify_hash_get_num_ops(&operation);
7377 TEST_ASSERT(num_ops_prior == 0);
7378
Paul Elliott91007972022-12-16 12:21:24 +00007379 /* Continue performing the signature until complete. */
Paul Elliottedfc8832023-01-20 17:13:10 +00007380 do {
Paul Elliott91007972022-12-16 12:21:24 +00007381 actual_status = psa_verify_hash_complete(&operation);
7382
Paul Elliott0c683352022-12-16 19:16:56 +00007383 num_completes++;
7384
Paul Elliott334d7262023-01-20 17:29:41 +00007385 if (actual_status == PSA_SUCCESS ||
7386 actual_status == PSA_OPERATION_INCOMPLETE) {
Paul Elliott91007972022-12-16 12:21:24 +00007387 num_ops = psa_verify_hash_get_num_ops(&operation);
Paul Elliott96b89b22023-02-15 23:10:37 +00007388 /* We are asserting here that every complete makes progress
7389 * (completes some ops), which is true of the internal
7390 * implementation and probably any implementation, however this is
7391 * not mandated by the PSA specification. */
Paul Elliott91007972022-12-16 12:21:24 +00007392 TEST_ASSERT(num_ops > num_ops_prior);
Paul Elliott0c683352022-12-16 19:16:56 +00007393
Paul Elliott91007972022-12-16 12:21:24 +00007394 num_ops_prior = num_ops;
7395 }
Paul Elliottedfc8832023-01-20 17:13:10 +00007396 } while (actual_status == PSA_OPERATION_INCOMPLETE);
Paul Elliott91007972022-12-16 12:21:24 +00007397
Paul Elliottc9774412023-02-06 15:14:07 +00007398 TEST_EQUAL(actual_status, expected_complete_status);
7399
Paul Elliottefebad02023-02-15 16:56:45 +00007400 /* Check that another complete returns BAD_STATE. */
7401 actual_status = psa_verify_hash_complete(&operation);
7402 TEST_EQUAL(actual_status, PSA_ERROR_BAD_STATE);
Paul Elliott334d7262023-01-20 17:29:41 +00007403
Paul Elliott0c683352022-12-16 19:16:56 +00007404 TEST_LE_U(min_completes, num_completes);
7405 TEST_LE_U(num_completes, max_completes);
7406
Paul Elliott91007972022-12-16 12:21:24 +00007407 PSA_ASSERT(psa_verify_hash_abort(&operation));
7408
Paul Elliott59ad9452022-12-18 15:09:02 +00007409 num_ops = psa_verify_hash_get_num_ops(&operation);
7410 TEST_ASSERT(num_ops == 0);
7411
Paul Elliott91007972022-12-16 12:21:24 +00007412exit:
7413 psa_reset_key_attributes(&attributes);
7414 psa_destroy_key(key);
7415 PSA_DONE();
7416}
7417/* END_CASE */
7418
Paul Elliott20a36062022-12-18 13:21:25 +00007419/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007420/**
7421 * interruptible_signverify_hash_state_test() test intentions:
7422 *
7423 * Note: This test can currently only handle ECDSA.
7424 *
7425 * 1. Test that calling the various interruptible sign and verify hash functions
7426 * in incorrect orders returns BAD_STATE errors.
7427 */
Paul Elliott76d671a2023-02-07 17:45:18 +00007428void interruptible_signverify_hash_state_test(int key_type_arg,
7429 data_t *key_data, int alg_arg, data_t *input_data)
Paul Elliott20a36062022-12-18 13:21:25 +00007430{
7431 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7432 psa_key_type_t key_type = key_type_arg;
7433 psa_algorithm_t alg = alg_arg;
7434 size_t key_bits;
7435 unsigned char *signature = NULL;
7436 size_t signature_size;
7437 size_t signature_length = 0xdeadbeef;
7438 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7439 psa_sign_hash_interruptible_operation_t sign_operation =
7440 psa_sign_hash_interruptible_operation_init();
7441 psa_verify_hash_interruptible_operation_t verify_operation =
7442 psa_verify_hash_interruptible_operation_init();
7443
7444 PSA_ASSERT(psa_crypto_init());
7445
7446 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7447 PSA_KEY_USAGE_VERIFY_HASH);
7448 psa_set_key_algorithm(&attributes, alg);
7449 psa_set_key_type(&attributes, key_type);
7450
7451 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7452 &key));
7453 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7454 key_bits = psa_get_key_bits(&attributes);
7455
7456 /* Allocate a buffer which has the size advertised by the
7457 * library. */
7458 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7459 key_bits, alg);
7460 TEST_ASSERT(signature_size != 0);
7461 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007462 TEST_CALLOC(signature, signature_size);
Paul Elliott20a36062022-12-18 13:21:25 +00007463
7464 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7465
7466 /* --- Attempt completes prior to starts --- */
7467 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7468 signature_size,
7469 &signature_length),
7470 PSA_ERROR_BAD_STATE);
7471
7472 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7473
7474 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7475 PSA_ERROR_BAD_STATE);
7476
7477 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7478
7479 /* --- Aborts in all other places. --- */
7480 psa_sign_hash_abort(&sign_operation);
7481
7482 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7483 input_data->x, input_data->len));
7484
7485 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7486
7487 psa_interruptible_set_max_ops(1);
7488
7489 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7490 input_data->x, input_data->len));
7491
7492 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7493 signature_size,
7494 &signature_length),
7495 PSA_OPERATION_INCOMPLETE);
7496
7497 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7498
7499 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7500
7501 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7502 input_data->x, input_data->len));
7503
7504 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7505 signature_size,
7506 &signature_length));
7507
7508 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7509
7510 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7511
7512 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7513 input_data->x, input_data->len,
7514 signature, signature_length));
7515
7516 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7517
7518 psa_interruptible_set_max_ops(1);
7519
7520 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7521 input_data->x, input_data->len,
7522 signature, signature_length));
7523
7524 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7525 PSA_OPERATION_INCOMPLETE);
7526
7527 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7528
7529 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7530
7531 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7532 input_data->x, input_data->len,
7533 signature, signature_length));
7534
7535 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7536
7537 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7538
7539 /* --- Attempt double starts. --- */
7540
7541 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7542 input_data->x, input_data->len));
7543
7544 TEST_EQUAL(psa_sign_hash_start(&sign_operation, key, alg,
7545 input_data->x, input_data->len),
7546 PSA_ERROR_BAD_STATE);
7547
7548 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7549
7550 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7551 input_data->x, input_data->len,
7552 signature, signature_length));
7553
7554 TEST_EQUAL(psa_verify_hash_start(&verify_operation, key, alg,
7555 input_data->x, input_data->len,
7556 signature, signature_length),
7557 PSA_ERROR_BAD_STATE);
7558
7559 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7560
Paul Elliott76d671a2023-02-07 17:45:18 +00007561exit:
7562 /*
7563 * Key attributes may have been returned by psa_get_key_attributes()
7564 * thus reset them as required.
7565 */
7566 psa_reset_key_attributes(&attributes);
7567
7568 psa_destroy_key(key);
7569 mbedtls_free(signature);
7570 PSA_DONE();
7571}
7572/* END_CASE */
7573
7574/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007575/**
Paul Elliottc2033502023-02-26 17:09:14 +00007576 * interruptible_signverify_hash_edgecase_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007577 *
7578 * Note: This test can currently only handle ECDSA.
7579 *
7580 * 1. Test various edge cases in the interruptible sign and verify hash
7581 * interfaces.
7582 */
Paul Elliottc2033502023-02-26 17:09:14 +00007583void interruptible_signverify_hash_edgecase_tests(int key_type_arg,
Paul Elliott76d671a2023-02-07 17:45:18 +00007584 data_t *key_data, int alg_arg, data_t *input_data)
7585{
7586 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7587 psa_key_type_t key_type = key_type_arg;
7588 psa_algorithm_t alg = alg_arg;
7589 size_t key_bits;
7590 unsigned char *signature = NULL;
7591 size_t signature_size;
7592 size_t signature_length = 0xdeadbeef;
7593 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7594 uint8_t *input_buffer = NULL;
7595 psa_sign_hash_interruptible_operation_t sign_operation =
7596 psa_sign_hash_interruptible_operation_init();
7597 psa_verify_hash_interruptible_operation_t verify_operation =
7598 psa_verify_hash_interruptible_operation_init();
7599
7600 PSA_ASSERT(psa_crypto_init());
7601
7602 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7603 PSA_KEY_USAGE_VERIFY_HASH);
7604 psa_set_key_algorithm(&attributes, alg);
7605 psa_set_key_type(&attributes, key_type);
7606
7607 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7608 &key));
7609 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7610 key_bits = psa_get_key_bits(&attributes);
7611
7612 /* Allocate a buffer which has the size advertised by the
7613 * library. */
7614 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type,
7615 key_bits, alg);
7616 TEST_ASSERT(signature_size != 0);
7617 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007618 TEST_CALLOC(signature, signature_size);
Paul Elliott76d671a2023-02-07 17:45:18 +00007619
Paul Elliott20a36062022-12-18 13:21:25 +00007620 /* --- Change function inputs mid run, to cause an error (sign only,
7621 * verify passes all inputs to start. --- */
7622
7623 psa_interruptible_set_max_ops(1);
7624
7625 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7626 input_data->x, input_data->len));
7627
7628 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7629 signature_size,
7630 &signature_length),
7631 PSA_OPERATION_INCOMPLETE);
7632
7633 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7634 0,
7635 &signature_length),
7636 PSA_ERROR_BUFFER_TOO_SMALL);
7637
Paul Elliottc9774412023-02-06 15:14:07 +00007638 /* And test that this invalidates the operation. */
7639 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7640 0,
7641 &signature_length),
7642 PSA_ERROR_BAD_STATE);
7643
Paul Elliott20a36062022-12-18 13:21:25 +00007644 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7645
Paul Elliottf9c91a72023-02-05 18:06:38 +00007646 /* Trash the hash buffer in between start and complete, to ensure
7647 * no reliance on external buffers. */
7648 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7649
Paul Elliott6c68df42023-10-23 15:33:37 +01007650 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007651
7652 memcpy(input_buffer, input_data->x, input_data->len);
7653
7654 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7655 input_buffer, input_data->len));
7656
7657 memset(input_buffer, '!', input_data->len);
7658 mbedtls_free(input_buffer);
7659 input_buffer = NULL;
7660
7661 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7662 signature_size,
7663 &signature_length));
7664
7665 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7666
Paul Elliott6c68df42023-10-23 15:33:37 +01007667 TEST_CALLOC(input_buffer, input_data->len);
Paul Elliottf9c91a72023-02-05 18:06:38 +00007668
7669 memcpy(input_buffer, input_data->x, input_data->len);
7670
7671 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7672 input_buffer, input_data->len,
7673 signature, signature_length));
7674
7675 memset(input_buffer, '!', input_data->len);
7676 mbedtls_free(input_buffer);
7677 input_buffer = NULL;
7678
7679 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7680
7681 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7682
Paul Elliott20a36062022-12-18 13:21:25 +00007683exit:
7684 /*
7685 * Key attributes may have been returned by psa_get_key_attributes()
7686 * thus reset them as required.
7687 */
7688 psa_reset_key_attributes(&attributes);
7689
7690 psa_destroy_key(key);
7691 mbedtls_free(signature);
Paul Elliott6c68df42023-10-23 15:33:37 +01007692 mbedtls_free(input_buffer);
Paul Elliott20a36062022-12-18 13:21:25 +00007693 PSA_DONE();
7694}
7695/* END_CASE */
7696
Paul Elliotta4cb9092023-02-07 18:01:55 +00007697/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
Paul Elliottc7f68822023-02-24 17:37:04 +00007698/**
Paul Elliott57702242023-02-26 20:36:10 +00007699 * interruptible_signverify_hash_ops_tests() test intentions:
Paul Elliottc7f68822023-02-24 17:37:04 +00007700 *
7701 * Note: This test can currently only handle ECDSA.
7702 *
7703 * 1. Test that setting max ops is reflected in both interruptible sign and
7704 * verify hash
Paul Elliott9e8819f2023-02-26 19:01:35 +00007705 * 2. Test that changing the value of max_ops to unlimited during an operation
7706 * causes that operation to complete in the next call.
Paul Elliottc1e04002023-02-26 20:27:23 +00007707 *
7708 * 3. Test that calling get_num_ops() between complete calls gives the same
7709 * result as calling get_num_ops() once at the end of the operation.
Paul Elliottc7f68822023-02-24 17:37:04 +00007710 */
Paul Elliott57702242023-02-26 20:36:10 +00007711void interruptible_signverify_hash_ops_tests(int key_type_arg,
7712 data_t *key_data, int alg_arg,
7713 data_t *input_data)
Paul Elliotta4cb9092023-02-07 18:01:55 +00007714{
7715 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7716 psa_key_type_t key_type = key_type_arg;
7717 psa_algorithm_t alg = alg_arg;
7718 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Paul Elliottf1743e22023-02-15 18:44:16 +00007719 size_t key_bits;
7720 unsigned char *signature = NULL;
7721 size_t signature_size;
Paul Elliott9e8819f2023-02-26 19:01:35 +00007722 size_t signature_length = 0xdeadbeef;
Paul Elliottc1e04002023-02-26 20:27:23 +00007723 uint32_t num_ops = 0;
7724 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
7725
Paul Elliotta4cb9092023-02-07 18:01:55 +00007726 psa_sign_hash_interruptible_operation_t sign_operation =
7727 psa_sign_hash_interruptible_operation_init();
Paul Elliottf1743e22023-02-15 18:44:16 +00007728 psa_verify_hash_interruptible_operation_t verify_operation =
7729 psa_verify_hash_interruptible_operation_init();
Paul Elliotta4cb9092023-02-07 18:01:55 +00007730
7731 PSA_ASSERT(psa_crypto_init());
7732
7733 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH |
7734 PSA_KEY_USAGE_VERIFY_HASH);
7735 psa_set_key_algorithm(&attributes, alg);
7736 psa_set_key_type(&attributes, key_type);
7737
Paul Elliottf1743e22023-02-15 18:44:16 +00007738 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len, &key));
7739 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7740 key_bits = psa_get_key_bits(&attributes);
7741
7742 /* Allocate a buffer which has the size advertised by the
7743 * library. */
7744 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7745
7746 TEST_ASSERT(signature_size != 0);
7747 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007748 TEST_CALLOC(signature, signature_size);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007749
7750 /* Check that default max ops gets set if we don't set it. */
7751 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7752 input_data->x, input_data->len));
7753
7754 TEST_EQUAL(psa_interruptible_get_max_ops(),
7755 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7756
7757 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7758
Paul Elliottf1743e22023-02-15 18:44:16 +00007759 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7760 input_data->x, input_data->len,
7761 signature, signature_size));
7762
7763 TEST_EQUAL(psa_interruptible_get_max_ops(),
7764 PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7765
7766 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7767
Paul Elliotta4cb9092023-02-07 18:01:55 +00007768 /* Check that max ops gets set properly. */
7769
7770 psa_interruptible_set_max_ops(0xbeef);
7771
Paul Elliottf1743e22023-02-15 18:44:16 +00007772 TEST_EQUAL(psa_interruptible_get_max_ops(), 0xbeef);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007773
Paul Elliott9e8819f2023-02-26 19:01:35 +00007774 /* --- Ensure changing the max ops mid operation works (operation should
7775 * complete successfully after setting max ops to unlimited --- */
7776 psa_interruptible_set_max_ops(1);
7777
7778 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7779 input_data->x, input_data->len));
7780
7781 TEST_EQUAL(psa_sign_hash_complete(&sign_operation, signature,
7782 signature_size,
7783 &signature_length),
7784 PSA_OPERATION_INCOMPLETE);
7785
7786 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7787
7788 PSA_ASSERT(psa_sign_hash_complete(&sign_operation, signature,
7789 signature_size,
7790 &signature_length));
7791
7792 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7793
7794 psa_interruptible_set_max_ops(1);
7795
7796 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7797 input_data->x, input_data->len,
7798 signature, signature_length));
7799
7800 TEST_EQUAL(psa_verify_hash_complete(&verify_operation),
7801 PSA_OPERATION_INCOMPLETE);
7802
7803 psa_interruptible_set_max_ops(PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED);
7804
7805 PSA_ASSERT(psa_verify_hash_complete(&verify_operation));
7806
7807 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7808
Paul Elliottc1e04002023-02-26 20:27:23 +00007809 /* --- Test that not calling get_num_ops inbetween complete calls does not
7810 * result in lost ops. ---*/
7811
7812 psa_interruptible_set_max_ops(1);
7813
7814 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7815 input_data->x, input_data->len));
7816
7817 /* Continue performing the signature until complete. */
7818 do {
7819 status = psa_sign_hash_complete(&sign_operation, signature,
7820 signature_size,
7821 &signature_length);
7822
7823 num_ops = psa_sign_hash_get_num_ops(&sign_operation);
7824
7825 } while (status == PSA_OPERATION_INCOMPLETE);
7826
7827 PSA_ASSERT(status);
7828
7829 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7830
7831 PSA_ASSERT(psa_sign_hash_start(&sign_operation, key, alg,
7832 input_data->x, input_data->len));
7833
7834 /* Continue performing the signature until complete. */
7835 do {
7836 status = psa_sign_hash_complete(&sign_operation, signature,
7837 signature_size,
7838 &signature_length);
7839 } while (status == PSA_OPERATION_INCOMPLETE);
7840
7841 PSA_ASSERT(status);
7842
7843 TEST_EQUAL(num_ops, psa_sign_hash_get_num_ops(&sign_operation));
7844
7845 PSA_ASSERT(psa_sign_hash_abort(&sign_operation));
7846
7847 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7848 input_data->x, input_data->len,
7849 signature, signature_length));
7850
7851 /* Continue performing the verification until complete. */
7852 do {
7853 status = psa_verify_hash_complete(&verify_operation);
7854
7855 num_ops = psa_verify_hash_get_num_ops(&verify_operation);
7856
7857 } while (status == PSA_OPERATION_INCOMPLETE);
7858
7859 PSA_ASSERT(status);
7860
7861 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7862
7863 PSA_ASSERT(psa_verify_hash_start(&verify_operation, key, alg,
7864 input_data->x, input_data->len,
7865 signature, signature_length));
7866
7867 /* Continue performing the verification until complete. */
7868 do {
7869 status = psa_verify_hash_complete(&verify_operation);
7870
7871 } while (status == PSA_OPERATION_INCOMPLETE);
7872
7873 PSA_ASSERT(status);
7874
7875 TEST_EQUAL(num_ops, psa_verify_hash_get_num_ops(&verify_operation));
7876
7877 PSA_ASSERT(psa_verify_hash_abort(&verify_operation));
7878
Paul Elliotta4cb9092023-02-07 18:01:55 +00007879exit:
7880 /*
7881 * Key attributes may have been returned by psa_get_key_attributes()
7882 * thus reset them as required.
7883 */
7884 psa_reset_key_attributes(&attributes);
7885
7886 psa_destroy_key(key);
Paul Elliottf1743e22023-02-15 18:44:16 +00007887 mbedtls_free(signature);
Paul Elliotta4cb9092023-02-07 18:01:55 +00007888 PSA_DONE();
7889}
7890/* END_CASE */
Paul Elliott76d671a2023-02-07 17:45:18 +00007891
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007892/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007893void sign_message_deterministic(int key_type_arg,
7894 data_t *key_data,
7895 int alg_arg,
7896 data_t *input_data,
7897 data_t *output_data)
gabor-mezei-arm53028482021-04-15 18:19:50 +02007898{
7899 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7900 psa_key_type_t key_type = key_type_arg;
7901 psa_algorithm_t alg = alg_arg;
7902 size_t key_bits;
7903 unsigned char *signature = NULL;
7904 size_t signature_size;
7905 size_t signature_length = 0xdeadbeef;
7906 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7907
Gilles Peskine449bd832023-01-11 14:50:10 +01007908 PSA_ASSERT(psa_crypto_init());
gabor-mezei-arm53028482021-04-15 18:19:50 +02007909
Gilles Peskine449bd832023-01-11 14:50:10 +01007910 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7911 psa_set_key_algorithm(&attributes, alg);
7912 psa_set_key_type(&attributes, key_type);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007913
Gilles Peskine449bd832023-01-11 14:50:10 +01007914 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7915 &key));
7916 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
7917 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007918
Gilles Peskine449bd832023-01-11 14:50:10 +01007919 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
7920 TEST_ASSERT(signature_size != 0);
7921 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007922 TEST_CALLOC(signature, signature_size);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007923
Gilles Peskine449bd832023-01-11 14:50:10 +01007924 PSA_ASSERT(psa_sign_message(key, alg,
7925 input_data->x, input_data->len,
7926 signature, signature_size,
7927 &signature_length));
gabor-mezei-arm53028482021-04-15 18:19:50 +02007928
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01007929 TEST_MEMORY_COMPARE(output_data->x, output_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01007930 signature, signature_length);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007931
7932exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007933 psa_reset_key_attributes(&attributes);
gabor-mezei-arm53028482021-04-15 18:19:50 +02007934
Gilles Peskine449bd832023-01-11 14:50:10 +01007935 psa_destroy_key(key);
7936 mbedtls_free(signature);
7937 PSA_DONE();
gabor-mezei-arm53028482021-04-15 18:19:50 +02007938
7939}
7940/* END_CASE */
7941
7942/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007943void sign_message_fail(int key_type_arg,
7944 data_t *key_data,
7945 int alg_arg,
7946 data_t *input_data,
7947 int signature_size_arg,
7948 int expected_status_arg)
7949{
7950 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7951 psa_key_type_t key_type = key_type_arg;
7952 psa_algorithm_t alg = alg_arg;
7953 size_t signature_size = signature_size_arg;
7954 psa_status_t actual_status;
7955 psa_status_t expected_status = expected_status_arg;
7956 unsigned char *signature = NULL;
7957 size_t signature_length = 0xdeadbeef;
7958 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7959
Tom Cosgrove05b2a872023-07-21 11:31:13 +01007960 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01007961
7962 PSA_ASSERT(psa_crypto_init());
7963
7964 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
7965 psa_set_key_algorithm(&attributes, alg);
7966 psa_set_key_type(&attributes, key_type);
7967
7968 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
7969 &key));
7970
7971 actual_status = psa_sign_message(key, alg,
7972 input_data->x, input_data->len,
7973 signature, signature_size,
7974 &signature_length);
7975 TEST_EQUAL(actual_status, expected_status);
7976 /* The value of *signature_length is unspecified on error, but
7977 * whatever it is, it should be less than signature_size, so that
7978 * if the caller tries to read *signature_length bytes without
7979 * checking the error code then they don't overflow a buffer. */
7980 TEST_LE_U(signature_length, signature_size);
7981
7982exit:
7983 psa_reset_key_attributes(&attributes);
7984 psa_destroy_key(key);
7985 mbedtls_free(signature);
7986 PSA_DONE();
7987}
7988/* END_CASE */
7989
7990/* BEGIN_CASE */
7991void sign_verify_message(int key_type_arg,
7992 data_t *key_data,
7993 int alg_arg,
7994 data_t *input_data)
7995{
7996 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7997 psa_key_type_t key_type = key_type_arg;
7998 psa_algorithm_t alg = alg_arg;
7999 size_t key_bits;
8000 unsigned char *signature = NULL;
8001 size_t signature_size;
8002 size_t signature_length = 0xdeadbeef;
8003 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8004
8005 PSA_ASSERT(psa_crypto_init());
8006
8007 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
8008 PSA_KEY_USAGE_VERIFY_MESSAGE);
8009 psa_set_key_algorithm(&attributes, alg);
8010 psa_set_key_type(&attributes, key_type);
8011
8012 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8013 &key));
8014 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8015 key_bits = psa_get_key_bits(&attributes);
8016
8017 signature_size = PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg);
8018 TEST_ASSERT(signature_size != 0);
8019 TEST_LE_U(signature_size, PSA_SIGNATURE_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008020 TEST_CALLOC(signature, signature_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008021
8022 PSA_ASSERT(psa_sign_message(key, alg,
8023 input_data->x, input_data->len,
8024 signature, signature_size,
8025 &signature_length));
8026 TEST_LE_U(signature_length, signature_size);
8027 TEST_ASSERT(signature_length > 0);
8028
8029 PSA_ASSERT(psa_verify_message(key, alg,
8030 input_data->x, input_data->len,
8031 signature, signature_length));
8032
8033 if (input_data->len != 0) {
8034 /* Flip a bit in the input and verify that the signature is now
8035 * detected as invalid. Flip a bit at the beginning, not at the end,
8036 * because ECDSA may ignore the last few bits of the input. */
8037 input_data->x[0] ^= 1;
8038 TEST_EQUAL(psa_verify_message(key, alg,
8039 input_data->x, input_data->len,
8040 signature, signature_length),
8041 PSA_ERROR_INVALID_SIGNATURE);
8042 }
8043
8044exit:
8045 psa_reset_key_attributes(&attributes);
8046
8047 psa_destroy_key(key);
8048 mbedtls_free(signature);
8049 PSA_DONE();
8050}
8051/* END_CASE */
8052
8053/* BEGIN_CASE */
8054void verify_message(int key_type_arg,
8055 data_t *key_data,
8056 int alg_arg,
8057 data_t *input_data,
8058 data_t *signature_data)
8059{
8060 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8061 psa_key_type_t key_type = key_type_arg;
8062 psa_algorithm_t alg = alg_arg;
8063 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8064
8065 TEST_LE_U(signature_data->len, PSA_SIGNATURE_MAX_SIZE);
8066
8067 PSA_ASSERT(psa_crypto_init());
8068
8069 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8070 psa_set_key_algorithm(&attributes, alg);
8071 psa_set_key_type(&attributes, key_type);
8072
8073 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8074 &key));
8075
8076 PSA_ASSERT(psa_verify_message(key, alg,
8077 input_data->x, input_data->len,
8078 signature_data->x, signature_data->len));
8079
8080exit:
8081 psa_reset_key_attributes(&attributes);
8082 psa_destroy_key(key);
8083 PSA_DONE();
8084}
8085/* END_CASE */
8086
8087/* BEGIN_CASE */
8088void verify_message_fail(int key_type_arg,
8089 data_t *key_data,
8090 int alg_arg,
8091 data_t *hash_data,
8092 data_t *signature_data,
8093 int expected_status_arg)
8094{
8095 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8096 psa_key_type_t key_type = key_type_arg;
8097 psa_algorithm_t alg = alg_arg;
8098 psa_status_t actual_status;
8099 psa_status_t expected_status = expected_status_arg;
8100 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8101
8102 PSA_ASSERT(psa_crypto_init());
8103
8104 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE);
8105 psa_set_key_algorithm(&attributes, alg);
8106 psa_set_key_type(&attributes, key_type);
8107
8108 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8109 &key));
8110
8111 actual_status = psa_verify_message(key, alg,
8112 hash_data->x, hash_data->len,
8113 signature_data->x,
8114 signature_data->len);
8115 TEST_EQUAL(actual_status, expected_status);
8116
8117exit:
8118 psa_reset_key_attributes(&attributes);
8119 psa_destroy_key(key);
8120 PSA_DONE();
8121}
8122/* END_CASE */
8123
8124/* BEGIN_CASE */
8125void asymmetric_encrypt(int key_type_arg,
gabor-mezei-arm53028482021-04-15 18:19:50 +02008126 data_t *key_data,
8127 int alg_arg,
8128 data_t *input_data,
Gilles Peskine449bd832023-01-11 14:50:10 +01008129 data_t *label,
8130 int expected_output_length_arg,
8131 int expected_status_arg)
Gilles Peskine656896e2018-06-29 19:12:28 +02008132{
Ronald Cron5425a212020-08-04 14:58:35 +02008133 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008134 psa_key_type_t key_type = key_type_arg;
8135 psa_algorithm_t alg = alg_arg;
8136 size_t expected_output_length = expected_output_length_arg;
8137 size_t key_bits;
8138 unsigned char *output = NULL;
8139 size_t output_size;
8140 size_t output_length = ~0;
8141 psa_status_t actual_status;
8142 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008143 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02008144
Gilles Peskine449bd832023-01-11 14:50:10 +01008145 PSA_ASSERT(psa_crypto_init());
Gilles Peskinebdf309c2018-12-03 15:36:32 +01008146
Gilles Peskine656896e2018-06-29 19:12:28 +02008147 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01008148 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8149 psa_set_key_algorithm(&attributes, alg);
8150 psa_set_key_type(&attributes, key_type);
8151 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8152 &key));
Gilles Peskine656896e2018-06-29 19:12:28 +02008153
8154 /* Determine the maximum output length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008155 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8156 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008157
Gilles Peskine449bd832023-01-11 14:50:10 +01008158 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8159 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008160 TEST_CALLOC(output, output_size);
Gilles Peskine656896e2018-06-29 19:12:28 +02008161
8162 /* Encrypt the input */
Gilles Peskine449bd832023-01-11 14:50:10 +01008163 actual_status = psa_asymmetric_encrypt(key, alg,
8164 input_data->x, input_data->len,
8165 label->x, label->len,
8166 output, output_size,
8167 &output_length);
8168 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008169 if (actual_status == PSA_SUCCESS) {
8170 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008171 } else {
8172 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008173 }
Gilles Peskine656896e2018-06-29 19:12:28 +02008174
Gilles Peskine68428122018-06-30 18:42:41 +02008175 /* If the label is empty, the test framework puts a non-null pointer
8176 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008177 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008178 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008179 if (output_size != 0) {
8180 memset(output, 0, output_size);
8181 }
8182 actual_status = psa_asymmetric_encrypt(key, alg,
8183 input_data->x, input_data->len,
8184 NULL, label->len,
8185 output, output_size,
8186 &output_length);
8187 TEST_EQUAL(actual_status, expected_status);
oberon-sk10c0f772023-02-13 13:42:02 +01008188 if (actual_status == PSA_SUCCESS) {
8189 TEST_EQUAL(output_length, expected_output_length);
Stephan Koch5819d2c2023-02-22 13:39:21 +01008190 } else {
8191 TEST_LE_U(output_length, output_size);
oberon-sk10c0f772023-02-13 13:42:02 +01008192 }
Gilles Peskine68428122018-06-30 18:42:41 +02008193 }
8194
Gilles Peskine656896e2018-06-29 19:12:28 +02008195exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008196 /*
8197 * Key attributes may have been returned by psa_get_key_attributes()
8198 * thus reset them as required.
8199 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008200 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008201
Gilles Peskine449bd832023-01-11 14:50:10 +01008202 psa_destroy_key(key);
8203 mbedtls_free(output);
8204 PSA_DONE();
Gilles Peskine656896e2018-06-29 19:12:28 +02008205}
8206/* END_CASE */
8207
8208/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008209void asymmetric_encrypt_decrypt(int key_type_arg,
8210 data_t *key_data,
8211 int alg_arg,
8212 data_t *input_data,
8213 data_t *label)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008214{
Ronald Cron5425a212020-08-04 14:58:35 +02008215 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008216 psa_key_type_t key_type = key_type_arg;
8217 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008218 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008219 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008220 size_t output_size;
8221 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008222 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008223 size_t output2_size;
8224 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008225 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008226
Gilles Peskine449bd832023-01-11 14:50:10 +01008227 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008228
Gilles Peskine449bd832023-01-11 14:50:10 +01008229 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
8230 psa_set_key_algorithm(&attributes, alg);
8231 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008232
Gilles Peskine449bd832023-01-11 14:50:10 +01008233 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8234 &key));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008235
8236 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008237 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8238 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008239
Gilles Peskine449bd832023-01-11 14:50:10 +01008240 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8241 TEST_LE_U(output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008242 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008243
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008244 output2_size = input_data->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008245 TEST_LE_U(output2_size,
8246 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg));
8247 TEST_LE_U(output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008248 TEST_CALLOC(output2, output2_size);
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008249
Gilles Peskineeebd7382018-06-08 18:11:54 +02008250 /* We test encryption by checking that encrypt-then-decrypt gives back
8251 * the original plaintext because of the non-optional random
8252 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008253 PSA_ASSERT(psa_asymmetric_encrypt(key, alg,
8254 input_data->x, input_data->len,
8255 label->x, label->len,
8256 output, output_size,
8257 &output_length));
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008258 /* We don't know what ciphertext length to expect, but check that
8259 * it looks sensible. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008260 TEST_LE_U(output_length, output_size);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03008261
Gilles Peskine449bd832023-01-11 14:50:10 +01008262 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8263 output, output_length,
8264 label->x, label->len,
8265 output2, output2_size,
8266 &output2_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008267 TEST_MEMORY_COMPARE(input_data->x, input_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008268 output2, output2_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008269
8270exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008271 /*
8272 * Key attributes may have been returned by psa_get_key_attributes()
8273 * thus reset them as required.
8274 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008275 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008276
Gilles Peskine449bd832023-01-11 14:50:10 +01008277 psa_destroy_key(key);
8278 mbedtls_free(output);
8279 mbedtls_free(output2);
8280 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008281}
8282/* END_CASE */
8283
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008284/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008285void asymmetric_decrypt(int key_type_arg,
8286 data_t *key_data,
8287 int alg_arg,
8288 data_t *input_data,
8289 data_t *label,
8290 data_t *expected_data)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008291{
Ronald Cron5425a212020-08-04 14:58:35 +02008292 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008293 psa_key_type_t key_type = key_type_arg;
8294 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01008295 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008296 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03008297 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008298 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008299 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008300
Gilles Peskine449bd832023-01-11 14:50:10 +01008301 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008302
Gilles Peskine449bd832023-01-11 14:50:10 +01008303 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8304 psa_set_key_algorithm(&attributes, alg);
8305 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008306
Gilles Peskine449bd832023-01-11 14:50:10 +01008307 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8308 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008309
Gilles Peskine449bd832023-01-11 14:50:10 +01008310 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
8311 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01008312
8313 /* Determine the maximum ciphertext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008314 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg);
8315 TEST_LE_U(output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008316 TEST_CALLOC(output, output_size);
gabor-mezei-armceface22021-01-21 12:26:17 +01008317
Gilles Peskine449bd832023-01-11 14:50:10 +01008318 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8319 input_data->x, input_data->len,
8320 label->x, label->len,
8321 output,
8322 output_size,
8323 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008324 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008325 output, output_length);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008326
Gilles Peskine68428122018-06-30 18:42:41 +02008327 /* If the label is empty, the test framework puts a non-null pointer
8328 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008329 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008330 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008331 if (output_size != 0) {
8332 memset(output, 0, output_size);
8333 }
8334 PSA_ASSERT(psa_asymmetric_decrypt(key, alg,
8335 input_data->x, input_data->len,
8336 NULL, label->len,
8337 output,
8338 output_size,
8339 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008340 TEST_MEMORY_COMPARE(expected_data->x, expected_data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008341 output, output_length);
Gilles Peskine68428122018-06-30 18:42:41 +02008342 }
8343
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008344exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008345 psa_reset_key_attributes(&attributes);
8346 psa_destroy_key(key);
8347 mbedtls_free(output);
8348 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008349}
8350/* END_CASE */
8351
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008352/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008353void asymmetric_decrypt_fail(int key_type_arg,
8354 data_t *key_data,
8355 int alg_arg,
8356 data_t *input_data,
8357 data_t *label,
8358 int output_size_arg,
8359 int expected_status_arg)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008360{
Ronald Cron5425a212020-08-04 14:58:35 +02008361 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008362 psa_key_type_t key_type = key_type_arg;
8363 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008364 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00008365 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02008366 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008367 psa_status_t actual_status;
8368 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008369 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008370
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008371 TEST_CALLOC(output, output_size);
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008372
Gilles Peskine449bd832023-01-11 14:50:10 +01008373 PSA_ASSERT(psa_crypto_init());
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008374
Gilles Peskine449bd832023-01-11 14:50:10 +01008375 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
8376 psa_set_key_algorithm(&attributes, alg);
8377 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheind7082602018-06-04 16:45:27 +03008378
Gilles Peskine449bd832023-01-11 14:50:10 +01008379 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8380 &key));
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008381
Gilles Peskine449bd832023-01-11 14:50:10 +01008382 actual_status = psa_asymmetric_decrypt(key, alg,
8383 input_data->x, input_data->len,
8384 label->x, label->len,
8385 output, output_size,
8386 &output_length);
8387 TEST_EQUAL(actual_status, expected_status);
8388 TEST_LE_U(output_length, output_size);
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008389
Gilles Peskine68428122018-06-30 18:42:41 +02008390 /* If the label is empty, the test framework puts a non-null pointer
8391 * in label->x. Test that a null pointer works as well. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008392 if (label->len == 0) {
Gilles Peskine68428122018-06-30 18:42:41 +02008393 output_length = ~0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008394 if (output_size != 0) {
8395 memset(output, 0, output_size);
8396 }
8397 actual_status = psa_asymmetric_decrypt(key, alg,
8398 input_data->x, input_data->len,
8399 NULL, label->len,
8400 output, output_size,
8401 &output_length);
8402 TEST_EQUAL(actual_status, expected_status);
8403 TEST_LE_U(output_length, output_size);
Gilles Peskine68428122018-06-30 18:42:41 +02008404 }
8405
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008406exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008407 psa_reset_key_attributes(&attributes);
8408 psa_destroy_key(key);
8409 mbedtls_free(output);
8410 PSA_DONE();
Nir Sonnenschein39e59142018-05-02 23:16:26 +03008411}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02008412/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02008413
8414/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008415void key_derivation_init()
Jaeden Amerod94d6712019-01-04 14:11:48 +00008416{
8417 /* Test each valid way of initializing the object, except for `= {0}`, as
8418 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
8419 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008420 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008421 size_t capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008422 psa_key_derivation_operation_t func = psa_key_derivation_operation_init();
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008423 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
8424 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00008425
Gilles Peskine449bd832023-01-11 14:50:10 +01008426 memset(&zero, 0, sizeof(zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008427
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008428 /* A default operation should not be able to report its capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008429 TEST_EQUAL(psa_key_derivation_get_capacity(&func, &capacity),
8430 PSA_ERROR_BAD_STATE);
8431 TEST_EQUAL(psa_key_derivation_get_capacity(&init, &capacity),
8432 PSA_ERROR_BAD_STATE);
8433 TEST_EQUAL(psa_key_derivation_get_capacity(&zero, &capacity),
8434 PSA_ERROR_BAD_STATE);
Jaeden Amero5229bbb2019-02-07 16:33:37 +00008435
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008436 /* A default operation should be abortable without error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008437 PSA_ASSERT(psa_key_derivation_abort(&func));
8438 PSA_ASSERT(psa_key_derivation_abort(&init));
8439 PSA_ASSERT(psa_key_derivation_abort(&zero));
Jaeden Amerod94d6712019-01-04 14:11:48 +00008440}
8441/* END_CASE */
8442
Janos Follath16de4a42019-06-13 16:32:24 +01008443/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008444void derive_setup(int alg_arg, int expected_status_arg)
Gilles Peskineea0fb492018-07-12 17:17:20 +02008445{
Gilles Peskineea0fb492018-07-12 17:17:20 +02008446 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008447 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008448 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02008449
Gilles Peskine449bd832023-01-11 14:50:10 +01008450 PSA_ASSERT(psa_crypto_init());
Gilles Peskineea0fb492018-07-12 17:17:20 +02008451
Gilles Peskine449bd832023-01-11 14:50:10 +01008452 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8453 expected_status);
Gilles Peskineea0fb492018-07-12 17:17:20 +02008454
8455exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008456 psa_key_derivation_abort(&operation);
8457 PSA_DONE();
Gilles Peskineea0fb492018-07-12 17:17:20 +02008458}
8459/* END_CASE */
8460
Janos Follathaf3c2a02019-06-12 12:34:34 +01008461/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008462void derive_set_capacity(int alg_arg, int capacity_arg,
8463 int expected_status_arg)
Janos Follatha27c9272019-06-14 09:59:36 +01008464{
8465 psa_algorithm_t alg = alg_arg;
8466 size_t capacity = capacity_arg;
8467 psa_status_t expected_status = expected_status_arg;
8468 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8469
Gilles Peskine449bd832023-01-11 14:50:10 +01008470 PSA_ASSERT(psa_crypto_init());
Janos Follatha27c9272019-06-14 09:59:36 +01008471
Gilles Peskine449bd832023-01-11 14:50:10 +01008472 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follatha27c9272019-06-14 09:59:36 +01008473
Gilles Peskine449bd832023-01-11 14:50:10 +01008474 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
8475 expected_status);
Janos Follatha27c9272019-06-14 09:59:36 +01008476
8477exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008478 psa_key_derivation_abort(&operation);
8479 PSA_DONE();
Janos Follatha27c9272019-06-14 09:59:36 +01008480}
8481/* END_CASE */
8482
8483/* BEGIN_CASE */
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308484void parse_binary_string_test(data_t *input, int output)
8485{
8486 uint64_t value;
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308487 value = mbedtls_test_parse_binary_string(input);
Kusumit Ghoderaod60dfc02023-05-01 17:39:27 +05308488 TEST_EQUAL(value, output);
8489}
8490/* END_CASE */
8491
8492/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008493void derive_input(int alg_arg,
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308494 int step_arg1, int key_type_arg1, data_t *input1,
8495 int expected_status_arg1,
8496 int step_arg2, int key_type_arg2, data_t *input2,
8497 int expected_status_arg2,
8498 int step_arg3, int key_type_arg3, data_t *input3,
8499 int expected_status_arg3,
Gilles Peskine449bd832023-01-11 14:50:10 +01008500 int output_key_type_arg, int expected_output_status_arg)
Janos Follathaf3c2a02019-06-12 12:34:34 +01008501{
8502 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008503 psa_key_derivation_step_t steps[] = { step_arg1, step_arg2, step_arg3 };
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308504 uint32_t key_types[] = { key_type_arg1, key_type_arg2, key_type_arg3 };
Gilles Peskine449bd832023-01-11 14:50:10 +01008505 psa_status_t expected_statuses[] = { expected_status_arg1,
8506 expected_status_arg2,
8507 expected_status_arg3 };
8508 data_t *inputs[] = { input1, input2, input3 };
Ronald Cron5425a212020-08-04 14:58:35 +02008509 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8510 MBEDTLS_SVC_KEY_ID_INIT,
8511 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01008512 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8513 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8514 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008515 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008516 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008517 psa_status_t expected_output_status = expected_output_status_arg;
8518 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01008519
Gilles Peskine449bd832023-01-11 14:50:10 +01008520 PSA_ASSERT(psa_crypto_init());
Janos Follathaf3c2a02019-06-12 12:34:34 +01008521
Gilles Peskine449bd832023-01-11 14:50:10 +01008522 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8523 psa_set_key_algorithm(&attributes, alg);
Janos Follathaf3c2a02019-06-12 12:34:34 +01008524
Gilles Peskine449bd832023-01-11 14:50:10 +01008525 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
Janos Follathaf3c2a02019-06-12 12:34:34 +01008526
Gilles Peskine449bd832023-01-11 14:50:10 +01008527 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8528 mbedtls_test_set_step(i);
8529 if (steps[i] == 0) {
Gilles Peskine4023c012021-05-27 13:21:20 +02008530 /* Skip this step */
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308531 } else if (((psa_key_type_t) key_types[i]) != PSA_KEY_TYPE_NONE &&
8532 key_types[i] != INPUT_INTEGER) {
8533 psa_set_key_type(&attributes, ((psa_key_type_t) key_types[i]));
Gilles Peskine449bd832023-01-11 14:50:10 +01008534 PSA_ASSERT(psa_import_key(&attributes,
8535 inputs[i]->x, inputs[i]->len,
8536 &keys[i]));
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308537 if (PSA_KEY_TYPE_IS_KEY_PAIR((psa_key_type_t) key_types[i]) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008538 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET) {
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008539 // When taking a private key as secret input, use key agreement
8540 // to add the shared secret to the derivation
Gilles Peskine449bd832023-01-11 14:50:10 +01008541 TEST_EQUAL(mbedtls_test_psa_key_agreement_with_self(
8542 &operation, keys[i]),
8543 expected_statuses[i]);
8544 } else {
8545 TEST_EQUAL(psa_key_derivation_input_key(&operation, steps[i],
8546 keys[i]),
8547 expected_statuses[i]);
Steven Cooreman0ee0d522020-10-05 16:03:42 +02008548 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008549 } else {
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308550 if (key_types[i] == INPUT_INTEGER) {
8551 TEST_EQUAL(psa_key_derivation_input_integer(
8552 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308553 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderao12e0b4b2023-04-27 16:58:23 +05308554 expected_statuses[i]);
8555 } else {
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308556 TEST_EQUAL(psa_key_derivation_input_bytes(
8557 &operation, steps[i],
8558 inputs[i]->x, inputs[i]->len),
8559 expected_statuses[i]);
Kusumit Ghoderao02326d52023-04-06 17:47:59 +05308560 }
Janos Follathaf3c2a02019-06-12 12:34:34 +01008561 }
8562 }
8563
Gilles Peskine449bd832023-01-11 14:50:10 +01008564 if (output_key_type != PSA_KEY_TYPE_NONE) {
8565 psa_reset_key_attributes(&attributes);
8566 psa_set_key_type(&attributes, output_key_type);
8567 psa_set_key_bits(&attributes, 8);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008568 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008569 psa_key_derivation_output_key(&attributes, &operation,
8570 &output_key);
8571 } else {
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008572 uint8_t buffer[1];
8573 actual_output_status =
Gilles Peskine449bd832023-01-11 14:50:10 +01008574 psa_key_derivation_output_bytes(&operation,
8575 buffer, sizeof(buffer));
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008576 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008577 TEST_EQUAL(actual_output_status, expected_output_status);
Gilles Peskine1a2904c2019-09-24 17:45:07 +02008578
Janos Follathaf3c2a02019-06-12 12:34:34 +01008579exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008580 psa_key_derivation_abort(&operation);
8581 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8582 psa_destroy_key(keys[i]);
8583 }
8584 psa_destroy_key(output_key);
8585 PSA_DONE();
Janos Follathaf3c2a02019-06-12 12:34:34 +01008586}
8587/* END_CASE */
8588
Kusumit Ghoderao42b02b92023-06-06 16:48:46 +05308589/* BEGIN_CASE*/
8590void derive_input_invalid_cost(int alg_arg, int64_t cost)
8591{
8592 psa_algorithm_t alg = alg_arg;
8593 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8594
8595 PSA_ASSERT(psa_crypto_init());
8596 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8597
8598 TEST_EQUAL(psa_key_derivation_input_integer(&operation,
8599 PSA_KEY_DERIVATION_INPUT_COST,
8600 cost),
8601 PSA_ERROR_NOT_SUPPORTED);
8602
8603exit:
8604 psa_key_derivation_abort(&operation);
8605 PSA_DONE();
8606}
8607/* END_CASE*/
8608
Janos Follathd958bb72019-07-03 15:02:16 +01008609/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008610void derive_over_capacity(int alg_arg)
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008611{
Janos Follathd958bb72019-07-03 15:02:16 +01008612 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02008613 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02008614 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008615 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01008616 unsigned char input1[] = "Input 1";
Gilles Peskine449bd832023-01-11 14:50:10 +01008617 size_t input1_length = sizeof(input1);
Janos Follathd958bb72019-07-03 15:02:16 +01008618 unsigned char input2[] = "Input 2";
Gilles Peskine449bd832023-01-11 14:50:10 +01008619 size_t input2_length = sizeof(input2);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008620 uint8_t buffer[42];
Gilles Peskine449bd832023-01-11 14:50:10 +01008621 size_t capacity = sizeof(buffer);
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02008622 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
8623 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
Gilles Peskine449bd832023-01-11 14:50:10 +01008624 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02008625 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008626
Gilles Peskine449bd832023-01-11 14:50:10 +01008627 PSA_ASSERT(psa_crypto_init());
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008628
Gilles Peskine449bd832023-01-11 14:50:10 +01008629 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8630 psa_set_key_algorithm(&attributes, alg);
8631 psa_set_key_type(&attributes, key_type);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008632
Gilles Peskine449bd832023-01-11 14:50:10 +01008633 PSA_ASSERT(psa_import_key(&attributes,
8634 key_data, sizeof(key_data),
8635 &key));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008636
8637 /* valid key derivation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008638 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8639 input1, input1_length,
8640 input2, input2_length,
8641 capacity)) {
Janos Follathd958bb72019-07-03 15:02:16 +01008642 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008643 }
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008644
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008645 /* state of operation shouldn't allow additional generation */
Gilles Peskine449bd832023-01-11 14:50:10 +01008646 TEST_EQUAL(psa_key_derivation_setup(&operation, alg),
8647 PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008648
Gilles Peskine449bd832023-01-11 14:50:10 +01008649 PSA_ASSERT(psa_key_derivation_output_bytes(&operation, buffer, capacity));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008650
Gilles Peskine449bd832023-01-11 14:50:10 +01008651 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, buffer, capacity),
8652 PSA_ERROR_INSUFFICIENT_DATA);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008653
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008654exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008655 psa_key_derivation_abort(&operation);
8656 psa_destroy_key(key);
8657 PSA_DONE();
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008658}
8659/* END_CASE */
8660
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008661/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008662void derive_actions_without_setup()
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008663{
8664 uint8_t output_buffer[16];
8665 size_t buffer_size = 16;
8666 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008667 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008668
Gilles Peskine449bd832023-01-11 14:50:10 +01008669 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8670 output_buffer, buffer_size)
8671 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008672
Gilles Peskine449bd832023-01-11 14:50:10 +01008673 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8674 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008675
Gilles Peskine449bd832023-01-11 14:50:10 +01008676 PSA_ASSERT(psa_key_derivation_abort(&operation));
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008677
Gilles Peskine449bd832023-01-11 14:50:10 +01008678 TEST_ASSERT(psa_key_derivation_output_bytes(&operation,
8679 output_buffer, buffer_size)
8680 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008681
Gilles Peskine449bd832023-01-11 14:50:10 +01008682 TEST_ASSERT(psa_key_derivation_get_capacity(&operation, &capacity)
8683 == PSA_ERROR_BAD_STATE);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03008684
8685exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008686 psa_key_derivation_abort(&operation);
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03008687}
8688/* END_CASE */
8689
8690/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008691void derive_output(int alg_arg,
8692 int step1_arg, data_t *input1, int expected_status_arg1,
8693 int step2_arg, data_t *input2, int expected_status_arg2,
8694 int step3_arg, data_t *input3, int expected_status_arg3,
8695 int step4_arg, data_t *input4, int expected_status_arg4,
8696 data_t *key_agreement_peer_key,
8697 int requested_capacity_arg,
8698 data_t *expected_output1,
8699 data_t *expected_output2,
8700 int other_key_input_type,
8701 int key_input_type,
8702 int derive_type)
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008703{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008704 psa_algorithm_t alg = alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01008705 psa_key_derivation_step_t steps[] = { step1_arg, step2_arg, step3_arg, step4_arg };
8706 data_t *inputs[] = { input1, input2, input3, input4 };
8707 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
8708 MBEDTLS_SVC_KEY_ID_INIT,
8709 MBEDTLS_SVC_KEY_ID_INIT,
8710 MBEDTLS_SVC_KEY_ID_INIT };
8711 psa_status_t statuses[] = { expected_status_arg1, expected_status_arg2,
8712 expected_status_arg3, expected_status_arg4 };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008713 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008714 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008715 uint8_t *expected_outputs[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008716 { expected_output1->x, expected_output2->x };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008717 size_t output_sizes[2] =
Gilles Peskine449bd832023-01-11 14:50:10 +01008718 { expected_output1->len, expected_output2->len };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008719 size_t output_buffer_size = 0;
8720 uint8_t *output_buffer = NULL;
8721 size_t expected_capacity;
8722 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008723 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
8724 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
8725 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
8726 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008727 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008728 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02008729 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008730
Gilles Peskine449bd832023-01-11 14:50:10 +01008731 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
8732 if (output_sizes[i] > output_buffer_size) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008733 output_buffer_size = output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008734 }
8735 if (output_sizes[i] == 0) {
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008736 expected_outputs[i] = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008737 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008738 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01008739 TEST_CALLOC(output_buffer, output_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008740 PSA_ASSERT(psa_crypto_init());
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008741
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008742 /* Extraction phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008743 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
8744 PSA_ASSERT(psa_key_derivation_set_capacity(&operation,
8745 requested_capacity));
8746 for (i = 0; i < ARRAY_LENGTH(steps); i++) {
8747 switch (steps[i]) {
Gilles Peskine1468da72019-05-29 17:35:49 +02008748 case 0:
8749 break;
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308750 case PSA_KEY_DERIVATION_INPUT_COST:
8751 TEST_EQUAL(psa_key_derivation_input_integer(
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308752 &operation, steps[i],
Kusumit Ghoderao7c61ffc2023-09-05 19:29:47 +05308753 mbedtls_test_parse_binary_string(inputs[i])),
Kusumit Ghoderaof28e0f52023-06-06 15:03:22 +05308754 statuses[i]);
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308755 if (statuses[i] != PSA_SUCCESS) {
8756 goto exit;
8757 }
8758 break;
8759 case PSA_KEY_DERIVATION_INPUT_PASSWORD:
Gilles Peskine1468da72019-05-29 17:35:49 +02008760 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008761 switch (key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008762 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008763 TEST_EQUAL(psa_key_derivation_input_bytes(
8764 &operation, steps[i],
8765 inputs[i]->x, inputs[i]->len),
8766 statuses[i]);
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008767
Gilles Peskine449bd832023-01-11 14:50:10 +01008768 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielfcdd0232022-05-19 10:28:58 +02008769 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008770 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008771 break;
8772 case 1: // input key
Gilles Peskine449bd832023-01-11 14:50:10 +01008773 psa_set_key_usage_flags(&attributes1, PSA_KEY_USAGE_DERIVE);
8774 psa_set_key_algorithm(&attributes1, alg);
8775 psa_set_key_type(&attributes1, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008776
Gilles Peskine449bd832023-01-11 14:50:10 +01008777 PSA_ASSERT(psa_import_key(&attributes1,
8778 inputs[i]->x, inputs[i]->len,
8779 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008780
Gilles Peskine449bd832023-01-11 14:50:10 +01008781 if (PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
8782 PSA_ASSERT(psa_get_key_attributes(keys[i], &attributes1));
8783 TEST_LE_U(PSA_BITS_TO_BYTES(psa_get_key_bits(&attributes1)),
8784 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008785 }
8786
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308787 TEST_EQUAL(psa_key_derivation_input_key(&operation,
Gilles Peskine449bd832023-01-11 14:50:10 +01008788 steps[i],
Kusumit Ghoderao81797fc2023-06-05 15:05:09 +05308789 keys[i]),
8790 statuses[i]);
8791
8792 if (statuses[i] != PSA_SUCCESS) {
8793 goto exit;
8794 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008795 break;
8796 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008797 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008798 break;
8799 }
8800 break;
8801 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +01008802 switch (other_key_input_type) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008803 case 0: // input bytes
Gilles Peskine449bd832023-01-11 14:50:10 +01008804 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
8805 steps[i],
8806 inputs[i]->x,
8807 inputs[i]->len),
8808 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008809 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02008810 case 1: // input key, type DERIVE
8811 case 11: // input key, type RAW
Gilles Peskine449bd832023-01-11 14:50:10 +01008812 psa_set_key_usage_flags(&attributes2, PSA_KEY_USAGE_DERIVE);
8813 psa_set_key_algorithm(&attributes2, alg);
8814 psa_set_key_type(&attributes2, PSA_KEY_TYPE_DERIVE);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008815
8816 // other secret of type RAW_DATA passed with input_key
Gilles Peskine449bd832023-01-11 14:50:10 +01008817 if (other_key_input_type == 11) {
8818 psa_set_key_type(&attributes2, PSA_KEY_TYPE_RAW_DATA);
8819 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008820
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 PSA_ASSERT(psa_import_key(&attributes2,
8822 inputs[i]->x, inputs[i]->len,
8823 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008824
Gilles Peskine449bd832023-01-11 14:50:10 +01008825 TEST_EQUAL(psa_key_derivation_input_key(&operation,
8826 steps[i],
8827 keys[i]),
8828 statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008829 break;
8830 case 2: // key agreement
Gilles Peskine449bd832023-01-11 14:50:10 +01008831 psa_set_key_usage_flags(&attributes3, PSA_KEY_USAGE_DERIVE);
8832 psa_set_key_algorithm(&attributes3, alg);
8833 psa_set_key_type(&attributes3,
8834 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008835
Gilles Peskine449bd832023-01-11 14:50:10 +01008836 PSA_ASSERT(psa_import_key(&attributes3,
8837 inputs[i]->x, inputs[i]->len,
8838 &keys[i]));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008839
Gilles Peskine449bd832023-01-11 14:50:10 +01008840 TEST_EQUAL(psa_key_derivation_key_agreement(
8841 &operation,
8842 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
8843 keys[i], key_agreement_peer_key->x,
8844 key_agreement_peer_key->len), statuses[i]);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008845 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008846 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01008847 TEST_FAIL("default case not supported");
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008848 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01008849 }
8850
Gilles Peskine449bd832023-01-11 14:50:10 +01008851 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008852 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008853 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008854 break;
8855 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008856 TEST_EQUAL(psa_key_derivation_input_bytes(
8857 &operation, steps[i],
8858 inputs[i]->x, inputs[i]->len), statuses[i]);
Przemek Stekielead1bb92022-05-11 12:22:57 +02008859
Gilles Peskine449bd832023-01-11 14:50:10 +01008860 if (statuses[i] != PSA_SUCCESS) {
Przemek Stekielead1bb92022-05-11 12:22:57 +02008861 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008862 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008863 break;
8864 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01008865 }
Gilles Peskine1468da72019-05-29 17:35:49 +02008866
Gilles Peskine449bd832023-01-11 14:50:10 +01008867 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8868 &current_capacity));
8869 TEST_EQUAL(current_capacity, requested_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008870 expected_capacity = requested_capacity;
8871
Gilles Peskine449bd832023-01-11 14:50:10 +01008872 if (derive_type == 1) { // output key
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008873 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
8874
8875 /* For output key derivation secret must be provided using
8876 input key, otherwise operation is not permitted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008877 if (key_input_type == 1) {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02008878 expected_status = PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +01008879 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008880
Gilles Peskine449bd832023-01-11 14:50:10 +01008881 psa_set_key_usage_flags(&attributes4, PSA_KEY_USAGE_EXPORT);
8882 psa_set_key_algorithm(&attributes4, alg);
8883 psa_set_key_type(&attributes4, PSA_KEY_TYPE_DERIVE);
8884 psa_set_key_bits(&attributes4, PSA_BYTES_TO_BITS(requested_capacity));
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008885
Gilles Peskine449bd832023-01-11 14:50:10 +01008886 TEST_EQUAL(psa_key_derivation_output_key(&attributes4, &operation,
8887 &derived_key), expected_status);
8888 } else { // output bytes
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008889 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008890 for (i = 0; i < ARRAY_LENGTH(expected_outputs); i++) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008891 /* Read some bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008892 status = psa_key_derivation_output_bytes(&operation,
8893 output_buffer, output_sizes[i]);
8894 if (expected_capacity == 0 && output_sizes[i] == 0) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008895 /* Reading 0 bytes when 0 bytes are available can go either way. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008896 TEST_ASSERT(status == PSA_SUCCESS ||
8897 status == PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008898 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008899 } else if (expected_capacity == 0 ||
8900 output_sizes[i] > expected_capacity) {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008901 /* Capacity exceeded. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008902 TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_DATA);
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008903 expected_capacity = 0;
8904 continue;
8905 }
8906 /* Success. Check the read data. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008907 PSA_ASSERT(status);
8908 if (output_sizes[i] != 0) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01008909 TEST_MEMORY_COMPARE(output_buffer, output_sizes[i],
Tom Cosgrove0540fe72023-07-27 14:17:27 +01008910 expected_outputs[i], output_sizes[i]);
Gilles Peskine449bd832023-01-11 14:50:10 +01008911 }
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02008912 /* Check the operation status. */
8913 expected_capacity -= output_sizes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01008914 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8915 &current_capacity));
8916 TEST_EQUAL(expected_capacity, current_capacity);
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008917 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008918 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008919 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008920
8921exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008922 mbedtls_free(output_buffer);
8923 psa_key_derivation_abort(&operation);
8924 for (i = 0; i < ARRAY_LENGTH(keys); i++) {
8925 psa_destroy_key(keys[i]);
8926 }
8927 psa_destroy_key(derived_key);
8928 PSA_DONE();
Gilles Peskine96ee5c72018-07-12 17:24:54 +02008929}
8930/* END_CASE */
8931
8932/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008933void derive_full(int alg_arg,
8934 data_t *key_data,
8935 data_t *input1,
8936 data_t *input2,
8937 int requested_capacity_arg)
Gilles Peskined54931c2018-07-17 21:06:59 +02008938{
Ronald Cron5425a212020-08-04 14:58:35 +02008939 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008940 psa_algorithm_t alg = alg_arg;
8941 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008942 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008943 unsigned char output_buffer[16];
8944 size_t expected_capacity = requested_capacity;
8945 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008946 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02008947
Gilles Peskine449bd832023-01-11 14:50:10 +01008948 PSA_ASSERT(psa_crypto_init());
Gilles Peskined54931c2018-07-17 21:06:59 +02008949
Gilles Peskine449bd832023-01-11 14:50:10 +01008950 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
8951 psa_set_key_algorithm(&attributes, alg);
8952 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
Gilles Peskined54931c2018-07-17 21:06:59 +02008953
Gilles Peskine449bd832023-01-11 14:50:10 +01008954 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
8955 &key));
Gilles Peskined54931c2018-07-17 21:06:59 +02008956
Gilles Peskine449bd832023-01-11 14:50:10 +01008957 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, key, alg,
8958 input1->x, input1->len,
8959 input2->x, input2->len,
8960 requested_capacity)) {
Janos Follathf2815ea2019-07-03 12:41:36 +01008961 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008962 }
Janos Follath47f27ed2019-06-25 13:24:52 +01008963
Gilles Peskine449bd832023-01-11 14:50:10 +01008964 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8965 &current_capacity));
8966 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008967
8968 /* Expansion phase. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008969 while (current_capacity > 0) {
8970 size_t read_size = sizeof(output_buffer);
8971 if (read_size > current_capacity) {
Gilles Peskined54931c2018-07-17 21:06:59 +02008972 read_size = current_capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +01008973 }
8974 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
8975 output_buffer,
8976 read_size));
Gilles Peskined54931c2018-07-17 21:06:59 +02008977 expected_capacity -= read_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01008978 PSA_ASSERT(psa_key_derivation_get_capacity(&operation,
8979 &current_capacity));
8980 TEST_EQUAL(current_capacity, expected_capacity);
Gilles Peskined54931c2018-07-17 21:06:59 +02008981 }
8982
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008983 /* Check that the operation refuses to go over capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008984 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output_buffer, 1),
8985 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskined54931c2018-07-17 21:06:59 +02008986
Gilles Peskine449bd832023-01-11 14:50:10 +01008987 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskined54931c2018-07-17 21:06:59 +02008988
8989exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008990 psa_key_derivation_abort(&operation);
8991 psa_destroy_key(key);
8992 PSA_DONE();
Gilles Peskined54931c2018-07-17 21:06:59 +02008993}
8994/* END_CASE */
8995
Stephan Koch78109f52023-04-12 14:19:36 +02008996/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:PSA_WANT_ALG_TLS12_ECJPAKE_TO_PMS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008997void derive_ecjpake_to_pms(data_t *input, int expected_input_status_arg,
8998 int derivation_step,
8999 int capacity, int expected_capacity_status_arg,
9000 data_t *expected_output,
9001 int expected_output_status_arg)
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009002{
9003 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
9004 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04009005 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009006 uint8_t *output_buffer = NULL;
9007 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04009008 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
9009 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
9010 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009011
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009012 TEST_CALLOC(output_buffer, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009013 PSA_ASSERT(psa_crypto_init());
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009014
Gilles Peskine449bd832023-01-11 14:50:10 +01009015 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9016 TEST_EQUAL(psa_key_derivation_set_capacity(&operation, capacity),
9017 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009018
Gilles Peskine449bd832023-01-11 14:50:10 +01009019 TEST_EQUAL(psa_key_derivation_input_bytes(&operation,
9020 step, input->x, input->len),
9021 expected_input_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009022
Gilles Peskine449bd832023-01-11 14:50:10 +01009023 if (((psa_status_t) expected_input_status) != PSA_SUCCESS) {
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009024 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009025 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009026
Gilles Peskine449bd832023-01-11 14:50:10 +01009027 status = psa_key_derivation_output_bytes(&operation, output_buffer,
9028 expected_output->len);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009029
Gilles Peskine449bd832023-01-11 14:50:10 +01009030 TEST_EQUAL(status, expected_output_status);
9031 if (expected_output->len != 0 && expected_output_status == PSA_SUCCESS) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009032 TEST_MEMORY_COMPARE(output_buffer, expected_output->len, expected_output->x,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009033 expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009034 }
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009035
9036exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009037 mbedtls_free(output_buffer);
9038 psa_key_derivation_abort(&operation);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04009039 PSA_DONE();
9040}
9041/* END_CASE */
9042
Janos Follathe60c9052019-07-03 13:51:30 +01009043/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009044void derive_key_exercise(int alg_arg,
9045 data_t *key_data,
9046 data_t *input1,
9047 data_t *input2,
9048 int derived_type_arg,
9049 int derived_bits_arg,
9050 int derived_usage_arg,
9051 int derived_alg_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009052{
Ronald Cron5425a212020-08-04 14:58:35 +02009053 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9054 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009055 psa_algorithm_t alg = alg_arg;
9056 psa_key_type_t derived_type = derived_type_arg;
9057 size_t derived_bits = derived_bits_arg;
9058 psa_key_usage_t derived_usage = derived_usage_arg;
9059 psa_algorithm_t derived_alg = derived_alg_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009060 size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009061 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009062 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009063 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009064
Gilles Peskine449bd832023-01-11 14:50:10 +01009065 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009066
Gilles Peskine449bd832023-01-11 14:50:10 +01009067 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9068 psa_set_key_algorithm(&attributes, alg);
9069 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
9070 PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
9071 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009072
9073 /* Derive a key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009074 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9075 input1->x, input1->len,
9076 input2->x, input2->len,
9077 capacity)) {
Janos Follathe60c9052019-07-03 13:51:30 +01009078 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009079 }
Janos Follathe60c9052019-07-03 13:51:30 +01009080
Gilles Peskine449bd832023-01-11 14:50:10 +01009081 psa_set_key_usage_flags(&attributes, derived_usage);
9082 psa_set_key_algorithm(&attributes, derived_alg);
9083 psa_set_key_type(&attributes, derived_type);
9084 psa_set_key_bits(&attributes, derived_bits);
9085 PSA_ASSERT(psa_key_derivation_output_key(&attributes, &operation,
9086 &derived_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009087
9088 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009089 PSA_ASSERT(psa_get_key_attributes(derived_key, &got_attributes));
9090 TEST_EQUAL(psa_get_key_type(&got_attributes), derived_type);
9091 TEST_EQUAL(psa_get_key_bits(&got_attributes), derived_bits);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009092
9093 /* Exercise the derived key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009094 if (!mbedtls_test_psa_exercise_key(derived_key, derived_usage, derived_alg)) {
Gilles Peskine0386fba2018-07-12 17:29:22 +02009095 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009096 }
Gilles Peskine0386fba2018-07-12 17:29:22 +02009097
9098exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009099 /*
9100 * Key attributes may have been returned by psa_get_key_attributes()
9101 * thus reset them as required.
9102 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009103 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009104
Gilles Peskine449bd832023-01-11 14:50:10 +01009105 psa_key_derivation_abort(&operation);
9106 psa_destroy_key(base_key);
9107 psa_destroy_key(derived_key);
9108 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009109}
9110/* END_CASE */
9111
Janos Follath42fd8882019-07-03 14:17:09 +01009112/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009113void derive_key_export(int alg_arg,
9114 data_t *key_data,
9115 data_t *input1,
9116 data_t *input2,
9117 int bytes1_arg,
9118 int bytes2_arg)
Gilles Peskine0386fba2018-07-12 17:29:22 +02009119{
Ronald Cron5425a212020-08-04 14:58:35 +02009120 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9121 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009122 psa_algorithm_t alg = alg_arg;
9123 size_t bytes1 = bytes1_arg;
9124 size_t bytes2 = bytes2_arg;
9125 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009126 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009127 uint8_t *output_buffer = NULL;
9128 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009129 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9130 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02009131 size_t length;
9132
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009133 TEST_CALLOC(output_buffer, capacity);
9134 TEST_CALLOC(export_buffer, capacity);
Gilles Peskine449bd832023-01-11 14:50:10 +01009135 PSA_ASSERT(psa_crypto_init());
Gilles Peskine0386fba2018-07-12 17:29:22 +02009136
Gilles Peskine449bd832023-01-11 14:50:10 +01009137 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9138 psa_set_key_algorithm(&base_attributes, alg);
9139 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9140 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9141 &base_key));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009142
9143 /* Derive some material and output it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009144 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9145 input1->x, input1->len,
9146 input2->x, input2->len,
9147 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009148 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009149 }
Janos Follath42fd8882019-07-03 14:17:09 +01009150
Gilles Peskine449bd832023-01-11 14:50:10 +01009151 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9152 output_buffer,
9153 capacity));
9154 PSA_ASSERT(psa_key_derivation_abort(&operation));
Gilles Peskine0386fba2018-07-12 17:29:22 +02009155
9156 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009157 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9158 input1->x, input1->len,
9159 input2->x, input2->len,
9160 capacity)) {
Janos Follath42fd8882019-07-03 14:17:09 +01009161 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009162 }
Janos Follath42fd8882019-07-03 14:17:09 +01009163
Gilles Peskine449bd832023-01-11 14:50:10 +01009164 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9165 psa_set_key_algorithm(&derived_attributes, 0);
9166 psa_set_key_type(&derived_attributes, PSA_KEY_TYPE_RAW_DATA);
9167 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes1));
9168 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9169 &derived_key));
9170 PSA_ASSERT(psa_export_key(derived_key,
9171 export_buffer, bytes1,
9172 &length));
9173 TEST_EQUAL(length, bytes1);
9174 PSA_ASSERT(psa_destroy_key(derived_key));
9175 psa_set_key_bits(&derived_attributes, PSA_BYTES_TO_BITS(bytes2));
9176 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9177 &derived_key));
9178 PSA_ASSERT(psa_export_key(derived_key,
9179 export_buffer + bytes1, bytes2,
9180 &length));
9181 TEST_EQUAL(length, bytes2);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009182
9183 /* Compare the outputs from the two runs. */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009184 TEST_MEMORY_COMPARE(output_buffer, bytes1 + bytes2,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009185 export_buffer, capacity);
Gilles Peskine0386fba2018-07-12 17:29:22 +02009186
9187exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009188 mbedtls_free(output_buffer);
9189 mbedtls_free(export_buffer);
9190 psa_key_derivation_abort(&operation);
9191 psa_destroy_key(base_key);
9192 psa_destroy_key(derived_key);
9193 PSA_DONE();
Gilles Peskine0386fba2018-07-12 17:29:22 +02009194}
9195/* END_CASE */
9196
9197/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009198void derive_key_type(int alg_arg,
9199 data_t *key_data,
9200 data_t *input1,
9201 data_t *input2,
9202 int key_type_arg, int bits_arg,
9203 data_t *expected_export)
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009204{
9205 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9206 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
9207 const psa_algorithm_t alg = alg_arg;
9208 const psa_key_type_t key_type = key_type_arg;
9209 const size_t bits = bits_arg;
9210 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9211 const size_t export_buffer_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009212 PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, bits);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009213 uint8_t *export_buffer = NULL;
9214 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9215 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9216 size_t export_length;
9217
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009218 TEST_CALLOC(export_buffer, export_buffer_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01009219 PSA_ASSERT(psa_crypto_init());
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009220
Gilles Peskine449bd832023-01-11 14:50:10 +01009221 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9222 psa_set_key_algorithm(&base_attributes, alg);
9223 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9224 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9225 &base_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009226
Gilles Peskine449bd832023-01-11 14:50:10 +01009227 if (mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009228 &operation, base_key, alg,
9229 input1->x, input1->len,
9230 input2->x, input2->len,
Gilles Peskine449bd832023-01-11 14:50:10 +01009231 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY) == 0) {
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009232 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009233 }
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009234
Gilles Peskine449bd832023-01-11 14:50:10 +01009235 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9236 psa_set_key_algorithm(&derived_attributes, 0);
9237 psa_set_key_type(&derived_attributes, key_type);
9238 psa_set_key_bits(&derived_attributes, bits);
9239 PSA_ASSERT(psa_key_derivation_output_key(&derived_attributes, &operation,
9240 &derived_key));
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009241
Gilles Peskine449bd832023-01-11 14:50:10 +01009242 PSA_ASSERT(psa_export_key(derived_key,
9243 export_buffer, export_buffer_size,
9244 &export_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009245 TEST_MEMORY_COMPARE(export_buffer, export_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009246 expected_export->x, expected_export->len);
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009247
9248exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009249 mbedtls_free(export_buffer);
9250 psa_key_derivation_abort(&operation);
9251 psa_destroy_key(base_key);
9252 psa_destroy_key(derived_key);
9253 PSA_DONE();
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01009254}
9255/* END_CASE */
9256
9257/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009258void derive_key(int alg_arg,
9259 data_t *key_data, data_t *input1, data_t *input2,
9260 int type_arg, int bits_arg,
9261 int expected_status_arg,
9262 int is_large_output)
Gilles Peskinec744d992019-07-30 17:26:54 +02009263{
Ronald Cron5425a212020-08-04 14:58:35 +02009264 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
9265 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02009266 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02009267 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02009268 size_t bits = bits_arg;
9269 psa_status_t expected_status = expected_status_arg;
9270 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
9271 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9272 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
9273
Gilles Peskine449bd832023-01-11 14:50:10 +01009274 PSA_ASSERT(psa_crypto_init());
Gilles Peskinec744d992019-07-30 17:26:54 +02009275
Gilles Peskine449bd832023-01-11 14:50:10 +01009276 psa_set_key_usage_flags(&base_attributes, PSA_KEY_USAGE_DERIVE);
9277 psa_set_key_algorithm(&base_attributes, alg);
9278 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9279 PSA_ASSERT(psa_import_key(&base_attributes, key_data->x, key_data->len,
9280 &base_key));
Gilles Peskinec744d992019-07-30 17:26:54 +02009281
Gilles Peskine449bd832023-01-11 14:50:10 +01009282 if (!mbedtls_test_psa_setup_key_derivation_wrap(&operation, base_key, alg,
9283 input1->x, input1->len,
9284 input2->x, input2->len,
9285 SIZE_MAX)) {
Gilles Peskinec744d992019-07-30 17:26:54 +02009286 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009287 }
Gilles Peskinec744d992019-07-30 17:26:54 +02009288
Gilles Peskine449bd832023-01-11 14:50:10 +01009289 psa_set_key_usage_flags(&derived_attributes, PSA_KEY_USAGE_EXPORT);
9290 psa_set_key_algorithm(&derived_attributes, 0);
9291 psa_set_key_type(&derived_attributes, type);
9292 psa_set_key_bits(&derived_attributes, bits);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009293
9294 psa_status_t status =
Gilles Peskine449bd832023-01-11 14:50:10 +01009295 psa_key_derivation_output_key(&derived_attributes,
9296 &operation,
9297 &derived_key);
9298 if (is_large_output > 0) {
9299 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9300 }
9301 TEST_EQUAL(status, expected_status);
Gilles Peskinec744d992019-07-30 17:26:54 +02009302
9303exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009304 psa_key_derivation_abort(&operation);
9305 psa_destroy_key(base_key);
9306 psa_destroy_key(derived_key);
9307 PSA_DONE();
Gilles Peskinec744d992019-07-30 17:26:54 +02009308}
9309/* END_CASE */
9310
9311/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009312void key_agreement_setup(int alg_arg,
9313 int our_key_type_arg, int our_key_alg_arg,
9314 data_t *our_key_data, data_t *peer_key_data,
9315 int expected_status_arg)
Gilles Peskine01d718c2018-09-18 12:01:02 +02009316{
Ronald Cron5425a212020-08-04 14:58:35 +02009317 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009318 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02009319 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009320 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009321 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009322 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02009323 psa_status_t expected_status = expected_status_arg;
9324 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02009325
Gilles Peskine449bd832023-01-11 14:50:10 +01009326 PSA_ASSERT(psa_crypto_init());
Gilles Peskine01d718c2018-09-18 12:01:02 +02009327
Gilles Peskine449bd832023-01-11 14:50:10 +01009328 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9329 psa_set_key_algorithm(&attributes, our_key_alg);
9330 psa_set_key_type(&attributes, our_key_type);
9331 PSA_ASSERT(psa_import_key(&attributes,
9332 our_key_data->x, our_key_data->len,
9333 &our_key));
Gilles Peskine01d718c2018-09-18 12:01:02 +02009334
Gilles Peskine77f40d82019-04-11 21:27:06 +02009335 /* The tests currently include inputs that should fail at either step.
9336 * Test cases that fail at the setup step should be changed to call
9337 * key_derivation_setup instead, and this function should be renamed
9338 * to key_agreement_fail. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009339 status = psa_key_derivation_setup(&operation, alg);
9340 if (status == PSA_SUCCESS) {
9341 TEST_EQUAL(psa_key_derivation_key_agreement(
9342 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
9343 our_key,
9344 peer_key_data->x, peer_key_data->len),
9345 expected_status);
9346 } else {
9347 TEST_ASSERT(status == expected_status);
Gilles Peskine77f40d82019-04-11 21:27:06 +02009348 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02009349
9350exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009351 psa_key_derivation_abort(&operation);
9352 psa_destroy_key(our_key);
9353 PSA_DONE();
Gilles Peskine01d718c2018-09-18 12:01:02 +02009354}
9355/* END_CASE */
9356
9357/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009358void raw_key_agreement(int alg_arg,
9359 int our_key_type_arg, data_t *our_key_data,
9360 data_t *peer_key_data,
9361 data_t *expected_output)
Gilles Peskinef0cba732019-04-11 22:12:38 +02009362{
Ronald Cron5425a212020-08-04 14:58:35 +02009363 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009364 psa_algorithm_t alg = alg_arg;
9365 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009366 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009367 unsigned char *output = NULL;
9368 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01009369 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009370
Gilles Peskine449bd832023-01-11 14:50:10 +01009371 PSA_ASSERT(psa_crypto_init());
Gilles Peskinef0cba732019-04-11 22:12:38 +02009372
Gilles Peskine449bd832023-01-11 14:50:10 +01009373 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9374 psa_set_key_algorithm(&attributes, alg);
9375 psa_set_key_type(&attributes, our_key_type);
9376 PSA_ASSERT(psa_import_key(&attributes,
9377 our_key_data->x, our_key_data->len,
9378 &our_key));
Gilles Peskinef0cba732019-04-11 22:12:38 +02009379
Gilles Peskine449bd832023-01-11 14:50:10 +01009380 PSA_ASSERT(psa_get_key_attributes(our_key, &attributes));
9381 key_bits = psa_get_key_bits(&attributes);
gabor-mezei-armceface22021-01-21 12:26:17 +01009382
Gilles Peskine992bee82022-04-13 23:25:52 +02009383 /* Validate size macros */
Gilles Peskine449bd832023-01-11 14:50:10 +01009384 TEST_LE_U(expected_output->len,
9385 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits));
9386 TEST_LE_U(PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(our_key_type, key_bits),
9387 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE);
Gilles Peskine992bee82022-04-13 23:25:52 +02009388
9389 /* Good case with exact output size */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009390 TEST_CALLOC(output, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009391 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9392 peer_key_data->x, peer_key_data->len,
9393 output, expected_output->len,
9394 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009395 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009396 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009397 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009398 output = NULL;
9399 output_length = ~0;
9400
9401 /* Larger buffer */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009402 TEST_CALLOC(output, expected_output->len + 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009403 PSA_ASSERT(psa_raw_key_agreement(alg, our_key,
9404 peer_key_data->x, peer_key_data->len,
9405 output, expected_output->len + 1,
9406 &output_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009407 TEST_MEMORY_COMPARE(output, output_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009408 expected_output->x, expected_output->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009409 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009410 output = NULL;
9411 output_length = ~0;
9412
9413 /* Buffer too small */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009414 TEST_CALLOC(output, expected_output->len - 1);
Gilles Peskine449bd832023-01-11 14:50:10 +01009415 TEST_EQUAL(psa_raw_key_agreement(alg, our_key,
9416 peer_key_data->x, peer_key_data->len,
9417 output, expected_output->len - 1,
9418 &output_length),
9419 PSA_ERROR_BUFFER_TOO_SMALL);
Gilles Peskine992bee82022-04-13 23:25:52 +02009420 /* Not required by the spec, but good robustness */
Gilles Peskine449bd832023-01-11 14:50:10 +01009421 TEST_LE_U(output_length, expected_output->len - 1);
9422 mbedtls_free(output);
Gilles Peskine992bee82022-04-13 23:25:52 +02009423 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02009424
9425exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009426 mbedtls_free(output);
9427 psa_destroy_key(our_key);
9428 PSA_DONE();
Gilles Peskinef0cba732019-04-11 22:12:38 +02009429}
9430/* END_CASE */
9431
9432/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009433void key_agreement_capacity(int alg_arg,
9434 int our_key_type_arg, data_t *our_key_data,
9435 data_t *peer_key_data,
9436 int expected_capacity_arg)
Gilles Peskine59685592018-09-18 12:11:34 +02009437{
Ronald Cron5425a212020-08-04 14:58:35 +02009438 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009439 psa_algorithm_t alg = alg_arg;
9440 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009441 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009442 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009443 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02009444 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02009445
Gilles Peskine449bd832023-01-11 14:50:10 +01009446 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009447
Gilles Peskine449bd832023-01-11 14:50:10 +01009448 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9449 psa_set_key_algorithm(&attributes, alg);
9450 psa_set_key_type(&attributes, our_key_type);
9451 PSA_ASSERT(psa_import_key(&attributes,
9452 our_key_data->x, our_key_data->len,
9453 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009454
Gilles Peskine449bd832023-01-11 14:50:10 +01009455 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9456 PSA_ASSERT(psa_key_derivation_key_agreement(
9457 &operation,
9458 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9459 peer_key_data->x, peer_key_data->len));
9460 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009461 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009462 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9463 PSA_KEY_DERIVATION_INPUT_INFO,
9464 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009465 }
Gilles Peskine59685592018-09-18 12:11:34 +02009466
Shaun Case8b0ecbc2021-12-20 21:14:10 -08009467 /* Test the advertised capacity. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009468 PSA_ASSERT(psa_key_derivation_get_capacity(
9469 &operation, &actual_capacity));
9470 TEST_EQUAL(actual_capacity, (size_t) expected_capacity_arg);
Gilles Peskine59685592018-09-18 12:11:34 +02009471
Gilles Peskinebf491972018-10-25 22:36:12 +02009472 /* Test the actual capacity by reading the output. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009473 while (actual_capacity > sizeof(output)) {
9474 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9475 output, sizeof(output)));
9476 actual_capacity -= sizeof(output);
Gilles Peskinebf491972018-10-25 22:36:12 +02009477 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009478 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9479 output, actual_capacity));
9480 TEST_EQUAL(psa_key_derivation_output_bytes(&operation, output, 1),
9481 PSA_ERROR_INSUFFICIENT_DATA);
Gilles Peskinebf491972018-10-25 22:36:12 +02009482
Gilles Peskine59685592018-09-18 12:11:34 +02009483exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009484 psa_key_derivation_abort(&operation);
9485 psa_destroy_key(our_key);
9486 PSA_DONE();
Gilles Peskine59685592018-09-18 12:11:34 +02009487}
9488/* END_CASE */
9489
Valerio Settiad819672023-12-29 12:14:41 +01009490/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9491void ecc_conversion_functions(int grp_id_arg, int psa_family_arg, int bits_arg)
Valerio Settibf999cb2023-12-28 17:48:13 +01009492{
9493 mbedtls_ecp_group_id grp_id = grp_id_arg;
Valerio Settiad819672023-12-29 12:14:41 +01009494 psa_ecc_family_t ecc_family = psa_family_arg;
9495 size_t bits = bits_arg;
9496 size_t bits_tmp;
Valerio Settibf999cb2023-12-28 17:48:13 +01009497
Valerio Settiad819672023-12-29 12:14:41 +01009498 TEST_EQUAL(ecc_family, mbedtls_ecc_group_to_psa(grp_id, &bits_tmp));
9499 TEST_EQUAL(bits, bits_tmp);
Valerio Settiac739522024-01-04 10:22:01 +01009500 TEST_EQUAL(grp_id, mbedtls_ecc_group_from_psa(ecc_family, bits));
Valerio Settibf999cb2023-12-28 17:48:13 +01009501}
9502/* END_CASE */
9503
Valerio Settiac739522024-01-04 10:22:01 +01009504/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9505void ecc_conversion_functions_fail()
9506{
9507 size_t bits;
9508
Valerio Settidb6e0292024-01-05 10:15:45 +01009509 /* Invalid legacy curve identifiers. */
9510 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_MAX, &bits));
9511 TEST_EQUAL(0, bits);
Valerio Settiac739522024-01-04 10:22:01 +01009512 TEST_EQUAL(0, mbedtls_ecc_group_to_psa(MBEDTLS_ECP_DP_NONE, &bits));
9513 TEST_EQUAL(0, bits);
9514
9515 /* Invalid PSA EC family. */
9516 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(0, 192));
9517 /* Invalid bit-size for a valid EC family. */
9518 TEST_EQUAL(MBEDTLS_ECP_DP_NONE, mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_SECP_R1, 512));
9519
9520 /* Twisted-Edward curves are not supported yet. */
9521 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9522 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 255));
9523 TEST_EQUAL(MBEDTLS_ECP_DP_NONE,
9524 mbedtls_ecc_group_from_psa(PSA_ECC_FAMILY_TWISTED_EDWARDS, 448));
9525}
9526/* END_CASE */
9527
9528
Valerio Settibf999cb2023-12-28 17:48:13 +01009529/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009530void key_agreement_output(int alg_arg,
9531 int our_key_type_arg, data_t *our_key_data,
9532 data_t *peer_key_data,
9533 data_t *expected_output1, data_t *expected_output2)
Gilles Peskine59685592018-09-18 12:11:34 +02009534{
Ronald Cron5425a212020-08-04 14:58:35 +02009535 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02009536 psa_algorithm_t alg = alg_arg;
9537 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009538 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009539 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009540 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02009541
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009542 TEST_CALLOC(actual_output, MAX(expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009543 expected_output2->len));
Gilles Peskine59685592018-09-18 12:11:34 +02009544
Gilles Peskine449bd832023-01-11 14:50:10 +01009545 PSA_ASSERT(psa_crypto_init());
Gilles Peskine59685592018-09-18 12:11:34 +02009546
Gilles Peskine449bd832023-01-11 14:50:10 +01009547 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
9548 psa_set_key_algorithm(&attributes, alg);
9549 psa_set_key_type(&attributes, our_key_type);
9550 PSA_ASSERT(psa_import_key(&attributes,
9551 our_key_data->x, our_key_data->len,
9552 &our_key));
Gilles Peskine59685592018-09-18 12:11:34 +02009553
Gilles Peskine449bd832023-01-11 14:50:10 +01009554 PSA_ASSERT(psa_key_derivation_setup(&operation, alg));
9555 PSA_ASSERT(psa_key_derivation_key_agreement(
9556 &operation,
9557 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
9558 peer_key_data->x, peer_key_data->len));
9559 if (PSA_ALG_IS_HKDF(PSA_ALG_KEY_AGREEMENT_GET_KDF(alg))) {
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009560 /* The test data is for info="" */
Gilles Peskine449bd832023-01-11 14:50:10 +01009561 PSA_ASSERT(psa_key_derivation_input_bytes(&operation,
9562 PSA_KEY_DERIVATION_INPUT_INFO,
9563 NULL, 0));
Gilles Peskinef8a9d942019-04-11 22:13:20 +02009564 }
Gilles Peskine59685592018-09-18 12:11:34 +02009565
Gilles Peskine449bd832023-01-11 14:50:10 +01009566 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9567 actual_output,
9568 expected_output1->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009569 TEST_MEMORY_COMPARE(actual_output, expected_output1->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009570 expected_output1->x, expected_output1->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009571 if (expected_output2->len != 0) {
9572 PSA_ASSERT(psa_key_derivation_output_bytes(&operation,
9573 actual_output,
9574 expected_output2->len));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009575 TEST_MEMORY_COMPARE(actual_output, expected_output2->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009576 expected_output2->x, expected_output2->len);
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02009577 }
Gilles Peskine59685592018-09-18 12:11:34 +02009578
9579exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009580 psa_key_derivation_abort(&operation);
9581 psa_destroy_key(our_key);
9582 PSA_DONE();
9583 mbedtls_free(actual_output);
Gilles Peskine59685592018-09-18 12:11:34 +02009584}
9585/* END_CASE */
9586
9587/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009588void generate_random(int bytes_arg)
Gilles Peskine05d69892018-06-19 22:00:52 +02009589{
Gilles Peskinea50d7392018-06-21 10:22:13 +02009590 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02009591 unsigned char *output = NULL;
9592 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02009593 size_t i;
9594 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02009595
Gilles Peskine449bd832023-01-11 14:50:10 +01009596 TEST_ASSERT(bytes_arg >= 0);
Simon Butcher49f8e312020-03-03 15:51:50 +00009597
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009598 TEST_CALLOC(output, bytes);
9599 TEST_CALLOC(changed, bytes);
Gilles Peskine05d69892018-06-19 22:00:52 +02009600
Gilles Peskine449bd832023-01-11 14:50:10 +01009601 PSA_ASSERT(psa_crypto_init());
Gilles Peskine05d69892018-06-19 22:00:52 +02009602
Gilles Peskinea50d7392018-06-21 10:22:13 +02009603 /* Run several times, to ensure that every output byte will be
9604 * nonzero at least once with overwhelming probability
9605 * (2^(-8*number_of_runs)). */
Gilles Peskine449bd832023-01-11 14:50:10 +01009606 for (run = 0; run < 10; run++) {
9607 if (bytes != 0) {
9608 memset(output, 0, bytes);
9609 }
9610 PSA_ASSERT(psa_generate_random(output, bytes));
Gilles Peskinea50d7392018-06-21 10:22:13 +02009611
Gilles Peskine449bd832023-01-11 14:50:10 +01009612 for (i = 0; i < bytes; i++) {
9613 if (output[i] != 0) {
Gilles Peskinea50d7392018-06-21 10:22:13 +02009614 ++changed[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01009615 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009616 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009617 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02009618
9619 /* Check that every byte was changed to nonzero at least once. This
9620 * validates that psa_generate_random is overwriting every byte of
9621 * the output buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009622 for (i = 0; i < bytes; i++) {
9623 TEST_ASSERT(changed[i] != 0);
Gilles Peskinea50d7392018-06-21 10:22:13 +02009624 }
Gilles Peskine05d69892018-06-19 22:00:52 +02009625
9626exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009627 PSA_DONE();
9628 mbedtls_free(output);
9629 mbedtls_free(changed);
Gilles Peskine05d69892018-06-19 22:00:52 +02009630}
9631/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02009632
9633/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009634void generate_key(int type_arg,
9635 int bits_arg,
9636 int usage_arg,
9637 int alg_arg,
9638 int expected_status_arg,
9639 int is_large_key)
Gilles Peskine12313cd2018-06-20 00:20:32 +02009640{
Ronald Cron5425a212020-08-04 14:58:35 +02009641 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009642 psa_key_type_t type = type_arg;
9643 psa_key_usage_t usage = usage_arg;
9644 size_t bits = bits_arg;
9645 psa_algorithm_t alg = alg_arg;
9646 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009647 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02009648 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02009649
Gilles Peskine449bd832023-01-11 14:50:10 +01009650 PSA_ASSERT(psa_crypto_init());
Gilles Peskine12313cd2018-06-20 00:20:32 +02009651
Gilles Peskine449bd832023-01-11 14:50:10 +01009652 psa_set_key_usage_flags(&attributes, usage);
9653 psa_set_key_algorithm(&attributes, alg);
9654 psa_set_key_type(&attributes, type);
9655 psa_set_key_bits(&attributes, bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009656
9657 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009658 psa_status_t status = psa_generate_key(&attributes, &key);
Steven Cooreman83fdb702021-01-21 14:24:39 +01009659
Gilles Peskine449bd832023-01-11 14:50:10 +01009660 if (is_large_key > 0) {
9661 TEST_ASSUME(status != PSA_ERROR_INSUFFICIENT_MEMORY);
9662 }
9663 TEST_EQUAL(status, expected_status);
9664 if (expected_status != PSA_SUCCESS) {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02009665 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009666 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009667
9668 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009669 PSA_ASSERT(psa_get_key_attributes(key, &got_attributes));
9670 TEST_EQUAL(psa_get_key_type(&got_attributes), type);
9671 TEST_EQUAL(psa_get_key_bits(&got_attributes), bits);
Gilles Peskine12313cd2018-06-20 00:20:32 +02009672
Gilles Peskine818ca122018-06-20 18:16:48 +02009673 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009674 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskine02b75072018-07-01 22:31:34 +02009675 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009676 }
Gilles Peskine12313cd2018-06-20 00:20:32 +02009677
9678exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009679 /*
9680 * Key attributes may have been returned by psa_get_key_attributes()
9681 * thus reset them as required.
9682 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009683 psa_reset_key_attributes(&got_attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009684
Gilles Peskine449bd832023-01-11 14:50:10 +01009685 psa_destroy_key(key);
9686 PSA_DONE();
Gilles Peskine12313cd2018-06-20 00:20:32 +02009687}
9688/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03009689
Valerio Setti19fec542023-07-25 12:31:50 +02009690/* 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 +01009691void generate_key_rsa(int bits_arg,
9692 data_t *e_arg,
9693 int expected_status_arg)
Gilles Peskinee56e8782019-04-26 17:34:02 +02009694{
Ronald Cron5425a212020-08-04 14:58:35 +02009695 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02009696 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02009697 size_t bits = bits_arg;
9698 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
9699 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
9700 psa_status_t expected_status = expected_status_arg;
9701 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9702 uint8_t *exported = NULL;
9703 size_t exported_size =
Gilles Peskine449bd832023-01-11 14:50:10 +01009704 PSA_EXPORT_KEY_OUTPUT_SIZE(PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009705 size_t exported_length = SIZE_MAX;
9706 uint8_t *e_read_buffer = NULL;
9707 int is_default_public_exponent = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009708 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE(type, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009709 size_t e_read_length = SIZE_MAX;
9710
Gilles Peskine449bd832023-01-11 14:50:10 +01009711 if (e_arg->len == 0 ||
9712 (e_arg->len == 3 &&
9713 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009714 is_default_public_exponent = 1;
9715 e_read_size = 0;
9716 }
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009717 TEST_CALLOC(e_read_buffer, e_read_size);
9718 TEST_CALLOC(exported, exported_size);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009719
Gilles Peskine449bd832023-01-11 14:50:10 +01009720 PSA_ASSERT(psa_crypto_init());
Gilles Peskinee56e8782019-04-26 17:34:02 +02009721
Gilles Peskine449bd832023-01-11 14:50:10 +01009722 psa_set_key_usage_flags(&attributes, usage);
9723 psa_set_key_algorithm(&attributes, alg);
9724 PSA_ASSERT(psa_set_key_domain_parameters(&attributes, type,
9725 e_arg->x, e_arg->len));
9726 psa_set_key_bits(&attributes, bits);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009727
9728 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009729 TEST_EQUAL(psa_generate_key(&attributes, &key), expected_status);
9730 if (expected_status != PSA_SUCCESS) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009731 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009732 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009733
9734 /* Test the key information */
Gilles Peskine449bd832023-01-11 14:50:10 +01009735 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9736 TEST_EQUAL(psa_get_key_type(&attributes), type);
9737 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009738 psa_status_t status = psa_get_key_domain_parameters(&attributes,
9739 e_read_buffer, e_read_size,
9740 &e_read_length);
9741
9742
9743#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
9744 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
9745 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +01009746 if (is_default_public_exponent) {
9747 TEST_EQUAL(e_read_length, 0);
9748 } else {
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009749 TEST_EQUAL(status, PSA_SUCCESS);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009750 TEST_MEMORY_COMPARE(e_read_buffer, e_read_length, e_arg->x, e_arg->len);
Gilles Peskine449bd832023-01-11 14:50:10 +01009751 }
Pengyu Lv9e976f32023-12-06 16:58:05 +08009752#else
Pengyu Lv9e976f32023-12-06 16:58:05 +08009753 (void) is_default_public_exponent;
Pengyu Lvd90fbf72023-12-08 17:13:22 +08009754 TEST_EQUAL(status, PSA_ERROR_NOT_SUPPORTED);
Pengyu Lv9e976f32023-12-06 16:58:05 +08009755#endif
Gilles Peskinee56e8782019-04-26 17:34:02 +02009756
9757 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009758 if (!mbedtls_test_psa_exercise_key(key, usage, alg)) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009759 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009760 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009761
9762 /* Export the key and check the public exponent. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009763 PSA_ASSERT(psa_export_public_key(key,
9764 exported, exported_size,
9765 &exported_length));
Gilles Peskinee56e8782019-04-26 17:34:02 +02009766 {
9767 uint8_t *p = exported;
9768 uint8_t *end = exported + exported_length;
9769 size_t len;
9770 /* RSAPublicKey ::= SEQUENCE {
9771 * modulus INTEGER, -- n
9772 * publicExponent INTEGER } -- e
9773 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009774 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9775 MBEDTLS_ASN1_SEQUENCE |
9776 MBEDTLS_ASN1_CONSTRUCTED));
9777 TEST_ASSERT(mbedtls_test_asn1_skip_integer(&p, end, bits, bits, 1));
9778 TEST_EQUAL(0, mbedtls_asn1_get_tag(&p, end, &len,
9779 MBEDTLS_ASN1_INTEGER));
9780 if (len >= 1 && p[0] == 0) {
Gilles Peskinee56e8782019-04-26 17:34:02 +02009781 ++p;
9782 --len;
9783 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009784 if (e_arg->len == 0) {
9785 TEST_EQUAL(len, 3);
9786 TEST_EQUAL(p[0], 1);
9787 TEST_EQUAL(p[1], 0);
9788 TEST_EQUAL(p[2], 1);
9789 } else {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009790 TEST_MEMORY_COMPARE(p, len, e_arg->x, e_arg->len);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009791 }
Gilles Peskinee56e8782019-04-26 17:34:02 +02009792 }
9793
9794exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009795 /*
9796 * Key attributes may have been returned by psa_get_key_attributes() or
9797 * set by psa_set_key_domain_parameters() thus reset them as required.
9798 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009799 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009800
Gilles Peskine449bd832023-01-11 14:50:10 +01009801 psa_destroy_key(key);
9802 PSA_DONE();
9803 mbedtls_free(e_read_buffer);
9804 mbedtls_free(exported);
Gilles Peskinee56e8782019-04-26 17:34:02 +02009805}
9806/* END_CASE */
9807
Darryl Greend49a4992018-06-18 17:27:26 +01009808/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01009809void persistent_key_load_key_from_storage(data_t *data,
9810 int type_arg, int bits_arg,
9811 int usage_flags_arg, int alg_arg,
9812 int generation_method)
Darryl Greend49a4992018-06-18 17:27:26 +01009813{
Gilles Peskine449bd832023-01-11 14:50:10 +01009814 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make(1, 1);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009815 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02009816 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9817 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009818 psa_key_type_t type = type_arg;
9819 size_t bits = bits_arg;
9820 psa_key_usage_t usage_flags = usage_flags_arg;
9821 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02009822 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01009823 unsigned char *first_export = NULL;
9824 unsigned char *second_export = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009825 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE(type, bits);
Sergeybef1f632023-03-06 15:25:06 -07009826 size_t first_exported_length = 0;
Darryl Greend49a4992018-06-18 17:27:26 +01009827 size_t second_exported_length;
9828
Gilles Peskine449bd832023-01-11 14:50:10 +01009829 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009830 TEST_CALLOC(first_export, export_size);
9831 TEST_CALLOC(second_export, export_size);
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009832 }
Darryl Greend49a4992018-06-18 17:27:26 +01009833
Gilles Peskine449bd832023-01-11 14:50:10 +01009834 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009835
Gilles Peskine449bd832023-01-11 14:50:10 +01009836 psa_set_key_id(&attributes, key_id);
9837 psa_set_key_usage_flags(&attributes, usage_flags);
9838 psa_set_key_algorithm(&attributes, alg);
9839 psa_set_key_type(&attributes, type);
9840 psa_set_key_bits(&attributes, bits);
Darryl Greend49a4992018-06-18 17:27:26 +01009841
Gilles Peskine449bd832023-01-11 14:50:10 +01009842 switch (generation_method) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009843 case IMPORT_KEY:
9844 /* Import the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009845 PSA_ASSERT(psa_import_key(&attributes, data->x, data->len,
9846 &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009847 break;
Darryl Greend49a4992018-06-18 17:27:26 +01009848
Darryl Green0c6575a2018-11-07 16:05:30 +00009849 case GENERATE_KEY:
9850 /* Generate a key */
Gilles Peskine449bd832023-01-11 14:50:10 +01009851 PSA_ASSERT(psa_generate_key(&attributes, &key));
Darryl Green0c6575a2018-11-07 16:05:30 +00009852 break;
9853
9854 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01009855#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01009856 {
9857 /* Create base key */
9858 psa_algorithm_t derive_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
9859 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
9860 psa_set_key_usage_flags(&base_attributes,
9861 PSA_KEY_USAGE_DERIVE);
9862 psa_set_key_algorithm(&base_attributes, derive_alg);
9863 psa_set_key_type(&base_attributes, PSA_KEY_TYPE_DERIVE);
9864 PSA_ASSERT(psa_import_key(&base_attributes,
9865 data->x, data->len,
9866 &base_key));
9867 /* Derive a key. */
9868 PSA_ASSERT(psa_key_derivation_setup(&operation, derive_alg));
9869 PSA_ASSERT(psa_key_derivation_input_key(
9870 &operation,
9871 PSA_KEY_DERIVATION_INPUT_SECRET, base_key));
9872 PSA_ASSERT(psa_key_derivation_input_bytes(
9873 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
9874 NULL, 0));
9875 PSA_ASSERT(psa_key_derivation_output_key(&attributes,
9876 &operation,
9877 &key));
9878 PSA_ASSERT(psa_key_derivation_abort(&operation));
9879 PSA_ASSERT(psa_destroy_key(base_key));
9880 base_key = MBEDTLS_SVC_KEY_ID_INIT;
9881 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009882#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009883 TEST_ASSUME(!"KDF not supported in this configuration");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009884#endif
9885 break;
9886
9887 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01009888 TEST_FAIL("generation_method not implemented in test");
Gilles Peskine6fea21d2021-01-12 00:02:15 +01009889 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00009890 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009891 psa_reset_key_attributes(&attributes);
Darryl Greend49a4992018-06-18 17:27:26 +01009892
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009893 /* Export the key if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009894 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9895 PSA_ASSERT(psa_export_key(key,
9896 first_export, export_size,
9897 &first_exported_length));
9898 if (generation_method == IMPORT_KEY) {
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009899 TEST_MEMORY_COMPARE(data->x, data->len,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009900 first_export, first_exported_length);
Gilles Peskine449bd832023-01-11 14:50:10 +01009901 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009902 }
Darryl Greend49a4992018-06-18 17:27:26 +01009903
9904 /* Shutdown and restart */
Gilles Peskine449bd832023-01-11 14:50:10 +01009905 PSA_ASSERT(psa_purge_key(key));
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009906 PSA_DONE();
Gilles Peskine449bd832023-01-11 14:50:10 +01009907 PSA_ASSERT(psa_crypto_init());
Darryl Greend49a4992018-06-18 17:27:26 +01009908
Darryl Greend49a4992018-06-18 17:27:26 +01009909 /* Check key slot still contains key data */
Gilles Peskine449bd832023-01-11 14:50:10 +01009910 PSA_ASSERT(psa_get_key_attributes(key, &attributes));
9911 TEST_ASSERT(mbedtls_svc_key_id_equal(
9912 psa_get_key_id(&attributes), key_id));
9913 TEST_EQUAL(psa_get_key_lifetime(&attributes),
9914 PSA_KEY_LIFETIME_PERSISTENT);
9915 TEST_EQUAL(psa_get_key_type(&attributes), type);
9916 TEST_EQUAL(psa_get_key_bits(&attributes), bits);
9917 TEST_EQUAL(psa_get_key_usage_flags(&attributes),
9918 mbedtls_test_update_key_usage_flags(usage_flags));
9919 TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
Darryl Greend49a4992018-06-18 17:27:26 +01009920
Gilles Peskine5c648ab2019-04-19 14:06:53 +02009921 /* Export the key again if permitted by the key policy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009922 if (usage_flags & PSA_KEY_USAGE_EXPORT) {
9923 PSA_ASSERT(psa_export_key(key,
9924 second_export, export_size,
9925 &second_exported_length));
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01009926 TEST_MEMORY_COMPARE(first_export, first_exported_length,
Tom Cosgrove0540fe72023-07-27 14:17:27 +01009927 second_export, second_exported_length);
Darryl Green0c6575a2018-11-07 16:05:30 +00009928 }
9929
9930 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009931 if (!mbedtls_test_psa_exercise_key(key, usage_flags, alg)) {
Darryl Green0c6575a2018-11-07 16:05:30 +00009932 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01009933 }
Darryl Greend49a4992018-06-18 17:27:26 +01009934
9935exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009936 /*
9937 * Key attributes may have been returned by psa_get_key_attributes()
9938 * thus reset them as required.
9939 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009940 psa_reset_key_attributes(&attributes);
Ronald Cron3a4f0e32020-11-19 17:55:23 +01009941
Gilles Peskine449bd832023-01-11 14:50:10 +01009942 mbedtls_free(first_export);
9943 mbedtls_free(second_export);
9944 psa_key_derivation_abort(&operation);
9945 psa_destroy_key(base_key);
9946 psa_destroy_key(key);
Gilles Peskine1153e7b2019-05-28 15:10:21 +02009947 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01009948}
9949/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009950
Neil Armstronga557cb82022-06-10 08:58:32 +02009951/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009952void ecjpake_setup(int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
9953 int primitive_arg, int hash_arg, int role_arg,
9954 int test_input, data_t *pw_data,
9955 int inj_err_type_arg,
9956 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02009957{
9958 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9959 psa_pake_operation_t operation = psa_pake_operation_init();
9960 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009961 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02009962 psa_key_type_t key_type_pw = key_type_pw_arg;
9963 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009964 psa_algorithm_t hash_alg = hash_arg;
9965 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009966 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9967 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009968 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
9969 psa_status_t expected_error = expected_error_arg;
9970 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009971 unsigned char *output_buffer = NULL;
9972 size_t output_len = 0;
9973
Gilles Peskine449bd832023-01-11 14:50:10 +01009974 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +02009975
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02009976 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
Gilles Peskine449bd832023-01-11 14:50:10 +01009977 PSA_PAKE_STEP_KEY_SHARE);
Tom Cosgrove05b2a872023-07-21 11:31:13 +01009978 TEST_CALLOC(output_buffer, buf_size);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009979
Gilles Peskine449bd832023-01-11 14:50:10 +01009980 if (pw_data->len > 0) {
9981 psa_set_key_usage_flags(&attributes, key_usage_pw);
9982 psa_set_key_algorithm(&attributes, alg);
9983 psa_set_key_type(&attributes, key_type_pw);
9984 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
9985 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +02009986 }
9987
Gilles Peskine449bd832023-01-11 14:50:10 +01009988 psa_pake_cs_set_algorithm(&cipher_suite, alg);
9989 psa_pake_cs_set_primitive(&cipher_suite, primitive);
9990 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +02009991
Gilles Peskine449bd832023-01-11 14:50:10 +01009992 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrong645cccd2022-06-08 17:36:23 +02009993
Gilles Peskine449bd832023-01-11 14:50:10 +01009994 if (inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS) {
9995 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
9996 expected_error);
9997 PSA_ASSERT(psa_pake_abort(&operation));
9998 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
9999 expected_error);
10000 PSA_ASSERT(psa_pake_abort(&operation));
10001 TEST_EQUAL(psa_pake_set_password_key(&operation, key),
10002 expected_error);
10003 PSA_ASSERT(psa_pake_abort(&operation));
10004 TEST_EQUAL(psa_pake_set_role(&operation, role),
10005 expected_error);
10006 PSA_ASSERT(psa_pake_abort(&operation));
10007 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10008 NULL, 0, NULL),
10009 expected_error);
10010 PSA_ASSERT(psa_pake_abort(&operation));
10011 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
10012 expected_error);
10013 PSA_ASSERT(psa_pake_abort(&operation));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010014 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010015 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010016
Gilles Peskine449bd832023-01-11 14:50:10 +010010017 status = psa_pake_setup(&operation, &cipher_suite);
10018 if (status != PSA_SUCCESS) {
10019 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010020 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010021 }
10022
Gilles Peskine449bd832023-01-11 14:50:10 +010010023 if (inj_err_type == INJECT_ERR_DUPLICATE_SETUP) {
10024 TEST_EQUAL(psa_pake_setup(&operation, &cipher_suite),
10025 expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010026 goto exit;
10027 }
10028
Gilles Peskine449bd832023-01-11 14:50:10 +010010029 status = psa_pake_set_role(&operation, role);
10030 if (status != PSA_SUCCESS) {
10031 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010032 goto exit;
10033 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010034
Gilles Peskine449bd832023-01-11 14:50:10 +010010035 if (pw_data->len > 0) {
10036 status = psa_pake_set_password_key(&operation, key);
10037 if (status != PSA_SUCCESS) {
10038 TEST_EQUAL(status, expected_error);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010039 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +010010040 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010041 }
10042
Gilles Peskine449bd832023-01-11 14:50:10 +010010043 if (inj_err_type == INJECT_ERR_INVALID_USER) {
10044 TEST_EQUAL(psa_pake_set_user(&operation, NULL, 0),
10045 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010046 goto exit;
10047 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010048
Gilles Peskine449bd832023-01-11 14:50:10 +010010049 if (inj_err_type == INJECT_ERR_INVALID_PEER) {
10050 TEST_EQUAL(psa_pake_set_peer(&operation, NULL, 0),
10051 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010052 goto exit;
10053 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010054
Gilles Peskine449bd832023-01-11 14:50:10 +010010055 if (inj_err_type == INJECT_ERR_SET_USER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010056 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010057 TEST_EQUAL(psa_pake_set_user(&operation, unsupported_id, 4),
10058 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010059 goto exit;
10060 }
10061
Gilles Peskine449bd832023-01-11 14:50:10 +010010062 if (inj_err_type == INJECT_ERR_SET_PEER) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010063 const uint8_t unsupported_id[] = "abcd";
Gilles Peskine449bd832023-01-11 14:50:10 +010010064 TEST_EQUAL(psa_pake_set_peer(&operation, unsupported_id, 4),
10065 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti1070aed2022-11-11 19:37:31 +010010066 goto exit;
10067 }
Neil Armstrong707d9572022-06-08 17:31:49 +020010068
Gilles Peskine449bd832023-01-11 14:50:10 +010010069 const size_t size_key_share = PSA_PAKE_INPUT_SIZE(alg, primitive,
10070 PSA_PAKE_STEP_KEY_SHARE);
10071 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE(alg, primitive,
10072 PSA_PAKE_STEP_ZK_PUBLIC);
10073 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE(alg, primitive,
10074 PSA_PAKE_STEP_ZK_PROOF);
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +020010075
Gilles Peskine449bd832023-01-11 14:50:10 +010010076 if (test_input) {
10077 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10078 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0),
10079 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010080 goto exit;
10081 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010082
Gilles Peskine449bd832023-01-11 14:50:10 +010010083 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10084 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10085 output_buffer, size_zk_proof),
10086 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010087 goto exit;
10088 }
10089
Gilles Peskine449bd832023-01-11 14:50:10 +010010090 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10091 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PROOF,
10092 output_buffer, size_zk_proof),
10093 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010094 goto exit;
10095 }
10096
Gilles Peskine449bd832023-01-11 14:50:10 +010010097 status = psa_pake_input(&operation, PSA_PAKE_STEP_KEY_SHARE,
10098 output_buffer, size_key_share);
10099 if (status != PSA_SUCCESS) {
10100 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010101 goto exit;
10102 }
10103
Gilles Peskine449bd832023-01-11 14:50:10 +010010104 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10105 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10106 output_buffer, size_zk_public + 1),
10107 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010108 goto exit;
10109 }
10110
Gilles Peskine449bd832023-01-11 14:50:10 +010010111 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010112 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010113 psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10114 output_buffer, size_zk_public + 1);
10115 TEST_EQUAL(psa_pake_input(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10116 output_buffer, size_zk_public),
10117 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010118 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010119 }
Valerio Setti1070aed2022-11-11 19:37:31 +010010120 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +010010121 if (inj_err_type == INJECT_EMPTY_IO_BUFFER) {
10122 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10123 NULL, 0, NULL),
10124 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010125 goto exit;
10126 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010127
Gilles Peskine449bd832023-01-11 14:50:10 +010010128 if (inj_err_type == INJECT_UNKNOWN_STEP) {
10129 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF + 10,
10130 output_buffer, buf_size, &output_len),
10131 PSA_ERROR_INVALID_ARGUMENT);
Valerio Setti1070aed2022-11-11 19:37:31 +010010132 goto exit;
10133 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010134
Gilles Peskine449bd832023-01-11 14:50:10 +010010135 if (inj_err_type == INJECT_INVALID_FIRST_STEP) {
10136 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PROOF,
10137 output_buffer, buf_size, &output_len),
10138 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010139 goto exit;
10140 }
10141
Gilles Peskine449bd832023-01-11 14:50:10 +010010142 status = psa_pake_output(&operation, PSA_PAKE_STEP_KEY_SHARE,
10143 output_buffer, buf_size, &output_len);
10144 if (status != PSA_SUCCESS) {
10145 TEST_EQUAL(status, expected_error);
Valerio Setti1070aed2022-11-11 19:37:31 +010010146 goto exit;
10147 }
10148
Gilles Peskine449bd832023-01-11 14:50:10 +010010149 TEST_ASSERT(output_len > 0);
Valerio Setti1070aed2022-11-11 19:37:31 +010010150
Gilles Peskine449bd832023-01-11 14:50:10 +010010151 if (inj_err_type == INJECT_WRONG_BUFFER_SIZE) {
10152 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10153 output_buffer, size_zk_public - 1, &output_len),
10154 PSA_ERROR_BUFFER_TOO_SMALL);
Valerio Setti1070aed2022-11-11 19:37:31 +010010155 goto exit;
10156 }
10157
Gilles Peskine449bd832023-01-11 14:50:10 +010010158 if (inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE) {
Valerio Setti1070aed2022-11-11 19:37:31 +010010159 // Just trigger any kind of error. We don't care about the result here
Gilles Peskine449bd832023-01-11 14:50:10 +010010160 psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10161 output_buffer, size_zk_public - 1, &output_len);
10162 TEST_EQUAL(psa_pake_output(&operation, PSA_PAKE_STEP_ZK_PUBLIC,
10163 output_buffer, buf_size, &output_len),
10164 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010165 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +020010166 }
10167 }
Neil Armstrongd597bc72022-05-25 11:28:39 +020010168
10169exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010170 PSA_ASSERT(psa_destroy_key(key));
10171 PSA_ASSERT(psa_pake_abort(&operation));
10172 mbedtls_free(output_buffer);
10173 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010174}
10175/* END_CASE */
10176
Neil Armstronga557cb82022-06-10 08:58:32 +020010177/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010178void ecjpake_rounds_inject(int alg_arg, int primitive_arg, int hash_arg,
10179 int client_input_first, int inject_error,
10180 data_t *pw_data)
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010181{
10182 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10183 psa_pake_operation_t server = psa_pake_operation_init();
10184 psa_pake_operation_t client = psa_pake_operation_init();
10185 psa_algorithm_t alg = alg_arg;
10186 psa_algorithm_t hash_alg = hash_arg;
10187 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10188 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10189
Gilles Peskine449bd832023-01-11 14:50:10 +010010190 PSA_INIT();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010191
Gilles Peskine449bd832023-01-11 14:50:10 +010010192 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10193 psa_set_key_algorithm(&attributes, alg);
10194 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10195 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10196 &key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010197
Gilles Peskine449bd832023-01-11 14:50:10 +010010198 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10199 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10200 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010201
10202
Gilles Peskine449bd832023-01-11 14:50:10 +010010203 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10204 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010205
Gilles Peskine449bd832023-01-11 14:50:10 +010010206 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10207 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010208
Gilles Peskine449bd832023-01-11 14:50:10 +010010209 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10210 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010211
Gilles Peskine449bd832023-01-11 14:50:10 +010010212 ecjpake_do_round(alg, primitive_arg, &server, &client,
10213 client_input_first, 1, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010214
Gilles Peskine449bd832023-01-11 14:50:10 +010010215 if (inject_error == 1 || inject_error == 2) {
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010216 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010010217 }
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010218
Gilles Peskine449bd832023-01-11 14:50:10 +010010219 ecjpake_do_round(alg, primitive_arg, &server, &client,
10220 client_input_first, 2, inject_error);
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010221
10222exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010223 psa_destroy_key(key);
10224 psa_pake_abort(&server);
10225 psa_pake_abort(&client);
10226 PSA_DONE();
Neil Armstrong8c2e8a62022-06-15 15:28:32 +020010227}
10228/* END_CASE */
10229
10230/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010231void ecjpake_rounds(int alg_arg, int primitive_arg, int hash_arg,
10232 int derive_alg_arg, data_t *pw_data,
10233 int client_input_first, int inj_err_type_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +020010234{
10235 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
10236 psa_pake_operation_t server = psa_pake_operation_init();
10237 psa_pake_operation_t client = psa_pake_operation_init();
10238 psa_algorithm_t alg = alg_arg;
10239 psa_algorithm_t hash_alg = hash_arg;
10240 psa_algorithm_t derive_alg = derive_alg_arg;
10241 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
10242 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
10243 psa_key_derivation_operation_t server_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010244 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010245 psa_key_derivation_operation_t client_derive =
Gilles Peskine449bd832023-01-11 14:50:10 +010010246 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +010010247 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +020010248
Gilles Peskine449bd832023-01-11 14:50:10 +010010249 PSA_INIT();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010250
Gilles Peskine449bd832023-01-11 14:50:10 +010010251 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
10252 psa_set_key_algorithm(&attributes, alg);
10253 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
10254 PSA_ASSERT(psa_import_key(&attributes, pw_data->x, pw_data->len,
10255 &key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010256
Gilles Peskine449bd832023-01-11 14:50:10 +010010257 psa_pake_cs_set_algorithm(&cipher_suite, alg);
10258 psa_pake_cs_set_primitive(&cipher_suite, primitive_arg);
10259 psa_pake_cs_set_hash(&cipher_suite, hash_alg);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010260
Neil Armstrong1e855602022-06-15 11:32:11 +020010261 /* Get shared key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010262 PSA_ASSERT(psa_key_derivation_setup(&server_derive, derive_alg));
10263 PSA_ASSERT(psa_key_derivation_setup(&client_derive, derive_alg));
Neil Armstrong1e855602022-06-15 11:32:11 +020010264
Gilles Peskine449bd832023-01-11 14:50:10 +010010265 if (PSA_ALG_IS_TLS12_PRF(derive_alg) ||
10266 PSA_ALG_IS_TLS12_PSK_TO_MS(derive_alg)) {
10267 PSA_ASSERT(psa_key_derivation_input_bytes(&server_derive,
10268 PSA_KEY_DERIVATION_INPUT_SEED,
10269 (const uint8_t *) "", 0));
10270 PSA_ASSERT(psa_key_derivation_input_bytes(&client_derive,
10271 PSA_KEY_DERIVATION_INPUT_SEED,
10272 (const uint8_t *) "", 0));
Neil Armstrong1e855602022-06-15 11:32:11 +020010273 }
10274
Gilles Peskine449bd832023-01-11 14:50:10 +010010275 PSA_ASSERT(psa_pake_setup(&server, &cipher_suite));
10276 PSA_ASSERT(psa_pake_setup(&client, &cipher_suite));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010277
Gilles Peskine449bd832023-01-11 14:50:10 +010010278 PSA_ASSERT(psa_pake_set_role(&server, PSA_PAKE_ROLE_SERVER));
10279 PSA_ASSERT(psa_pake_set_role(&client, PSA_PAKE_ROLE_CLIENT));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010280
Gilles Peskine449bd832023-01-11 14:50:10 +010010281 PSA_ASSERT(psa_pake_set_password_key(&server, key));
10282 PSA_ASSERT(psa_pake_set_password_key(&client, key));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010283
Gilles Peskine449bd832023-01-11 14:50:10 +010010284 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1) {
10285 TEST_EQUAL(psa_pake_get_implicit_key(&server, &server_derive),
10286 PSA_ERROR_BAD_STATE);
10287 TEST_EQUAL(psa_pake_get_implicit_key(&client, &client_derive),
10288 PSA_ERROR_BAD_STATE);
Valerio Setti1070aed2022-11-11 19:37:31 +010010289 goto exit;
10290 }
Neil Armstrong1e855602022-06-15 11:32:11 +020010291
Neil Armstrongf983caf2022-06-15 15:27:48 +020010292 /* First round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010293 ecjpake_do_round(alg, primitive_arg, &server, &client,
10294 client_input_first, 1, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010295
Gilles Peskine449bd832023-01-11 14:50:10 +010010296 if (inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2) {
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 /* Second round */
Gilles Peskine449bd832023-01-11 14:50:10 +010010305 ecjpake_do_round(alg, primitive_arg, &server, &client,
10306 client_input_first, 2, 0);
Neil Armstrongd597bc72022-05-25 11:28:39 +020010307
Gilles Peskine449bd832023-01-11 14:50:10 +010010308 PSA_ASSERT(psa_pake_get_implicit_key(&server, &server_derive));
10309 PSA_ASSERT(psa_pake_get_implicit_key(&client, &client_derive));
Neil Armstrongd597bc72022-05-25 11:28:39 +020010310
10311exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010010312 psa_key_derivation_abort(&server_derive);
10313 psa_key_derivation_abort(&client_derive);
10314 psa_destroy_key(key);
10315 psa_pake_abort(&server);
10316 psa_pake_abort(&client);
10317 PSA_DONE();
Neil Armstrongd597bc72022-05-25 11:28:39 +020010318}
10319/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010320
10321/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010010322void ecjpake_size_macros()
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010323{
10324 const psa_algorithm_t alg = PSA_ALG_JPAKE;
10325 const size_t bits = 256;
10326 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +010010327 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010328 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
Gilles Peskine449bd832023-01-11 14:50:10 +010010329 PSA_ECC_FAMILY_SECP_R1);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010330
10331 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
10332 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
Gilles Peskine449bd832023-01-11 14:50:10 +010010333 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10334 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
10335 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10336 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010337 /* The output for ZK_PROOF is the same bitsize as the curve */
Gilles Peskine449bd832023-01-11 14:50:10 +010010338 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10339 PSA_BITS_TO_BYTES(bits));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010340
10341 /* Input sizes are the same as output sizes */
Gilles Peskine449bd832023-01-11 14:50:10 +010010342 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10343 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE));
10344 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10345 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC));
10346 TEST_EQUAL(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10347 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF));
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010348
10349 /* These inequalities will always hold even when other PAKEs are added */
Gilles Peskine449bd832023-01-11 14:50:10 +010010350 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10351 PSA_PAKE_OUTPUT_MAX_SIZE);
10352 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10353 PSA_PAKE_OUTPUT_MAX_SIZE);
10354 TEST_LE_U(PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10355 PSA_PAKE_OUTPUT_MAX_SIZE);
10356 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
10357 PSA_PAKE_INPUT_MAX_SIZE);
10358 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
10359 PSA_PAKE_INPUT_MAX_SIZE);
10360 TEST_LE_U(PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
10361 PSA_PAKE_INPUT_MAX_SIZE);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +020010362}
10363/* END_CASE */